|
@@ -1,6 +1,7 @@
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
from sudoku import Game
|
|
|
+from copy import deepcopy
|
|
|
|
|
|
MAX_V = 9
|
|
|
class State:
|
|
@@ -15,17 +16,16 @@ class State:
|
|
|
self.resultboard = resultboard
|
|
|
|
|
|
choicesboard = []
|
|
|
- # Populate the numbers vector with values from 1 to MAX_V
|
|
|
-
|
|
|
- # choicesboard is a bidimensional list with numbers for every element
|
|
|
+ numbers = set()
|
|
|
+ for value in range(1,10):
|
|
|
+ numbers.add(value)
|
|
|
for x in range(MAX_V):
|
|
|
row = []
|
|
|
for y in range(MAX_V):
|
|
|
- numbers = set()
|
|
|
- for value in range(1,10):
|
|
|
- numbers.add(value)
|
|
|
- row.append(numbers)
|
|
|
- choicesboard.append(row)
|
|
|
+ choices = deepcopy(numbers)
|
|
|
+ row.append(choices)
|
|
|
+ newrow=deepcopy(row)
|
|
|
+ choicesboard.append(newrow)
|
|
|
self.choicesboard = choicesboard
|
|
|
|
|
|
def actions(state):
|
|
@@ -41,7 +41,9 @@ def actions(state):
|
|
|
return actions
|
|
|
|
|
|
def result(state,move):
|
|
|
- state.resultboard[move['col']][move['row']] = move['num']
|
|
|
+ newState = deepcopy(state)
|
|
|
+ newState.resultboard[move['col']][move['row']] = move['num']
|
|
|
+ return newState
|
|
|
|
|
|
def goalTest(state):
|
|
|
incomplete = False
|
|
@@ -52,24 +54,26 @@ def goalTest(state):
|
|
|
return incomplete
|
|
|
|
|
|
def eliminateIllValues(state):
|
|
|
+ newState = deepcopy(state)
|
|
|
for row in range(MAX_V):
|
|
|
for col in range(MAX_V):
|
|
|
- if state.resultboard[col][row] != 0:
|
|
|
- num = state.resultboard[col][row]
|
|
|
+ if newState.resultboard[col][row] != 0:
|
|
|
+ num = newState.resultboard[col][row]
|
|
|
# Eliminate duplicates numbers in a row
|
|
|
for x in range(MAX_V):
|
|
|
- state.choicesboard[x][row].remove(num)
|
|
|
+ newState.choicesboard[x][row].remove(num)
|
|
|
# Eliminate duplicates numbers in a column
|
|
|
for y in range(MAX_V):
|
|
|
- state.choicesboard[col][y].remove(num)
|
|
|
+ newState.choicesboard[col][y].remove(num)
|
|
|
# Eliminate duplicates in block (working only for 9x9 cells)
|
|
|
#for y in range(MAX_V)
|
|
|
# choicesboard[col][y].del(state.resultboard[col][row])
|
|
|
+ return newState
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
node = State()
|
|
|
while not goalTest(node):
|
|
|
- move=actions(node).pop()
|
|
|
- result(node,move)
|
|
|
- eliminateIllValues(node)
|
|
|
+ move = actions(node).pop()
|
|
|
+ node = result(node,move)
|
|
|
+ node = eliminateIllValues(node)
|
|
|
print('Result found!')
|