-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
171 lines (127 loc) · 6.04 KB
/
game.py
File metadata and controls
171 lines (127 loc) · 6.04 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Defines the CardGame class which defines the logic of the card game"""
from deck import CardDeck
class CardGame:
"""Represents the game itself and implement the card game logic
Attributes (data/information):
_cardDeck: the list of cards in the deck
_playerScore: the score of the player
_houseScore: the score of the house
_playerCard: the card being played by the player in the current round
_houseCard: the card being played by the house in the current round
Methods (functionality):
- access methods for cards being played and scores
- checking winner and whether the game is done
- dealing cards
- game play
"""
def __init__(self):
"""Initialize the game objects and its attributes"""
#define the deck used in the card game
self._cardDeck = CardDeck()
#keep track of the score of the player and the house
self._playerScore = 0
self._houseScore = 0
#track the current hand, the cards currently in play in the current round
self._playerCard = None
self._houseCard = None
def getPlayerScore(self):
"""Access the score of the player"""
return self._playerScore
def getHouseScore(self):
"""Access the score of the house"""
return self._houseScore
def getPlayerCard(self):
"""Access the card being played by the player in the current round"""
return self._playerCard
def getHouseCard(self):
"""Access the card being played by the house in the current round"""
return self._houseCard
def playerWins(self):
"""Check whether the player won the game"""
return self.isOver() and self._playerScore > self._houseScore
def houseWins(self):
"""Check whether the house won the game"""
return self.isOver() and self._houseScore > self._playerScore
def isOver(self):
"""Check whether the game is over"""
return self._cardDeck.getCardCount() < 2
def dealCards(self):
"""Deals cards to the player and the house"""
self._playerCard, self._houseCard, cardsDealt = self._cardDeck.getPairOfCards()
if not cardsDealt:
assert false, 'Unexpected call to playRound(). The cards cannot be dealt which may mean the game is over'
return 0
print(f'Cards for the round are dealt.')
def switchCards(self):
"""Switches the card of the player with the card of the house"""
self._playerCard, self._houseCard = self._cardDeck.exchangeCards(self._playerCard, self._houseCard)
def play(self):
"""Plays the game rounds until no more cards are left keeping the score."""
#shuffle the cards
self._cardDeck.shuffleCards()
while not self.isOver():
#game still in progress, play another round
roundResult = self.playRound()
#show the result of the round
self.showRoundResult(roundResult)
else:
#game is over
self.showGameOver()
def playRound(self):
"""Plays a round in the game
Returns:
+1: the player won the round
-1: the house won the round
0: the round was a draw
"""
#show the beginning of the round
print('\n', '-'*80, '\n', sep='')
#deal the cards for the round
self.dealCards()
#give the user an opportunity to exchange the cardsDealt
exchangeInput = input('Do you want to exchange the cards with the house [y/n]?')
if len(exchangeInput) > 0 and exchangeInput.lower()[0] == 'y':
self.switchCards()
#determine the rank of the card which may be different than its value (e.g. Ace is 1)
playerCardRank = self.determineCardRank(self._playerCard)
houseCardRank = self.determineCardRank(self._houseCard)
#compare the player card with the house card to see which one wins the round
if playerCardRank > houseCardRank:
#the player won the round, update the score
self._playerScore += 1
return 1
elif houseCardRank > playerCardRank:
#the house won the card, update the score
self._houseScore += 1
return -1
else:
#the player and house card values are the same. The round is a draw.
return 0
def determineCardRank(self, card):
"""Determines the ranks cards have in the game based on their value and suit"""
#use the ternary operator to return the value of a conditional expression
#that ensures the Ace is the highest card value in this game.
return 14 if card.getValue() == 1 else card.getValue()
def showRoundResult(self, roundResult):
"""Displays the result of a game round"""
#inform the user what is the result of the round
if roundResult == 1:
print(f'Player won the round with card "{self.getPlayerCard()}" against "{self.getHouseCard()}".')
elif roundResult == -1:
print(f'House won the round with the card "{self.getHouseCard()}" against "{self.getPlayerCard()}".')
elif roundResult == 0:
print(f'The round was a draw. The user had "{self.getPlayerCard()}" and the house had "{self.getHouseCard()}"')
else:
assert false, 'Unexpected game round play result.'
#show the updated score
print(f'The score is {self.getPlayerScore()} (user) to {self.getHouseScore()} (house)')
def showGameOver(self):
"""Displays the winner information at the end of the game"""
#defensive programming best practice: make sure the method is called in the correct state of the game
assert self.isOver(), 'The game must be over in order to call this method'
if self.playerWins():
print('GameOver\nPlayer Wins!')
elif self.houseWins():
print('Game Over!\nHouse Wins!')
else:
print('Game Over!\nThe Game Is a Draw')