War card game: Difference between revisions

Line 244:
Player 1 wins the game.
</pre>
 
=={{header|Nim}}==
We use module "playing_cards" from task https://rosettacode.org/wiki/Playing_cards.
<lang Nim>import strformat
import playing_cards
 
const
None = -1
Player1 = 0
Player2 = 1
 
type Player = range[None..Player2]
 
const PlayerNames: array[Player1..Player2, string] = ["Player 1", "Player 2"]
 
#---------------------------------------------------------------------------------------------------
 
proc `<`(a, b: Card): bool =
## Compare two cards by their rank, Ace being the greatest.
if a.rank == Ace: false
elif b.rank == Ace: true
else: a.rank < b.rank
 
#---------------------------------------------------------------------------------------------------
 
proc displayRound(round: int; hands: openArray[Hand]; card1, card2: string; text: string) =
## Display text for a round.
stdout.write &"Round {round:<4} "
stdout.write &"Cards: {hands[Player1].len:>2}/{hands[Player2].len:<2} "
stdout.write &"{card1:>3} {card2:>3} "
echo text
 
#---------------------------------------------------------------------------------------------------
 
proc outOfCards(player: Player) =
## Display a message when a player has run out of cards.
echo &"{PlayerNames[player]} has run out of cards."
 
#---------------------------------------------------------------------------------------------------
 
proc doRound(hands: var openArray[Hand]; num: Positive) =
## Execute a round.
 
var stack1, stack2: seq[Card]
var winner: Player = None
 
while winner == None:
let card1 = hands[Player1].draw()
let card2 = hands[Player2].draw()
stack1.add card1
stack2.add card2
if card1.rank != card2.rank:
winner = if card1 < card2: Player2 else: Player1
displayRound(num, hands, $card1, $card2, &"{PlayerNames[winner]} takes the cards.")
else:
# There is a war.
displayRound(num, hands, $card1, $card2, "This is a war.")
if hands[Player1].len == 0:
winner = Player2
elif hands[Player2].len == 0:
winner = Player1
else:
# Add a hidden card on stacks.
stack1.add hands[Player1].draw()
stack2.add hands[Player2].draw()
displayRound(num, hands, " ?", " ?", "Cards are face down.")
# Check if each player has enough cards to continue the war.
if hands[Player1].len == 0:
Player1.outOfCards()
winner = Player2
elif hands[Player2].len == 0:
Player2.outOfCards()
winner = Player1
 
# Update hands.
var stack = stack1 & stack2
stack.shuffle()
hands[winner] = stack & hands[winner]
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
var deck = initDeck()
deck.shuffle()
 
var hands = deck.deal(2, 26)
var num = 0
while true:
inc num
hands.doRound(num)
if hands[Player1].len == 0:
echo "Player 2 wins this game."
break
if hands[Player2].len == 0:
echo "Player 1 wins this game."
break</lang>
 
