War card game: Difference between revisions

(Added FreeBASIC)
 
Line 952:
2S 2H War!
Player B has won the game
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
Since jq currently has no built-in PRNG, the following solution uses /dev/random
on the assumption that jq is invoked along the lines of the following:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -cnrR -f war.jq
</pre>
where "war.jq" is the name of a file containing the jq program.
<syntaxhighlight lang="jq">
### Generic functions
 
# Set .popped to the first item of the specified array, and remove it therefrom
@ Usage example: {a:[1,2]} | pop(.a; .x)
def pop(array; popped):
popped = array[0]
| array |= .[1:];
 
# like while/2 but emit the final term rather than the first one
def whilst(cond; update):
def _whilst:
if cond then update | (., _whilst) else empty end;
_whilst;
 
# Output: a prn in range(0;.)
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def knuthShuffle:
length as $n
| if $n <= 1 then .
else {i: $n, a: .}
| until(.i == 0;
.i += -1
| (.i + 1 | prn) as $j
| .a[.i] as $t
| .a[.i] = .a[$j]
| .a[$j] = $t)
| .a
end;
 
 
### War Card Game
def suits: ["♣", "♦", "♥", "♠"];
def faces: ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" ];
 
def cards: [range( 0; 52) | "\(faces[.%13])\(suits[(./13)|floor])"];
def ranks: [range( 0; 52) | . % 13];
 
def war:
cards as $cards
| ranks as $ranks
| ([range(0; 52)] | knuthShuffle) as $deck
| reduce range(0; 26) as $i ({};
.hand1 |= [$deck[2*$i]] + .
| .hand2 |= [$deck[2*$i+1]] + .)
| .numPlayed=0
| whilst( .numPlayed == 0 and
(.hand1|length) > 0 and (.hand2|length) > 0;
 
pop(.hand1; .card1)
| pop(.hand2; .card2)
| .played1 = [.card1]
| .played2 = [.card2]
| .numPlayed = 2
| whilst(.numPlayed > 0 ;
.emit = "\($cards[.card1])\t\($cards[.card2])\t"
| if $ranks[.card1] > $ranks[.card2]
then .hand1 += .played1 + .played2
| .emit += "Player 1 takes the \(.numPlayed) cards. Now has \(.hand1|length)."
| .numPlayed = 0
 
elif $ranks[.card1] < $ranks[.card2]
then .hand2 += .played2 + .played1
| .emit += "Player 2 takes the \(.numPlayed) cards. Now has \(.hand2|length)."
| .numPlayed = 0
 
else .emit += "War!\n"
| if (.hand1|length) < 2
then .emit += "Player 1 has insufficient cards left."
| .hand2 = .hand2 + .played2 + .played1 + .hand1
| .hand1 = []
| .numPlayed = 0
 
elif .hand2|length < 2
then .emit += "Player 2 has insufficient cards left."
| .hand1 = .hand1 + .played1 + .played2 + .hand2
| .hand2 = []
| .numPlayed = 0
 
else
pop(.hand1; .card1) # face down card
| .played1 += [.card1]
 
| pop(.hand1; .card1) # face up card
| .played1 += [.card1]
 
| pop( .hand2; .card2) # face down card 2
| .played2 += [.card2]
| pop(.hand2; .card2) # face up card 2
| .played2 += [.card2]
| .numPlayed += 4
| .emit += "? \t?\tFace down cards."
end
end
)
)
| .emit,
(if .hand1|length == 52
then "Player 1 wins the game!"
elif .hand2|length == 52
then "Player 2 wins the game!"
else empty
end) ;
 
