from deck import Deck
from player import Player
from random import shuffle
       
def deal(deck):
    '''Deal deck it into two piles of equal size, each a list of cards. Return a
    tuple containing the two piles. Precondition: deck contains an even number
    of cards.'''

    pile1 = []
    pile2 = []
    while not deck.empty():
        # Since deck size is even, we can deal pairs of cards.
        pile1.append(deck.get_next_card())
        pile2.append(deck.get_next_card())
        
    return pile1, pile2

def get_ready(player1, player2, pot):
    '''Prepare for a hand by taking one card from each player, face up. Return
    those two cards as a tuple and add them to the top of the pot. If one of the
    players runs out of cards during this process, None(s) will appear.'''
    
    card1 = player1.play_card()
    card2 = player2.play_card()
    if card1 is not None:
        card1.turn_over()
    if card2 is not None:
        card2.turn_over()
    pot.extend([card1, card2])
    
    return card1, card2

def print_hand_result(card1, comparison, card2, player1, player2):
    '''Print the result of a hand; card1 and card2 are the cards played,
    comparison is either '>' or '<' as appropriate, and player1 and player2 are
    the two Players.'''

    print '%s %s %s; Score = %s to %s' % \
       (card1, comparison, card2, player1.total_cards(), player2.total_cards())


def deal_to_pot(num_cards, player1, player2, pot):
    '''Get num_cards extra face-down cards from each player (the cards could
    be None), and add them to the pot.'''

    for i in range(num_cards):
        card1 = player1.play_card()
        card2 = player2.play_card()
        pot.extend([card1, card2])

def play_hand(card1, card2, pot, player1, player2):
    '''Process one hand of War.  card1 and card2 are the cards that were
    played by player1 and player2 and pot is the list of cards that are at stake
    in this hand, to be won by whichever player played the higher card. If card1
    and card2 are equal, deal two more face-down cards from each player to the
    pot.'''
    
    if card1 > card2:
        shuffle(pot)
        player1.win_hand(pot)
        print_hand_result(card1, '>', card2, player1, player2)
        pot = []

    elif card2 > card1:
        shuffle(pot)
        player2.win_hand(pot)
        # Indent a bit so we can differentiate player2 wins as they fly by in
        # the output.
        print "     ",
        print_hand_result(card1, '<', card2, player1, player2)
        pot = []

    else:
        # It's a tie.
        print "War!"
        deal_to_pot(2, player1, player2, pot)
            
    # If we just initiated a war, the pot has cards in it.  Otherwise,
    # we just completed a hand and the pot is empty.
    card1, card2 = get_ready(player1, player2, pot)

    # card1 could be None, as could card2, but if so, the calling loop will
    # terminate.
    return card1, card2, pot

def play():
    '''Play a round of the game War.'''

    # Create and shuffle a new deck, then deal it into two even piles.
    deck = Deck()
    deck.shuffle()
    pile1, pile2 = deal(deck)
    
    # Create the two players, each with one of the piles dealt.
    player1 = Player(pile1)
    player2 = Player(pile2)
    
    num_hands = 0
    # The pot of cards available to be won in this hand.
    pot = []
    card1, card2 = get_ready(player1, player2, pot)

    # As long as both players have cards left, keep playing hands of the game.
    # But if we hit 5000 hands, stop the game.
    while num_hands < 5000 and card1 is not None and card2 is not None:
        card1, card2, pot = play_hand(card1, card2, pot, player1, player2)
        num_hands += 1

    # Declare the winner.
    if player1 > player2:
        print "Player 1 wins the game!"
    else:
        print "Player 2 wins the game!"

        
if __name__ == "__main__":

    play()
