War - Puzzle Discussion

The amount of wins doesn’t determine the winner. Check the problem statement.

Victory Conditions

A player wins when the other player no longer has cards in their deck.

1 Like

I think there is some defect for the rules.
" Victory Conditions:
A player wins when the other player no longer has cards in their deck."
If two player has the some cards and their are different in the some position(there is not any “war” in all of game turns). In this case, the cards in each player’s deck will be zero at some time,it can not decide which player win according to the rules of ‘Victory Conditions’.(just as test case 01).

Cards used during a turn are placed back under the turn’s winner, respecting a specific order, as it is stated in the rules.

So, at every turn’s sart, all cards are splitted between both players. Only one player can have 0 cards left.

1 Like

Continuing the discussion from War - Puzzle Discussion:

Why does my project pretend to DIE ???

I am also failing only in long test. Pretty hard to debug because of the number of rounds involved in this test!

I think there is a bug in either the conditions or the long test case.
My win count is p1: 632 and p2: 630 but p2 won the last round.

I cheated to print the last winner “2 1262”.

… and after checking other solutions the Output section should be rephrased:
“Player with cards on his hands wins.”

I am not able to get the Long and two PAT cases to work. Can someone who passed PAT test cases 8 and 9 share the number of rounds it took? It’s kind of annoying that when the answer is PAT, the number of rounds is not shown. This makes debugging more difficult.

By the way, the error message I get is “Process has timed out. This may mean that your solution is not optimized enough to handle some cases.”. I did check that in test case 7, my code kept running after at least 2000 rounds, while I believe the solution ended in 1262 rounds. Thus, I assumed that there’s a logic error in my code, not a resource problem. Please do correct me if I am misreading that. Thanks!

I don’t think I will reveal the solution by answering your question … so

89 for test 08 and 56 for test 09 !

1 Like

Solved it. Thanks for the help.

I cannot figure what is wrong with my code.

Here is the output of test case 26 Cards, Medium Length:
(I strictly insist to go through output first because I cannot find anything wrong in output)

cC22222 means C2 > C1 and vice versa


P1: 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
P2: JH AH KD AD 9C 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H
cC22222 | c1=6 c2=11 c1wins=0 c2wins=1 turn=1


P1: 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
P2: AH KD AD 9C 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H
cC22222 | c1=7 c2=14 c1wins=0 c2wins=2 turn=2


P1: 6C QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D
P2: KD AD 9C 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H
cC22222 | c1=6 c2=13 c1wins=0 c2wins=3 turn=3


P1: QS 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D
P2: AD 9C 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C
cC22222 | c1=12 c2=14 c1wins=0 c2wins=4 turn=4


P1: 7S 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D
P2: 9C 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS
cC22222 | c1=7 c2=9 c1wins=0 c2wins=5 turn=5


P1: 8D 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D
P2: 2D 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S
C1111111 | c1=8 c2=2 c1wins=1 c2wins=5 turn=6


P1: 6D 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D
P2: 2H JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S
C1111111 | c1=6 c2=2 c1wins=2 c2wins=5 turn=7


P1: 5S 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: JC 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S
cC22222 | c1=5 c2=11 c1wins=2 c2wins=6 turn=8


P1: 6S QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 10C KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S
cC22222 | c1=6 c2=10 c1wins=2 c2wins=7 turn=9


P1: QH 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: KC 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S
cC22222 | c1=12 c2=13 c1wins=2 c2wins=8 turn=10


P1: 4D 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 10D JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH
cC22222 | c1=4 c2=10 c1wins=2 c2wins=9 turn=11


P1: 3S 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: JS JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D
cC22222 | c1=3 c2=11 c1wins=2 c2wins=10 turn=12


P1: 7C 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: JD 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S
cC22222 | c1=7 c2=11 c1wins=2 c2wins=11 turn=13


P1: 3C 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 9D 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C
cC22222 | c1=3 c2=9 c1wins=2 c2wins=12 turn=14


P1: 4S 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 9S KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C
cC22222 | c1=4 c2=9 c1wins=2 c2wins=13 turn=15


P1: 5H QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: KS AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S
cC22222 | c1=5 c2=13 c1wins=2 c2wins=14 turn=16


P1: QD 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: AS KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H
cC22222 | c1=12 c2=14 c1wins=2 c2wins=15 turn=17


P1: 5C 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: KH 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD
cC22222 | c1=5 c2=13 c1wins=2 c2wins=16 turn=18


P1: 3H 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 10S 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C
cC22222 | c1=3 c2=10 c1wins=2 c2wins=17 turn=19


P1: 3D 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 8S 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H
cC22222 | c1=3 c2=8 c1wins=2 c2wins=18 turn=20


P1: 8C 4H 4C QC 5D 7D 8D 2D 6D 2H
P2: 2S 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D
C1111111 | c1=8 c2=2 c1wins=3 c2wins=18 turn=21


P1: 4H 4C QC 5D 7D 8D 2D 6D 2H 8C 2S
P2: 10H 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D
cC22222 | c1=4 c2=10 c1wins=3 c2wins=19 turn=22


P1: 4C QC 5D 7D 8D 2D 6D 2H 8C 2S
P2: 8H AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H
cC22222 | c1=4 c2=8 c1wins=3 c2wins=20 turn=23


