-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWar.py
More file actions
124 lines (99 loc) · 3.63 KB
/
War.py
File metadata and controls
124 lines (99 loc) · 3.63 KB
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
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 self.rank + " of " + self.suit
class Deck:
def __init__(self):
self.all_cards = []
for suit in suits:
for rank in ranks:
self.all_cards.append(Card(suit, rank))
def shuffle(self):
random.shuffle(self.all_cards)
def deal_one(self):
return self.all_cards.pop()
class Player:
def __init__(self, name):
self.name = name
self.all_cards = []
def remove_one(self):
return self.all_cards.pop(0)
def add_cards(self, new_cards):
if type(new_cards) == type([]):
self.all_cards.extend(new_cards)
else:
self.all_cards.append(new_cards)
def __str__ (self):
return f'Player {self.name} has {len(self.all_cards)} cards.'
player_one = Player("One")
player_two = Player("Two")
new_Deck = Deck()
new_Deck.shuffle()
for num in range(26):
player_one.add_cards(new_Deck.deal_one())
player_two.add_cards(new_Deck.deal_one())
gameOn = True
roundNum = 0
while gameOn:
roundNum += 1
print(f"Round {roundNum}")
if len(player_one.all_cards) == 0:
print("Player One, out of cards! Player Two Wins!")
gameOn = False
break
if len(player_two.all_cards) == 0:
print("Player Two, out of cards! Player Two Wins!")
gameOn = False
break
#Start a new round
player_one_cards = []
player_one_cards.append(player_one.remove_one())
player_two_cards = []
player_two_cards.append(player_two.remove_one())
#Checking if in war
war_on = False
if player_one_cards[-1].value > player_two_cards[-1].value:
player_one.add_cards(player_one_cards)
player_one.add_cards(player_two_cards)
elif player_one_cards[-1].value < player_two_cards[-1].value:
player_two.add_cards(player_one_cards)
player_two.add_cards(player_two_cards)
else:
war_on = True
while war_on:
if len(player_one.all_cards) < 6:
print("Player One does not have enough cards to participate in war. Player Two has Won!")
war_on = False
gameOn = False
break
elif len(player_two.all_cards) < 6:
print("Player Two does not have enough cards to participate in war. Player One has Won!")
war_on = False
gameOn = False
break
for item in range(6):
player_one_cards = []
player_one_cards.append(player_one.remove_one())
player_two_cards = []
player_two_cards.append(player_two.remove_one())
if player_one_cards[-1].value > player_two_cards[-1].value:
player_one.add_cards(player_one_cards)
player_one.add_cards(player_two_cards)
print("Player One has won the war!")
war_on = False
break
elif player_one_cards[-1].value < player_two_cards[-1].value:
player_two.add_cards(player_one_cards)
player_two.add_cards(player_two_cards)
print("Player Two has won the war!")
war_on = False
break
else:
print("War was not resolved. Moving to next round of war.")