# The Card Game 'War' # import cards PLYR1 = 0 PLYR2 = 1 PRMPT = """ Enter: 'c' - to continue 'q' - to quit """ players = [input("Enter the name of the first player: "), input("Enter the name of the second player: ")] def print_piles(players, piles): for i in range(len(players)): this_pile = piles[i] print(players[i]+":") while len(this_pile) > 10: p_str = " ".join([str(c) for c in this_pile[:10]]) print(p_str) this_pile = this_pile[10:] for c in this_pile: print(c, end = " ") print() def get_command(PRMPT): command = input(PRMPT).lower() while command != 'c' and command != 'q': print("Illegal command:", command) command = input(PRMPT).lower() return command def game_over(piles): return piles[PLYR1] == [] or piles[PLYR2] == [] deck = cards.Deck() deck.shuffle() piles = [[deck.deal() for i in range(26)], [deck.deal() for i in range(26)]] print_piles(players, piles) command = get_command(PRMPT) while command != 'q' and not game_over(piles): card1, card2 = piles[PLYR1][0], piles[PLYR2][0] piles = [piles[PLYR1][1:], piles[PLYR2][1:]] print(players[PLYR1], "plays", card1) print(players[PLYR2], "plays", card2) rank1, rank2 = card1.get_rank(), card2.get_rank() if rank1 == 1: rank1 = 13 # make Ace be high if rank2 == 1: rank2 = 13 # make Ace be high if rank1 == rank2: print("This round ends in a tie.") piles[PLYR1] += [card1] piles[PLYR2] += [card2] print_piles(players, piles) else: if rank1 > rank2: winner = PLYR1 else: winner = PLYR2 print(players[winner],"takes this round") piles[winner] += [card1, card2] print_piles(players, piles) command = get_command(PRMPT) if len(piles[PLYR1]) == len(piles[PLYR2]): print("Game ended in a tie.") else: if len(piles[PLYR1]) > len(piles[PLYR2]): winner = PLYR1 else: winner = PLYR2 print(players[winner], "won the game.")