-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoko.py
260 lines (214 loc) · 6.6 KB
/
sudoko.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
import pygame
pygame.init()
#colours
blue = (102,255,255)
white=(255,255,255)
dblack=(0,0,0)
green = (51,255,51)
bg1 = (153, 0, 76)
bg2 = (255,51,51)
ic = (125,229,0)
solve = (51,255,51)
reset = (255,255,51)
#game logo and title
logo = pygame.image.load('joystick.png')
pygame.display.set_icon(logo)
pygame.display.set_caption('Suduko solver')
#game window
screen = pygame.display.set_mode((410, 500))
game_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]]
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]]
colour = [white,ic]
class block():
def __init__(self):
self.x = 0
self.y = 0
self.colour = colour
self.num = [256,257,258,259,260,261,262,263,264,265]
self.icolour = white
self.acolour = green
def first_block(self,grid):
grid[self.y][self.x]=1
return grid
def erase(self,grid):
for i in range(9):
for j in range(9):
grid[i][j] = 0
return grid
def move_left(self,grid):
result = (self.x>0)
if result:
self.erase(grid)
self.x += -1
return grid
def move_right(self,grid):
result = (self.x <8)
if result:
self.erase(grid)
self.x += 1
return grid
def move_up(self,grid):
result = (self.y > 0)
if result:
self.erase(grid)
self.y += -1
return grid
def move_down(self,grid):
result = (self.y < 8)
if result:
self.erase(grid)
self.y += 1
return grid
def pointer_mouse(self,grid,pos):
(x ,y) = pos
x = x-25
y = y-60
x1 = x//40
y1 = y//40
result = (((x1<9) and (x1>-1)) and ((y1<9) and (y1>-1)))
if result:
self.erase(grid)
self.x = x1
self.y = y1
return grid
def input(self,key):
game_grid[self.y].pop(self.x)
game_grid[self.y].insert(self.x,key%256)
def draw_grid(grid):
game.first_block(grid)
for x in range(9):
for y in range(9):
pygame.draw.rect(screen,colour[grid[y][x]],(25+x*40+3,60+y*40+3,35,35))
def draw_game_grid(game_grid):
for x in range(9):
for y in range(9):
q = game_grid[y][x]
if q > 0:
message_to_screen(str(q),30,dblack,25+x*40+10,60+y*40+8)
def reset():
for y in range(9):
for x in range(9):
game_grid[y][x] = 0
def solve(bo):
find = find_empty(bo)
if not find: # if find is None or False
return True
else:
row, col = find
for num in range(1, 10):
if valid(bo, num, (row, col)):
bo[row][col] = num
if solve(bo):
return True
bo[row][col] = 0
return False
def valid(bo, num, pos):
# Check row
for i in range(len(bo[0])):
if bo[pos[0]][i] == num and pos[1] != i:
return False
# Check column
for i in range(len(bo)):
if bo[i][pos[1]] == num and pos[0] != i:
return False
# Check box
box_x = pos[1] // 3
box_y = pos[0] // 3
for i in range(box_y*3, box_y*3 + 3):
for j in range(box_x*3, box_x*3 + 3):
if bo[i][j] == num and (i, j) != pos:
return False
return True
def print_board(bo):
for i in range(len(bo)):
for j in range(len(bo[0])):
print(bo[i][j], end=" ")
print()
def find_empty(bo):
for i in range(len(bo)):
for j in range(len(bo[0])):
if bo[i][j] == 0:
return i, j # row, column
return None
def solve_state(game_grid):
solve_state=False
for y in range(9):
for x in range(9):
if game_grid[y][x] != 0:
solve_state = True
return solve_state
def message_to_screen(q,size,colour,x,y):
message = pygame.font.Font('freesansbold.ttf',size)
messages = message.render(q,True,colour)
screen.blit(messages,(x,y))
def frame():
pygame.draw.rect(screen, bg1, (0, 0, 410, 160))
pygame.draw.rect(screen, bg2, (0, 160, 410, 340))
pygame.draw.rect(screen, white, (25, 60, 360, 360))
i = 1
for x in range(10):
if x%3 == 0:
i=3
pygame.draw.line(screen, dblack, (25+x*40, 60), (25+x*40, 420),i)
i=1
for y in range(10):
if y%3 ==0:
i=3
pygame.draw.line(screen,dblack,(25,60+y*40),(385,60+y*40),i)
i=1
message_to_screen("Sudoko solver",40,blue,65,10)
message_to_screen("-Press 'Enter' to solve",15,dblack,25,425)
message_to_screen("-Press 'Backspace' to reset",15,dblack,25,445)
message_to_screen("-Press '0' to reset particular cell",15,dblack,25,465)
message_to_screen("(OR) Press another number to overwrite the number ",15,dblack,25,485)
game = block()
running = True
reset_state = True
while running:
screen.fill((dblack))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
game.pointer_mouse(grid, pos)
elif event.type == pygame.KEYDOWN:
if event.key in game.num:
if reset_state:
game.input(event.key)
elif event.key == pygame.K_RETURN:
if solve_state(game_grid):
solve(game_grid)
reset_state = False
elif event.key == pygame.K_BACKSPACE:
reset()
reset_state = True
elif event.key == pygame.K_RIGHT:
game.move_right(grid)
elif event.key == pygame.K_LEFT:
game.move_left(grid)
elif event.key == pygame.K_UP:
game.move_up(grid)
elif event.key == pygame.K_DOWN:
game.move_down(grid)
frame()
draw_grid(grid)
draw_game_grid(game_grid)
pygame.display.update()