War - Puzzle Discussion

If the condition to break out of that 1 loop is never reached, the program will never end and that is a possible reason for your “err : Le délai d’exécution du processus a été dépassé.”

i am learning python since 3 days
all tips are welcome

The war requires the players to each place 3 cards down and then compare their 4th cards. Are you comparing their 4th cards in your code?

yes the battle and double battle check are good

It looks like you’re comparing the 5th cards to me. Could you please double check? Also, does your code clear your lists of 4 war cards after adding all of the cards to the war winner?

battle 1v1
discard 2/3/4
check 5
put (1/2/3/4/5)p1 + (1/2/3/4/5)p2 on player win file
right?
i have optimise with .extend but still the same issue

check 5 fail
got pat waitng 1 52

check 7 & 8 fail (time execution)

Ah sorry, you’re right about 2/3/4/5. But the execution still differs from the puzzle statement. I’ll discuss with you further via PM.

1 Like

Hi, I have a problem with medium lengh, one game one battle and long game.
My returns are smaller the thay should be. I don’t know why even if I checked it manualy i got same results. Does someone know why or what i’m doing wrong?

Do you mean the “26 cards, medium length” test case?
Does any of the discussion above give you any idea about what the problem may be?

Here’s my code. I haven’t programmed wars in yet, but test 3 still fails. It returns “2 50” rather than “2 56”.
Any ideas?

import sys
import math

values = dict()
values['2'] = 0
values['3'] = 1
values['4'] = 2
values['5'] = 3
values['6'] = 4
values['7'] = 5
values['8'] = 6
values['9'] = 7
values['10'] = 8
values['J'] = 9
values['Q'] = 10
values['K'] = 11
values['A'] = 12

p1cards = []
p2cards = []

n = int(input())  # the number of cards for player 1
for i in range(n):
    cardp_1 = input()[:-1]  # the n cards of player 1
    p1cards.append(values[cardp_1])
    #Only look at the value, not the suit
m = int(input())  # the number of cards for player 2
for i in range(m):
    cardp_2 = input()[:-1]  # the m cards of player 2
    p2cards.append(values[cardp_2])



moves = 0
while True:
    moves += 1
    
    print("P1 cards:", p1cards, file=sys.stderr)
    print("P2 cards:", p2cards, file=sys.stderr)

    p1card = p1cards.pop(-1)
    p2card = p2cards.pop(-1)
    print("P1 card:", p1card, file=sys.stderr)
    print("P2 card:", p2card, file=sys.stderr)
    if p1card > p2card:
        print("P1 wins", file=sys.stderr)
        p1cards.insert(0, p1card)
        p1cards.insert(0, p2card)
    elif p2card > p1card:
        print("P2 wins", file=sys.stderr)
        p2cards.insert(0, p1card)
        p2cards.insert(0, p2card)
    else:
        print("Error: war", file=sys.stderr)
        sys.exit(1)
    
    if len(p1cards) == 0:
        print(2, moves)
        break
    elif len(p2cards) == 0:
        print(1, moves)
        break
p1card = p1cards.pop(-1)
p2card = p2cards.pop(-1)

These are wrong. You are supposed to get the first card instead of the last card.

        p1cards.insert(0, p1card)
        p1cards.insert(0, p2card)
        p2cards.insert(0, p1card)
        p2cards.insert(0, p2card)

These are also wrong. You are supposed to put the cards at the back of the piles instead of the front.

I am not sure whether there are other issues, but I think you may fix these first, and then re-read the puzzle statement to make sure your code implements the rules accurately. :slight_smile:

Dude, Thank you!
I was struggling with the long test. And the problem was me for not reading. :slight_smile:

No way! But both players play simultaneously dont they? So if ‘first player’ doesnt mean ‘the player who came first’ (or won) what does ‘first player’ mean - surely there isnt one?

The order of who plays first and the consequent order of the cards played make a difference to the outcome in this game.

1 Like

The problem isn’t explained well. Do I have to start from right or left of the array. When someone wins a fight or a war in which order do the cards get back under the deck?

First, please note that the inputs are given to you in LINES. You aren’t given any arrays. So it’s up to you whether to insert each card at the beginning (“left”?) of the array or at the end (“right”?) of the array.

All that can be said is that the cards are given to you in the order that would be played.

And the puzzle statement does tell you the order which the cards get back under the deck:

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).

Even if you think the above is not clear, you are given an example in the part immediately below. So you can refer to that for a better understanding.

Hope this helps.

1 Like

Hey. ^^

I got an issue on the 3rd test “26 cards, medium length” : for an unknow reason, all my tries finish with a war. Here are a part of my main loop :

$duel = compare($p1->showOut(),$p2->showOut());
error_log(var_export("Tour $turns", true));
error_log(var_export($p1->showOut()." v ".$p2->showOut()." : ".($duel != 0 ? ($duel == 1 ? "P1" : "P2") : "War"), true));

