-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateFilled.py
57 lines (43 loc) · 1.7 KB
/
GenerateFilled.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
from Solution import BackTrackSolution
import numpy.random as npr
class GenerateFilled:
def __init__(self, dim):
self.dim = dim
def generator(self):
assert (self.dim ** 0.5) // 1 == (self.dim ** 0.5), "Improper self.dimensions"
sudokuMat = []
for i in range(self.dim):
rowZero = []
for j in range(self.dim):
rowZero.append(0)
sudokuMat.append(rowZero)
rootDim = int((self.dim ** 0.5) // 1)
squareZ = []
for i in range(rootDim * rootDim):
squareZ.append(((i % rootDim), (i // rootDim)))
for i in range(0, self.dim, rootDim):
randomList = npr.permutation(list(range(1, self.dim + 1)))
for j in range(len(squareZ)):
j_0 = squareZ[j][0]
j_1 = squareZ[j][1]
sudokuMat[j_0 + i][j_1 + i] = randomList[j]
return sudokuMat
def getGenerated(self):
obDiagonalFilled = self.generator()
try:
obDiagonalFilled = self.generator()
obSolved = BackTrackSolution(obDiagonalFilled).ob
except Exception as e:
try:
obDiagonalFilled = self.generator()
obSolved = BackTrackSolution(obDiagonalFilled).ob
except Exception as e:
obDiagonalFilled = self.generator()
obSolved = BackTrackSolution(obDiagonalFilled).ob
return obSolved
if __name__ == '__main__':
# For testing purposes only,
# Run Code by entering your own Sudoku Matrix in parameter
# and by python3 GenerateFilled.py
ret = GenerateFilled(9).getGenerated()
print(ret, sep = " ", end = "\n")