-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2_card_game_war.py
134 lines (105 loc) · 4.26 KB
/
_2_card_game_war.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import random
suits= { 'Hearts', 'Diamonds', 'Spades', 'Clubs'}
ranks= { 'Two','Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace'}
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}
class Card:
def __init__(self,suit,rank):
self.suit=suit
self.rank=rank
self.value=values[rank]
def __str__(self):
return (f"{self.rank} of {self.suit} rank {self.value}")
class Deck:
def __init__(self):
self.deck=[]
for suit in suits:
for rank in ranks:
self.deck.append(Card(suit,rank))
def shuffle(self):
random.shuffle(self.deck)
def __str__(self):
deck_cards=''
for card in self.deck:
deck_cards+="\n"+str(card)
return deck_cards
def deal(self):
return self.deck.pop()
class Player:
def __init__(self,name):
self.name=name
self.player_hand=[]
def remove_one(self):
return self.player_hand.pop(0)
def add_one(self,cards):
if type(cards)==type([]):
self.player_hand.extend(cards)
else:
self.player_hand.append(cards)
def __str__(self):
return f"Player {self.name} has {len(self.player_hand)}"
if __name__=="__main__":
play_again=True
while play_again:
print("COMPUTER VS COMPUTER")
player1=Player("player1")
player2=Player("player2")
deck=Deck()
deck.shuffle()
for i in range(26):
player1.add_one(deck.deal())
player2.add_one(deck.deal())
game_size=5
game_on=True
round=0
while game_on:
round+=1
print(f"Round {round}")
if len(player1.player_hand)== 0:
print(f"{player1.name} out of cards \n Game Over \n {player2.name} wins")
game_on=False
break
if len(player2.player_hand)== 0:
print(f"{player2.name} out of cards \n Game Over \n {player1.name} wins")
game_on=False
break
player1_cards_on_table=[]
player2_cards_on_table=[]
player1_cards_on_table.append(player1.remove_one())
player2_cards_on_table.append(player2.remove_one())
war=True
while war:
if player1_cards_on_table[-1].rank > player2_cards_on_table[-1].rank:
player1.add_one(player1_cards_on_table)
player1.add_one(player2_cards_on_table)
war=False
break
if player2_cards_on_table[-1].value > player1_cards_on_table[-1].value:
player2.add_one(player1_cards_on_table)
player2.add_one(player2_cards_on_table)
war=False
break
else:
print("WAARRR!!!!")
if len(player1.player_hand) < game_size:
print(f"{player1.name} doesn't have enough cards to continue war\nGame Over!\n{player2.name} wins")
game_on=False
break
if len(player2.player_hand) < game_size :
print(f"{player2.name} doesn't have enough cards to continue war\nGame Over!\n{player1.name} wins")
game_on=False
break
else:
for i in range(game_size):
player1_cards_on_table.append(player1.remove_one())
player2_cards_on_table.append(player2.remove_one())
while True:
try:
cont_game=input("Simulate game again ? Y/N")
if cont_game[0]=='y' or cont_game[0]=='Y':
play_again=True
break
if cont_game[0]=='n' or cont_game[0]=='N':
play_again=False
break
except:
print("wrong input try again")