Playing cards: Difference between revisions

mNo edit summary
Line 4,999:
24 cards remaining in the deck:
♥A ♥6 ♦2 ♦5 ♥J ♣T ♦3 ♠5 ♠A ♣8 ♥8 ♥7 ♥K ♦7 ♥T ♦6 ♦T ♣6 ♣3 ♣A ♦4 ♥4 ♠6 ♣9</pre>
 
=={{header|Picat}}==
<lang Picat>go =>
% Create and print the deck
deck(Deck),
print_deck(Deck),
nl,
% Shuffle the deck
print_deck(shuffle(Deck)),
nl,
 
% Deal 3 cards
Deck := deal(Deck,Card1),
Deck := deal(Deck,Card2),
Deck := deal(Deck,Card3),
 
println(deal1=Card1),
println(deal2=Card2),
println(deal3=Card3),
% The remaining deck
print_deck(Deck),
nl,
 
% Deal 5 cards
Deck := deal(Deck,Card4),
Deck := deal(Deck,Card5),
Deck := deal(Deck,Card6),
Deck := deal(Deck,Card7),
Deck := deal(Deck,Card8),
 
println(cards4_to_8=[Card4,Card5,Card6,Card7,Card8]),
nl,
 
% Deal 5 cards
Deck := deal_n(Deck,5,FiveCards),
println(fiveCards=FiveCards),
print_deck(Deck),
nl,
 
% And deal some more cards
% This chaining works since deal/1 returns the remaining deck
Deck := Deck.deal(Card9).deal(Card10).deal(Card11).deal(Card12),
println("4_more_cards"=[Card9,Card10,Card11,Card12]),
print_deck(Deck),
 
nl.
 
% suits(Suits) => Suits = ["♠","♥","♦","♣"].
suits(Suits) => Suits = ["C","H","S","D"].
values(Values) => Values = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"].
 
% Create a (sorted) deck.
deck(Deck) =>
suits(Suits),
values(Values),
Deck =[S++V :V in Values, S in Suits].sort().
 
% Shuffle a deck
shuffle(Deck) = Deck2 =>
Deck2 = Deck,
Len = Deck2.length,
foreach(I in 1..Len)
R2 = random(1,Len),
Deck2 := swap(Deck2,I,R2)
end.
 
% Swap position I <=> J in list L
swap(L,I,J) = L2, list(L) =>
L2 = L,
T = L2[I],
L2[I] := L2[J],
L2[J] := T.
 
 
% The first card is returned as the out parameter Card.
% The deck is returned as the function value.
deal(Deck, Card) = Deck.tail() => Card = Deck.first().
 
% Deal N cards
deal_n(Deck, N, Cards) = [Deck[I] : I in Len+1..Deck.length] =>
Len = min(N,Deck.length),
Cards = [Deck[I] : I in 1..Len].
 
% Print deck
print_deck(Deck) =>
println("Deck:"),
foreach({Card,I} in zip(Deck,1..Deck.len))
printf("%w ", Card),
if I mod 10 == 0 then
nl
end
end,
nl.</lang>
 
{{out}}
<pre>Deck:
C2 C3 C4 C5 C6 C7 C8 C9 CA CJ
CK CQ CT D2 D3 D4 D5 D6 D7 D8
D9 DA DJ DK DQ DT H2 H3 H4 H5
H6 H7 H8 H9 HA HJ HK HQ HT S2
S3 S4 S5 S6 S7 S8 S9 SA SJ SK
SQ ST
 
Deck:
SA C2 D7 C7 C4 S8 HT D9 DA H6
D6 S6 H2 C5 H8 ST SJ HQ S9 DQ
D5 C6 CJ H4 S5 HK CK HJ H5 D8
H9 C8 D4 CQ H3 H7 SK S4 DK SQ
CA S7 D2 C9 DJ HA DT S2 C3 CT
D3 S3
 
deal1 = SA
deal2 = C2
deal3 = D7
Deck:
C7 C4 S8 HT D9 DA H6 D6 S6 H2
C5 H8 ST SJ HQ S9 DQ D5 C6 CJ
H4 S5 HK CK HJ H5 D8 H9 C8 D4
CQ H3 H7 SK S4 DK SQ CA S7 D2
C9 DJ HA DT S2 C3 CT D3 S3
 
cards4_to_8 = [C7,C4,S8,HT,D9]
 
fiveCards = [DA,H6,D6,S6,H2]
Deck:
C5 H8 ST SJ HQ S9 DQ D5 C6 CJ
H4 S5 HK CK HJ H5 D8 H9 C8 D4
CQ H3 H7 SK S4 DK SQ CA S7 D2
C9 DJ HA DT S2 C3 CT D3 S3
 
4_more_cards = [C5,H8,ST,SJ]
Deck:
HQ S9 DQ D5 C6 CJ H4 S5 HK CK
HJ H5 D8 H9 C8 D4 CQ H3 H7 SK
S4 DK SQ CA S7 D2 C9 DJ HA DT
S2 C3 CT D3 S3 </pre>
 
 
=={{header|PicoLisp}}==
Line 5,015 ⟶ 5,153:
(de shuffle (Lst)
(by '(NIL (rand)) sort Lst) )</lang>
 
=={{header|PowerShell}}==
This implementation was designed as a pre-requisite to other cards games. For this reason, this has the ability to have more than one persistent deck, more than one persistent hand, and shuffle the ''same deck'' over and over again or after some cards have been dealt out. There is also a full help file accessed by typing
495

edits