예제 #1
0
    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 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")
예제 #5
0
    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")