-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
153 lines (119 loc) · 3.18 KB
/
solver.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
import numpy as np
#gameBoard = np.zeros((9,9), dtype=int)
boardEasy = np.array([[0,0,0,2,6,0,7,0,1],
[6,8,0,0,7,0,0,9,0],
[1,9,0,0,0,4,5,0,0],
[8,2,0,1,0,0,0,4,0],
[0,0,4,6,0,2,9,0,0],
[0,5,0,0,0,3,0,2,8],
[0,0,9,3,0,0,0,7,4],
[0,4,0,0,5,0,0,3,6],
[7,0,3,0,1,8,0,0,0]])
boardDifficult = np.array([[0,0,0,6,0,0,4,0,0],
[7,0,0,0,0,3,6,0,0],
[0,0,0,0,9,1,0,8,0],
[0,0,0,0,0,0,0,0,0],
[0,5,0,1,8,0,0,0,3],
[0,0,0,3,0,6,0,4,5],
[0,4,0,2,0,0,0,6,0],
[9,0,3,0,0,0,0,0,0],
[0,2,0,0,0,0,1,0,0]])
boardHard = np.array([[0,2,0,0,0,0,0,0,0],
[0,0,0,6,0,0,0,0,3],
[0,7,4,0,8,0,0,0,0],
[0,0,0,0,0,3,0,0,2],
[0,8,0,0,4,0,0,1,0],
[6,0,0,5,0,0,0,0,0],
[0,0,0,0,1,0,7,8,0],
[5,0,0,0,0,9,0,0,0],
[0,0,0,0,0,0,0,4,0]])
boardWrong = np.array([[0,2,0,0,0,0,0,0,0],
[0,0,0,6,0,0,0,0,3],
[0,7,4,0,8,0,0,0,0],
[0,0,0,0,0,3,0,0,2],
[0,8,0,0,4,0,0,1,0],
[6,0,0,5,0,0,0,0,0],
[0,0,0,0,1,0,7,8,0],
[5,0,0,0,0,9,4,0,0],
[0,0,0,0,0,0,0,4,0]])
def easySolve(board):
notSolved = True
while notSolved:
notSolved = False
for i in np.arange(9):
for j in np.arange(9):
if board[i][j] == 0:
possibleHorizontals = checkHorizontal(board, i)
possibleVerticals = checkVertical(board, j)
possibleSquare = checkSquare(board,i,j)
possibles = list( set(possibleVerticals) & set(possibleHorizontals) & set(possibleSquare))
if len(possibles) == 1:
board[i][j] = possibles[0]
notSolved = True
print(board)
def checkHorizontal(board, x):
row = set(board[x])
row.remove(0)
possibles = []
for a in np.arange(1, 10):
if a not in row:
possibles.append(a)
return possibles
def checkVertical(board, y):
column = set(board[:, y])
column.remove(0)
possibles = []
for b in np.arange(1, 10):
if b not in column:
possibles.append(b)
return possibles
def checkSquare(board, x, y):
squareX = (x // 3) * 3
squareY = (y // 3) * 3
subArray = board[squareX:squareX+3,squareY:squareY+3]
square = set(subArray.flatten())
possibles = []
for c in np.arange(1, 10):
if c not in square:
possibles.append(c)
return possibles
def checkIntegrity(board):
for i in np.arange(9):
row = board[i].tolist()
row = [i for i in row if i != 0]
column = board[:, i].tolist()
column = [i for i in column if i != 0]
if (len(set(column)) != len(column)):
return False
if (len(set(row)) != len(row)):
return False
for j in np.arange(9):
squareX = (i // 3) * 3
squareY = (j // 3) * 3
subArray = board[squareX:squareX+3,squareY:squareY+3]
square = subArray.flatten().tolist()
square = [i for i in square if i != 0]
if len(set(square)) != len(square):
return False
return True
def find_zero(board):
for i in np.arange(9):
for j in np.arange(9):
if board[i][j] == 0:
return i,j
return None
def backTrackSolve(board):
zeros = find_zero(board)
if not zeros:
print(board)
return True
else:
i,j = zeros
for num in np.arange(1,10):
board[i][j] = num
if checkIntegrity(board):
if backTrackSolve(board):
return True
board[i][j] = 0
return False
backTrackSolve(boardHard)