-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
87 lines (68 loc) · 2.33 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
from SolveSudoku import isSafe, FindEmptyLocation
import Boards
from Box import Box
import pygame
pygame.init()
class GUI:
def __init__(self, grid, width, height, window):
self.height = height
self.width = width
self.window = window
self.grid = grid
self.rows = 9
self.cols = 9
self.boxes = [[Box(grid[i][j], i, j, self.width, self.height) for j in range(self.cols)]
for i in range(self.rows)]
def draw_grid(self):
space = self.width / 9
for i in range(self.rows + 1):
if i % 3 == 0 and i != 0:
thickness = 4
else:
thickness = 1
pygame.draw.line(self.window, (255, 255, 255), (0, i * space), (self.width, i * space), thickness)
pygame.draw.line(self.window, (255, 255, 255), (i * space, 0), (i * space, self.height), thickness)
for i in range(self.rows):
for j in range(self.cols):
self.boxes[i][j].draw_boxes(self.window)
def solve(self):
box = FindEmptyLocation(self.grid)
if box:
r, c = box
else:
return True
for i in range(1, 10):
if isSafe(self.grid, r, c, i):
self.grid[r][c] = i
self.boxes[r][c].add_num(i)
self.boxes[r][c].update_box(self.window, True)
pygame.display.update()
pygame.time.delay(100)
if self.solve():
return True
self.grid[r][c] = 0
self.boxes[r][c].add_num(0)
self.boxes[r][c].update_box(self.window, False)
pygame.display.update()
pygame.time.delay(100)
return False
def redrawWindow(win, grid):
win.fill((0, 0, 0))
grid.draw_grid()
def main():
g = Boards.get_board()
window = pygame.display.set_mode((540, 600))
pygame.display.set_caption("Sudoku")
grid = GUI(g, 540, 540, window)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
grid.solve()
redrawWindow(window, grid)
pygame.display.update()
main()
pygame.quit()