War - Puzzle Discussion

Hi everyone!
this puzzle drives me crazy…. does anyone reach 100% ?
I get a problem with test #7 (Long) : (1146 instead of 1262)
and with test #9 (other PAT) looks like infinite game ….
thanks for your answers.

yes , 15213 ( 4 yesterday ) had reached 100%

OK thanks….
I’ll keep on working on my solution !!!

Bonjour,

je n’ai jamais réussi à passer les tests 3, 5 et 7.
J’ai refait avec un jeu de cartes le test 5. Je fini comme mon programme en “2 41” au lieu de “2 56”. Mes tas de cartes se comportent comme dans le programme (console.error).
J’ai lu tous les messages du forum. Je ne trouve plus d’idée.
Comment avancer ? Quelqu’un a t-il réussi ce test 5 ?
Merci pour votre aide.

yes … 15321 players did it …
“2 56” is test 3 … not 5 …

The only way i get “2 41” is forgetting to add cards won to my/oppenent’s deck.
don’t you know how to play this game (or haven’t you read the rules) ? :wink:

Hi dwarfie, thanks for trying to help me.
Ok for test03 and not 05. I red the rules a lot of times as also the forum. I acheaved all tests but 3, 5 and 7.
I verified again if i add cards correctly. About this, when i reproduce the example, i get the right order for both players.
But … as many of you did it, i know i have to look and look again what could be wrong with my code.

edit: ok for all tests now. i found my problem. At the end of each test i gave the number of turns of the winner instead of the total number of turns.

Thanks! I was also having this same issue because I was counting “wins” but they are irrelevant. The winner is the one with remaining card at the end of the game.

Victory Conditions

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

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.

2 Likes

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!

1 Like

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: