War - Puzzle Discussion

This challenge could be vastly improved by adding another simple PAT testcase such as e.g.:

Player1: ‘2C 5D’
Player2: ‘2D QD’

Alternatively, it would help significantly if the turn count were also required on a ‘PAT’ outcome.

1 Like

I’m having an issue with PAT as well. I pass all tests except those because in each case, I’m resolving winners (PAT#1-> p2 at turn 89 and PAT#2 -> p1 at turn 56). Clearly, I don’t understand the PAT conditions as the code passes all other tests easily. If you run out of cards in a war does that result in PAT? In my version, it results in a loss consistent with the rules of the card game IRL. Is that where I’m at odds with the instructions?

Good bless you.

8C KD AH QH 2S
8D 2D 3H 4D 3S

8C vs 8D. (player 1 and player 2 PAT) so war (we are on the same roll)

then 8C KD AH QH and 8D 2D 3H 4D on the table but just get them to see who takes them

then 2S vs 3S so player2 wins an take the rest of the cards

one roll and player 2 wins

2 1

2

1 Like

Good bless you. Test 3
Initial 1 6H 7H 6C QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Initial 2 JH AH KD AD 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H

Player 1 6H 7H 6C QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Player 2 JH AH KD AD 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H

Player 1 7H 6C QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Player 2 AH KD AD 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H 6H JH

Player 1 6C QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Player 2 KD AD 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H 6H JH 7H AH

Player 1 QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Player 2 AD 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H 6H JH 7H AH 6C KD

Player 1 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D

Player 2 9C 2D 2H JC 10 KC 10 JS JD 9D 9S KS AS KH 10 8S 2S 10 8H AC 2C 9H 6H JH 7H AH 6C KD QS AD

0 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC

Player 1 7S 8C 5S 8D 6S

Player 2 10 QH KC 4D 10 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC

Player 1 8C 5S 8D 6S

Player 2 QH KC 4D 10 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10

Player 1 5S 8D 6S

Player 2 KC 4D 10 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH

Player 1 8D 6S

Player 2 4D 10 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC

Player 1 6S 8D 4D

Player 2 10 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC

Player 1 8D 4D

Player 2 3S JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10

Player 1 4D 8D 3S

Player 2 JS 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10

Player 1 8D 3S

Player 2 7C JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS

Player 1 3S 8D 7C

Player 2 JD 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS

Player 1 8D 7C

Player 2 3C 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD

Player 1 7C 8D 3C

Player 2 9D 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD

Player 1 8D 3C

Player 2 4S 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D

Player 1 3C 8D 4S

Player 2 9S 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D

Player 1 8D 4S

Player 2 5H KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D 3C 9S

Player 1 4S 8D 5H

Player 2 KS QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D 3C 9S

Player 1 8D 5H

Player 2 QD AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D 3C 9S 4S KS

Player 1 5H

Player 2 AS 5C KH 3H 10 3D 8S 4H 10 4C 8H QC AC 7D 9H 2D JH 6D 7H 2H AH 2S KD 5D QS 2C AD 6H 9C 6C JC 7S 10 8C QH 5S KC 6S 10 4D JS 3S JD 7C 9D 3C 9S 4S KS 8D QD

2 56

2 56

And my last error. Who wins the game is not the one who wins more battles is the one won last one, or the one that has all the cards

I cant find my mistake can someone help me (my result to testcase 5: PAT instead of 1 52). To illustrate whats happening, every new round I print the round number and the amount of cards left per player. Every battle I print the value of the players cards in the battle (11=J, 12=Q, 13=K, 14=A).
Every war I print the cards that every player closes.
Here my logs:

Round 1 Cards left: 26 vs. 26
battle: 10 vs. 2
Round 2 Cards left: 27 vs. 25
battle: 13 vs. 9
Round 3 Cards left: 28 vs. 24
battle: 6 vs. 8
Round 4 Cards left: 27 vs. 25
battle: 10 vs. 4
Round 5 Cards left: 28 vs. 24
battle: 8 vs. 5
Round 6 Cards left: 29 vs. 23
battle: 14 vs. 14
WAR
p1 closes:
12
3
7
P2 closes:
11
12
7
battle: 13 vs. 5
Round 7 Cards left: 34 vs. 18
battle: 9 vs. 4
Round 8 Cards left: 35 vs. 17
battle: 2 vs. 6
Round 9 Cards left: 34 vs. 18
battle: 11 vs. 6
Round 10 Cards left: 35 vs. 17
battle: 13 vs. 12
Round 11 Cards left: 36 vs. 16
battle: 3 vs. 9
Round 12 Cards left: 35 vs. 17
battle: 2 vs. 10
Round 13 Cards left: 34 vs. 18
battle: 12 vs. 4
Round 14 Cards left: 35 vs. 17
battle: 14 vs. 11
Round 15 Cards left: 36 vs. 16
battle: 11 vs. 6
Round 16 Cards left: 37 vs. 15
battle: 7 vs. 3
Round 17 Cards left: 38 vs. 14
battle: 13 vs. 8
Round 18 Cards left: 39 vs. 13
battle: 10 vs. 3
Round 19 Cards left: 40 vs. 12
battle: 4 vs. 7
Round 20 Cards left: 39 vs. 13
battle: 14 vs. 9
Round 21 Cards left: 40 vs. 12
battle: 5 vs. 8
Round 22 Cards left: 39 vs. 13
battle: 5 vs. 2
Round 23 Cards left: 40 vs. 12
battle: 10 vs. 6
Round 24 Cards left: 41 vs. 11
battle: 2 vs. 8
Round 25 Cards left: 40 vs. 12
battle: 13 vs. 2
Round 26 Cards left: 41 vs. 11
battle: 9 vs. 6
Round 27 Cards left: 42 vs. 10
battle: 10 vs. 3
Round 28 Cards left: 43 vs. 9
battle: 4 vs. 9
Round 29 Cards left: 42 vs. 10
battle: 8 vs. 2
Round 30 Cards left: 43 vs. 9
battle: 5 vs. 10
Round 31 Cards left: 42 vs. 10
battle: 14 vs. 4
Round 32 Cards left: 43 vs. 9
battle: 12 vs. 7
Round 33 Cards left: 44 vs. 8
battle: 3 vs. 5
Round 34 Cards left: 43 vs. 9
battle: 7 vs. 8
Round 35 Cards left: 42 vs. 10
battle: 14 vs. 2
Round 36 Cards left: 43 vs. 9
battle: 11 vs. 8
Round 37 Cards left: 44 vs. 8
battle: 12 vs. 4
Round 38 Cards left: 45 vs. 7
battle: 7 vs. 9
Round 39 Cards left: 44 vs. 8
battle: 13 vs. 5
Round 40 Cards left: 45 vs. 7
battle: 5 vs. 10
Round 41 Cards left: 44 vs. 8
battle: 9 vs. 3
Round 42 Cards left: 45 vs. 7
battle: 4 vs. 5
Round 43 Cards left: 44 vs. 8
battle: 11 vs. 7
Round 44 Cards left: 45 vs. 7
battle: 6 vs. 8
Round 45 Cards left: 44 vs. 8
battle: 13 vs. 7
Round 46 Cards left: 45 vs. 7
battle: 12 vs. 9
Round 47 Cards left: 46 vs. 6
battle: 12 vs. 5
Round 48 Cards left: 47 vs. 5
battle: 4 vs. 10
Round 49 Cards left: 46 vs. 6
battle: 14 vs. 4
Round 50 Cards left: 47 vs. 5
battle: 11 vs. 5
Round 51 Cards left: 48 vs. 4
battle: 11 vs. 6
Round 52 Cards left: 49 vs. 3
battle: 6 vs. 8
Round 53 Cards left: 48 vs. 4
battle: 7 vs. 4
Round 54 Cards left: 49 vs. 3
battle: 3 vs. 10
Round 55 Cards left: 48 vs. 4
battle: 13 vs. 6
Round 56 Cards left: 49 vs. 3
battle: 8 vs. 8
WAR
p1 closes:
10
3
14
P2 closes:
3
10
PAT

Hopefully my comments below will help some people. (Also I want to add that for C# users this code is running in old .net 4 version.)

  • How to get cards of both players in proper order after one of the player’s win:
    • if player N wins the battle, cards are added to the bottom of his deck in this order:
    • 1st player’s card he used to battle
    • 1st player’s three war cards
    • 1st player’s card used to win the war through battle (or if not resolved it is still added and new war starts)
    • 1st player’s next three war cards if previous battle card didn’t resolve the war and new war started
    • … and so on until all war and battle cards of player 1 are retrieved and put at the bottom of player’s N deck
    • then …
    • 2nd player’s card he used to battle
    • … and so one and as above but for 2nd player
  • PAT happens if:
    • if any player runs out of cards but battle is in progress (if war is in progress it means that battle is in progress too)
    • battle is in progress if battle cards are out but winner cannot be determined as there are not enough cards on both sides to go to war
  • ROUNDS increase:
    • round only increase when battle is won/resolved (war is not won without battle being won/resolved first)
    • in complex case you can get:
    • unresolved battle (equal cards’ strength) which means war starts
    • war (which means just additional 3 cards taken (if there are cards available, otherwise PAT)
    • another unresolved battle
    • another war
    • resolved/won battle
    • … above is counted not as 3 rounds but as one as only one battle was won/resolved
1 Like

After get 3 card on step2 back to step 1 and get one more card and compare ( 3+1 =4 )

1 Like

This is a fairly good puzzle. I was however surprised that despite plenty of feedback the rules are still a bit ambiguous, forcing users to read the forum for answers.
I recommend that the author reviews the rules for clarity - especially useful to clarify the player 1 used cards before player 2. (“first” is ambiguous - could mean winner).
Also good to clarify the war rules regarding drawing 3 cards, and then repeating the battle/war process with a fourth card, etc. It was a bit mysterious, could have meant that a sub-stack was drawn and then battles played on the sub-stack. Best 2 of 3 won :slight_smile:

3 Likes

I’ve finally got it. Thank you all for comments.
So, I misunderstood how to do “war” part.

You battle with 1st card from each deck, if they are the same, you start the war.
For war you take next 3 cards and put them aside (don’t battle them), then you take 4th card and battle only that one. After that winner takes all (10 cards, 5 from P1, 5 from P2).

1 Like

@_CG_SaiksyApo

Each player gets a variable number of cards of the beginning of the game: that’s the player’s deck. Cards are placed face down on top of each deck.

I feel like the tests are performed as it the cards were placed at the bottom of each deck during the initial distribution.

If cards are placed on top of each deck during the initial distribution, the last input card should be the first to be played. But I feel like the first card to be played is the one that has been distributed first.

Tleilaxi’s example is the missing piece of information to complete the ambiguous puzzle description. This, or something similar, should be part of the official text.

The translation of the french statement is :

Each player has in his possession a variable number of cards at the start of the game which he keeps in his hand in the form of a pile, face down on top.

less ambiguous …

Looks like it is already in the statement

When a player wins a battle, they put back the cards at the bottom of their deck in a precise order. First the cards from the first player, then the one from the second player (for a “war”, all the cards from the first player then all the cards from the second player).

Hi dwarfie - here are the ambiguous cases I’m talking about that aren’t addressed by the description:

  1. When resolving a single “war” scenario, it doesn’t say which of Player 1’s cards to put at the bottom of the deck first. The war-starting card? the war-finishing card? Or should the 3 face-down cards be put at the bottom of the deck first?

  2. When a war starts, you are instructed to pull the next 3 cards and place them to the side. Do you draw and stack them one at a time, or draw all three and leave them face down in that vertical order? For example:

if this is P1’s deck (1st element is the top of P1’s deck): 5H QC KS 2D 4H …

one-at-a-time: KS QC 5H
three-at-at-time: 5H QC KS

Hey there,
Actually it’s three-at-a-time. or better to say 5-at-a-time. For example if it is as below:
P1: 5H QC KS 2D 4H 6C…
P2: 5C AH JD 4S 10S 9C…
after this round it will be:
P1: 6C …
P2: 9C … 5H QC KS 2D 4H 5C AH JD 4S 10S
:wink:

1 Like

The wording is atrocious. I also pointed out, a year ago:
“Special cases:
If a player runs out of cards during a “war” (when giving up the three cards or when doing the battle), then the game ends and both players are placed equally first.”

“Victory Conditions
A player wins when the other player no longer has cards in their deck.”

If a player runs out of cards during a war, they “no longer have cards in their deck,” but this is considered PAT. I was accused of playing with words. The puzzle description is undeniably inaccurate, in addition to being confusing with what “first” means.
The players are identified as “Player 1” and “Player 2”.
Seriously, it would take less than ten minutes to correct these problems. I abandoned the puzzle when my observations were dismissed.

1 Like

Hi all !

why does case 4 stop after one turn?

It would be nice if under “Step 1: the fight” it would also explain that the first players cards always get taken before the second players cards. I hadn’t read the part where it says that, and inferred that the winner takes their cards before they take the losers cards based on the example.

The worst part about this assumption is that it succeeds when tested against about 50% of the cases, leading me to believe the problem was something else. I spent a lot of time unnecessarily debugging, and testing my own inputs, just to realize I miss-understood the instructions.

Exactly.

Still a fun puzzle, I enjoyed it.

1 Like

Sorry you had to wait 3 months mate, you’ve probably already moved on from this puzzle. I’m assuming you miss-understood the way “wars” work. The decks look something like this:
P1: 8, K, A, Q, 2
P2: 8, 2, 3, 4, 3

The reason it ends is because both players tie the first card:
P1 lays an 8
P2 lays an 8 (Ties)
Then both players lay face-down 3 more cards ontop of the first card they layed down.
Then they reveal their last card and the winner takes everything.
P1 lays a 2
P2 lays a 3 (P2 wins)

P1 has no more cards.

1 Like