#War Card Game


War Card Game

This is a version of the card game War, and it is also my final Python A project.

# War Card game # A player wins if they gain 52 cards or have more cards after 5 rounds. These amounts can be adjusted if needed. # There are 2 checks placed in the code for player and opponent deck contents, these show the movement of cards. # A tie breaker within a tie breaker is decided with a coin flip in this version of the game. from random import randint total_cards = ["2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5", "5", "5", "5", "6", "6", "6", "6", "7", "7", "7", "7", "8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11", "12", "12", "12", "12", "13", "13", "13", "13", "14", "14", "14", "14"] player_cards = [] opponent_cards = [] for x in range(0, 26): y = 51-x card_number = randint(0, y) card = total_cards[card_number] player_cards.append(card) total_cards.pop(card_number) for x in range(0, 26): y = 25-x card_number = randint(0, y) card = total_cards[card_number] opponent_cards.append(card) total_cards.pop(card_number) print("player") print(player_cards) print("opponent") print(opponent_cards) # This is a check to see the starting moves, feel free to remove if using the code. p = len(player_cards) o = len(opponent_cards) moves = 0 while p != 52 and o != 52 and moves < 5: p_card = int(player_cards[0]) o_card = int(opponent_cards[0]) print("You have placed down a ", p_card) print("The opponent has placed down a ", o_card) moves = moves + 1 if p_card > o_card: continue_playing = input("Your card is higher, take all cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": player_cards.append(str(o_card)) player_cards.pop(0) player_cards.append(str(p_card)) opponent_cards.pop(0) elif o_card > p_card: continue_playing = input("Your card is lower, give opponent cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": opponent_cards.append(str(p_card)) opponent_cards.pop(0) opponent_cards.append(str(o_card)) player_cards.pop(0) elif o_card == p_card: continue_playing = input("The cards are the same, time for a tie-breaker? Enter yes to continue: ") if continue_playing == "yes": print("You and the opponent place 3 cards face down and one face up. Lets see who wins the war!") p_card1 = player_cards[1] p_card2 = player_cards[2] p_card3 = player_cards[3] p_card4 = int(player_cards[4]) o_card1 = opponent_cards[1] o_card2 = opponent_cards[2] o_card3 = opponent_cards[3] o_card4 = int(opponent_cards[4]) print("You place down a ", p_card4, "and your opponent has placed down a ", o_card4) if p_card4 > o_card4: print("Congrats! You won the war and gain a ", o_card1, o_card2, o_card3, "and", o_card4) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif o_card4 > p_card4: print("You lost the war, your opponent gains a ", p_card1, p_card2, p_card3, "and", p_card4) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) elif o_card4 == p_card4: print("Wow, another tie! You placed a ", p_card4, "and your opponent placed a ", o_card4, ".") p_coin = input("Lets decide this one with a coin flip. Heads or tails? ") continue_playing = input("Would you like to keep playing? Enter yes to continue: ") coin = randint(1, 2) if coin == 1 and p_coin == "heads" and continue_playing == "yes": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif coin == 2 and p_coin == "tails": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) else: print("You lost the coin flip and your opponent gains the cards.") opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) # This is another check to see how the content of the decks has changed during the round. print(player_cards) print(opponent_cards) p = len(player_cards) o = len(opponent_cards) if p > o: print("You have won the game!") elif o > p: print("You lost, the opponent won this game.") else: print("You and the opponent have tied.")
1