P1: QC 5D 7D 8D 2D 6D 2H 8C 2S
P2: AC 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C
cC22222 | c1=12 c2=14 c1wins=3 c2wins=21 turn=24


P1: 5D 7D 8D 2D 6D 2H 8C 2S
P2: 2C 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC
C1111111 | c1=5 c2=2 c1wins=4 c2wins=21 turn=25


P1: 7D 8D 2D 6D 2H 8C 2S 5D 2C
P2: 9H JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC
cC22222 | c1=7 c2=9 c1wins=4 c2wins=22 turn=26


P1: 8D 2D 6D 2H 8C 2S 5D 2C
P2: JH 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D
cC22222 | c1=8 c2=11 c1wins=4 c2wins=23 turn=27


P1: 2D 6D 2H 8C 2S 5D 2C
P2: 6H AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D
cC22222 | c1=2 c2=6 c1wins=4 c2wins=24 turn=28


P1: 6D 2H 8C 2S 5D 2C
P2: AH 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D
cC22222 | c1=6 c2=14 c1wins=4 c2wins=25 turn=29


P1: 2H 8C 2S 5D 2C
P2: 7H KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D AH 6D
cC22222 | c1=2 c2=7 c1wins=4 c2wins=26 turn=30


P1: 8C 2S 5D 2C
P2: KD 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D AH 6D 7H 2H
cC22222 | c1=8 c2=13 c1wins=4 c2wins=27 turn=31


P1: 2S 5D 2C
P2: 6C AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D AH 6D 7H 2H KD 8C
cC22222 | c1=2 c2=6 c1wins=4 c2wins=28 turn=32


P1: 5D 2C
P2: AD QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D AH 6D 7H 2H KD 8C 6C 2S
cC22222 | c1=5 c2=14 c1wins=4 c2wins=29 turn=33


P1: 2C
P2: QS 9C 7S JC 5S 10C 6S KC QH 10D 4D JS 3S JD 7C 9D 3C 9S 4S KS 5H AS QD KH 5C 10S 3H 8S 3D 10H 4H 8H 4C AC QC 9H 7D JH 8D 6H 2D AH 6D 7H 2H KD 8C 6C 2S AD 5D
cC22222 | c1=2 c2=12 c1wins=4 c2wins=30 turn=34
INSIDE ANS = 2
2 34

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#define FOR(i,a,b) for(int i=a; i<b; i++)

using namespace std;

queue p1,p2,temp1,temp2;

int evaluate(string card){

int val;
if(card[0] == 'J')
    val=11;
else if (card[0] == 'Q')
    val=12;
else if (card[0] == 'K')
    val=13;
else if(card[0] == 'A')
    val=14;
else if(card[0] == '1' && card[1] == '0')
    val=10;
else
    val = card[0] - '0';

return val;

}

void push_temp1(){

while(!temp1.empty()){
    p1.push(temp1.front());
    temp1.pop();
}
while(!temp2.empty()){
    p1.push(temp2.front());
    temp2.pop();
}

}
void push_temp2(){

while(!temp2.empty()){
    p2.push(temp2.front());
    temp2.pop();
}
while(!temp1.empty()){
    p2.push(temp1.front());
    temp1.pop();
}

}
int main()
{
string ans;
bool flag=false,war=false;
int n,c1,c2,turns=0; // the number of cards for player 1
cin >> n; cin.ignore();
for (int i = 0; i < n; i++) {
string cardp1; // the n cards of player 1
cin >> cardp1; cin.ignore();
p1.push(cardp1);
}
int m; // the number of cards for player 2
cin >> m; cin.ignore();
for (int i = 0; i < m; i++) {
string cardp2; // the m cards of player 2
cin >> cardp2; cin.ignore();
p2.push(cardp2);
}

while(!flag){
     
     if(war == true && (p1.empty() || p2.empty())){//&& temp1.empty())|| (p2.empty() && temp2.empty() ) ) ){
        ans="PAT";
        break;
    }
    
    c1 = evaluate(p1.front());
    c2 = evaluate(p2.front());

    temp1.push(p1.front());
    temp2.push(p2.front());
    
    p1.pop();
    p2.pop();

    if(c1 > c2){
        turns++;
        push_temp1();
        war=false;
    }
    else if(c2 > c1){
        turns++;
        push_temp2();
        war=false;
    }
    else{
        war=true;
        FOR(i,0,3){
            if(p1.empty() || p2.empty()){
                ans="PAT";
                flag=true;
                break;
            }
            temp1.push(p1.front());
            temp2.push(p2.front());
    
            p1.pop();
            p2.pop();
        }
    }
    
   if (war == false && p1.empty() && temp1.empty()){
     flag=true;
     ans='2';  
   } 
   else if(war == false && p2.empty() && temp2.empty()){
     flag=true;
     ans='1';
   }
}
if(ans=="PAT")
    cout<<ans<<endl;
else
    cout <<ans<<' '<<turns<< endl;

}

Your solution does not respect the rules for placing the cards won back in the deck.
Hence the order of plays is incorrect, and you get the wrong answer.

3 Likes

Thankyou @hemhel. Now, I got it right.

Moral of the struggle: Simple misunderstanding of English can cause big bugs in your code :stuck_out_tongue:

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.

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

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