-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudokuSolverVisual.py
144 lines (109 loc) · 3.97 KB
/
sudokuSolverVisual.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
import pygame
# colours
WHITE = 255, 255, 255
GREY = 192, 192, 192
RED = 255, 0, 0
BLUE = 0, 0, 255
GREEN = 0, 255, 0
# number of frames per second
FPS = 60
# initialising pygame
pygame.init()
pygame.display.set_caption("Sudoku Solver")
numbers = pygame.font.SysFont("notosans", 30)
# window size
screen = pygame.display.set_mode((9*50+5, 9*50+5))
def valid_check(board):
valid = check_row(board) and check_column(board) and check_square(board)
return valid
def check_row(board):
bools = []
for i in range(9):
validity = bools.append(
[x for x in sorted(board[i]) if x != 0] == [x for x in sorted(list(set(board[i]))) if x != 0])
return all(bools)
def check_column(board):
bools = []
board = list(map(list, zip(*board)))
for i in range(9):
validity = bools.append(
[x for x in sorted(board[i]) if x != 0] == [x for x in sorted(list(set(board[i]))) if x != 0])
return all(bools)
def check_square(board):
bools = []
box_board = []
for j in range(3):
for i in range(3):
box = take(j * 3, j * 3 + 3, [x for x in board[3 * i]]) +\
take(j * 3, j * 3 + 3, [x for x in board[3 * i + 1]]) +\
take(j * 3, j * 3 + 3, [x for x in board[3 * i + 2]])
box_board.append(box)
for i in range(9):
bools.append([x for x in sorted(box_board[i]) if x != 0] ==
[x for x in sorted(list(set(box_board[i]))) if x != 0])
return all(bools)
def take(n, p, t_list):
new = []
for i in range(n):
t_list.pop(0)
for i in range(p - n):
new.append(t_list.pop(0))
return new
def find_empty(board, i, j):
for x in range(i, 9):
for y in range(j, 9):
if board[x][y] == 0:
return x, y
for x in range(0, 9):
for y in range(0, 9):
if board[x][y] == 0:
return x, y
return -1, -1
def solve(board, i=0, j=0):
i, j = find_empty(board, i, j)
if i == -1:
return True
for e in range(1, 10):
board[i][j] = e
if valid_check(board):
if solve(board, i, j):
return True
board[i][j] = 0
return False
def main(board):
while True:
# refreshes screen each turn
screen.fill(WHITE)
pygame.draw.line(screen, GREY, (0, 150), (500, 150), 1)
pygame.draw.line(screen, GREY, (0, 300), (500, 300), 1)
pygame.draw.line(screen, GREY, (150, 0), (150, 500), 1)
pygame.draw.line(screen, GREY, (300, 0), (300, 500), 1)
for x in range(9):
for y in range(9):
number_text = numbers.render(str(board[y][x]), True, (0, 0, 0))
screen.blit(number_text, ((x+1)*50-45+10, (y+1)*50-45))
solve(board)
pygame.display.update()
pygame.time.Clock().tick(FPS)
if __name__ == "__main__":
puzzle = eval(input("Enter puzzle here: "))
main(puzzle)
sampleBoard = [[9, 0, 0, 8, 1, 0, 0, 0, 0],
[0, 0, 5, 0, 0, 4, 7, 0, 6],
[0, 0, 0, 2, 0, 5, 8, 0, 1],
[0, 9, 0, 7, 4, 0, 5, 0, 0],
[0, 0, 0, 0, 0, 3, 0, 7, 0],
[7, 4, 0, 0, 0, 0, 0, 0, 0],
[3, 0, 0, 9, 5, 0, 6, 0, 0],
[0, 0, 6, 4, 0, 0, 0, 1, 3],
[1, 7, 0, 0, 0, 0, 0, 0, 4]]
sampleBoard2 = [[5, 1, 7, 6, 0, 0, 0, 3, 4],
[2, 8, 9, 0, 0, 4, 0, 0, 0],
[3, 4, 6, 2, 0, 5, 0, 9, 0],
[6, 0, 2, 0, 0, 0, 0, 1, 0],
[0, 3, 8, 0, 0, 6, 0, 4, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 9, 0, 0, 0, 0, 0, 7, 8],
[7, 0, 3, 4, 0, 0, 5, 6, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
# [[9, 0, 0, 8, 1, 0, 0, 0, 0],[0, 0, 5, 0, 0, 4, 7, 0, 6],[0, 0, 0, 2, 0, 5, 8, 0, 1],[0, 9, 0, 7, 4, 0, 5, 0, 0],[0, 0, 0, 0, 0, 3, 0, 7, 0],[7, 4, 0, 0, 0, 0, 0, 0, 0],[3, 0, 0, 9, 5, 0, 6, 0, 0],[0, 0, 6, 4, 0, 0, 0, 1, 3],[1, 7, 0, 0, 0, 0, 0, 0, 4]]