-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.py
179 lines (152 loc) · 4.03 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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 25 11:33:57 2016
@author: Graham Cooke
"""
#PS Future self remember to comment (;
import random
import sys
import copy
def update():
n = 0
for a in range(3):
for b in range(3):
#for y in range(3):
cell[n] = grid[(a*3)][(b*3):((b*3)+3)] + grid[1+(a*3)][(b*3):((b*3)+3)] + grid[2+(a*3)][(b*3):((b*3)+3)]
n += 1
for y in range(9):
row[y] = grid[y]
for x in range(9):
for y in range(9):
column[x][y] = grid[y][x]
return
def updateSolved():
n = 0
for a in range(3):
for b in range(3):
#for y in range(3):
sCell[n] = solvedGrid[(a*3)][(b*3):((b*3)+3)] + solvedGrid[1+(a*3)][(b*3):((b*3)+3)] + solvedGrid[2+(a*3)][(b*3):((b*3)+3)]
n += 1
for y in range(9):
sRow[y] = solvedGrid[y]
for x in range(9):
for y in range(9):
sColumn[x][y] = solvedGrid[y][x]
return
def findCell(x,y):
xC = x // 3
yC = y // 3
if(yC != 0):
nC = xC + yC*3
else:
nC = xC
return nC
def generate(x,y):
l = [1,2,3,4,5,6,7,8,9]
c = findCell(x,y)
for n in range(9):
if(n+1 in column[x] or n+1 in row[y] or n+1 in cell[c]):
i = l.index(n+1)
l.pop(i)
if(l != []):
grid[y][x] = random.choice(l)
else:
print("error on {},{}; no possible number".format(x,y))
for i in range(9):
print(row[i])
sys.exit()
update()
return
def possible(x,y):
l = [1,2,3,4,5,6,7,8,9]
c = findCell(x,y)
for n in range(9):
if(n+1 in column[x] or n+1 in row[y] or n+1 in cell[c]):
i = l.index(n+1)
l.pop(i)
return len(l)
def getUnsolved():
unsolved = []
for x in range(9):
for y in range(9):
if solvedGrid[y][x] == 0:
unsolved.append([x,y])
return unsolved
def solve():
global solvedGrid
global sRow
global sColumn
global sCell
sColumn = copy.deepcopy(column)
sRow = copy.deepcopy(row)
sCell = copy.deepcopy(cell)
solved = []
solvedGrid = copy.deepcopy(grid)
unsolved = getUnsolved()
updateSolved()
cycles = 0
while(unsolved):
cycles += 1
x = unsolved[0][0]
y = unsolved[0][1]
c = findCell(x,y)
n = solvedGrid[y][x]
n += 1
while((n in sColumn[x] or n in sRow[y] or n in sCell[c]) and n <= 9):
n += 1
if(n > 9):
try:
wipe = solved[-1]
except:
return False
unsolved.insert(0, wipe)
del solved[-1]
solvedGrid[y][x] = 0
else:
solvedGrid[y][x] = n
del unsolved[0]
solved.append([x,y])
updateSolved()
print(cycles)
return True
#Define the sudoku grid
grid = [[0 for x in range(9)] for y in range(9)]
cell = [[0 for x in range(9)] for y in range(9)]
row = [[0 for x in range(9)] for y in range(9)]
column = [[0 for x in range(9)] for y in range(9)]
solvedGrid = [[0 for x in range(9)] for y in range(9)]
sRow= [[0 for x in range(9)] for y in range(9)]
sColumn = [[0 for x in range(9)] for y in range(9)]
sCell = [[0 for x in range(9)] for y in range(9)]
givens = random.randint(20,40)
update()
generated = []
spaces = []
for x in range(9):
for y in range(9):
spaces.append((x,y))
while(len(generated) < givens):
loc = random.choice(spaces)
x = loc[0]
y = loc[1]
print(x,y)
if(possible(x,y) > 0):
generate(x,y)
if(solve()):
generated.append(loc)
i = spaces.index(loc)
spaces.pop(i)
print(len(generated))
else:
grid[y][x] = 0
print("tick")
print("Grid")
print(grid)
print("Row")
print(row)
print("Column")
print(column)
print("Cell")
print(cell)
for i in range(9):
print(row[i])