def parsePuzzle(filename): start = time() puzzle = PuzzleFactory.createEmptySudoku() # Set the initial data to work with myFile = open(filename) values = range(1,10) stringArray = myFile.readlines() y = 0 for line in stringArray: x = 0 for el in line.split(' '): try: if(int(el) in values): puzzle.grid.setConstraintGridValue(x,y,int(el)) except ValueError: pass x += 1 y += 1 # And solve solver = Solver(puzzle) #print solver #grid.printAsSudoku() #print "Done in", int((time() - start) *1000), "ms" return puzzle, solver
def testNakedPair(self): sudoku = PuzzleFactory.createEmptySudoku() #initialize a grid that contains a naked pair sudoku.grid.setConstraintGridValue(3, 1, 7) sudoku.grid.setConstraintGridValue(4, 1, 4) sudoku.grid.setConstraintGridValue(6, 2, 7) sudoku.grid.setConstraintGridValue(7, 2, 4) sudoku.grid.setConstraintGridValue(0, 3, 7) sudoku.grid.setConstraintGridValue(0, 4, 4) # now the second and third cell must contain the possibleValues ([4,7]) as these are the only # two cells that can contain these values, they are also the only possibleValues c1_0 = sudoku.grid.getCellFromSquareGrid(1, 0) c2_0 = sudoku.grid.getCellFromSquareGrid(2, 0) solver = Solver(sudoku) solver.solve() print sudoku.grid self.assertEqual(c1_0.getPossibleValues(), set([4, 7]), "Did not correctly identify naked pair") self.assertEqual(c2_0.getPossibleValues(), set([4, 7]), "Did not correctly identify naked pair")
def testNakedPair(self): sudoku = PuzzleFactory.createEmptySudoku() #initialize a grid that contains a naked pair sudoku.grid.setConstraintGridValue(3,1,7) sudoku.grid.setConstraintGridValue(4,1,4) sudoku.grid.setConstraintGridValue(6,2,7) sudoku.grid.setConstraintGridValue(7,2,4) sudoku.grid.setConstraintGridValue(0,3,7) sudoku.grid.setConstraintGridValue(0,4,4) # now the second and third cell must contain the possibleValues ([4,7]) as these are the only # two cells that can contain these values, they are also the only possibleValues c1_0 = sudoku.grid.getCellFromSquareGrid(1,0) c2_0 = sudoku.grid.getCellFromSquareGrid(2,0) solver = Solver(sudoku) solver.solve() print sudoku.grid self.assertEqual(c1_0.getPossibleValues(), set([4,7]), "Did not correctly identify naked pair") self.assertEqual(c2_0.getPossibleValues(), set([4,7]), "Did not correctly identify naked pair")
def parsePuzzle(filename): start = time() puzzle = PuzzleFactory.createEmptySudoku() # Set the initial data to work with myFile = open(filename) values = range(1, 10) stringArray = myFile.readlines() y = 0 for line in stringArray: x = 0 for el in line.split(' '): try: if (int(el) in values): puzzle.grid.setConstraintGridValue(x, y, int(el)) except ValueError: pass x += 1 y += 1 # And solve solver = Solver(puzzle) #print solver #grid.printAsSudoku() #print "Done in", int((time() - start) *1000), "ms" return puzzle, solver
def testSimpleUVC(self): puzzle = PuzzleFactory.createSingleConstraintPuzzle(set([1,2]), 2, UniqueValueConstraint) cells = puzzle.grid.getCells() cells[0].setValue(1) self.assertEqual(cells[1].getPossibleValues(), set([2]), "Incorrect exclusion of value")
def parsePuzzle(self, filename): start = time() puzzle = PuzzleFactory.createFutoshikiPuzzle(7) # Set the initial data to work with myFile = open("./futoshiki.txt") values = range(1,10) stringArray = myFile.readlines() y = 0 addingValues = True for line in stringArray: if line.startswith("relativity"): addingValues = False if addingValues: x = 0 for el in line.split(' '): try: if(int(el) in values): puzzle.grid.setConstraintGridValue(x,y,int(el)) except ValueError: pass x += 1 y += 1 else: cellStrings = line.split(">") if len(cellStrings) == 2: c1, c2 = cellStrings x,y = c1.split(",") x = int(x) y = int(y) cell1 = puzzle.grid.getCellFromSquareGrid(x-1,y-1) x,y = c2.split(",") x = int(x) y = int(y) cell2 = puzzle.grid.getCellFromSquareGrid(x-1,y-1) cg = puzzle.addConstraintGroup("Relativity") constraint = cg.addConstraint(CellGreaterThanCellConstraint) cg.addCell(cell1) cg.addCell(cell2) constraint.setLesserCell(cell1) constraint.setGreaterCell(cell2) # And solve solver = Solver(puzzle) #print solver #grid.printAsSudoku() #print "Done in", int((time() - start) *1000), "ms" return puzzle, solver
def testUVCWithSudoku(self): sudoku = PuzzleFactory.createEmptySudoku() for i in range(1,10): sudoku.grid.setConstraintGridValue(i,0,i) possibleValues = sudoku.grid.getCellFromSquareGrid(9, 0).getPossibleValues() self.assertEqual(possibleValues, set([9]), "Exact cover failed to identify single value")
def testSimpleTSVC(self): """Test the basic properties of the total sum value constraint""" puzzle = PuzzleFactory.createSingleConstraintPuzzle(set([1,2]), 2, TotalSumValueConstraint) for cg in puzzle.getConstraintGroups(): for constraint in cg.getConstraints(): constraint.setTotalValue(3); constraint.applyConstraint() possibleValues = puzzle.grid.getCells()[1].getPossibleValues() self.assertTrue(1 in possibleValues, "Incorrectly removed value from constraint") self.assertTrue(2 in possibleValues, "Incorrectly removed value from constraint") puzzle.grid.getCells()[0].setValue(1) for cg in puzzle.getConstraintGroups(): for constraint in cg.getConstraints(): constraint.applyConstraint() possibleValues = puzzle.grid.getCells()[1].getPossibleValues() self.assertTrue(1 not in possibleValues, "Incorrectly kept value from constraint") self.assertTrue(2 in possibleValues, "Incorrectly removed value from constraint")
def testTSVCWithKakuro(self): kakuro = PuzzleFactory.createExampleKakuro()