-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_game_with_checker_offline.py
306 lines (255 loc) · 12 KB
/
sudoku_game_with_checker_offline.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
import pygame
import copy
########################
# Global Variables #
########################
width = 800
play_width = 550
background_color = (255, 255, 255)
x_tuning = 3.5
y_tuning = 4
######################
# Example Sudoku #
######################
grid = '1.4....7.......81.........44.1..87...6...3....9.6....27....659.38..9.6..9..31....'
grid_original = [[1, 0, 4, 0, 0, 0, 0, 7, 0],
[0, 0, 0, 0, 0, 0, 8, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 4],
[4, 0, 1, 0, 0, 8, 7, 0, 0],
[0, 6, 0, 0, 0, 3, 0, 0, 0],
[0, 9, 0, 6, 0, 0, 0, 0, 2],
[7, 0, 0, 0, 0, 6, 5, 9, 0],
[3, 8, 0, 0, 9, 0, 6, 0, 0],
[9, 0, 0, 3, 1, 0, 0, 0, 0]]
grid_for_computer = copy.deepcopy(grid_original)
grid_for_player = copy.deepcopy(grid_original)
####################################
# Functions for Solving Sudoku #
####################################
def find_empty(sudoku):
for i in range(len(sudoku)):
for ii in range(len(sudoku[0])):
if sudoku[i][ii] == 0:
return i, ii
return None
def valid(sudoku, number, position):
# Check row
for i in range(len(sudoku[0])):
if sudoku[position[0]][i] == number and position[1] != i:
return False
# Check column
for i in range(len(sudoku)):
if sudoku[i][position[1]] == number and position[0] != i:
return False
# Check box
box_x = position[1] // 3
box_y = position[0] // 3
for i in range(box_y * 3, box_y * 3 + 3):
for ii in range(box_x * 3, box_x * 3 + 3):
if sudoku[i][ii] == number and (i, ii) != position:
return False
return True
def solve(sudoku):
find = find_empty(sudoku)
if not find:
return True
else:
row, column = find
for i in range(1, 10):
if valid(sudoku, i, (row, column)):
sudoku[row][column] = i
if solve(sudoku):
return True
sudoku[row][column] = 0
return False
######################################
# Function for Drawing the Title #
######################################
def compare(y, x):
if grid_for_player[y][x] != grid_for_computer[y][x]:
return False
else:
return True
######################################
# Function for Drawing the Title #
######################################
def draw_title(win):
pygame.font.init()
title_font = pygame.font.SysFont('arial', 40, bold=False)
game_label = title_font.render('SUDOKU', 1, (0, 0, 0))
win.blit(game_label, (width / 2 - game_label.get_width() / 2 + 26, 12))
#######################################
# Function for Creating the Grids #
#######################################
def draw_grid(win):
for i in range(10):
if i % 3 == 0:
pygame.draw.line(win, (0, 0, 0), (50 + 50 * i, 50), (50 + 50 * i, 500), 4)
pygame.draw.line(win, (0, 0, 0), (50, 50 + 50 * i), (500, 50 + 50 * i), 4)
pygame.draw.line(win, (0, 0, 0), (50 + 50 * i, 50), (50 + 50 * i, 500), 2)
pygame.draw.line(win, (0, 0, 0), (50, 50 + 50 * i), (500, 50 + 50 * i), 2)
pygame.display.update()
#######################################################
# Function for Placing the Numbers into the Grids #
#######################################################
def place_numbers(win):
font = pygame.font.SysFont('arial', 35)
for i in range(9):
for ii in range(9):
if 0 < grid_original[i][ii] < 10:
value = font.render(str(grid_original[i][ii]), 1, (0, 0, 0))
win.blit(value, ((ii + 1) * 50 + 18, (i + 1) * 50 + 8))
pygame.display.update()
################################
# Function for Click Event #
################################
def insert(win, position):
x, y = position[0]//50, position[1]//50
font = pygame.font.SysFont('arial', 35)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if 0 < x < 10 and 0 < y < 10:
if grid_original[y-1][x-1] != 0:
return
else:
if event.key == pygame.K_1 or event.key == pygame.K_KP1:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 1
check = compare(y-1, x-1)
if check:
value = font.render(str(1), 1, (0, 136, 3))
else:
value = font.render(str(1), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_2 or event.key == pygame.K_KP2:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 2
check = compare(y - 1, x - 1)
if check:
value = font.render(str(2), 1, (0, 136, 3))
else:
value = font.render(str(2), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_3 or event.key == pygame.K_KP3:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 3
check = compare(y - 1, x - 1)
if check:
value = font.render(str(3), 1, (0, 136, 3))
else:
value = font.render(str(3), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_4 or event.key == pygame.K_KP4:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 4
check = compare(y - 1, x - 1)
if check:
value = font.render(str(4), 1, (0, 136, 3))
else:
value = font.render(str(4), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_5 or event.key == pygame.K_KP5:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 5
check = compare(y - 1, x - 1)
if check:
value = font.render(str(5), 1, (0, 136, 3))
else:
value = font.render(str(5), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_6 or event.key == pygame.K_KP6:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 6
check = compare(y - 1, x - 1)
if check:
value = font.render(str(6), 1, (0, 136, 3))
else:
value = font.render(str(6), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_7 or event.key == pygame.K_KP7:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 7
check = compare(y - 1, x - 1)
if check:
value = font.render(str(7), 1, (0, 136, 3))
else:
value = font.render(str(7), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_8 or event.key == pygame.K_KP8:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 8
check = compare(y - 1, x - 1)
if check:
value = font.render(str(8), 1, (0, 136, 3))
else:
value = font.render(str(8), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_9 or event.key == pygame.K_KP9:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
grid_for_player[y - 1][x - 1] = 9
check = compare(y - 1, x - 1)
if check:
value = font.render(str(9), 1, (0, 136, 3))
else:
value = font.render(str(9), 1, (125, 36, 69))
win.blit(value, (x * 50 + 18, y * 50 + 8))
pygame.display.update()
return
if event.key == pygame.K_0 or event.key == pygame.K_KP_0:
pygame.draw.rect(win, (255, 255, 255), (x * 54 - (x - 1) * x_tuning, y * 54 - (y - 1) * y_tuning, 40, 40))
pygame.display.update()
grid_for_player[y - 1][x - 1] = 0
return
return
def main():
###########################
# Setting up the Game #
###########################
solve(grid_for_computer)
pygame.init()
win = pygame.display.set_mode((play_width, play_width))
pygame.display.set_caption("Sudoku")
win.fill(background_color)
#########################
# Drawing the Title #
#########################
draw_title(win)
##########################
# Creating the Grids #
##########################
draw_grid(win)
##########################################
# Placing the Numbers into the Grids #
##########################################
place_numbers(win)
########################
# Quiting the game #
########################
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
pos = pygame.mouse.get_pos()
insert(win, pos)
if event.type == pygame.QUIT:
pygame.quit()
return
main()