-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
584 lines (464 loc) · 19.4 KB
/
main.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import numpy as np
import pygame
import pygame_gui
from board import Board
class Main:
def __init__(self):
pygame.init()
# WINDOW DIMENSIONS
self.WIDTH = 450
self.HEIGHT = 550
# PYGAME GUI ELEMENTS
self.manager = pygame_gui.UIManager((self.WIDTH, self.HEIGHT))
menuButtonWidth = 60
menuButtonHeight = 30
self.menuButton = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect(
int(self.WIDTH / 2 - menuButtonWidth - 5), 517, menuButtonWidth, menuButtonHeight),
text="Menu",
manager=self.manager,
starting_height=30)
self.restartButton = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect(
int(self.WIDTH / 2 + 5), 517, menuButtonWidth + 10, menuButtonHeight),
text="Restart",
manager=self.manager,
starting_height=30
)
self.drawMenu()
# BOARD DIMENSIONS
self.BOARD_HEIGHT = 450
self.BOARD_WIDTH = 450
pygame.display.set_caption("Sudoku!")
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
self.screen.fill(pygame.Color("#A7B3A1"))
self.backgound = pygame.Surface((self.WIDTH, self.BOARD_HEIGHT))
self.backgound.fill(pygame.Color("#ffffff"))
# FONTS
self.numbersFont = pygame.font.SysFont('arial', 35)
# start game
self.startGame(1, True)
self.running = True
self.clock = pygame.time.Clock()
def drawGrid(self):
"""
Draw the 9x9 grid.
"""
resY = int(self.BOARD_HEIGHT / 9)
resX = int(self.BOARD_WIDTH / 9)
# border lines
pygame.draw.line(self.backgound, (0, 0, 0),
(0, 0), (self.BOARD_WIDTH, 0))
pygame.draw.line(self.screen, (0, 0, 0),
(0, self.BOARD_HEIGHT), (self.BOARD_WIDTH, self.BOARD_HEIGHT), 3)
pygame.draw.line(self.backgound, (0, 0, 0),
(0, 0), (0, self.BOARD_HEIGHT))
pygame.draw.line(self.backgound, (0, 0, 0),
(self.BOARD_WIDTH, 0), (self.BOARD_WIDTH, self.BOARD_HEIGHT))
# 8 because I want to draw the border lines out of the loop
for i in range(8):
# horizontal lines
y = i * resY + resY
# vertical lines
x = i * resX + resX
if (i + 1) % 3 == 0:
# horizontal bold
pygame.draw.line(self.screen, (0, 0, 0),
(0, y),
(self.BOARD_WIDTH, y), 3)
# vertical bold
pygame.draw.line(self.screen, (0, 0, 0),
(x, 0),
(x, self.BOARD_HEIGHT), 3)
else:
# horizontal
pygame.draw.line(self.screen, (0, 0, 0),
(0, y),
(self.BOARD_WIDTH, y))
# vertical
pygame.draw.line(self.screen, (0, 0, 0),
(x, 0),
(x, self.BOARD_HEIGHT))
pygame.draw.line(self.screen, (0, 0, 0),
(x, 0),
(x, self.BOARD_HEIGHT))
def drawNumbers(self):
"""
Draw the number on the grid.
"""
resY = int(self.BOARD_HEIGHT / 9)
resX = int(self.BOARD_WIDTH / 9)
for i in range(9):
for j in range(9):
# the main board
if self.game.board[i][j] != 0:
# the current number
text = str(self.game.board[i][j])
if self.originalBoard[i][j] != 0:
n = self.numbersFont.render(
text, True, pygame.Color("#B32D32"))
else:
n = self.numbersFont.render(
text, True, pygame.Color("#000000"))
# to center the number
x, y = self.numbersFont.size(text)
yPos = int((i * resY + resY / 2) - y / 2)
xPos = int((j * resX + resX / 2) - x / 2)
self.screen.blit(n, (xPos, yPos))
# the pencil board
if self.pencilBoard[i][j] != 0:
# the current number
text = str(self.pencilBoard[i][j])
n = self.numbersFont.render(
text, True, pygame.Color("#8C8C8C"))
# to center the number
x, y = self.numbersFont.size(text)
yPos = int((i * resY + resY / 2) - y / 2)
xPos = int((j * resX + resX / 2) - x / 2)
self.screen.blit(n, (xPos, yPos))
def selectCell(self, mouse_pos):
"""
Select a cell from the grid. If the cell is already selected, deselect it.
Args:
mouse_pos (tuple of integers): The x and y positions of the mouse. e.g. selectCell(pygame.mouse.get_pos())
"""
mouseX, mouseY = mouse_pos
resY = int(self.BOARD_HEIGHT / 9)
resX = int(self.BOARD_WIDTH / 9)
# a number between 0 and 8 to indicate the position in the outter array
cellY = mouseY // resY
# a number between 0 and 8 to indicate the position in the inner array
cellX = mouseX // resX
if (cellX, cellY) == self.currentSelected:
self.currentSelected = None
else:
self.currentSelected = (cellX, cellY)
def moveSelected(self, direction):
"""
Move the selected cell in the grid.
Args:
direction (str): The direction to move. Can be: "up", "down", "right", or "left".
"""
x, y = self.currentSelected
canMove = False
offset = 1
if direction == "up":
if y != 0:
self.currentSelected = (x, y - offset)
elif direction == "down":
if y != 8:
self.currentSelected = (x, y + offset)
elif direction == "right":
if x != 8:
self.currentSelected = (x + offset, y)
elif direction == "left":
if x != 0:
self.currentSelected = (x - offset, y)
def highlightCell(self, pos, color):
"""
Highlight a cell in a given postion with a given color
Args:
pos (int): A tuple containing x and y coordinates in the array. e.g. (3, 2)
color (Pygame Color Object / tuple of three int): The color to highlight the cell. Can be either a Pygame Color Object or a tuple, like (255, 255, 255)
"""
resY = int(self.BOARD_HEIGHT / 9)
resX = int(self.BOARD_WIDTH / 9)
x, y = pos
xRec = x * resX
yRec = y * resY
pygame.draw.rect(self.screen, color,
pygame.Rect(xRec, yRec, resX, resY))
def drawSelected(self):
"""
Highlight the selected cell
"""
resY = int(self.BOARD_HEIGHT / 9)
resX = int(self.BOARD_WIDTH / 9)
if self.currentSelected is not None:
self.highlightCell(self.currentSelected, pygame.Color(
"#64DE84"))
def drawAllEqual(self):
"""
Highlight the cells that contain the same number in the selected cell.
"""
if self.currentSelected is not None:
x, y = self.currentSelected
curr = self.game.board[y][x]
if curr != 0:
for i in range(9):
for j in range(9):
if self.game.board[i][j] == curr:
self.highlightCell((j, i), pygame.Color("#AEFFAB"))
def setNumber(self, num):
"""
Receive a number and set the current selected cell to this number.
Args:
num (int): The number to be set
"""
x, y = self.currentSelected
if self.originalBoard[y][x] == 0:
if self.pencilBoard[y][x] != 0:
self.pencilBoard[y][x] = 0
self.game.board[y][x] = num
def pencilNumber(self, num):
"""
Receive a number and set it to temporary number. Mark the number as a "pencil".
Args:
num (int): The number to be set
"""
x, y = self.currentSelected
if self.originalBoard[y][x] == 0:
if self.game.board[y][x] != 0:
self.game.board[y][x] = 0
self.pencilBoard[y][x] = num
def startGame(self, difficulty, isStart=False):
"""
Set everything up to the game begin.
Args:
difficulty (int): Number between 1 and 3 (inclusive) to determine the difficulty of the game.
"""
# create game instance with given difficulty
dif = [
"easy-boards.json",
"medium-boards.json",
"hard-boards.json"
]
if isStart:
# initialize the self.game variable if is the start of the game
self.game = Board(board_path=f"./boards/{dif[difficulty - 1]}")
else:
# temporary var to grant that the game is a new one
temp = Board(board_path=f"./boards/{dif[difficulty - 1]}")
while (temp.board == self.game.board):
temp = Board(board_path=f"./boards/{dif[difficulty - 1]}")
self.game = temp
self.solvedBoard = self.game.solveBoard()
# copy the board to a new array to keep track of things
self.originalBoard = np.zeros((9, 9), dtype=int).tolist()
for i in range(9):
for j in range(9):
self.originalBoard[i][j] = self.game.board[i][j]
# the board to keep track of the pencil numbers
self.pencilBoard = np.zeros((9, 9), dtype=int).tolist()
self.currentSelected = None
def restartGame(self):
"""
Restart the current game.
"""
self.game.board = self.originalBoard
def validateBoard(self):
"""
Validates the current board.
Returns:
boolean: True if the game is valid, False if invalid.
"""
filled = True
# check if there are any spot still available
for i in range(9):
for j in range(9):
if self.game.board[i][j] == 0:
filled = False
if filled:
for i in range(9):
for j in range(9):
# if any of the game board's numbers is different from the solved board, return False
if self.solvedBoard[i][j] != self.game.board[i][j]:
return False
# if didn't returned False, return True
return True
else:
return None
# INFO BOARD
def drawNumsLeft(self):
"""
Show to the player how many numbers are left to complete the board.
Params:
width (int): The width of the board for position.
"""
resX = self.WIDTH / 9
# to loop for every number from 1 to 9
for i in range(1, 10):
# to keep track of how many numbers currently exist in the board
existing = 0
for k in range(9):
for j in range(9):
if self.game.board[k][j] == i:
existing += 1
left = 9 - existing
# texts
numText = str(i)
leftText = str(left)
# background for each number
circleX = int(resX / 2 + resX * (i - 1))
circleY = 490
if left == 0:
pygame.draw.circle(
self.screen, pygame.Color("#30332E"), (circleX, circleY), 24)
else:
pygame.draw.circle(
self.screen, pygame.Color("#6B7367"), (circleX, circleY), 24)
# CURRENT NUMBER
# font
numberFont = pygame.font.SysFont('arial', 26)
x, y = numberFont.size(numText)
number = numberFont.render(numText, True, pygame.Color("#EEFFE6"))
xPos = int(((i - 1) * resX + resX / 2) - x / 2)
yPos = circleY - 24
self.screen.blit(number, (xPos, yPos))
# LEFT NUMBER
leftFont = pygame.font.SysFont('arial', 17)
if left < 0:
leftText = str(left * -1)
x, y = leftFont.size(leftText)
leftNumber = leftFont.render(
leftText, True, pygame.Color("#B33A34"))
else:
x, y = leftFont.size(leftText)
leftNumber = leftFont.render(
leftText, True, pygame.Color("#B3BFAC"))
xPos = int(((i - 1) * resX + resX / 2) - x / 2)
yPos = circleY + 1
self.screen.blit(leftNumber, (xPos, yPos))
def refreshInfoBar(self):
"""
Refresh info bar to draw the appropriate content.
"""
pygame.draw.rect(self.screen, pygame.Color("#A7B3A1"), pygame.Rect(
0, self.BOARD_HEIGHT, self.WIDTH, self.HEIGHT - self.BOARD_HEIGHT))
def drawMenu(self):
"""
Draw the difficulty menu.
"""
# SET DIFFICULTIES
buttonWidth = 60
buttonHeight = 30
chunk = int(self.WIDTH / 3)
# easy button
pos = (int((chunk / 2) * 1 - (buttonWidth / 2)), 470)
self.easyButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(pos, (buttonWidth, buttonHeight)),
text="Easy",
manager=self.manager,
starting_height=30)
# normal button
pos = (int((chunk / 2) * 3 - (buttonWidth / 2)), 470)
self.normalButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(pos, (buttonWidth, buttonHeight)),
text="Normal",
manager=self.manager,
starting_height=30)
# hard button
pos = (int((chunk / 2) * 5 - (buttonWidth / 2)), 470)
self.hardButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect(pos, (buttonWidth, buttonHeight)),
text="Hard",
manager=self.manager,
starting_height=30)
def killMenu(self):
"""
Kill the difficulty menu.
"""
self.easyButton.kill()
self.easyButton.set_position((0, 1000))
self.normalButton.kill()
self.normalButton.set_position((0, 1000))
self.hardButton.kill()
self.hardButton.set_position((0, 1000))
def run(self):
menuIsActive = False
while self.running:
dt = self.clock.tick(30)/1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
# handle mouse clicked
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
x, y = pos
if x < self.BOARD_WIDTH and y < self.BOARD_HEIGHT:
self.selectCell(pos)
# handle keys pressed
if event.type == pygame.KEYDOWN:
if self.currentSelected is not None:
if event.key == pygame.K_BACKSPACE:
self.setNumber(0)
elif event.key == pygame.K_UP:
self.moveSelected("up")
elif event.key == pygame.K_DOWN:
self.moveSelected("down")
elif event.key == pygame.K_RIGHT:
self.moveSelected("right")
elif event.key == pygame.K_LEFT:
self.moveSelected("left")
else:
try:
if event.mod == pygame.KMOD_NONE:
# set the number pressed
self.setNumber(
int(pygame.key.name(event.key)))
else:
if event.mod & pygame.KMOD_LSHIFT:
# set the number as drawn with a pencil
self.pencilNumber(
int(pygame.key.name(event.key)))
except Exception:
pass
else:
self.currentSelected = (0, 0)
# handle UI elements
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
# MENU BUTTON
if event.ui_element == self.menuButton:
menuIsActive = not menuIsActive
self.killMenu()
self.refreshInfoBar()
# RESTART BUTTON
if event.ui_element == self.restartButton:
self.restartGame()
# EASY BUTTON
if event.ui_element == self.easyButton:
self.startGame(1)
self.killMenu()
self.refreshInfoBar()
menuIsActive = False
# NORMAL BUTTON
if event.ui_element == self.normalButton:
self.startGame(2)
self.killMenu()
self.refreshInfoBar()
menuIsActive = False
# HARD BUTTON
if event.ui_element == self.hardButton:
self.startGame(3)
self.killMenu()
self.refreshInfoBar()
menuIsActive = False
self.manager.process_events(event)
self.screen.blit(self.backgound, (0, 0))
self.manager.draw_ui(self.screen)
# GAME LOGIC
isWon = self.validateBoard()
if isWon is not None and isWon == True:
# what happens when you win the game?
print("WON!")
# HIGHLIGHTS
self.drawAllEqual()
self.drawSelected()
# GAME UI
self.drawGrid()
self.drawNumbers()
# info bar
if menuIsActive != True:
self.menuButton.set_text("Menu")
self.killMenu()
self.drawNumsLeft()
# temporary boolean to execute the drawMenu function just once
tempBool = True
else:
if tempBool:
self.drawMenu()
tempBool = False
self.menuButton.set_text("Return")
pygame.display.update()
self.manager.update(dt)
if __name__ == "__main__":
main = Main()
main.run()