Coding Solution

Winamax Battle – CodinGame solution in Python3

Mediumsimulationqueuescard gameUpdated
Short answer

Winamax Battle is a pure simulation of the card game War. Keep each deck as a queue, play rounds until one deck is empty, and on equal cards run a war where each player puts three extra cards face down and a fourth face up, repeating if they tie again. The winner takes player 1's played cards first, then player 2's, and if a player runs out of cards during a war the result is PAT.

The problem

Two players each start with a deck from a standard 52-card set. Every round, both reveal their top card; the higher value wins both cards, which go to the bottom of the winner's deck. Values go from 2 up to Ace, suits are irrelevant. On a tie, a war starts: each player places three cards face down and one face up, and the new face-up cards decide, possibly triggering further wars.

Print the winner (1 or 2) and the number of rounds played, where a round including all its wars counts once. If a player cannot place the cards required by a war, print PAT instead.

Approach, step by step

  1. Convert cards to numeric valuesStrip the last character (the suit) and map the rest: 2 to 10 as numbers, then J, Q, K, A to 11, 12, 13, 14. Watch out for '10', the only two-character rank.
  2. Use real queuesStore both decks in a deque (or a circular buffer). Taking from the top and appending to the bottom are then O(1), and the code reads like the rules.
  3. Play one roundPop one card from each deck into two piles. While the last cards of both piles are equal, check that each player has at least 4 cards; if not, the game ends in PAT; otherwise move 4 cards from each deck to its pile.
  4. Award the cards in the right orderThe round winner appends pile1 (all of player 1's cards, in order) and then pile2 to the bottom of their own deck, regardless of who won. Increment the round counter once per round.
  5. Stop when a deck is emptyBefore starting a round, if one deck is empty, print the other player's number followed by the round count.

Complexity

TimeO(R)
SpaceO(52)

R is the number of rounds; each round moves a bounded number of cards. The posted code uses list.pop(0) and list concatenation, which are O(deck size), but with at most 52 cards that is negligible.

Solution code

Winamax Battle – Python3

This version relies on IndexError to detect an empty deck, both at the start of a round (game over) and during a war (PAT). Note that the docstring of compare says it returns 1 when card 2 wins; the code actually returns -1 in that case.

# Game configuration : the differents cards possible and their value
# We would only need to modify this dictionnary if the order of the 
# card changed, or if we wanted to play with a 32 card game for example
values = {'2D':2, '2H':2, '2C':2, '2S':2, 
          '3D':3, '3H':3, '3C':3, '3S':3,
          '4D':4, '4H':4, '4C':4, '4S':4, 
          '5D':5, '5H':5, '5C':5, '5S':5,
          '6D':6, '6H':6, '6C':6, '6S':6,
          '7D':7, '7H':7, '7C':7, '7S':7,
          '8D':8, '8H':8, '8C':8, '8S':8, 
          '9D':9, '9H':9, '9C':9, '9S':9,
          '10D':10, '10H':10, '10C':10, '10S':10, 
          'JD':11, 'JH':11, 'JC':11, 'JS':11,
          'QD':12, 'QH':12, 'QC':12, 'QS':12, 
          'KD':13, 'KH':13, 'KC':13, 'KS':13,
          'AD':14, 'AH':14, 'AC':14, 'AS':14}
    
def compare(c1, c2):
    ''' returns : 
    - 0 if card1 and card 2 have equal values
    - 1 if card1 wins over card2
    - 1 if card2 wins over card1'''
    global values

    if values[c1] < values[c2]:
        return -1
    elif values[c1] > values[c2]:
        return 1
    else:
        return 0


# Reads input to initialize decks
deck1, deck2 = [], []
for i in range(int(input())):
    deck1.append(input())
for i in range(int(input())):
    deck2.append(input())

nbRounds = 0

while True:
    # Each loop is one game round
    # pile1 and pile2 are the cards played this turn by player 1 and 2

    try:    
        pile1, pile2 = [deck1.pop(0)], [deck2.pop(0)]
    except IndexError:
        # this error means one of the two decks is empty
        winner = '1' if len(deck1) > 0 else '2'
        print(winner, nbRounds)
        break

    nbRounds+=1
    
    # In case of war(s)
    try:
        # While there is a war, each player plays 3 other cards
        while compare(pile1[-1], pile2[-1]) == 0:
            for i in range(4):
                pile1.append(deck1.pop(0))
                pile2.append(deck2.pop(0))
    except IndexError:
        # an index error means one of the two decks got empty
        # during a war, and both players are equally first
        print('PAT')
        break

    # Moves the 2 piles to the round winner's deck bottom
    if compare(pile1[-1], pile2[-1]) == -1:
        deck2 = deck2 + pile1 + pile2
    else:
        deck1 = deck1 + pile1 + pile2
            

Common mistakes

Going further

Most bugs in Winamax Battle come from the order of cards, so a strong debugging trick is to print both decks after every round on the first test and compare with a hand simulation of three or four rounds; the statement's example is short enough to verify. Another detail worth knowing: the game can theoretically loop forever with some deck orders, and robust solutions guard against that with a round limit or by storing seen (deck1, deck2) states in a set. CodinGame's tests always terminate, but the cycle check is a good habit for simulation puzzles and costs very little with 52 cards.

FAQ

How many cards are played in a war in Winamax Battle?

Each player adds three face-down cards and one face-up card, so four per player. The face-up cards are compared; another tie starts another war with the same rule.

Does a war count as an extra round?

No. The whole sequence, from the first face-off to the resolution of the last war, counts as a single round.

When is the answer PAT?

When either player does not have enough cards to complete a war. The game stops immediately and both players are considered tied.

Similar puzzles

★ 5.0 · 16 reviews · 928 users Add to Chrome