-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
387 lines (317 loc) · 11.9 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import tkinter
from tkinter import filedialog
import numpy as np
import time
import pygame
from button import button
from img_processing import img_processing
from solvealg import solve
pygame.init()
######################################################################################################
# preaparing boards
levels = [
[
[0, 0, 5, 0, 0, 0, 0, 0, 4],
[7, 0, 4, 0, 9, 8, 0, 2, 0],
[0, 0, 0, 0, 6, 0, 0, 0, 0],
[5, 0, 0, 4, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 0],
[1, 0, 7, 6, 0, 0, 0, 0, 2],
[0, 0, 0, 2, 0, 6, 0, 1, 8],
[0, 0, 0, 3, 0, 0, 0, 0, 0],
[9, 2, 0, 8, 0, 0, 4, 0, 0]
],
[
[0, 0, 5, 0, 0, 0, 0, 0, 4],
[7, 0, 4, 0, 9, 8, 0, 2, 0],
[0, 0, 0, 0, 6, 0, 0, 0, 0],
[5, 0, 0, 4, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 0],
[1, 0, 7, 6, 0, 0, 0, 0, 2],
[0, 0, 0, 2, 0, 6, 0, 1, 8],
[0, 0, 0, 3, 0, 0, 2, 0, 0],
[9, 2, 0, 8, 0, 0, 4, 0, 0]
],
[
[9, 5, 7, 0, 0, 0, 0, 0, 1],
[0, 0, 8, 0, 5, 0, 4, 0, 0],
[0, 4, 0, 8, 0, 1, 0, 0, 0],
[0, 1, 3, 4, 9, 0, 7, 0, 6],
[4, 0, 0, 0, 8, 0, 0, 0, 5],
[8, 0, 5, 0, 6, 2, 3, 4, 0],
[0, 0, 0, 2, 0, 9, 0, 3, 0],
[0, 0, 2, 0, 4, 0, 1, 0, 0],
[6, 0, 0, 0, 0, 0, 5, 7, 2],
]
]
# GUI
################################################################################################################
black_x = pygame.image.load('imgs/black_X.png')
red_x = pygame.image.load('imgs/red_X.png')
def drawing_play_board():
pygame.draw.rect(board, (0, 0, 0), (20, 50, 472, 4))
pygame.draw.rect(board, (0, 0, 0), (20, 206, 472, 4))
pygame.draw.rect(board, (0, 0, 0), (20, 362, 472, 4))
pygame.draw.rect(board, (0, 0, 0), (20, 518, 472, 4))
############################################################################
pygame.draw.rect(board, (0, 0, 0), (20, 104, 472, 1))
pygame.draw.rect(board, (0, 0, 0), (20, 155, 472, 1))
pygame.draw.rect(board, (0, 0, 0), (20, 260, 472, 1))
pygame.draw.rect(board, (0, 0, 0), (20, 311, 472, 1))
pygame.draw.rect(board, (0, 0, 0), (20, 416, 472, 1))
pygame.draw.rect(board, (0, 0, 0), (20, 467, 472, 1))
#############################################################################
pygame.draw.rect(board, (0, 0, 0), (20, 50, 4, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (176, 50, 4, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (332, 50, 4, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (488, 50, 4, 522 - 50))
#####################################################################################
pygame.draw.rect(board, (0, 0, 0), (74, 50, 1, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (125, 50, 1, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (230, 50, 1, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (281, 50, 1, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (386, 50, 1, 522 - 50))
pygame.draw.rect(board, (0, 0, 0), (437, 50, 1, 522 - 50))
useless_variable = 1 # to run solve function only once
def drawing_numbers_on_board():
global solved, printed_board, useless_variable
# if solve button is clcked run solve function and display it, else display unsolved board
if solve_butt():
solved = True
printed_board = completed_board
else:
if not solved and useless_variable == 1:
printed_board = num
######################################################################################################
font = pygame.font.SysFont('Comic Sans MS', 30)
xna = 0
yna = 0
for y in range(9):
if (y) % 3 == 0 or y == 0:
yna += 4
else:
yna += 1
for x in range(9):
if (x) % 3 == 0 or x == 0:
xna += 4
else:
xna += 1
###############################################
x_text = 20 + (x * 50) + xna
y_text = 50 + (y * 50) + yna
if printed_board[y][x] != 0:
text = font.render(str(printed_board[y][x]), False, (0, 0, 0))
board.blit(text, (
(x_text + 50 // 2) - (text.get_width() // 2), (y_text + 50 // 2) - (text.get_height() // 2)))
else:
if useless_variable == 1:
inputed.append([num_inputs(x_text, y_text, ('')), y, x])
############################################################################################
if x == 8:
xna = 0
useless_variable = 2
#########################################################################################################
class num_inputs:
def __init__(self, x, y, text, r=30, color=(255, 255, 255)):
self.x = x
self.y = y
self.text = text
self.r = r
self.color = color
self.return_text = ''
self.clicked = False
self.correct = 2
self.waiting_variable = 0
def buttons(self):
global lives
self.button_nam = button(self.color, self.x, self.y, 50, 50)
if self.correct == 2:
if self.clicked == True:
self.color = (191, 143, 143)
elif self.button_nam.isOver(pygame.mouse.get_pos()):
self.color = (200, 200, 200)
else:
self.color = (255, 255, 255)
elif self.correct == 0:
self.color = (67, 210, 105)
if self.waiting_variable != 20:
self.waiting_variable += 1
else:
self.waiting_variable = 0
return True
else:
self.color = (219, 43, 43)
# show red color for some amount of time, than remove the text
if self.waiting_variable != 20:
self.waiting_variable += 1
else:
self.return_text = ''
self.waiting_variable = 0
lives += 1
self.button_nam = button(self.color, self.x, self.y, 50, 50)
self.button_nam.draw(board)
def isClicked(self):
global key_unicode, pressed_key
if pygame.mouse.get_pressed()[0]:
if self.button_nam.isOver(pygame.mouse.get_pos()):
self.clicked = True
if self.clicked:
if pressed_key == pygame.K_RETURN:
self.text = self.return_text
self.clicked = False
return self.text
else:
if key_unicode.isnumeric():
self.return_text = key_unicode
self.text = self.return_text
self.clicked = False
key_unicode = ''
def check_correct(self, y, x):
global printed_board, inputed
if self.return_text.isnumeric() and completed_board[y][x] == int(self.return_text):
self.correct = 0
elif self.return_text.isnumeric() and completed_board[y][x] != int(self.return_text):
self.correct = 1
else:
self.correct = 2
def display_user_input(self):
x_hranica = self.x + 50
font = pygame.font.SysFont('Comic Sans MS', self.r)
rend = font.render(self.return_text, False, (0, 0, 0))
board.blit(rend, ((((x_hranica - self.x) // 2) + self.x) -
(rend.get_width() // 2), self.y))
def input_blank():
global curently_clicked, printed_board
if not solved:
for i in range(len(inputed)):
if inputed[i][0].buttons():
printed_board[inputed[i][1]][inputed[i]
[2]] = inputed[i][0].return_text
inputed.pop(i)
break
inputed[i][0].isClicked()
inputed[i][0].check_correct(inputed[i][1], inputed[i][2])
# So only 1 bracket can be activated!1
if inputed[i][0].clicked == True and inputed[i][0] not in curently_clicked:
curently_clicked.append(inputed[i][0])
if len(curently_clicked) >= 2:
curently_clicked[0].clicked = False
curently_clicked.pop(0)
inputed[i][0].display_user_input()
var = True
def load_image_to_sudoku():
global num, useless_variable, solved, inputed, var
path_img = ''
# draw_button
if load_img_button() and var:
# open file browser
var = False
root = tkinter.Tk()
root.withdraw() # hide tkinter window
currdir = os.getcwd()
path_img = filedialog.askopenfilename(
parent=root, initialdir=currdir, title='Please select a img', filetypes=[('Image files', '.jpg .png')])
root.destroy()
font = pygame.font.SysFont('Comic Sans MS', 30)
txt = font.render('Loading...', False, (0, 0, 0))
board.blit(txt, (550, 300))
# process image and get sudoku board from it
if len(path_img) > 0:
brd = img_processing(path_img)
# reset variables for new board
num = brd
solve()
useless_variable = 1
solved = False
inputed = []
def load_img_button():
load_img_button = button((147, 158, 202), 600, 150,
250, 50, text='Load image', r=40)
if load_img_button.isOver(pygame.mouse.get_pos()):
load_img_button.color = (164, 164, 164)
else:
load_img_button.color = (147, 158, 202)
load_img_button.draw(board)
if load_img_button.isclicked():
return True
else:
return False
def solve_butt():
solve_button = button((147, 158, 202), 600, 50,
250, 50, text='Solve', r=40)
if solve_button.isOver(pygame.mouse.get_pos()):
solve_button.color = (164, 164, 164)
else:
solve_button.color = (147, 158, 202)
solve_button.draw(board)
if solve_button.isclicked():
return True
else:
return False
def change_board():
global board_num, num, useless_variable, solved, inputed
font = pygame.font.SysFont('Comic Sans MS', 30)
txt = font.render('Loading...', False, (0, 0, 0))
#####################################
next_button = button((147, 158, 202), 750, 450, 120, 50, 'skip', r=50)
if next_button.isOver(pygame.mouse.get_pos()):
next_button.color = (164, 164, 164)
else:
next_button.color = (147, 158, 202)
next_button.draw(board)
if next_button.isclicked():
board.blit(txt, (550, 300))
time.sleep(0.5)
if board_num + 1 > len(levels):
board_num = 0
else:
board_num += 1
# reset everything for new board
num = levels[board_num]
solve()
useless_variable = 1
solved = False
inputed = []
def game_over():
a = 80
board.blit(black_x, (40, 530))
board.blit(black_x, (40 + a, 530))
board.blit(black_x, (40 + 2 * a, 530))
if lives >= 4:
font = pygame.font.SysFont('Comic Sans MS', 30)
for i in range(lives):
board.blit(red_x, (40 + a * i, 530))
def display_game_window():
board.fill((255, 255, 255))
solve_butt()
drawing_play_board()
drawing_numbers_on_board()
input_blank()
load_image_to_sudoku()
change_board()
game_over()
pygame.display.update()
# main looop#########################################################################################################
if __name__ == '__main__':
lives = 0
board_num = 0
num = levels[board_num]
curently_clicked = []
inputed = []
solved = False
run = True
solve()
board = pygame.display.set_mode((1000, 600))
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
pressed_key = event.key
key_unicode = event.unicode
else:
pressed_key = ''
key_unicode = ''
display_game_window()
pygame.quit()