-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_pg.py
349 lines (297 loc) · 14.7 KB
/
game_pg.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
import os
import pickle
from setup import *
from solver import MastermindSolver
class Mastermind:
def __init__(self):
self.guesses_left = 6
self.current_hole = 0
self.game_status = "start"
self.win_status = None
FileStore = open("stored_objects/win_dict.pickle", "rb")
self.lookup_table = pickle.load(FileStore)
FileStore.close()
@staticmethod
def draw_guess_grid(guess_grid, x, y):
grid_y = y
for row in guess_grid:
grid_x = x
for val in row:
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (40, 40)), (grid_x - 20, grid_y - 20))
if val:
pg.draw.circle(SCREEN, GUESS_COLOR_MAP[val], (grid_x, grid_y), GUESS_RADIUS)
grid_x += 60
grid_y += 110
@staticmethod
def draw_hint_grid(hint_grid, x, y):
grid_y = y
for row in hint_grid:
# draw empty peg when there is no hint peg to draw
row = [let for let in row]
if len(row) < 5:
row.extend([""] * (5 - len(row)))
grid_x = x
for i, val in enumerate(row):
if i in (0, 1):
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 15, grid_y - 15))
if val:
pg.draw.circle(SCREEN, HINT_COLOR_MAP[val], (grid_x, grid_y), HINT_RADIUS)
grid_x += 60
elif i == 2:
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 90 - 15, grid_y + 30 - 15))
if val:
pg.draw.circle(SCREEN, HINT_COLOR_MAP[val], (grid_x - 90, grid_y + 30), HINT_RADIUS)
grid_x = 55
elif i in (3, 4):
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 15, grid_y + 60 - 15))
if val:
pg.draw.circle(SCREEN, HINT_COLOR_MAP[val], (grid_x, grid_y + 60), HINT_RADIUS)
grid_x += 60
grid_y += 110
@staticmethod
def draw_choices(choice_grid, x, y):
choice_rects = []
for val in choice_grid:
choice_rects.append(pg.draw.circle(SCREEN, GUESS_COLOR_MAP[val], (x, y), GUESS_RADIUS))
x += 60
return choice_rects
@staticmethod
def draw_separators():
# draw separator between guesses and choices
pg.draw.line(SCREEN, (0, 0, 0), (0, 670), (500, 670), 10)
# draw separator between guess and hints
pg.draw.line(SCREEN, (0, 0, 0), (155, 0), (155, 900), 10)
@staticmethod
def draw_button(x, y, word):
button_rect = pg.Rect(0, 0, 90, 40)
button_rect.center = x, y
pg.draw.rect(SCREEN, (0, 0, 0), button_rect, 1)
button_word = GAME_FONT.render(word, True, (0, 0, 0))
button_word_rect = button_word.get_rect(center=button_rect.center)
SCREEN.blit(button_word, button_word_rect)
return button_rect
def draw_win_screen(self, status):
if status:
status_text = TITLE_FONT.render(f"You {status}!", True, (0, 0, 0))
status_rect = status_text.get_rect()
status_rect.center = (SCREEN_WIDTH / 2 + 75, SCREEN_HEIGHT / 2 - 30)
SCREEN.blit(status_text, status_rect)
self.draw_button(SCREEN_WIDTH / 2 + 75, SCREEN_HEIGHT / 2 + 75, "RESET")
if status == "LOSE":
self.draw_choices(ANSWER, 200, 710)
def draw_codebreaker_screen(self):
self.draw_guess_grid(GUESS_GRID, 200, 65)
self.draw_hint_grid(HINT_GRID, 55, 35)
self.draw_choices(COLOR_CHOICES, 200, 710)
self.draw_separators()
self.draw_button(75, 710, "SUBMIT")
def draw_solver_screen(self):
self.draw_guess_grid(GUESS_GRID, 200, 65)
self.draw_hint_grid(HINT_GRID, 55, 35)
self.draw_separators()
self.draw_choices(CODEMAKER_ANSWER, 200, 710)
self.draw_button(75, SCREEN_HEIGHT - 40, "RESET")
def draw_codemaker_screen(self):
# draw explainer text
instruction_text1 = SUB_TITLE_FONT.render("Choose a secret code", True, (0, 0, 0))
instruction_rect1 = instruction_text1.get_rect()
instruction_rect1.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3.5)
SCREEN.blit(instruction_text1, instruction_rect1)
instruction_text2 = SUB_TITLE_FONT.render("For computer to guess", True, (0, 0, 0))
instruction_rect2 = instruction_text2.get_rect()
instruction_rect2.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2.8)
SCREEN.blit(instruction_text2, instruction_rect2)
# draw secret answer
self.draw_guess_grid([CODEMAKER_ANSWER], x=SCREEN_WIDTH / 3.75, y=SCREEN_HEIGHT / 2 - 30)
# draw choices
self.draw_choices(COLOR_CHOICES, x=SCREEN_WIDTH / 3.75, y=SCREEN_HEIGHT / 2 + 30)
# draw submit button
self.draw_button(x=SCREEN_WIDTH / 2, y=SCREEN_HEIGHT / 2 + 150, word="SUBMIT")
@staticmethod
def draw_title():
# draw title
title_word = TITLE_FONT.render("MASTERMIND", True, (0, 0, 0))
title_rect = title_word.get_rect()
title_rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 6)
SCREEN.blit(title_word, title_rect)
# draw subtitle
subtitle_text = SUB_TITLE_FONT.render("Select game mode", True, (0, 0, 0))
subtitle_rect = subtitle_text.get_rect()
subtitle_rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3)
SCREEN.blit(subtitle_text, subtitle_rect)
@staticmethod
def draw_codebreaker():
# codebreaker
codebreaker_text = SUB_TITLE_FONT.render("Codebreaker", True, (0, 0, 0))
codebreaker_rect = codebreaker_text.get_rect()
codebreaker_rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
SCREEN.blit(codebreaker_text, codebreaker_rect)
# codebreaker icon
grid_x = SCREEN_WIDTH / 3.75
grid_y = SCREEN_HEIGHT / 2 + 45
for val in COLOR_CHOICES:
pg.draw.circle(SCREEN, GUESS_COLOR_MAP[val], (grid_x, grid_y), GUESS_RADIUS)
grid_x += 60
# codebreaker selection rect
window = pg.Rect(100, 340, 300, 110)
pg.draw.rect(SCREEN, (0, 0, 0), window, width=1)
return window
@staticmethod
def draw_codemaker():
# codemaker text
codemaker_text = SUB_TITLE_FONT.render("Codemaker", True, (0, 0, 0))
codemaker_rect = codemaker_text.get_rect()
codemaker_rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 1.4)
SCREEN.blit(codemaker_text, codemaker_rect)
# codemaker icon
grid_x = SCREEN_WIDTH / 2 - 32
grid_y = SCREEN_HEIGHT / 1.3
for i in range(5):
if i in (0, 1):
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 15, grid_y - 15))
pg.draw.circle(SCREEN, HINT_COLOR_MAP["B"], (grid_x, grid_y), HINT_RADIUS)
grid_x += 60
elif i == 2:
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 90 - 15, grid_y + 30 - 15))
pg.draw.circle(SCREEN, HINT_COLOR_MAP["W"], (grid_x - 90, grid_y + 30), HINT_RADIUS)
grid_x = SCREEN_WIDTH / 2 - 30
elif i in (3, 4):
SCREEN.blit(pg.transform.scale(HOLE_BACKGROUND, (30, 30)), (grid_x - 15, grid_y + 60 - 15))
grid_x += 60
# codebreaker selection rect
window = pg.Rect(100, 500, 300, 160)
pg.draw.rect(SCREEN, (0, 0, 0), window, width=1)
return window
def draw_start_screen(self):
self.draw_title()
self.draw_codebreaker()
self.draw_codemaker()
@staticmethod
def validate_guess(guess, ans):
# iterates through guess and answer lists element-by-element. Whenever it finds a match,
# removes the value from a copy of answer so that nothing is double counted.
hints = []
ans_temp = ans.copy()
guess_temp = guess.copy()
# first pass for black pegs
for i, (guess_elem, ans_elem) in enumerate(zip(guess_temp, ans_temp)):
if guess_elem == ans_elem:
hints.append("B")
ans_temp[i] = ""
guess_temp[i] = ""
# second pass for white pegs
for guess_elem, ans_elem in zip(guess_temp, ans_temp):
if guess_elem in ans_temp and guess_elem:
hints.append("W")
ans_temp[ans_temp.index(guess_elem)] = ""
return hints
def reset_game(self):
self.game_status = "start"
self.win_status = None
self.guesses_left = 6
self.current_hole = 0
new_answer = random.choices(COLOR_CHOICES, k=5)
for i in range(6):
GUESS_GRID[i] = ["" for _ in range(5)]
HINT_GRID[i] = ["" for _ in range(5)]
if i < 5:
CODEMAKER_ANSWER[i] = ""
COMPUTER_GUESSES[i] = ["", "", "", "", ""]
COMPUTER_HINTS[i] = ["", "", "", "", ""]
ANSWER[i] = new_answer[i]
def play(self):
current_time = pg.time.get_ticks()
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pg.mouse.get_pos()
if self.game_status == "start":
breaker_rect = self.draw_codebreaker()
maker_rect = self.draw_codemaker()
if breaker_rect.collidepoint(mouse_x, mouse_y):
self.game_status = "codebreaker"
elif maker_rect.collidepoint(mouse_x, mouse_y):
self.game_status = "codemaker"
elif self.game_status == "codebreaker":
# choosing which colors to play
choice_rects = self.draw_choices(COLOR_CHOICES, 200, 710)
for color, rect in zip(COLOR_CHOICES, choice_rects):
if rect.collidepoint(mouse_x, mouse_y):
if self.current_hole < 5:
GUESS_GRID[6 - self.guesses_left][self.current_hole] = color
self.current_hole += 1
# submitting a guess
submit_rect = self.draw_button(75, 710, "SUBMIT")
if submit_rect.collidepoint(mouse_x, mouse_y):
if self.current_hole == 5:
guess = GUESS_GRID[6 - self.guesses_left]
hints = self.validate_guess(guess, ANSWER)
random.shuffle(hints)
HINT_GRID[6 - self.guesses_left] = hints
self.guesses_left -= 1
self.current_hole = 0
if guess == ANSWER:
self.win_status = "WIN"
elif guess != ANSWER and self.guesses_left == 0:
self.win_status = "LOSE"
if self.win_status in ("WIN", "LOSE"):
exit_rect = self.draw_button(SCREEN_WIDTH / 2 + 75, SCREEN_HEIGHT / 2 + 75, "RESET")
if exit_rect.collidepoint(mouse_x, mouse_y):
self.reset_game()
elif self.game_status == "codemaker":
# choosing which colors to play
choice_rects = self.draw_choices(COLOR_CHOICES, x=SCREEN_WIDTH / 3.75, y=SCREEN_HEIGHT / 2 + 45)
for color, rect in zip(COLOR_CHOICES, choice_rects):
if rect.collidepoint(mouse_x, mouse_y):
if self.current_hole < 5:
CODEMAKER_ANSWER[self.current_hole] = color
self.current_hole += 1
submit_rect = self.draw_button(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 150, "SUBMIT")
if submit_rect.collidepoint(mouse_x, mouse_y):
if self.current_hole == 5:
guesses = self.lookup_table[tuple(CODEMAKER_ANSWER)]
self.current_hole = 0
for i, guess in enumerate(guesses):
COMPUTER_GUESSES[i] = guess
COMPUTER_HINTS[i] = self.validate_guess([let for let in guess], CODEMAKER_ANSWER)
self.game_status = "solver_showcase"
elif self.game_status == "solver_showcase":
exit_rect = self.draw_button(50, SCREEN_HEIGHT - 40, "RESET")
if exit_rect.collidepoint(mouse_x, mouse_y):
self.reset_game()
if self.game_status == "codebreaker":
# changing color choices before submitting
if event.type == pg.KEYDOWN:
if event.key == pg.K_BACKSPACE:
if self.current_hole > 0:
self.current_hole -= 1
GUESS_GRID[6 - self.guesses_left][self.current_hole] = ""
elif self.game_status == "codemaker":
# changing color choices before submitting
if event.type == pg.KEYDOWN:
if event.key == pg.K_BACKSPACE:
if self.current_hole > 0:
self.current_hole -= 1
CODEMAKER_ANSWER[self.current_hole] = ""
SCREEN.blit(BACKGROUND, (0, 0))
if self.game_status == "start":
self.draw_start_screen()
elif self.game_status == "codebreaker":
self.draw_codebreaker_screen()
self.draw_win_screen(self.win_status)
elif self.game_status == "codemaker":
self.draw_codemaker_screen()
elif self.game_status == "solver_showcase":
self.draw_solver_screen()
if pg.time.get_ticks() - current_time > 1500:
i = self.current_hole
if COMPUTER_GUESSES[i]:
GUESS_GRID[i] = COMPUTER_GUESSES[i]
HINT_GRID[i] = COMPUTER_HINTS[i]
current_time = pg.time.get_ticks()
self.current_hole += 1
pg.display.update()
Mastermind().play()