war
</syntaxhighlight>
{{output}}
For ease of comparability, the following is based on a run without the deck shuffling.
<pre style="height:20lh;overflow:auto>
K♠ A♠ Player 2 takes the 2 cards. Now has 27.
J♠ Q♠ Player 2 takes the 2 cards. Now has 28.
9♠ T♠ Player 2 takes the 2 cards. Now has 29.
7♠ 8♠ Player 2 takes the 2 cards. Now has 30.
5♠ 6♠ Player 2 takes the 2 cards. Now has 31.
3♠ 4♠ Player 2 takes the 2 cards. Now has 32.
A♥ 2♠ Player 1 takes the 2 cards. Now has 21.
Q♥ K♥ Player 2 takes the 2 cards. Now has 32.
T♥ J♥ Player 2 takes the 2 cards. Now has 33.
8♥ 9♥ Player 2 takes the 2 cards. Now has 34.
6♥ 7♥ Player 2 takes the 2 cards. Now has 35.
4♥ 5♥ Player 2 takes the 2 cards. Now has 36.
2♥ 3♥ Player 2 takes the 2 cards. Now has 37.
K♦ A♦ Player 2 takes the 2 cards. Now has 38.
J♦ Q♦ Player 2 takes the 2 cards. Now has 39.
9♦ T♦ Player 2 takes the 2 cards. Now has 40.
7♦ 8♦ Player 2 takes the 2 cards. Now has 41.
5♦ 6♦ Player 2 takes the 2 cards. Now has 42.
3♦ 4♦ Player 2 takes the 2 cards. Now has 43.
A♣ 2♦ Player 1 takes the 2 cards. Now has 10.
Q♣ K♣ Player 2 takes the 2 cards. Now has 43.
T♣ J♣ Player 2 takes the 2 cards. Now has 44.
8♣ 9♣ Player 2 takes the 2 cards. Now has 45.
6♣ 7♣ Player 2 takes the 2 cards. Now has 46.
4♣ 5♣ Player 2 takes the 2 cards. Now has 47.
2♣ 3♣ Player 2 takes the 2 cards. Now has 48.
A♥ A♠ War!
?2♠ ?K♠ Face down cards.
A♣ Q♠ Player 1 takes the 6 cards. Now has 7.
2♦ J♠ Player 2 takes the 2 cards. Now has 46.
A♥ T♠ Player 1 takes the 2 cards. Now has 7.
2♠ 9♠ Player 2 takes the 2 cards. Now has 46.
A♣ 8♠ Player 1 takes the 2 cards. Now has 7.
A♠ 7♠ Player 1 takes the 2 cards. Now has 8.
K♠ 6♠ Player 1 takes the 2 cards. Now has 9.
Q♠ 5♠ Player 1 takes the 2 cards. Now has 10.
A♥ 4♠ Player 1 takes the 2 cards. Now has 11.
T♠ 3♠ Player 1 takes the 2 cards. Now has 12.
A♣ K♥ Player 1 takes the 2 cards. Now has 13.
8♠ Q♥ Player 2 takes the 2 cards. Now has 40.
A♠ J♥ Player 1 takes the 2 cards. Now has 13.
7♠ T♥ Player 2 takes the 2 cards. Now has 40.
K♠ 9♥ Player 1 takes the 2 cards. Now has 13.
6♠ 8♥ Player 2 takes the 2 cards. Now has 40.
Q♠ 7♥ Player 1 takes the 2 cards. Now has 13.
5♠ 6♥ Player 2 takes the 2 cards. Now has 40.
A♥ 5♥ Player 1 takes the 2 cards. Now has 13.
4♠ 4♥ War!
?T♠ ?3♥ Face down cards.
3♠ 2♥ Player 1 takes the 6 cards. Now has 16.
A♣ A♦ War!
?K♥ ?K♦ Face down cards.
A♠ Q♦ Player 1 takes the 6 cards. Now has 19.
J♥ J♦ War!
?K♠ ?T♦ Face down cards.
9♥ 9♦ War!
?Q♠ ?8♦ Face down cards.
7♥ 7♦ War!
?A♥ ?6♦ Face down cards.
5♥ 5♦ War!
?4♠ ?4♦ Face down cards.
T♠ 3♦ Player 1 takes the 18 cards. Now has 28.
3♠ K♣ Player 2 takes the 2 cards. Now has 25.
4♥ Q♣ Player 2 takes the 2 cards. Now has 26.
3♥ J♣ Player 2 takes the 2 cards. Now has 27.
2♥ T♣ Player 2 takes the 2 cards. Now has 28.
A♣ 9♣ Player 1 takes the 2 cards. Now has 25.
K♥ 8♣ Player 1 takes the 2 cards. Now has 26.
A♠ 7♣ Player 1 takes the 2 cards. Now has 27.
A♦ 6♣ Player 1 takes the 2 cards. Now has 28.
K♦ 5♣ Player 1 takes the 2 cards. Now has 29.
Q♦ 4♣ Player 1 takes the 2 cards. Now has 30.
J♥ 3♣ Player 1 takes the 2 cards. Now has 31.
K♠ 2♣ Player 1 takes the 2 cards. Now has 32.
9♥ J♠ Player 2 takes the 2 cards. Now has 21.
Q♠ 2♦ Player 1 takes the 2 cards. Now has 32.
7♥ 9♠ Player 2 takes the 2 cards. Now has 21.
A♥ 2♠ Player 1 takes the 2 cards. Now has 32.
5♥ Q♥ Player 2 takes the 2 cards. Now has 21.
4♠ 8♠ Player 2 takes the 2 cards. Now has 22.
T♠ T♥ War!
?J♦ ?7♠ Face down cards.
T♦ 8♥ Player 1 takes the 6 cards. Now has 33.
9♦ 6♠ Player 1 takes the 2 cards. Now has 34.
8♦ 6♥ Player 1 takes the 2 cards. Now has 35.
7♦ 5♠ Player 1 takes the 2 cards. Now has 36.
6♦ K♣ Player 2 takes the 2 cards. Now has 17.
5♦ 3♠ Player 1 takes the 2 cards. Now has 36.
4♦ Q♣ Player 2 takes the 2 cards. Now has 17.
3♦ 4♥ Player 2 takes the 2 cards. Now has 18.
A♣ J♣ Player 1 takes the 2 cards. Now has 35.
9♣ 3♥ Player 1 takes the 2 cards. Now has 36.
K♥ T♣ Player 1 takes the 2 cards. Now has 37.
8♣ 2♥ Player 1 takes the 2 cards. Now has 38.
A♠ J♠ Player 1 takes the 2 cards. Now has 39.
7♣ 9♥ Player 2 takes the 2 cards. Now has 14.
A♦ 9♠ Player 1 takes the 2 cards. Now has 39.
6♣ 7♥ Player 2 takes the 2 cards. Now has 14.
K♦ Q♥ Player 1 takes the 2 cards. Now has 39.
5♣ 5♥ War!
?Q♦ ?8♠ Face down cards.
4♣ 4♠ War!
?J♥ ?K♣ Face down cards.
3♣ 6♦ Player 2 takes the 10 cards. Now has 18.
K♠ Q♣ Player 1 takes the 2 cards. Now has 35.
2♣ 4♦ Player 2 takes the 2 cards. Now has 18.
Q♠ 4♥ Player 1 takes the 2 cards. Now has 35.
2♦ 3♦ Player 2 takes the 2 cards. Now has 18.
A♥ 9♥ Player 1 takes the 2 cards. Now has 35.
2♠ 7♣ Player 2 takes the 2 cards. Now has 18.
T♠ 7♥ Player 1 takes the 2 cards. Now has 35.
J♦ 6♣ Player 1 takes the 2 cards. Now has 36.
T♦ 5♥ Player 1 takes the 2 cards. Now has 37.
T♥ 8♠ Player 1 takes the 2 cards. Now has 38.
7♠ 4♠ Player 1 takes the 2 cards. Now has 39.
8♥ K♣ Player 2 takes the 2 cards. Now has 14.
9♦ 6♦ Player 1 takes the 2 cards. Now has 39.
6♠ 5♣ Player 1 takes the 2 cards. Now has 40.
8♦ Q♦ Player 2 takes the 2 cards. Now has 13.
6♥ 4♣ Player 1 takes the 2 cards. Now has 40.
7♦ J♥ Player 2 takes the 2 cards. Now has 13.
5♠ 3♣ Player 1 takes the 2 cards. Now has 40.
5♦ 4♦ Player 1 takes the 2 cards. Now has 41.
3♠ 2♣ Player 1 takes the 2 cards. Now has 42.
A♣ 3♦ Player 1 takes the 2 cards. Now has 43.
J♣ 2♦ Player 1 takes the 2 cards. Now has 44.
9♣ 7♣ Player 1 takes the 2 cards. Now has 45.
3♥ 2♠ Player 1 takes the 2 cards. Now has 46.
K♥ K♣ War!
?T♣ ?8♥ Face down cards.
8♣ Q♦ Player 2 takes the 6 cards. Now has 9.
2♥ 8♦ Player 2 takes the 2 cards. Now has 10.
A♠ J♥ Player 1 takes the 2 cards. Now has 43.
J♠ 7♦ Player 1 takes the 2 cards. Now has 44.
A♦ K♣ Player 1 takes the 2 cards. Now has 45.
9♠ 8♥ Player 1 takes the 2 cards. Now has 46.
K♦ Q♦ Player 1 takes the 2 cards. Now has 47.
Q♥ K♥ Player 2 takes the 2 cards. Now has 6.
K♠ T♣ Player 1 takes the 2 cards. Now has 47.
Q♣ 8♣ Player 1 takes the 2 cards. Now has 48.
Q♠ 8♦ Player 1 takes the 2 cards. Now has 49.
4♥ 2♥ Player 1 takes the 2 cards. Now has 50.
A♥ K♥ Player 1 takes the 2 cards. Now has 51.
9♥ Q♥ Player 2 takes the 2 cards. Now has 2.
T♠ Q♥ Player 2 takes the 2 cards. Now has 3.
7♥ 9♥ Player 2 takes the 2 cards. Now has 4.
J♦ Q♥ Player 2 takes the 2 cards. Now has 5.
6♣ T♠ Player 2 takes the 2 cards. Now has 6.
T♦ 9♥ Player 1 takes the 2 cards. Now has 47.
5♥ 7♥ Player 2 takes the 2 cards. Now has 6.
T♥ Q♥ Player 2 takes the 2 cards. Now has 7.
8♠ J♦ Player 2 takes the 2 cards. Now has 8.
7♠ T♠ Player 2 takes the 2 cards. Now has 9.
4♠ 6♣ Player 2 takes the 2 cards. Now has 10.
9♦ 7♥ Player 1 takes the 2 cards. Now has 43.
6♦ 5♥ Player 1 takes the 2 cards. Now has 44.
6♠ Q♥ Player 2 takes the 2 cards. Now has 9.
5♣ T♥ Player 2 takes the 2 cards. Now has 10.
6♥ J♦ Player 2 takes the 2 cards. Now has 11.
4♣ 8♠ Player 2 takes the 2 cards. Now has 12.
5♠ T♠ Player 2 takes the 2 cards. Now has 13.
3♣ 7♠ Player 2 takes the 2 cards. Now has 14.
5♦ 6♣ Player 2 takes the 2 cards. Now has 15.
4♦ 4♠ War!
?3♠ ?Q♥ Face down cards.
2♣ 6♠ Player 2 takes the 6 cards. Now has 18.
A♣ T♥ Player 1 takes the 2 cards. Now has 35.
3♦ 5♣ Player 2 takes the 2 cards. Now has 18.
J♣ J♦ War!
?2♦ ?6♥ Face down cards.
9♣ 8♠ Player 1 takes the 6 cards. Now has 37.
7♣ 4♣ Player 1 takes the 2 cards. Now has 38.
3♥ T♠ Player 2 takes the 2 cards. Now has 15.
2♠ 5♠ Player 2 takes the 2 cards. Now has 16.
A♠ 7♠ Player 1 takes the 2 cards. Now has 37.
J♥ 3♣ Player 1 takes the 2 cards. Now has 38.
J♠ 6♣ Player 1 takes the 2 cards. Now has 39.
7♦ 5♦ Player 1 takes the 2 cards. Now has 40.
A♦ 4♠ Player 1 takes the 2 cards. Now has 41.
K♣ Q♥ Player 1 takes the 2 cards. Now has 42.
9♠ 6♠ Player 1 takes the 2 cards. Now has 43.
8♥ 4♦ Player 1 takes the 2 cards. Now has 44.
K♦ 3♠ Player 1 takes the 2 cards. Now has 45.
Q♦ 2♣ Player 1 takes the 2 cards. Now has 46.
K♠ 5♣ Player 1 takes the 2 cards. Now has 47.
T♣ 3♦ Player 1 takes the 2 cards. Now has 48.
Q♣ T♠ Player 1 takes the 2 cards. Now has 49.
8♣ 3♥ Player 1 takes the 2 cards. Now has 50.
Q♠ 5♠ Player 1 takes the 2 cards. Now has 51.
8♦ 2♠ Player 1 takes the 2 cards. Now has 52.
Player 1 wins the game!
</pre>
 
2,502

edits