-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
359 lines (313 loc) Β· 16 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
import time
import curses
import settings
from deck import Deck
class Game:
def __init__(self):
"""
__init__(): show the rules and initialize the game settings
"""
# Show the logo during setup
settings.mainscreen.bkgd(' ', curses.color_pair(1))
settings.scoreboard.addstr(0, 0, settings.buildLogo())
settings.scoreboard.refresh()
# Show the instuctions
settings.mainscreen.addstr(1, 2, 'π The objective is to get the lowest score over all rounds.')
settings.mainscreen.addstr(3, 2, 'π Each player starts each round with 6 face-down cards, and flips over 2')
settings.mainscreen.addstr(4, 2, ' during their first turn.')
settings.mainscreen.addstr(6, 2, 'π During each turn, players draw a card from the discard or stock stack. This card ')
settings.mainscreen.addstr(7, 2, ' can either be swapped with one of the six from the playerβs hand or discarded.')
settings.mainscreen.addstr(9, 2, 'π The round ends when one playerβs cards are all face up.')
settings.mainscreen.addstr(11, 2, 'π The game ends when all rounds are over or [ctrl-c] is pressed')
settings.mainscreen.addstr(13, 2, 'βββββββββ¬βββββββββ¬ββββββββββββββββββββ¬βββββββββββββ¬ββββββββ')
settings.mainscreen.addstr(14, 2, 'β A = 1 β 2 = -2 β 3-10 = Face Value β Q & J = 10 β K = 0 β')
settings.mainscreen.addstr(15, 2, 'βββββββββ΄βββββββββ΄ββββββββββββββββββββ΄βββββββββββββ΄ββββββββ')
settings.mainscreen.addstr(17, 31, '[ Press any key to begin ]', curses.color_pair(4))
start = settings.mainscreen.getch() # Wait for feedback
# Initialize number of players
settings.mainscreen.clear()
self.numPlayers = -1
while(self.numPlayers < 2 or self.numPlayers > 4):
settings.mainscreen.addstr(2, 2, "How many people are playing? [2-4]: ")
self.numPlayers = settings.mainscreen.getch() - 48
if(self.numPlayers < 2 or self.numPlayers > 4):
settings.mainscreen.addstr(3 , 2, "Please enter a number between 2 and 4")
# Initialize number of rounds
self.numRounds = -1
settings.mainscreen.clear()
while(self.numRounds < 1 or self.numRounds > 9):
settings.mainscreen.addstr(2, 2, "How many rounds would you like to play? [1-9]: ")
self.numRounds = settings.mainscreen.getch() - 48
if(self.numRounds < 1 or self.numRounds > 9):
settings.mainscreen.addstr(3 , 2, "Please enter a number between 1 and 9")
curses.curs_set(0); # Hide the cursor
# Start game from scratch
self.scores = [0] * self.numPlayers
self.prevScores = [0] * self.numPlayers
self.curPlayer = 1
self.curRound = 0
self.curTurn = 0
self.roundFinished = False
# Prepare the screen for gameplay
settings.mainscreen.clear()
# Hide the logo if the terminal is not wide enough to fit it
if(curses.COLS < 100): settings.scoreboard.clear()
def buildScoreboard(self):
"""
buildScoreboard(): Updates the scoreboard on the left of scoreboard screen
"""
scoreboardText = []
line = 0
# Upper Border
settings.scoreboard.addstr(line, 0, ' βββββββββ' + ('β¬βββββ' * self.numPlayers) + 'β')
line += 1
# Middle Rows
for i in range(2):
# Row label
row = ' β ' + ('Player β ' if i == 0 else ' Score β ')
# Row content
for j in range(self.numPlayers):
row += '{:^3d}'.format((j+1) if i == 0 else self.scores[j]) + ' β '
# Display row
settings.scoreboard.addstr(line, 0, row)
line += 1
# Middle Border
settings.scoreboard.addstr(line, 0, ' βββββββββ' + ('βΌβββββ' * self.numPlayers) + 'β€')
line += 1
# Bottom Border
settings.scoreboard.addstr(line-1, 0, ' βββββββββ' + ('β΄βββββ' * self.numPlayers) + 'β')
# Show the scoreboard
settings.scoreboard.refresh()
def buildRoundStatus(self):
"""
buildRoundStatus(): Updates the counter on the right of scoreboard screen
"""
roundIndicator = [('_', 'ποΈβ')[round == self.curRound] for round in range(1, self.numRounds+1)]
roundStatus = 'β Round ' + str(self.curRound) + ': ' + ' '.join(roundIndicator) + ' β'
playerStatus = ' P' + str(self.curPlayer) + '\'s turn!'
boxWidth = len(roundStatus)
x = curses.COLS - boxWidth - 1
settings.scoreboard.addstr(0, x, 'β' + ('β' * (boxWidth - 3)) + 'β')
settings.scoreboard.addstr(1, x, roundStatus)
settings.scoreboard.addstr(2, x, 'β' + (' ' * (boxWidth - 3)) + 'β')
settings.scoreboard.addstr(3, x, 'β' + playerStatus + (' ' * (boxWidth - 14)) + 'β')
settings.scoreboard.addstr(4, x, 'β' + ('β' * (boxWidth - 3)) + 'β')
settings.scoreboard.refresh()
def showHand(self):
"""
showHand(): display the current players hand, as well as discard and draw piles
"""
# Show the deck and discard pile
if(len(self.deck.cards) > 0): settings.displayCard(1, 48, self.deck.cards[-1], "[7] Draw")
if(len(self.discard.cards) > 0): settings.displayCard(1, 62, self.discard.cards[-1], "[8] Discard")
# Print the Player's Hand
i = 0
for row in range(2):
for col in range(3):
settings.displayCard((row*9)+1, (col*14)+1, self.hands[self.curPlayer-1].cards[i], '['+str(i+1)+']')
i += 1
def calculateScores(self):
"""
calculateScores(): updated the self.scores for the current player
"""
# For the current player
playerHand = self.hands[self.curPlayer-1]
# The current tally for this round, which will change every turn.
# Therefore it is not cemented until the end of the round
curScore = 0
# Matching columns count as 0 points
for col in range(3):
topCard = playerHand.cards[col]
bottomCard = playerHand.cards[col+3]
if((topCard.faceUp and bottomCard.faceUp) and (topCard.value == bottomCard.value)):
continue
else:
for card in [topCard, bottomCard]:
if(card.faceUp):
if(card.value == 2):
curScore -= 2
elif(card.value == 11 or card.value == 12):
curScore += 10
elif(card.value != 13):
curScore += card.value
# Update current total score, including previous rounds
self.scores[self.curPlayer-1] = curScore + self.prevScores[self.curPlayer-1]
self.buildScoreboard()
def takeTurn(self):
"""
TakeTurn(): One player takes a turn in a round
There are three turn behaviors: first turn, normal turn, and last turn
"""
# Update the Screen
settings.mainscreen.clear()
self.buildRoundStatus()
self.showHand()
# Print the current player
settings.mainscreen.addstr(11, 48, 'Player ' + str(self.curPlayer) + ':', curses.color_pair(2))
# Check if this is the player's first turn
if(self.curTurn < self.numPlayers):
settings.mainscreen.addstr(12, 48, 'This is your first turn!', curses.color_pair(2))
settings.mainscreen.addstr(14, 48, 'Choose two cards [1-6]', curses.color_pair(2))
settings.mainscreen.addstr(15, 48, 'to flip over.', curses.color_pair(2))
settings.mainscreen.refresh()
# Let the player decide which 2 cards to flip
for i in range(2):
move = -1
validInput = False
while not validInput:
move = settings.mainscreen.getch() - 48
curses.flushinp()
if(move < 1 or move > 6):
settings.mainscreen.addstr(17, 48, 'Press [1-6]', curses.color_pair(4))
else:
if not(self.hands[self.curPlayer-1].cards[move-1].faceUp):
self.hands[self.curPlayer-1].cards[move-1].flipUp()
validInput = True
self.showHand()
# Take a normal turn
else:
settings.mainscreen.addstr(13, 48, 'Draw from pile', curses.color_pair(2))
settings.mainscreen.addstr(14, 48, '[7] or [8]', curses.color_pair(2))
# Warn the player if it is their last turn
if(self.roundFinished):
settings.mainscreen.addstr(16, 48, 'You have one last turn', curses.color_pair(4))
settings.mainscreen.addstr(17, 48, 'before your cards are flipped!', curses.color_pair(4))
settings.mainscreen.refresh()
# Let the player decide where to draw from
drawFrom = None
validInput = False
while not validInput:
drawFrom = settings.mainscreen.getch() - 48
curses.flushinp()
if(drawFrom == 7):
# Draw top card of draw pile
self.deck.cards[-1].flipUp()
self.showHand()
settings.mainscreen.addstr(9, 48, '[7]', curses.color_pair(4))
validInput = True
elif(drawFrom == 8):
# Draw top card of discard pile
settings.mainscreen.addstr(9, 62, '[8]', curses.color_pair(4))
validInput = True
else:
# Invalid Input
settings.mainscreen.addstr(17, 48, 'Press [7] or [8]', curses.color_pair(4))
# Let the player decide where to place the drawn card
settings.mainscreen.addstr(11, 48, 'Player ' + str(self.curPlayer) + ':', curses.color_pair(2))
settings.mainscreen.addstr(13, 48, 'Swap with a card [1-6]', curses.color_pair(2))
settings.mainscreen.addstr(14, 48, 'or choose discard [8]', curses.color_pair(2))
putCard = None
validInput = False
while not validInput:
putCard = settings.mainscreen.getch() - 48
curses.flushinp()
if(putCard == 8):
# Put the card in the discard pile
if(drawFrom == 7):
# Move from Drawn to Discard
drawnCard = self.deck.cards.pop()
self.discard.addCard(drawnCard)
# Otherwise leave in discard
validInput = True
elif(putCard >= 1 and putCard <= 6):
# Swap with card [putCard-1]
toDiscard = self.hands[self.curPlayer-1].cards.pop(putCard-1)
toDiscard.flipUp()
if(drawFrom == 7):
# Draw top card of draw pile
drawnCard = self.deck.cards.pop()
else:
# Draw top card of discard
drawnCard = self.discard.cards.pop()
# Perform the swap
self.hands[self.curPlayer-1].cards.insert(putCard-1, drawnCard)
self.discard.addCard(toDiscard)
validInput = True
else:
# Invalid Input
settings.mainscreen.addstr(17, 48, 'Invalid input', curses.color_pair(4))
if(self.roundFinished):
# Flip up remaining cards if round is finished
for card in self.hands[self.curPlayer-1].cards:
if not(card.faceUp): card.flipUp()
# Calculate scores
self.showHand()
self.calculateScores()
# Give players a chance to see their hand on the last round
if(self.roundFinished): time.sleep(1)
# Increment the turn counter
time.sleep(1)
self.curTurn += 1
# Move to the next player
self.curPlayer += 1
if (self.curPlayer > self.numPlayers):
self.curPlayer = 1
def playGolf(self):
"""
playGolf(): play a full game of golf
"""
self.buildScoreboard()
# For each round
for round in range(self.numRounds):
self.roundFinished = False # Flag for when all cards are flipped
self.prevScores = self.scores.copy() # Update based on final scores of previous round
self.curRound = round + 1 # Label of current round
self.curTurn = 0 # The turn within the round
# Initialize a full deck
self.deck = Deck()
self.deck.fillDeck()
self.deck.shuffle()
# Deal each player 6 cards
self.hands = []
for i in range(self.numPlayers):
curHand = Deck()
for c in range(6):
curHand.addCard(self.deck.drawCard())
self.hands.append(curHand)
# Add one card from draw pile to the discard pile
self.discard = Deck()
self.discard.addCard(self.deck.drawCard())
self.discard.cards[0].flipUp()
# Each player can take turns moving
# Until one player has all 6 cards face up
while not self.roundFinished:
# One player takes a turn
self.takeTurn()
# End the round if the previous player has flipped all their cards
numFaceUp = len([c for c in self.hands[self.curPlayer-2].cards if c.faceUp])
if(numFaceUp == 6):
self.roundFinished = True
# Let each player play one more turn
for p in range(self.numPlayers - 1):
self.takeTurn()
# Announce the winner of the round
settings.mainscreen.clear()
roundScores = [self.scores[i]-self.prevScores[i] for i in range(len(self.scores))]
# Check for ties
if(len(roundScores) != len(set(roundScores))):
winner = 'There was a tie in'
else:
winnerId = roundScores.index(min(roundScores)) + 1
winner = 'Player ' + str(winnerId) + ' is the winner of'
settings.mainscreen.addstr(2, 0, winner.center(curses.COLS-1))
settings.mainscreen.addstr(3, 0, ('Round #' + str(self.curRound) + ' of').center(curses.COLS-1))
settings.mainscreen.addstr(5, 0, settings.buildLogo())
settings.mainscreen.addstr(17, 31, '[ Press any key to continue ]', curses.color_pair(4))
settings.mainscreen.refresh()
start = settings.mainscreen.getch() # Wait for feeback
# Announce the winner of the game
settings.mainscreen.clear()
# Check for ties
if(len(self.scores) != len(set(self.scores))):
winner = 'There was a tie for this game of'
else:
winner = 'Player ' + str(self.scores.index(min(self.scores))+1) + ' is the winner of'
settings.mainscreen.addstr(2, 0, winner.center(curses.COLS-1))
settings.mainscreen.addstr(5, 0, settings.buildLogo())
settings.mainscreen.addstr(17, 31, '[ Press any key to continue ]', curses.color_pair(1))
settings.mainscreen.bkgd(' ', curses.color_pair(4))
settings.mainscreen.refresh()
start = settings.mainscreen.getch() # Wait for feeback
# Clean for next game
settings.mainscreen.clear()