-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.py
354 lines (309 loc) · 12.2 KB
/
Sudoku.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
import tkinter
from tkinter import *
import random
guess = str()
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]]
root = Tk()
root.title("Sudoku")
root.minsize(width=500, height=635)
root.attributes("-alpha", 0.90)
root.configure(background="#282828")
title = Label(root, text='Sudoku', fg="#382888",font="Geneva 30",bg="#282828")
title.pack()
box = Canvas(root, width = 435, height = 435,background="#282828",bd=6, highlightthickness=6)
box.pack()
entry_list = [[],[],[]]
var =[]
done = False
def one_grid(row):
global grid,entry_list
g1 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
entry_list[0].append(g1)
g1.place(x = 5,y = 5)
g2 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
entry_list[0].append(g2)
g2.place(x = 52,y = 5)
g3 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g3.place(x = 102,y = 5)
entry_list[0].append(g3)
g4 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g4.place(x = 5,y = 52)
entry_list[1].append(g4)
g5 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g5.place(x = 52,y = 52)
entry_list[1].append(g5)
g6 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g6.place(x = 102,y = 52)
entry_list[1].append(g6)
g7 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g7.place(x = 5,y = 102)
entry_list[2].append(g7)
g8 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g8.place(x = 52,y = 102)
entry_list[2].append(g8)
g9 = Entry(row,textvariable=var,width = 2, fg="white", font="Geneva 30 bold",bg="#373737",justify=CENTER)
g9.place(x = 102,y = 102)
entry_list[2].append(g9)
def display_val():
global entry_list
u=0
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(grid[u][y] != 0):
a_splited[0][y].insert(0,grid[u][y])
u+=1
u=3
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(grid[u][y] != 0):
a_splited[1][y].insert(0,grid[u][y])
u+=1
u=6
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(grid[u][y] != 0):
a_splited[2][y].insert(0,grid[u][y])
u+=1
def clear():
global grid
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 scramble():
global grid
clear()
for a in entry_list:
for b in a:
b.delete(first=0,last=100)
amount = 20
for i in range(amount):
y = random.randint(0,len(grid)-1)
x = random.randint(0,len(grid)-1)
num = random.randint(1,len(grid))
allow = 0
for e in range(len(grid)):
if num not in grid[x] and num != grid[e][y]:
allow +=1
grid[x][y] = num
tempo = grid
tempo = rearrange(tempo)
for e in range(len(grid)):
if(duplicate_checker(tempo[e])):
allow = 0
if allow !=len(grid):
grid[x][y] = 0
display_val()
def stay(num,x,y):
for e in range(9):
if grid[y][e] ==num:
return False
for e in range(9):
if grid[e][x] ==num:
return False
for i in range(3):
for e in range(3):
if grid[((y//3)*3)+i][((x//3)*3)+e] ==num:
return False
return True
def pressed_solve():
global grid,done
done =False
solver()
def solver():
global grid,done
if(done ==False):
for y in range(9):
for x in range(9):
if grid[y][x] == 0:
for num in range(1,10):
if stay(num,x,y):
grid[y][x]=num
solver()
grid[y][x]=0
return
done =True
for a in entry_list:
for b in a:
b.delete(first=0,last=100)
display_val()
return
def rearrange(a):
temp=[[],[],[],[],[],[],[],[],[]]
count =0
ch = 0
for e in range(len(a)):
for x in range(3):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
for e in range(len(a)):
for x in range(3,6):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
for e in range(len(a)):
for x in range(6,9):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
return temp
def duplicate_checker(a):
b = set(a)
result = len(a) != len(b)
#print(result)
if(result == True):
return True
def checkrow_horz(a):
for x in a:
if(duplicate_checker(x) == True):
return True
def checkrow_vert(a):
for y in range(len(a)):
temp = []
for x in a:
temp.append(x[y])
if(duplicate_checker(temp) == True):
return True
def checkcol(a):
for y in range(3):
temp = []
for x in range(int(len(a)/3)):
temp.append(a[x][3*y])
temp.append(a[x][3*y+1])
temp.append(a[x][3*y+2])
if(duplicate_checker(temp) == True):
return True
temp = []
for x in range(3,(int(len(a)/3))*2):
temp.append(a[x][3*y])
temp.append(a[x][3*y+1])
temp.append(a[x][3*y+2])
if(duplicate_checker(temp) == True):
return True
temp = []
for x in range(6,(int(len(a)/3))*3):
temp.append(a[x][3*y])
temp.append(a[x][3*y+1])
temp.append(a[x][3*y+2])
if(duplicate_checker(temp) == True):
return True
def submit():
global entry_list
temp = [[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
u=0
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(a_splited[0][y].get() != '' ):
temp[u][y]= int(a_splited[0][y].get())
u+=1
u=3
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(a_splited[1][y].get() != '' ):
temp[u][y]= int(a_splited[1][y].get())
u+=1
u=6
for a in entry_list:
a_splited = [a[x:x+9] for x in range(0, len(a), 9)]
for y in range(9):
if(a_splited[2][y].get() != '' ):
temp[u][y]= int(a_splited[2][y].get())
u+=1
if(checkrow_horz(temp) == True or checkrow_vert(temp) ==True or checkcol(temp) ==True):
wrong()
else:
correct()
def wrong():
title.config(fg="#990000",text="Try Again")
row1.config(highlightbackground="#990000")
row2.config(highlightbackground="#990000")
row3.config(highlightbackground="#990000")
row4.config(highlightbackground="#990000")
row5.config(highlightbackground="#990000")
row6.config(highlightbackground="#990000")
row7.config(highlightbackground="#990000")
row8.config(highlightbackground="#990000")
row9.config(highlightbackground="#990000")
row1.after(2100, lambda: row1.config(highlightbackground="white"))
row2.after(2100, lambda: row2.config(highlightbackground="white"))
row3.after(2100, lambda: row3.config(highlightbackground="white"))
row4.after(2100, lambda: row4.config(highlightbackground="white"))
row5.after(2100, lambda: row5.config(highlightbackground="white"))
row6.after(2100, lambda: row6.config(highlightbackground="white"))
row7.after(2100, lambda: row7.config(highlightbackground="white"))
row8.after(2100, lambda: row8.config(highlightbackground="white"))
row9.after(2100, lambda: row9.config(highlightbackground="white"))
title.after(2100, lambda: title.config(fg="#382888",text ="Sudoku"))
def correct():
title.config(fg="#288888",text="Correct")
row1.config(highlightbackground="#288888")
row2.config(highlightbackground="#288888")
row3.config(highlightbackground="#288888")
row4.config(highlightbackground="#288888")
row5.config(highlightbackground="#288888")
row6.config(highlightbackground="#288888")
row7.config(highlightbackground="#288888")
row8.config(highlightbackground="#288888")
row9.config(highlightbackground="#288888")
row1.after(2100, lambda: row1.config(highlightbackground="white"))
row2.after(2100, lambda: row2.config(highlightbackground="white"))
row3.after(2100, lambda: row3.config(highlightbackground="white"))
row4.after(2100, lambda: row4.config(highlightbackground="white"))
row5.after(2100, lambda: row5.config(highlightbackground="white"))
row6.after(2100, lambda: row6.config(highlightbackground="white"))
row7.after(2100, lambda: row7.config(highlightbackground="white"))
row8.after(2100, lambda: row8.config(highlightbackground="white"))
row9.after(2100, lambda: row9.config(highlightbackground="white"))
title.after(2100, lambda: title.config(fg="#382888",text ="Sudoku"))
row1 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row1)
row1.place(x = 0,y = 0)
row2 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row2)
row2.place(x = 150,y = 0)
row3 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row3)
row3.place(x = 300,y = 0)
row4 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row4)
row4.place(x = 0,y = 150)
row5 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row5)
row5.place(x = 150,y = 150)
row6 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row6)
row6.place(x = 300,y = 150)
row7 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row7)
row7.place(x = 0,y = 300)
row8 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row8)
row8.place(x = 150,y = 300)
row9 = Canvas(box, width = 120, height = 120,background="#282828",bd=10, highlightthickness=10)
one_grid(row9)
row9.place(x = 300,y = 300)
scramble()
but = Canvas(root, width = 500, height = 45,background="#282828",bd=13, highlightthickness=0)
but.pack()
enter = Button(but,text ="Submit", fg="#282828",command=submit, font="Geneva 30 bold",bg='#282828',activebackground='#282828',highlightbackground="#288888",disabledforeground="#288888",justify=CENTER)
enter.configure(bg='#282828',width = 12)
enter.place(x = 0,y = 10)
reset = Button(but,text ="Reset", fg="#282828",command=scramble, font="Geneva 30 bold",bg='#382888',activebackground='#382888',highlightbackground="#382888",disabledforeground="#382888",justify=CENTER)
reset.configure(bg='#382888',width = 12)
reset.place(x = 250,y = 10)
solve = Button(root,text ="Solve", fg="#282828",command=pressed_solve, font="Geneva 30 bold",bg='#FCD4D4',activebackground='#FCD4D4',highlightbackground="#FCD4D4",disabledforeground="#FCD4D4",justify=CENTER)
solve.configure(bg='#FCD4D4',width = 12)
solve.place(x = 120,y = 575)
root.mainloop()