War card game: Difference between revisions

m (added related tasks)
Line 102:
Q♠ 5♦ Player 1 takes the cards.
8♥ 4♣ Player 1 takes the cards.
Player 1 wins the game.
</pre>
 
=={{header|Phix}}==
Shuffles on pickup to significantly shorten the games
<lang Phix>sequence deck = shuffle(tagset(52)),
hands = {deck[1..26],deck[27..52]},
pending = {}
 
function pop(integer hand)
integer res
{res, hands[hand]} = {hands[hand][1],hands[hand][2..$]}
return res
end function
 
function show(integer c)
integer r = remainder(c-1,13)+1,
s = floor((c-1)/13)+1
printf(1,"%s ",{"23456789TJQKA"[r]&"SHDC"[s]})
return r
end function
 
while true do
if length(hands[1])=0 then
if length(hands[2])=0 then
printf(1,"Game ends as a tie.\n")
exit
end if
printf(1,"Player 2 wins the game.\n")
exit
elsif length(hands[2])=0 then
printf(1,"Player 1 wins the game.\n")
exit
end if
integer c1 = pop(1),
c2 = pop(2),
r1 = show(c1),
r2 = show(c2)
if r1>r2 then
printf(1,"Player 1 takes the cards.\n")
hands[1] &= shuffle(c1&c2&pending)
pending = {}
elsif r1<r2 then
printf(1,"Player 2 takes the cards.\n")
hands[2] &= shuffle(c1&c2&pending)
pending = {}
else -- r1==r2
printf(1,"Tie!\n")
if length(hands[1])!=0 and length(hands[2])!=0 then
pending &= shuffle(c1&c2&pop(1)&pop(2))
printf(1,"?? ?? Cards are face down.\n")
end if
end if
end while</lang>
{{out}}
<pre>
9H 3C Player 1 takes the cards.
AD KD Player 1 takes the cards.
3D KS Player 2 takes the cards.
...
2H 9S Player 2 takes the cards.
KC 7H Player 1 takes the cards.
5C JS Player 2 takes the cards.
3S 3D Tie!
?? ?? Cards are face down.
8D 2H Player 1 takes the cards.
2C JS Player 2 takes the cards.
JD 5C Player 1 takes the cards.
6D 2C Player 1 takes the cards.
QD JS Player 1 takes the cards.
Player 1 wins the game.
</pre>
7,795

edits