csp_solver.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. from sudoku import Game
  3. MAX_V = 9
  4. class State:
  5. """Represents a state of the environment"""
  6. def __init__(self):
  7. elements = [x for x in range(MAX_SIZE**2)]
  8. shuffle(elements)
  9. self.matrix = [[elements[x+(y*MAX_SIZE)] for x in range(MAX_SIZE)] for y in range(MAX_SIZE)]
  10. def __iter__(self):
  11. self.row = 0
  12. self.col = 0
  13. return self
  14. def __next__(self):
  15. if self.row == MAX_SIZE:
  16. self.row = 0
  17. self.col = self.col+1
  18. if self.col == MAX_SIZE:
  19. raise StopIteration
  20. elem = self.matrix[self.col][self.row]
  21. self.row = self.row + 1
  22. return elem
  23. def printState(self):
  24. for i in range(MAX_SIZE):
  25. print(self.matrix[i])
  26. print("\n")
  27. class Solver:
  28. def __init__(self):
  29. self.tree = {}
  30. numbers = set()
  31. choicesboard = []
  32. # Populate the numbers vector with values from 0 to MAX_V-1
  33. for x in range(MAX_V):
  34. numbers.add(str(x))
  35. # choicesboard is a bidimensional list with numbers for every element
  36. for x in range(MAX_V):
  37. row = []
  38. for y in range(MAX_V):
  39. row.append(numbers)
  40. choicesboard.append(row)
  41. self.choicesboard = choicesboard
  42. result = getBoard()
  43. def actions(state):
  44. actions = []
  45. return actions
  46. #def result(state,action):
  47. #def goalTest(state):
  48. def eliminateIllValues(state):
  49. for row in range(MAX_V):
  50. for col in range(MAX_V):
  51. if result[col][row] != 0:
  52. # Eliminate duplicates numbers in a row
  53. for x in range(MAX_V):
  54. choicesboard[x][row].remove(result[col][row])
  55. # Eliminate duplicates numbers in a column
  56. for y in range(MAX_V):
  57. choicesboard[col][y].remove(result[col][row])
  58. # Eliminate duplicates in block (working only for 9x9 cells)
  59. #for y in range(MAX_V)
  60. # choicesboard[col][y].del(result[col][row])
  61. #if __name__ == "__main__":
  62. # while not goalTest(state):
  63. # for action in actions(node):