Playing cards: Difference between revisions

Added Easylang
(Added Easylang)
 
(8 intermediate revisions by 6 users not shown)
Line 366:
 
<syntaxhighlight lang="apl">
⍝2023-02-18
 
deck←,'23456789TJQKA'∘.,'CDSH'
shuffle←{⍵[?⍨⍴⍵]}
Line 378 ⟶ 376:
shuffle deck
7H 3D 2H 9D KS 7C JH TC JS ..
5↑deck
7H 3D 2H 9D KS
5↓deck
7C JH TC JS ..
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">ranks: 1..13
suits: [`♣`, `♦`, `♥`, `♠`]
 
define :card [rank, suit][
init: [
ensure ->
and? -> contains? ranks this\rank
-> contains? suits this\suit
]
 
print: [
R: ø
case [this\rank=]
when? [1] -> R: `A`
when? [11]-> R: `J`
when? [12]-> R: `Q`
when? [13]-> R: `K`
else -> R: this\rank
~{|R||this\suit|}
]
]
 
define :deck [][
init: [
this\cards: []
loop ranks 'rank ->
loop suits 'suit ->
this\cards: this\cards ++ to :card @[rank, suit]
]
]
 
shuffleUp: function [this :deck][
this\cards: shuffle this\cards
]
 
deal: function [this :deck, cnt :integer][
if cnt > size this\cards ->
panic "Not enough cards in deck"
 
cards: []
do.times: cnt [
dealt: sample this\cards
'cards ++ dealt
this\cards: remove this\cards dealt
]
return cards
]
 
; create a new deck
Deck: to :deck []
 
; and shuffle it
shuffleUp Deck
 
; deal 5 cards
print deal Deck 5</syntaxhighlight>
 
{{out}}
 
<pre>A♥ 6♣ 7♣ Q♦ K♥</pre>
 
=={{header|ATS}}==
Line 977 ⟶ 1,034:
 
=={{header|BASIC}}==
==={{header|QuickBASIC}}===
{{works with|QuickBASIC|QBX 7.1}}
 
Line 1,046 ⟶ 1,104:
END SUB</syntaxhighlight>
 
{{out}}
Sample output:
<pre>
Dealt: 7D
Dealt: 6D
Line 1,055 ⟶ 1,115:
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC
2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD
</pre>
 
=={{header|BASIC256}}==
{{trans|Run BASIC}}
<syntaxhighlight lang="vb">suite$ = "CDHS" #Club, Diamond, Heart, Spade
card$ = "A23456789TJQK" #Cards Ace to King
card = 0
 
dim n(55) #make ordered deck
for i = 1 to 52 # of 52 cards
n[i] = i
next i
 
for i = 1 to 52 * 3 #shuffle deck 3 times
i1 = int(rand * 52) + 1
i2 = int(rand * 52) + 1
h2 = n[i1]
n[i1] = n[i2]
n[i2] = h2
next i
 
for hand = 1 to 4 #4 hands
for deal = 1 to 13 #deal each 13 cards
card += 1 #next card in deck
s = (n[card] mod 4) + 1 #determine suite
c = (n[card] mod 13) + 1 #determine card
print mid(card$,c,1);mid(suite$,s,1);" "; #show the card
next deal
print
next hand
end
 
function word$(sr$, wn, delim$)
j = wn
if j = 0 then j += 1
res$ = "" : s$ = sr$ : d$ = delim$
if d$ = "" then d$ = " "
sd = length(d$) : sl = length(s$)
while true
n = instr(s$,d$) : j -= 1
if j = 0 then
if n=0 then res$=s$ else res$=mid(s$,1,n-1)
return res$
end if
if n = 0 then return res$
if n = sl - sd then res$ = "" : return res$
sl2 = sl-n : s$ = mid(s$,n+1,sl2) : sl = sl2
end while
return res$
end function</syntaxhighlight>
{{out}}
<pre>Same as Run BASIC entry.</pre>
 
=={{header|Batch File}}==
Line 2,528 ⟶ 2,640:
=={{header|E}}==
See [[Playing Cards/E]]
 
=={{header|EasyLang}}==
<syntaxhighlight>
global deck[] top .
proc new . .
deck[] = [ ]
for i to 52
deck[] &= i
.
top = 52
.
suit$[] = [ "♠" "♦" "♥" "♣" ]
val$[] = [ 2 3 4 5 6 7 8 9 10 "J" "Q" "K" "A" ]
func$ name card .
return suit$[card mod1 4] & val$[card div1 4]
.
proc show . .
for i to top
write name deck[i] & " "
.
print ""
print ""
.
proc shuffle . .
for i = 52 downto 2
r = randint i
swap deck[i] deck[r]
.
top = 52
.
func deal .
top -= 1
return deck[top + 1]
.
new
show
shuffle
show
for i to 10
write name deal & " "
.
print ""
print ""
show
</syntaxhighlight>
 
