War card game: Difference between revisions

J draft
(J draft)
Line 412:
Player 1 wins the game!
</pre>
 
=={{header|J}}==
Implementation:<lang J>DECK=: '2 3 4 5 6 7 8 9 10 J K Q A' (,' of ',])&.>/&cut '♣ ♦ ♥ ♠'
rank=: DECK {{ {."1 ($m)#:(,m) i. y }}
shuffle=: {~ ?~@#
deal=: _26 ]\ shuffle@,@DECK
cardwar=: {{
TURNS=: 0
'P1 P2'=: <each deal''
while. 0<P1 *&# P2 do. turn TURNS=: TURNS+1 end.
after=. ' after ',TURNS,&":' turn','s'#~1~:TURNS
if. #P1 do. 'Player 1 wins', after
elseif. #P2 do. 'Player 2 wins', after
else. 'Tie',after end.
}}
plays=: {{ ((m)=: }.".m)](echo n,~(m,' plays '),;'nothing'&[^:(0=#@]){.".m)]({.".m) }}
turn=: {{
c=. (b=. 'P2' plays''),(a =. 'P1' plays'')
while. a =&rank b do.
if. 0=P1 *&# P2 do. echo 'No more cards to draw' return. end.
c=. c, (a=. 'P1' plays''),(b =. 'P2' plays''), 'P1' plays' face down', 'P2' plays' face down'
end.
('P',":1+a <&rank b) takes ({~ ?~@#)c-.a:
}}
takes=: {{
(m)=: (".m),y
echo m,' takes ',;(<' and ') _2}}.,(<', '),.y
echo''
}}</lang>
 
Example run:<lang J> cardwar''
P1 plays 6 of ♦
P2 plays 9 of ♥
P2 takes 6 of ♦ and 9 of ♥
 
P1 plays K of ♣
P2 plays 6 of ♣
P1 takes K of ♣ and 6 of ♣
 
P1 plays A of ♦
P2 plays A of ♥
P2 plays 5 of ♠ face down
P1 plays J of ♥ face down
P2 plays 2 of ♣
P1 plays J of ♦
P1 takes A of ♥, J of ♥, A of ♦, 2 of ♣, 5 of ♠ and J of ♦
 
P1 plays 7 of ♥
P2 plays 10 of ♣
P2 takes 10 of ♣ and 7 of ♥
 
(... many lines deleted ...)
 
P1 plays 2 of ♦
P2 plays 5 of ♥
P2 takes 2 of ♦ and 5 of ♥
 
P1 plays K of ♥
P2 plays 4 of ♥
P1 takes 4 of ♥ and K of ♥
 
P1 plays 3 of ♦
P2 plays 3 of ♣
P2 plays 2 of ♦ face down
P1 plays 6 of ♠ face down
P2 plays 5 of ♥
P1 plays K of ♠
P1 takes 6 of ♠, K of ♠, 5 of ♥, 3 of ♣, 3 of ♦ and 2 of ♦
 
Player 1 wins after 86 turns</lang>
 
=={{header|Julia}}==
6,962

edits