-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
58 lines (50 loc) · 1.52 KB
/
Game.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
from Deck import Deck
from Card import Card
class Game:
def __init__(self, ace_split="default"):
self.playing = True
self.turns = 0
d = Deck()
self.p1_deck, self.p2_deck = d.split(ace_split)
def printAll(self):
print("P1:")
self.p1_deck.printAll()
print("P2:")
self.p2_deck.printAll()
def playAll(self):
status = 'ok'
while(status == 'ok'):
self.turns += 1
status = self.fight()
return [status, self.turns]
def fight(self, wager=None):
if wager is None:
wager = []
if not (self.playing):
raise Exception("This game is over")
# Draw the first card from each deck. If we can't, then the other player won.
card1 = self.p1_deck.pop()
if(card1==[]):
return "p2"
card1 = card1[0]
card2 = self.p2_deck.pop()
if(card2==[]):
return "p1"
card2 = card2[0]
wager.append(card1)
wager.append(card2)
if(card1.compare(card2)=="Less"):
self.p2_deck.add(wager)
return "ok"
if(card1.compare(card2)=="Greater"):
self.p1_deck.add(wager)
return "ok"
if(card1.compare(card2)=="Equal"):
wager = self.war(wager)
return self.fight(wager)
def war(self, wager):
p1wager = self.p1_deck.pop(3)
p2wager = self.p2_deck.pop(3)
wager += p1wager
wager += p2wager
return wager