|
@@ -1,32 +1,76 @@
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
-from sudoku import Game, printBoard, setValue, getBoard
|
|
|
|
|
|
+from sudoku import Game
|
|
|
|
|
|
-MAX_V = 10
|
|
|
|
|
|
+MAX_V = 9
|
|
|
|
+class State:
|
|
|
|
+ """Represents a state of the environment"""
|
|
|
|
|
|
|
|
+ def __init__(self):
|
|
|
|
+ elements = [x for x in range(MAX_SIZE**2)]
|
|
|
|
+ shuffle(elements)
|
|
|
|
+ self.matrix = [[elements[x+(y*MAX_SIZE)] for x in range(MAX_SIZE)] for y in range(MAX_SIZE)]
|
|
|
|
+
|
|
|
|
+ def __iter__(self):
|
|
|
|
+ self.row = 0
|
|
|
|
+ self.col = 0
|
|
|
|
+ return self
|
|
|
|
+
|
|
|
|
+ def __next__(self):
|
|
|
|
+ if self.row == MAX_SIZE:
|
|
|
|
+ self.row = 0
|
|
|
|
+ self.col = self.col+1
|
|
|
|
+ if self.col == MAX_SIZE:
|
|
|
|
+ raise StopIteration
|
|
|
|
+ elem = self.matrix[self.col][self.row]
|
|
|
|
+ self.row = self.row + 1
|
|
|
|
+ return elem
|
|
|
|
+
|
|
|
|
+ def printState(self):
|
|
|
|
+ for i in range(MAX_SIZE):
|
|
|
|
+ print(self.matrix[i])
|
|
|
|
+ print("\n")
|
|
|
|
+
|
|
class Solver:
|
|
class Solver:
|
|
def __init__(self):
|
|
def __init__(self):
|
|
self.tree = {}
|
|
self.tree = {}
|
|
- choices = set()
|
|
|
|
- board = []
|
|
|
|
- # Populate the choice vector with values from 0 to MAX_V-1
|
|
|
|
- for x in range(MAX_V)
|
|
|
|
- choices.add(str(x))
|
|
|
|
- # Creates a bidimensional list with choices for every element
|
|
|
|
|
|
+ numbers = set()
|
|
|
|
+ choicesboard = []
|
|
|
|
+ # Populate the numbers vector with values from 0 to MAX_V-1
|
|
|
|
+ for x in range(MAX_V):
|
|
|
|
+ numbers.add(str(x))
|
|
|
|
+ # choicesboard is a bidimensional list with numbers for every element
|
|
for x in range(MAX_V):
|
|
for x in range(MAX_V):
|
|
row = []
|
|
row = []
|
|
for y in range(MAX_V):
|
|
for y in range(MAX_V):
|
|
- row.append(choices)
|
|
|
|
- board.append(row)
|
|
|
|
- self.board = board
|
|
|
|
|
|
+ row.append(numbers)
|
|
|
|
+ choicesboard.append(row)
|
|
|
|
+ self.choicesboard = choicesboard
|
|
result = getBoard()
|
|
result = getBoard()
|
|
|
|
|
|
def actions(state):
|
|
def actions(state):
|
|
|
|
+ actions = []
|
|
|
|
+
|
|
|
|
+ return actions
|
|
|
|
+
|
|
|
|
+ #def result(state,action):
|
|
|
|
|
|
- def result(state,action):
|
|
|
|
|
|
+ #def goalTest(state):
|
|
|
|
|
|
- def goalTest(state):
|
|
|
|
|
|
+ def eliminateIllValues(state):
|
|
|
|
+ for row in range(MAX_V):
|
|
|
|
+ for col in range(MAX_V):
|
|
|
|
+ if result[col][row] != 0:
|
|
|
|
+ # Eliminate duplicates numbers in a row
|
|
|
|
+ for x in range(MAX_V):
|
|
|
|
+ choicesboard[x][row].remove(result[col][row])
|
|
|
|
+ # Eliminate duplicates numbers in a column
|
|
|
|
+ for y in range(MAX_V):
|
|
|
|
+ choicesboard[col][y].remove(result[col][row])
|
|
|
|
+ # Eliminate duplicates in block (working only for 9x9 cells)
|
|
|
|
+ #for y in range(MAX_V)
|
|
|
|
+ # choicesboard[col][y].del(result[col][row])
|
|
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
- while not goalTest(state):
|
|
|
|
- for action in actions(node):
|
|
|
|
|
|
+#if __name__ == "__main__":
|
|
|
|
+# while not goalTest(state):
|
|
|
|
+# for action in actions(node):
|