if($duel != 0)
        ($duel == 1 ? $p1 : $p2)->add($p1->remove())->add($p2->remove());
else {
        break; // Not working on war logic yet
}
error_log(var_export(count($p1)." - ".count($p2), true));

And here the logs of my last test :

Queue::__set_state(array(
   'storage' => 
  array (
    0 => '6',
    1 => '7',
    2 => '6',
    3 => 'Q',
    4 => '7',
    5 => '8',
    6 => '6',
    7 => '5',
    8 => '6',
    9 => 'Q',
    10 => '4',
    11 => '3',
    12 => '7',
    13 => '3',
    14 => '4',
    15 => '5',
    16 => 'Q',
    17 => '5',
    18 => '3',
    19 => '3',
    20 => '8',
    21 => '4',
    22 => '4',
    23 => 'Q',
    24 => '5',
    25 => '7',
  ),
))
Queue::__set_state(array(
   'storage' => 
  array (
    0 => 'J',
    1 => 'A',
    2 => 'K',
    3 => 'A',
    4 => '9',
    5 => '2',
    6 => '2',
    7 => 'J',
    8 => '10',
    9 => 'K',
    10 => '10',
    11 => 'J',
    12 => 'J',
    13 => '9',
    14 => '9',
    15 => 'K',
    16 => 'A',
    17 => 'K',
    18 => '10',
    19 => '8',
    20 => '2',
    21 => '10',
    22 => '8',
    23 => 'A',
    24 => '2',
    25 => '9',
  ),
))
'Tour 1'
'6 v J : P2'
'25 - 27'
''
'Tour 2'
'7 v A : P2'
'24 - 28'
''
'Tour 3'
'6 v K : P2'
'23 - 29'
''
'Tour 4'
'Q v A : P2'
'22 - 30'
''
'Tour 5'
'7 v 9 : P2'
'21 - 31'
''
'Tour 6'
'8 v 2 : P1'
'22 - 30'
''
'Tour 7'
'6 v 2 : P1'
'23 - 29'
''
'Tour 8'
'5 v J : P2'
'22 - 30'
''
'Tour 9'
'6 v 10 : P2'
'21 - 31'
''
'Tour 10'
'Q v K : P2'
'20 - 32'
''
'Tour 11'
'4 v 10 : P2'
'19 - 33'
''
'Tour 12'
'3 v J : P2'
'18 - 34'
''
'Tour 13'
'7 v J : P2'
'17 - 35'
''
'Tour 14'
'3 v 9 : P2'
'16 - 36'
''
'Tour 15'
'4 v 9 : P2'
'15 - 37'
''
'Tour 16'
'5 v K : P2'
'14 - 38'
''
'Tour 17'
'Q v A : P2'
'13 - 39'
''
'Tour 18'
'5 v K : P2'
'12 - 40'
''
'Tour 19'
'3 v 10 : P2'
'11 - 41'
''
'Tour 20'
'3 v 8 : P2'
'10 - 42'
''
'Tour 21'
'8 v 2 : P1'
'11 - 41'
''
'Tour 22'
'4 v 10 : P2'
'10 - 42'
''
'Tour 23'
'4 v 8 : P2'
'9 - 43'
''
'Tour 24'
'Q v A : P2'
'8 - 44'
''
'Tour 25'
'5 v 2 : P1'
'9 - 43'
''
'Tour 26'
'7 v 9 : P2'
'8 - 44'
''
'Tour 27'
'8 v 6 : P1'
'9 - 43'
''
'Tour 28'
'2 v J : P2'
'8 - 44'
''
'Tour 29'
'6 v 7 : P2'
'7 - 45'
''
'Tour 30'
'2 v A : P2'
'6 - 46'
''
'Tour 31'
'8 v 6 : P1'
'7 - 45'
''
'Tour 32'
'2 v K : P2'
'6 - 46'
''
'Tour 33'
'5 v Q : P2'
'5 - 47'
''
'Tour 34'
'2 v A : P2'
'4 - 48'
''
'Tour 35'
'8 v 7 : P1'
'5 - 47'
''
'Tour 36'
'6 v 9 : P2'
'4 - 48'
''
'Tour 37'
'8 v 5 : P1'
'5 - 47'
''
'Tour 38'
'6 v J : P2'
'4 - 48'
''
'Tour 39'
'8 v 6 : P1'
'5 - 47'
''
'Tour 40'
'7 v 1 : P1'
'6 - 46'
''
'Tour 41'
'8 v Q : P2'
'5 - 47'
''
'Tour 42'
'5 v K : P2'
'4 - 48'
''
'Tour 43'
'8 v 4 : P1'
'5 - 47'
''
'Tour 44'
'6 v 1 : P1'
'6 - 46'
''
'Tour 45'
'7 v 3 : P1'
'7 - 45'
''
'Tour 46'
'1 v J : P2'
'6 - 46'
''
'Tour 47'
'8 v 7 : P1'
'7 - 45'
''
'Tour 48'
'4 v J : P2'
'6 - 46'
''
'Tour 49'
'6 v 3 : P1'
'7 - 45'
''
'Tour 50'
'1 v 9 : P2'
'6 - 46'
''
'Tour 51'
'7 v 4 : P1'
'7 - 45'
''
'Tour 52'
'3 v 9 : P2'
'6 - 46'
''
'Tour 53'
'8 v 5 : P1'
'7 - 45'
''
'Tour 54'
'7 v K : P2'
'6 - 46'
''
'Tour 55'
'6 v Q : P2'
'5 - 47'
''
'Tour 56'
'3 v A : P2'
'4 - 48'
''
'Arrêt supposé de la partie...'
'Tour 57'
'7 v 5 : P1'
'5 - 47'
''
'Tour 58'
'4 v K : P2'
'4 - 48'
''
'Tour 59'
'8 v 3 : P1'
'5 - 47'
''
'Tour 60'
'5 v 1 : P1'
'6 - 46'
''
'Tour 61'
'7 v 3 : P1'
'7 - 45'
''
'Tour 62'
'5 v 8 : P2'
'6 - 46'
''
'Tour 63'
'8 v 4 : P1'
'7 - 45'
''
'Tour 64'
'3 v 1 : P1'
'8 - 44'
''
'Tour 65'
'5 v 4 : P1'
'9 - 43'
''
'Tour 66'
'1 v 8 : P2'
'8 - 44'
''
'Tour 67'
'7 v Q : P2'
'7 - 45'
''
'Tour 68'
'3 v A : P2'
'6 - 46'
''
'Tour 69'
'8 v 7 : P1'
'7 - 45'
''
'Tour 70'
'4 v 9 : P2'
'6 - 46'
''
'Tour 71'
'3 v 2 : P1'
'7 - 45'
''
'Tour 72'
'1 v J : P2'
'6 - 46'
''
'Tour 73'
'5 v 6 : P2'
'5 - 47'
''
'Tour 74'
'4 v 7 : P2'
'4 - 48'
''
'Tour 75'
'8 v 2 : P1'
'5 - 47'
''
'Tour 76'
'7 v A : P2'
'4 - 48'
''
'Tour 77'
'3 v 2 : P1'
'5 - 47'
''
'Tour 78'
'2 v K : P2'
'4 - 48'
''
'Tour 79'
'8 v 5 : P1'
'5 - 47'
''
'Tour 80'
'2 v Q : P2'
'4 - 48'
''
'Tour 81'
'3 v 2 : P1'
'5 - 47'
''
'Tour 82'
'2 v A : P2'
'4 - 48'
''
'Tour 83'
'8 v 6 : P1'
'5 - 47'
''
'Tour 84'
'5 v 9 : P2'
'4 - 48'
''
'Tour 85'
'3 v 6 : P2'
'3 - 49'
''
'Tour 86'
'2 v J : P2'
'2 - 50'
''
'Tour 87'
'8 v 8 : War'

