|
| 1 | +import random |
| 2 | + |
| 3 | +suits=('Hearts','Diamonds','Spades','Clubs') |
| 4 | +ranks=('Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace') |
| 5 | +#Values are assigned to make the comparison easier. A dictionary has been used. |
| 6 | +values={'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14} |
| 7 | + |
| 8 | +#Object oriented programming is used. |
| 9 | +#Various classes are created. |
| 10 | + |
| 11 | +#card class |
| 12 | + |
| 13 | +class Card(): |
| 14 | + |
| 15 | + def __init__(self,suit,rank): |
| 16 | + self.suit=suit |
| 17 | + self.rank=rank |
| 18 | + self.value=values[rank] |
| 19 | + |
| 20 | + def __str__(self): |
| 21 | + return self.rank + ' of ' + self.suit |
| 22 | + |
| 23 | +#deck class |
| 24 | + |
| 25 | +class Deck(): |
| 26 | + def __init__(self): |
| 27 | + #Create a list to store all the cards of the deck |
| 28 | + self.allcards = [] |
| 29 | + for suit in suits: |
| 30 | + for rank in ranks: |
| 31 | + self.allcards.append(Card(suit,rank)) |
| 32 | + |
| 33 | + def shuffle(self): |
| 34 | + #Shuffle the deck using "random" which was imported |
| 35 | + random.shuffle(self.allcards) |
| 36 | + |
| 37 | + def deal_one(self): |
| 38 | + #Deal one card from the deck |
| 39 | + return self.allcards.pop() |
| 40 | + |
| 41 | +#player class |
| 42 | + |
| 43 | +class Player(): |
| 44 | + def __init__(self,name): |
| 45 | + #define the player |
| 46 | + self.name = name |
| 47 | + self.allcards = [] |
| 48 | + |
| 49 | + def remove_one(self): |
| 50 | + #Remove one card |
| 51 | + return self.allcards.pop(0) |
| 52 | + |
| 53 | + def add_cards(self,new_cards): |
| 54 | + #add cards |
| 55 | + if type(new_cards) == type([]): |
| 56 | + self.allcards.extend(new_cards) |
| 57 | + else: |
| 58 | + self.allcards.append(new_cards) |
| 59 | + |
| 60 | + |
| 61 | + def __str__(self): |
| 62 | + #Returns the number of cards the player has |
| 63 | + return f'Player {self.name} has {len(self.allcards)} cards.' |
| 64 | + |
| 65 | +#Main logic of the game |
| 66 | + |
| 67 | +#First define the names of both players |
| 68 | +player_one=Player("player1") |
| 69 | +player_two=Player("player2") |
| 70 | + |
| 71 | +#Now, create a deck and shuffle it |
| 72 | +new_deck=Deck() |
| 73 | +new_deck.shuffle() |
| 74 | + |
| 75 | +#divide the cards equally amongst both the players (a deck has 52 cards) |
| 76 | +for x in range(26): |
| 77 | + player_one.add_cards(new_deck.deal_one()) |
| 78 | + player_two.add_cards(new_deck.deal_one()) |
| 79 | + |
| 80 | +import pdb |
| 81 | + |
| 82 | +game_on=True |
| 83 | + |
| 84 | +print("Welcome!!") |
| 85 | + |
| 86 | +round_num = 0 |
| 87 | +while game_on: |
| 88 | + |
| 89 | + round_num += 1 |
| 90 | + #print the round number to keep track. |
| 91 | + print(f"Round {round_num}") |
| 92 | + |
| 93 | + |
| 94 | + if len(player_one.allcards) == 0: |
| 95 | + print("Player One out of cards! Game Over") |
| 96 | + print("Player Two Wins!") |
| 97 | + game_on = False |
| 98 | + break |
| 99 | + |
| 100 | + if len(player_two.allcards) == 0: |
| 101 | + print("Player Two out of cards! Game Over") |
| 102 | + print("Player One Wins!") |
| 103 | + game_on = False |
| 104 | + break |
| 105 | + |
| 106 | + player_one_cards = [] |
| 107 | + player_one_cards.append(player_one.remove_one()) |
| 108 | + |
| 109 | + player_two_cards = [] |
| 110 | + player_two_cards.append(player_two.remove_one()) |
| 111 | + |
| 112 | + #check for a condition where both the players have an equal value on their card |
| 113 | + #This condition is called war |
| 114 | + at_war = True |
| 115 | + |
| 116 | + while at_war: |
| 117 | + if player_one_cards[-1].value > player_two_cards[-1].value: |
| 118 | + |
| 119 | + |
| 120 | + player_one.add_cards(player_one_cards) |
| 121 | + player_one.add_cards(player_two_cards) |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + at_war = False |
| 126 | + |
| 127 | + |
| 128 | + elif player_one_cards[-1].value < player_two_cards[-1].value: |
| 129 | + |
| 130 | + |
| 131 | + player_two.add_cards(player_one_cards) |
| 132 | + player_two.add_cards(player_two_cards) |
| 133 | + |
| 134 | + |
| 135 | + at_war = False |
| 136 | + |
| 137 | + else: |
| 138 | + print('WAR!') |
| 139 | + |
| 140 | + if len(player_one.allcards) < 5: #It can be any number other than 5 as well, depends on game rules. |
| 141 | + print("Player One unable to play war! Game Over at War") |
| 142 | + print("Player Two Wins! Player One Loses!") |
| 143 | + game_on = False |
| 144 | + break |
| 145 | + |
| 146 | + elif len(player_two.allcards) < 5: |
| 147 | + print("Player Two unable to play war! Game Over at War") |
| 148 | + print("Player One Wins! Player Two Loses!") |
| 149 | + game_on = False |
| 150 | + break |
| 151 | + |
| 152 | + else: |
| 153 | + for num in range(5): |
| 154 | + player_one_cards.append(player_one.remove_one()) |
| 155 | + player_two_cards.append(player_two.remove_one()) |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
0 commit comments