# War - Puzzle Discussion

Hi I am new to scripting and trying to improve my scripting skills.
I am trying to solve the puzzle “war” but found it hard.
Could someone help me find what is wrong in my script?
Sorry for such post.

(using python)

import sys

indata=[[input() for a in range(int(input()))] for i in [0]*2]
print(“player 1 cards: %a” % indata[0], file=sys.stderr)
print(“player 2 cards: %a” % indata[1], file=sys.stderr)
data=[[int(a[0]) if a[0].isdigit() else 11 if a[0]==“J” else 12 if a[0]==“Q” else 13 if a[0]==“K” else 14 for a in b] for b in indata]
print(“player 1 cards: %a” % data[0], file=sys.stderr)
print(“player 2 cards: %a” % data[1], file=sys.stderr)

t=0
n=1
while 1:
if data[0][n-1]==data[1][n-1]:
n+=4
print(“battle! check%dth card” % n, file=sys.stderr)
if n>min(len(data[0]),len(data[1])):
print(“PAT”)
break
continue
t+=1
if data[0][n-1]>data[1][n-1]:
data[0]=data[0][n:]+data[0][:n]+data[1][:n]
data[1]=data[1][n:]
elif data[0][n-1]<data[1][n-1]:
data[1]=data[1][n:]+data[0][:n]+data[1][:n]
data[0]=data[0][n:]
n=1
print(“turn:%d checkpoint:%d player 1 cards: %a” % (t,n,data[0]), file=sys.stderr)
print(“turn:%d checkpoint:%d player 2 cards: %a” % (t,n,data[1]), file=sys.stderr)
if len(data[0])<1 and len(data[1])<1:
print(“PAT”)
break
elif len(data[0])<1:
print(“2 %a” % t)
break
elif len(data[1])<1:
print(“1 %a” % t)
break

print(“end of calc”, file=sys.stderr)

this game 's pretty confuse, got around 87% and can not pass the long test, not sure why, anyone got 100% please if you can send me the method to do this one , thanks.

1 Like

In the first paragraph, the rules stated “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 took this to mean that in the beginning, each dealt card is placed on top of the deck i.e. to the front of the queue, like this:

``````for (int i = 0; i < n; i++) {
string cardp1; // the n cards of player 1
cin >> cardp1; cin.ignore();
deck1.push_front(cardValue(cardp1[0]));
}
``````

But in the whole game (including the beginning!), cards are only ever inserted to the bottom and taken from the top.

Please at least mention in the Input-section, that the first card should be on top of the deck.

Failing test case 5:

Found: 1 56
Expected: 1 52
How am I off by just 4 rounds?

Also, when submitting it says that I pass test case 5.

Thanks

is A worth 11 or 1

Neither… it’s worth one more than a King, so 14. It could be a million, really, as long as it’s worth more than everything else.

@LokKamCheung : Did you able to solve it? I am also facing same issue.

I seem to keep failing case 7 (long game) in testing modus, and case 5 (one game, one battle) and case 7 again in submit modus.

Concerning case 7 in testing modus, my code gives me a PAT in 174 rounds instead of p2 win in 1262 rounds.

My understanding of the rules is (in pseudocode):

while (both players have at least 1 card left)
{
if card p1 > p2 -> add p1 and p2 to end of p1’s cards
else if card p1 < p2 -> add p1 and p2 to end of p2’s cards
else {
//Battle
if (either player have less than 4 cards left in their hand) PAT
else {
player with best card gets all of p1’s table cards and all of p2’s table cards at end of his hand cards
if equal, repeat battle
}
}

Can anyone figure out what I’m doing wrong?

1 Like

I don’t see this in the previous comments. Some configurations are cyclic, or reach a cycle. For instance, these simple ones:
P1: KC 8D
P2: 8C KD
or
P1: 8C KD
P2: KC 8D

They should probably be considered PAT. Maybe something to take into account if you ever decided to tweak the puzzle to make a Hard version of it ?

I am having a very similar problem. It is frustrating because as far as I am concerned, my code should work perfectly fine (though, I am new to c++ and may just have some errors). Case 7 is the only unsuccessful one for me because I get PAT as well, and have no idea why.

1 Like

Hi, have same problem.
Passed all tests except “long game”.
In the end of game have:
p1 score: 632 <- win
p2 score: 630
max war dept: 2
games total: 1262
out: 1 1262 <- wrong !!! Why!?
expected out: 2 1262

But if i have a wrong logic to calculate a wins for players, how can i pass all other tests?

3 Likes

How is it possible that my code passes every test case except for “long game”, in which it returns PAT? There must be a bug in my code or something. I tried this problem about 2 weeks ago, and couldn’t do it. I just came back to it and still can’t figure it out.

Update: Fixed the problem. I was declaring a variable in my recursive “war()” function instead of passing by reference.

Hey man, thanks very much. That was so simple.
I probably need to learn again how to read and then start learning programming.
xD

I pass every case except the long game. It times out and i see it running up into the tens of thousands of turns when it expects it to end on 1000 something.

THIS!!! this has solved my issues =)) same here, taking only the first char forgeting that 10 is made from 2 chars…
THANK YOU!!!

2 Likes

When I saw “2 34” I instantly knew someone had the same issue. That rules makes no sense =))))

2 Likes

I had the same problem, for me it was because I wasn’t returning the cards to the winner of a chained war in the correct order. Originally I returned them to the winner from most recent war first then the war before that, all the way back to the original war. I had similar results to you, “Long game” timed out, and it was into the hundreds of thousands of turns by the time it got there.

Try changing your algorithm so that it’s reversed, so the winner of a number of chained wars gets P1’s cards from the first war, P2’s cards from the first war, P1’s cards from the second war, P2’s cards from the second war and so on.

Hi,

There’s something that I didn’t understand in the game rules. It’s mentioned that:

For example, if the card distribution is the following:
Player 1 : 10D 9S 8D KH 7D 5H 6S
Player 2 : 10H 7H 5C QC 2C 4H 6D
Then after one game turn, it will be:
Player 1 : 5H 6S 10D 9S 8D KH 7D 10H 7H 5C QC 2C
Player 2 : 4H 6D

but, the final result (4H 6D) is obtained after 2 game turns, first the war (we have 10D vs 10H) then the battle (7D vs 2C), I can’t figure out why the game example mentions 1 turn.

Does any one knows how this is possible ?

1 Like

A war involves 5 cards; the initial one, 3 cards, and then the deciding battle.
The battle happens on the same turn as the war.

4 Likes

Thanks a lot, that was very helpful.