Do you know what did I miss or was doing wrong ?

All seem fine until ‘Tour 40’. Below is what I have for the turns just before and after for your reference.

Player 1: ['8D', '7S', '8C', '5S']
Player 2: ['6S', '10C', 'QH', 'KC', '4D', '10D', '3S', 'JS', '7C', 'JD', '3C', '9D', '4S', '9S', '5H', 'KS', 'QD', 'AS', '5C', 'KH', '3H', '10S', '3D', '8S', '4H', '10H', '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: ['10C', 'QH', 'KC', '4D', '10D', '3S', 'JS', '7C', 'JD', '3C', '9D', '4S', '9S', '5H', 'KS', 'QD', 'AS', '5C', 'KH', '3H', '10S', '3D', '8S', '4H', '10H', '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', '10D', '3S', 'JS', '7C', 'JD', '3C', '9D', '4S', '9S', '5H', 'KS', 'QD', 'AS', '5C', 'KH', '3H', '10S', '3D', '8S', '4H', '10H', '4C', '8H', 'QC', 'AC', '7D', '9H', '2D', 'JH', '6D', '7H', '2H', 'AH', '2S', 'KD', '5D', 'QS', '2C', 'AD', '6H', '9C', '6C', 'JC', '7S', '10C']
1 Like

Thanks for your answer, I finally notice what’s wrong : my code re-add “10” card at the bottom of the queue without the “0” ! I don’t know why this happen, but I can figure out this bug, now. ^^

EDIT : I found it. When the “add” method is called, its argument is verified before insertion, and if its length was equal to 1 or 2, the last character was removed. I fix it by replacing “substr” by “preg_replace”.

2 Likes

In description you put “0 < N, M < 1000”.
But it’s false. The last test pass the 1000 turns