-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
199 lines (170 loc) · 4.58 KB
/
gui.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
import pygame
from main import solve_sudoku
sub = 500 / 9
grid =[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
empty_grid =[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
def valid_move(matrix, col, row, val):
for i in range(9):
if matrix[col][i]== val:
return False
if matrix[i][row]== val:
return False
a = col//3
b = row//3
for x in range(a * 3, a * 3 + 3):
for y in range (b * 3, b * 3 + 3):
if matrix[x][y]== val:
return False
return True
pygame.font.init()
screen = pygame.display.set_mode((500, 800))
pygame.display.set_caption("Sudoku Solver with AC3 and Backtracking")
val = 0
x = 0
y = 0
font1 = pygame.font.SysFont("Times New Roman", 30, "bold")
font2 = pygame.font.SysFont("Times New Roman", 26, "bold")
def get_cord(pos):
global x
x = pos[0]//sub
global y
y = pos[1]//sub
def draw_box():
for i in range(2):
pygame.draw.line(screen, (150, 0, 0), (x * sub-3, (y + i)*sub), (x * sub + sub + 3, (y + i)*sub), 7)
pygame.draw.line(screen, (150, 0, 0), ( (x + i)* sub, y * sub ), ((x + i) * sub, y * sub + sub), 7)
def draw():
for i in range (9):
for j in range (9):
if grid[i][j]!= 0:
pygame.draw.rect(screen, (255, 255, 0), (i * sub, j * sub, sub + 1, sub + 1))
text1 = font1.render(str(grid[i][j]), 1, (0, 0, 0))
screen.blit(text1, (i * sub + 20, j * sub + 11))
for i in range(10):
if i % 3 == 0 :
thick = 5
colourbold = 1.20
else:
thick = 2
colourbold = 0
pygame.draw.line(screen, (0, 120 * colourbold, 0), (0, i * sub), (500, i * sub), thick)
pygame.draw.line(screen, (0, 120* colourbold, 0), (i * sub, 0), (i * sub, 500), thick)
def draw_val(val):
text1 = font1.render(str(val), 1, (0, 0, 0))
screen.blit(text1, (x * sub + 15, y * sub + 15))
def instruction():
text1 = font2.render("Press D to get a default configuration", 1, (0, 50, 0))
text2 = font2.render("Press S to solve Sudoku", 1, (0, 50, 0))
text3 = font2.render("Press A to clear screen", 1, (0, 50, 0))
screen.blit(text1, (20, 520))
screen.blit(text2, (20, 620))
screen.blit(text3, (20, 720))
run = True
state1 = 0
state2 = 0
while run:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
state1 = 1
pos = pygame.mouse.get_pos()
get_cord(pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x-= 1
state1 = 1
if event.key == pygame.K_RIGHT:
x+= 1
state1 = 1
if event.key == pygame.K_UP:
y-= 1
state1 = 1
if event.key == pygame.K_DOWN:
y+= 1
state1 = 1
if event.key == pygame.K_1:
val = 1
if event.key == pygame.K_2:
val = 2
if event.key == pygame.K_3:
val = 3
if event.key == pygame.K_4:
val = 4
if event.key == pygame.K_5:
val = 5
if event.key == pygame.K_6:
val = 6
if event.key == pygame.K_7:
val = 7
if event.key == pygame.K_8:
val = 8
if event.key == pygame.K_9:
val = 9
if event.key == pygame.K_RETURN:
state2 = 1
if event.key == pygame.K_a:
state2 = 0
grid =[
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
if event.key == pygame.K_d:
state2 = 0
grid =[
[7, 8, 0, 4, 0, 0, 1, 2, 0],
[6, 0, 0, 0, 7, 5, 0, 0, 9],
[0, 0, 0, 6, 0, 1, 0, 7, 8],
[0, 0, 7, 0, 4, 0, 2, 6, 0],
[0, 0, 1, 0, 5, 0, 9, 3, 0],
[9, 0, 4, 0, 6, 0, 0, 0, 5],
[0, 7, 0, 3, 0, 0, 0, 1, 2],
[1, 2, 0, 0, 0, 7, 4, 0, 0],
[0, 4, 9, 2, 0, 6, 0, 0, 7]
]
if event.key == pygame.K_s:
if grid != empty_grid:
state2 = 0
grid = solve_sudoku(grid)
if val != 0:
draw_val(val)
if valid_move(grid, int(x), int(y), val)== True:
grid[int(x)][int(y)]= val
state1 = 0
else:
grid[int(x)][int(y)]= 0
val = 0
draw()
if state1 == 1:
draw_box()
instruction()
pygame.display.update()
pygame.quit()