{{out}}
Example of a short game.
<pre>Round 1 Cards: 25/25 10♣ 5♠ Player 1 takes the cards.
Round 2 Cards: 26/24 3♦ 7♦ Player 2 takes the cards.
Round 3 Cards: 25/25 10♦ 10♥ This is a war.
Round 3 Cards: 24/24 ? ? Cards are face down.
Round 3 Cards: 23/23 A♦ 4♥ Player 1 takes the cards.
Round 4 Cards: 28/22 4♠ J♦ Player 2 takes the cards.
Round 5 Cards: 27/23 9♠ Q♣ Player 2 takes the cards.
Round 6 Cards: 26/24 A♣ 2♦ Player 1 takes the cards.
Round 7 Cards: 27/23 Q♦ 3♠ Player 1 takes the cards.
Round 8 Cards: 28/22 5♥ 3♥ Player 1 takes the cards.
Round 9 Cards: 29/21 2♥ 2♠ This is a war.
Round 9 Cards: 28/20 ? ? Cards are face down.
Round 9 Cards: 27/19 4♦ 10♠ Player 2 takes the cards.
Round 10 Cards: 26/24 K♠ 6♣ Player 1 takes the cards.
Round 11 Cards: 27/23 8♥ 7♣ Player 1 takes the cards.
Round 12 Cards: 28/22 4♣ 5♦ Player 2 takes the cards.
Round 13 Cards: 27/23 8♣ 8♦ This is a war.
Round 13 Cards: 26/22 ? ? Cards are face down.
Round 13 Cards: 25/21 9♦ 6♠ Player 1 takes the cards.
Round 14 Cards: 30/20 K♥ 9♥ Player 1 takes the cards.
Round 15 Cards: 31/19 J♠ 7♥ Player 1 takes the cards.
Round 16 Cards: 32/18 J♣ K♦ Player 2 takes the cards.
Round 17 Cards: 31/19 2♣ 3♣ Player 2 takes the cards.
Round 18 Cards: 30/20 A♥ 6♥ Player 1 takes the cards.
Round 19 Cards: 31/19 A♠ 6♦ Player 1 takes the cards.
Round 20 Cards: 32/18 8♠ 5♣ Player 1 takes the cards.
Round 21 Cards: 33/17 10♣ 7♦ Player 1 takes the cards.
Round 22 Cards: 34/16 5♠ 3♦ Player 1 takes the cards.
Round 23 Cards: 35/15 Q♥ 4♠ Player 1 takes the cards.
Round 24 Cards: 36/14 10♦ J♦ Player 2 takes the cards.
Round 25 Cards: 35/15 Q♠ 9♠ Player 1 takes the cards.
Round 26 Cards: 36/14 A♦ Q♣ Player 1 takes the cards.
Round 27 Cards: 37/13 4♥ 4♦ This is a war.
Round 27 Cards: 36/12 ? ? Cards are face down.
Round 27 Cards: 35/11 2♦ 10♠ Player 2 takes the cards.
Round 28 Cards: 34/16 A♣ 2♠ Player 1 takes the cards.
Round 29 Cards: 35/15 Q♦ 7♠ Player 1 takes the cards.
Round 30 Cards: 36/14 3♠ 2♥ Player 1 takes the cards.
Round 31 Cards: 37/13 5♥ 5♦ This is a war.
Round 31 Cards: 36/12 ? ? Cards are face down.
Round 31 Cards: 35/11 6♣ J♣ Player 2 takes the cards.
Round 32 Cards: 34/16 K♠ K♦ This is a war.
Round 32 Cards: 33/15 ? ? Cards are face down.
Round 32 Cards: 32/14 7♣ 2♣ Player 1 takes the cards.
Round 33 Cards: 37/13 K♣ J♦ Player 1 takes the cards.
Round 34 Cards: 38/12 6♠ 10♦ Player 2 takes the cards.
Round 35 Cards: 37/13 J♥ 10♥ Player 1 takes the cards.
Round 36 Cards: 38/12 8♣ 2♦ Player 1 takes the cards.
Round 37 Cards: 39/11 9♦ 9♣ This is a war.
Round 37 Cards: 38/10 ? ? Cards are face down.
Round 37 Cards: 37/9 9♥ 10♠ Player 2 takes the cards.
Round 38 Cards: 36/14 K♥ 4♦ Player 1 takes the cards.
Round 39 Cards: 37/13 7♥ 6♣ Player 1 takes the cards.
Round 40 Cards: 38/12 J♠ 5♥ Player 1 takes the cards.
Round 41 Cards: 39/11 A♥ 4♣ Player 1 takes the cards.
Round 42 Cards: 40/10 6♥ J♣ Player 2 takes the cards.
Round 43 Cards: 39/11 A♠ 3♥ Player 1 takes the cards.
Round 44 Cards: 40/10 6♦ 5♦ Player 1 takes the cards.
Round 45 Cards: 41/9 8♠ 10♦ Player 2 takes the cards.
Round 46 Cards: 40/10 5♣ 6♠ Player 2 takes the cards.
Round 47 Cards: 39/11 7♦ 8♦ Player 2 takes the cards.
Round 48 Cards: 38/12 10♣ 9♦ Player 1 takes the cards.
Round 49 Cards: 39/11 3♦ 10♠ Player 2 takes the cards.
Round 50 Cards: 38/12 5♠ 4♥ Player 1 takes the cards.
Round 51 Cards: 39/11 4♠ 9♥ Player 2 takes the cards.
Round 52 Cards: 38/12 Q♥ 9♣ Player 1 takes the cards.
Round 53 Cards: 39/11 Q♠ 6♥ Player 1 takes the cards.
Round 54 Cards: 40/10 9♠ J♣ Player 2 takes the cards.
Round 55 Cards: 39/11 Q♣ 8♠ Player 1 takes the cards.
Round 56 Cards: 40/10 A♦ 10♦ Player 1 takes the cards.
Round 57 Cards: 41/9 A♣ 5♣ Player 1 takes the cards.
Round 58 Cards: 42/8 2♠ 6♠ Player 2 takes the cards.
Round 59 Cards: 41/9 Q♦ 7♦ Player 1 takes the cards.
Round 60 Cards: 42/8 7♠ 8♦ Player 2 takes the cards.
Round 61 Cards: 41/9 3♠ 3♦ This is a war.
Round 61 Cards: 40/8 ? ? Cards are face down.
Round 61 Cards: 39/7 7♣ 9♥ Player 2 takes the cards.
Round 62 Cards: 38/12 8♥ 4♠ Player 1 takes the cards.
Round 63 Cards: 39/11 K♦ 9♠ Player 1 takes the cards.
Round 64 Cards: 40/10 2♣ J♣ Player 2 takes the cards.
Round 65 Cards: 39/11 3♣ 2♠ Player 1 takes the cards.
Round 66 Cards: 40/10 K♠ 6♠ Player 1 takes the cards.
Round 67 Cards: 41/9 J♦ 7♠ Player 1 takes the cards.
Round 68 Cards: 42/8 K♣ 8♦ Player 1 takes the cards.
Round 69 Cards: 43/7 10♥ 10♠ This is a war.
Round 69 Cards: 42/6 ? ? Cards are face down.
Round 69 Cards: 41/5 8♣ 2♥ Player 1 takes the cards.
Round 70 Cards: 46/4 2♦ 9♥ Player 2 takes the cards.
Round 71 Cards: 45/5 K♥ 3♠ Player 1 takes the cards.
Round 72 Cards: 46/4 4♦ 7♣ Player 2 takes the cards.
Round 73 Cards: 45/5 7♥ 2♣ Player 1 takes the cards.
Round 74 Cards: 46/4 6♣ J♣ Player 2 takes the cards.
Round 75 Cards: 45/5 5♥ 2♦ Player 1 takes the cards.
Round 76 Cards: 46/4 J♠ 9♥ Player 1 takes the cards.
Round 77 Cards: 47/3 A♥ 7♣ Player 1 takes the cards.
Round 78 Cards: 48/2 4♣ 4♦ This is a war.
Round 78 Cards: 47/1 ? ? Cards are face down.
Round 78 Cards: 46/0 A♠ J♣ Player 1 takes the cards.
Player 1 wins this game.</pre>
 
=={{header|Perl}}==
Anonymous user