-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermindGUI.py
410 lines (315 loc) · 14 KB
/
mastermindGUI.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import pygame
import numpy as np
from mastermind import Mastermind
from mastermindAI import MastermindAI
class MainGUI():
def __init__(self):
self.HEIGHT, self.WIDTH = 400, 600
self.BLACK = (0, 0, 0)
self.WHITE = (255 , 255 , 255)
self.game = Mastermind()
self.size = self.game.size
#Initialize the window
pygame.init()
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
pygame.display.set_caption("Mastermind 1.0")
self.screen.fill((self.BLACK))
def launch(self):
intro_menu = introMenu(self.screen, (self.BLACK, self.WHITE), (self.WIDTH, self.HEIGHT))
event = intro_menu.launch()
#When the button is clicked
if event == 0:
main_screen = mainGame(self.screen, (self.BLACK, self.WHITE), (self.WIDTH, self.HEIGHT), self.size, self.game)
main_screen.launch()
elif event == 1:
AI = MastermindAI()
main_screen_ai = mainGameAI(self.screen, (self.BLACK, self.WHITE), (self.WIDTH, self.HEIGHT), self.size, self.game, AI)
main_screen_ai.launch()
else:
print("Pygame quitted")
return -1
class introMenu():
def __init__(self, screen, colors, size):
self.screen = screen
self.BLACK, self.WHITE = colors
self.WIDTH, self.HEIGHT = size
self.text = "Welcome to Mastermind 1.0!"
self.LARGE_FONT = pygame.font.Font("OpenSans-Regular.ttf", 30)
self.SMALL_FONT = pygame.font.Font("OpenSans-Regular.ttf", 20)
self.buttons = []
def draw(self):
#Draw the text
intro_text = self.LARGE_FONT.render(self.text, True, self.WHITE)
intro_text_rect = intro_text.get_rect()
intro_text_rect.center = (self.WIDTH / 2, 100)
self.screen.blit(intro_text, intro_text_rect)
#Draw the human button
button_human = self.LARGE_FONT.render("Click here to begin: Human guesses", True, self.BLACK)
button_human_rect = button_human.get_rect()
button_human_rect.center = (self.WIDTH / 2, 200)
self.buttons.append(button_human_rect)
pygame.draw.rect(self.screen, self.WHITE, button_human_rect)
self.screen.blit(button_human, button_human_rect)
#Draw the AI button
button_AI = self.LARGE_FONT.render("Click here to begin: AI guesses", True, self.BLACK)
button_AI_rect = button_AI.get_rect()
button_AI_rect.center = (self.WIDTH / 2, 270)
self.buttons.append(button_AI_rect)
pygame.draw.rect(self.screen, self.WHITE, button_AI_rect)
self.screen.blit(button_AI, button_AI_rect)
#Update the view
pygame.display.update()
def launch(self):
self.draw()
#The waiting loop
while True:
for event in pygame.event.get():
#Check for events in the main menu
if event.type == pygame.QUIT:
pygame.quit()
return -1
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
for i, button in enumerate(self.buttons):
if button.collidepoint(pos):
return i
class mainGame():
def __init__(self, screen, colors, gui_size, size, game):
self.screen = screen
self.BLACK, self.WHITE = colors
self.GREEN, self.RED, self.YELLOW = (0, 255, 0), (255, 0, 0), (255, 255, 0)
self.WIDTH, self.HEIGHT = gui_size
self.LARGE_FONT = pygame.font.Font("OpenSans-Regular.ttf", 30)
self.MID_FONT = pygame.font.Font("OpenSans-Regular.ttf", 25)
self.SMALL_FONT = pygame.font.Font("OpenSans-Regular.ttf", 20)
self.buttons = []
self.game = game
self.board_size = size
self.answer = [-1] * self.board_size
def backend_play(self, guess):
"""
Perform playing in the background
"""
result = self.game.check(guess)
return result
def draw_text(self, text, color, center_pos, left_coor, font):
#Draw the text
text_to_draw = font.render(text, True, color)
if center_pos:
text_to_draw_rect = text_to_draw.get_rect()
text_to_draw_rect.center = center_pos
self.screen.blit(text_to_draw, text_to_draw_rect)
elif left_coor:
self.screen.blit(text_to_draw, left_coor)
#Update the display
pygame.display.update()
def draw_outline(self, rect, color):
pygame.draw.rect(self.screen, color, rect, 3)
pygame.display.update()
def draw_entered_guess(self, number, rect):
if number:
self.draw_text(str(number), self.BLACK, rect.center, None, self.SMALL_FONT)
else:
pygame.draw.rect(self.screen, self.WHITE, rect)
self.draw_outline(rect, self.GREEN)
def draw_past_guess(self, guess, turn, hist_board):
"""
Draw the past guesses from the user
"""
topleft_x, topleft_y = hist_board.topleft
text = f" {turn}. {guess}"
self.draw_text(text, self.WHITE, None, (topleft_x + 10, topleft_y + 10 + (turn-1)*30), self.SMALL_FONT)
pygame.display.update()
def draw_result_squares(self, result=[0,0,0,0]):
#Draw text
self.draw_text("The results", self.WHITE, None, (50, 150), self.LARGE_FONT)
#Draw the results
for i in range(self.board_size):
rect = pygame.Rect(50 + i*(35 + 15), 200, 35, 35)
#Draw the result squares, depending on the result
if result[i] == 0:
color = self.RED
elif result[i] == 1:
color = self.YELLOW
else:
color = self.GREEN
pygame.draw.rect(self.screen, color, rect)
#Update display
pygame.display.update()
def draw_guessing_squares(self, text="Your guess"):
#Delete other components on the screen
self.screen.fill((self.BLACK))
#Draw the text on the playing side
self.draw_text(text, self.WHITE, None, (50, 30), self.LARGE_FONT)
#Draw the board
for i in range(self.board_size):
rect = pygame.Rect(50 + i*(35 + 15), 100, 35, 35)
self.buttons.append(rect)
pygame.draw.rect(self.screen, self.WHITE, rect)
#Update the display
pygame.display.update()
def draw_scoreboard(self, turn, text1="Your guesses: ", text2="Your past guess"):
#Draw the turn
self.screen.fill((self.BLACK), pygame.Rect((350, 30, 220, 40)))
self.draw_text(text1 + f"{turn}", self.WHITE, None, (350, 30), self.MID_FONT)
#Draw the history
self.draw_text(text2, self.WHITE, None, (350, 80), self.MID_FONT)
hist_board = pygame.Rect((350, 130, 200, 250))
pygame.draw.rect(self.screen, self.WHITE, hist_board, 1)
#Update the display
pygame.display.update()
return hist_board
def winning(self, text="You solved it!"):
"""
Create a winning text
"""
self.draw_text(text, self.RED, None, (50, 320), self.LARGE_FONT)
def keyboard_pressed(self, event):
"""
Return the value of the pressed keyboard
"""
if event.key == pygame.K_0:
return 0
if event.key == pygame.K_1:
return 1
if event.key == pygame.K_2:
return 2
if event.key == pygame.K_3:
return 3
if event.key == pygame.K_4:
return 4
if event.key == pygame.K_5:
return 5
if event.key == pygame.K_6:
return 6
if event.key == pygame.K_7:
return 7
if event.key == pygame.K_8:
return 8
if event.key == pygame.K_9:
return 9
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE:
return "delete"
if event.key == pygame.K_RETURN:
return "submit"
if event.key == pygame.K_LEFT:
return "left"
if event.key == pygame.K_RIGHT:
return "right"
def launch(self):
self.draw_guessing_squares()
self.draw_result_squares()
hist_board = self.draw_scoreboard(0)
previous = 0
turn = 0
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
#Handling the clicking of the squares
for i, square in enumerate(self.buttons):
if square.collidepoint(pos):
self.draw_outline(self.buttons[previous], self.BLACK)
self.draw_outline(square, self.GREEN)
previous = i
if event.type == pygame.QUIT:
pygame.quit()
return False
#The user enters from a keyboard
if event.type == pygame.KEYDOWN:
output = self.keyboard_pressed(event)
if isinstance(output, int):
self.draw_entered_guess(output, self.buttons[previous])
#Handling error
#TODO
if output > 5:
#Do something
pass
#If the answer is not full, then raise an error
self.answer[previous] = output
if output == "delete":
self.draw_entered_guess(None, self.buttons[previous])
if output == "submit":
turn += 1
result = self.backend_play(self.answer)
hist_board = self.draw_scoreboard(turn)
self.draw_result_squares(result)
self.draw_past_guess(self.answer, turn, hist_board)
#Check for winning
if np.all(result == ([2] * self.board_size)):
self.winning()
if output == "left":
self.draw_outline(self.buttons[previous], self.BLACK)
previous -= 1
if previous == -1:
previous = self.board_size - 1
self.draw_outline(self.buttons[previous], self.GREEN)
if output == "right":
self.draw_outline(self.buttons[previous], self.BLACK)
previous += 1
if previous == self.board_size:
previous = 0
self.draw_outline(self.buttons[previous], self.GREEN)
class mainGameAI(mainGame):
def __init__(self, screen, colors, gui_size, size, game, AI):
super().__init__(screen, colors, gui_size, size, game)
self.AI = AI
def launch(self):
self.draw_guessing_squares(text="Your test")
hist_board = self.draw_scoreboard(0, text1="AI guesses: ", text2="AI past guesses")
previous = 0
turn = 0
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
#Handling the clicking of the squares
for i, square in enumerate(self.buttons):
if square.collidepoint(pos):
self.draw_outline(self.buttons[previous], self.BLACK)
self.draw_outline(square, self.GREEN)
previous = i
if event.type == pygame.QUIT:
pygame.quit()
return False
#The user enters from a keyboard
if event.type == pygame.KEYDOWN:
output = self.keyboard_pressed(event)
if isinstance(output, int):
self.draw_entered_guess(output, self.buttons[previous])
#Handling error
#TODO
if output > 5:
#Do something
pass
#If the answer is not full, then raise an error
self.answer[previous] = output
if output == "left":
self.draw_outline(self.buttons[previous], self.BLACK)
previous -= 1
if previous == -1:
previous = self.board_size - 1
self.draw_outline(self.buttons[previous], self.GREEN)
if output == "right":
self.draw_outline(self.buttons[previous], self.BLACK)
previous += 1
if previous == self.board_size:
previous = 0
self.draw_outline(self.buttons[previous], self.GREEN)
if output == "delete":
self.draw_entered_guess(None, self.buttons[previous])
if output == "submit":
min_val = min(self.answer)
max_val = max(self.answer)
size = len(self.answer)
board = Mastermind(size=size, min_value=min_val, max_value=max_val, rand=False)
board.set_board(self.answer)
self.AI.set_board(board)
guesses = self.AI.solve()
hist_board = self.draw_scoreboard(len(guesses), text1="AI guesses: ", text2="AI past guesses")
for i in range(len(guesses)):
self.draw_past_guess(guesses[i], i+1, hist_board)
self.winning(text="AI solves it!")
if __name__ == "__main__":
GUI = MainGUI()
GUI.launch()