Playing cards: Difference between revisions

Added Easylang
(Added Easylang)
 
(14 intermediate revisions by 7 users not shown)
Line 362:
{{Out|Example output}}
<pre>((King OF Clubs), (6 OF Hearts), (7 OF Diamonds), (Ace OF Hearts), (9 OF Spades), (10 OF Clubs), (Ace OF Spades), (8 OF Clubs), (4 OF Spades), (8 OF Hearts), (Jack OF Hearts), (3 OF Clubs), (7 OF Hearts), (10 OF Hearts), (Jack OF Clubs), (Ace OF Clubs), (King OF Spades), (9 OF Clubs), (7 OF Spades), (5 OF Spades), (7 OF Clubs), (Queen OF Clubs), (9 OF Diamonds), (2 OF Spades), (6 OF Diamonds), (Ace OF Diamonds), (Queen OF Diamonds), (5 OF Hearts), (4 OF Clubs), (5 OF Clubs), (4 OF Hearts), (3 OF Diamonds), (4 OF Diamonds), (3 OF Hearts), (King OF Diamonds), (2 OF Clubs), (Jack OF Spades), (2 OF Diamonds), (5 OF Diamonds), (Queen OF Spades), (10 OF Diamonds), (King OF Hearts), (Jack OF Diamonds), (Queen OF Hearts), (8 OF Spades), (2 OF Hearts), (8 OF Diamonds), (10 OF Spades), (9 OF Hearts), (6 OF Clubs), (3 OF Spades), (6 OF Spades))</pre>
 
=={{header|APL}}==
 
<syntaxhighlight lang="apl">
deck←,'23456789TJQKA'∘.,'CDSH'
shuffle←{⍵[?⍨⍴⍵]}
</syntaxhighlight>
 
{{out}}
<pre>
deck
2C 2D 2S 2H 3C 3D 3S 3H 4C ..
shuffle deck
7H 3D 2H 9D KS 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 956 ⟶ 1,034:
 
=={{header|BASIC}}==
==={{header|QuickBASIC}}===
{{works with|QuickBASIC|QBX 7.1}}
 
Line 1,025 ⟶ 1,104:
END SUB</syntaxhighlight>
 
{{out}}
Sample output:
<pre>
Dealt: 7D
Dealt: 6D
Line 1,034 ⟶ 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,090 ⟶ 2,223:
(declare (ignore x y))
(zerop (random 2)))))</syntaxhighlight>
====Alternative version====
 
''DRAFT''
 
 
1. Program
 
<syntaxhighlight lang="lisp>;; 22.10.28
 
(defconstant +pips+ '(ace 2 3 4 5 6 7 8 9 10 jack queen king))
(defconstant +suits+ '(club diamond heart spade))
 
 
(defvar *deck* nil)
 
(defun make-deck ()
(loop for p in +pips+ append (loop for s in +suits+ collect (list p s))))
(defun shuffle-deck (deck)
(loop repeat 1024 do
(rotatef (nth (random 52) deck) (nth (random 52) deck))))</syntaxhighlight>
 
2. Execution
 
<pre>(setf *deck* (make-deck))
(shuffle-deck *deck*)</pre>
 
{{out}}
<pre>((ACE CLUB) (ACE DIAMOND) (ACE HEART) (ACE SPADE) (2 CLUB) (2 DIAMOND)
(2 HEART) (2 SPADE) (3 CLUB) (3 DIAMOND) (3 HEART) (3 SPADE) (4 CLUB)
(4 DIAMOND) (4 HEART) (4 SPADE) (5 CLUB) (5 DIAMOND) (5 HEART) (5 SPADE)
(6 CLUB) (6 DIAMOND) (6 HEART) (6 SPADE) (7 CLUB) (7 DIAMOND) (7 HEART)
(7 SPADE) (8 CLUB) (8 DIAMOND) (8 HEART) (8 SPADE) (9 CLUB) (9 DIAMOND)
(9 HEART) (9 SPADE) (10 CLUB) (10 DIAMOND) (10 HEART) (10 SPADE) (JACK CLUB)
(JACK DIAMOND) (JACK HEART) (JACK SPADE) (QUEEN CLUB) (QUEEN DIAMOND)
(QUEEN HEART) (QUEEN SPADE) (KING CLUB) (KING DIAMOND) (KING HEART)
(KING SPADE))
 
((8 DIAMOND) (4 SPADE) (7 DIAMOND) (ACE DIAMOND) (10 HEART) (7 CLUB) (6 SPADE)
(9 HEART) (JACK CLUB) (JACK DIAMOND) (3 SPADE) (7 HEART) (KING HEART)
(4 DIAMOND) (QUEEN SPADE) (6 HEART) (10 SPADE) (QUEEN HEART) (3 DIAMOND)
(2 SPADE) (8 CLUB) (2 DIAMOND) (4 CLUB) (6 CLUB) (9 SPADE) (6 DIAMOND)
(JACK SPADE) (2 HEART) (2 CLUB) (10 DIAMOND) (4 HEART) (JACK HEART)
(ACE SPADE) (KING SPADE) (5 HEART) (5 CLUB) (7 SPADE) (ACE CLUB) (9 CLUB)
(9 DIAMOND) (ACE HEART) (8 SPADE) (8 HEART) (10 CLUB) (3 CLUB) (KING DIAMOND)
(KING CLUB) (5 DIAMOND) (3 HEART) (QUEEN CLUB) (QUEEN DIAMOND) (5 SPADE))</pre>
 
That's all Folks !
 
''cyril nocton (cyril.nocton@gmail.com) w/ google translate''
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.typecons, std.algorithm, std.traits, std.array,
Line 2,558 ⟶ 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,650 ⟶ 4,788:
 
use strict;
use warnings;
 
@Playing_Card_Deck::suits = qw
Line 4,665 ⟶ 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,674 ⟶ 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,688 ⟶ 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 7,477 ⟶ 7,616:
 
=={{header|Swift}}==
{{works with|Swift |5}}
<syntaxhighlight lang="swift">
struct Card: CustomStringConvertible
Line 8,034 ⟶ 8,173:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var FACES = "23456789TJQKA"
Line 8,131 ⟶ 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,049

edits