{{out}}
<pre>
♠2 ♦2 ♥2 ♣2 ♠3 ♦3 ♥3 ♣3 ♠4 ♦4 ♥4 ♣4 ♠5 ♦5 ♥5 ♣5 ♠6 ♦6 ♥6 ♣6 ♠7 ♦7 ♥7 ♣7 ♠8 ♦8 ♥8 ♣8 ♠9 ♦9 ♥9 ♣9 ♠10 ♦10 ♥10 ♣10 ♠J ♦J ♥J ♣J ♠Q ♦Q ♥Q ♣Q ♠K ♦K ♥K ♣K ♠A ♦A ♥A ♣A
 
♦2 ♣2 ♦6 ♠10 ♦5 ♥3 ♣4 ♦7 ♣9 ♥2 ♣7 ♣K ♦K ♠Q ♠2 ♦Q ♥7 ♥8 ♣8 ♥A ♠3 ♥10 ♥Q ♣10 ♠K ♠5 ♦8 ♠9 ♠4 ♣5 ♣J ♥5 ♠J ♠7 ♦4 ♦3 ♦10 ♥6 ♣Q ♥4 ♠6 ♣3 ♥K ♦J ♠A ♠8 ♥J ♥9 ♣A ♦9 ♣6 ♦A
 
♦A ♣6 ♦9 ♣A ♥9 ♥J ♠8 ♠A ♦J ♥K
 
♦2 ♣2 ♦6 ♠10 ♦5 ♥3 ♣4 ♦7 ♣9 ♥2 ♣7 ♣K ♦K ♠Q ♠2 ♦Q ♥7 ♥8 ♣8 ♥A ♠3 ♥10 ♥Q ♣10 ♠K ♠5 ♦8 ♠9 ♠4 ♣5 ♣J ♥5 ♠J ♠7 ♦4 ♦3 ♦10 ♥6 ♣Q ♥4 ♠6 ♣3
</pre>
 
=={{header|Elixir}}==
Line 4,620 ⟶ 4,788:
 
use strict;
use warnings;
 
@Playing_Card_Deck::suits = qw
Line 4,635 ⟶ 4,804:
{my $invocant = shift;
my $class = ref($invocant) || $invocant;
my @cards = ();
foreach my $suit (@Playing_Card_Deck::suits)
{foreach my $pip (@Playing_Card_Deck::pips)
Line 4,644 ⟶ 4,813:
# Removes the top card of the given deck and returns it as a hash
# with the keys "suit" and "pip".
{return %{ shift( @{shift(@_)} ) };}
 
sub shuffle
Line 4,658 ⟶ 4,827:
sub print_cards
# Prints out a description of every card in the deck, in order.
{print "$_->{pip} of $_->{suit}\n" foreach @{shift(@_)};}</syntaxhighlight>
Some examples of use:
<syntaxhighlight lang="perl">my $deck = new Playing_Card_Deck->new;
$deck->shuffle;
my %card = $deck->deal;
Line 8,004 ⟶ 8,173:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var FACES = "23456789TJQKA"
Line 8,101 ⟶ 8,270:
QD KS 7S QH 6S JD QC 6D KD AC 7D AS AD
</pre>
 
=={{header|Yabasic}}==
{{trans|Run BASIC}}
<syntaxhighlight lang="vb">suite$ = "CDHS" //Club, Diamond, Heart, Spade
card$ = "A23456789TJQK" //Cards Ace to King
 
dim n(55) //make ordered deck
for i = 1 to 52 // of 52 cards
n(i) = i
next i
 
for i = 1 to 52 * 3 //shuffle deck 3 times
i1 = int(ran(52)) + 1
i2 = int(ran(52)) + 1
h2 = n(i1)
n(i1) = n(i2)
n(i2) = h2
next i
 
for hand = 1 to 4 //4 hands
for deal = 1 to 13 //deal each 13 cards
card = card + 1 //next card in deck
s = mod(n(card), 4) + 1 //determine suite
c = mod(n(card), 13) + 1 //determine card
print mid$(card$,c,1),mid$(suite$,s,1)," "; //show the card
next deal
print
next hand
end</syntaxhighlight>
{{out}}
<pre>Same as Run BASIC entry.</pre>
 
=={{header|zkl}}==
2,023

edits