예제 #1
0
파일: tests.py 프로젝트: MattFerraro/sudoku
    def test_fill_spaces(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
            solved = sudoku.loads(dat)

        unsolved = copy.copy(solved)
        unsolved[2][2] = 0  # mark a single space as unknown
        self.assertFalse(sudoku.validate(unsolved))
        self.assertEqual(sudoku.count_unknown(unsolved), 1)

        # Can we solve it?
        maybe_solved = sudoku.fill_spaces(unsolved)
        self.assertEqual(sudoku.count_unknown(maybe_solved), 0)
        self.assertEqual(maybe_solved, solved)
        self.assertTrue(sudoku.validate(maybe_solved))
예제 #2
0
    def test_no_candidates(self):
        """If a cell has no candidates, the puzzle is malformed"""
        grid = [[
            1, 2, 3,
            set((4, 5)),
            set((4, 5)),
            set((6, 7, 8)),
            set((6, 7, 8)),
            set((6, 7, 8)),
            set((9, ))
        ]]
        self.assertIsNone(sudoku.validate(grid))

        grid[0][-1] = set()
        with self.assertRaises(ValueError):
            sudoku.validate(grid)
예제 #3
0
파일: tests.py 프로젝트: MattFerraro/sudoku
    def test_validate(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
        # This test sudoku is a valid one!
        sudoku_arry = sudoku.loads(dat)
        self.assertTrue(sudoku.validate(sudoku_arry))

        # Violate several rules...
        sudoku_arry[0][0] = 6
        sudoku_arry[1][1] = 6
        self.assertFalse(sudoku.validate(sudoku_arry))

        # Try using an unsolved sudoku!
        with open("unsolved.su", "r") as f:
            dat = f.read().strip()
        # This sudoku is not even solved!
        sudoku_arry = sudoku.loads(dat)
        self.assertFalse(sudoku.validate(sudoku_arry))
예제 #4
0
    def test_ok(self):
        """Test a grid with conflicting cells"""
        grid = [[
            1, 2, 3,
            set((4, 5)),
            set((4, 5)),
            set((6, 7, 8)),
            set((6, 7, 8)),
            set((6, 7, 8)),
            set((8, 9))
        ]]
        self.assertIsNone(sudoku.validate(grid))

        grid[0][-1] = 9
        self.assertIsNone(sudoku.validate(grid))

        grid[0][-1] = 1
        with self.assertRaises(ValueError):
            sudoku.validate(grid)
예제 #5
0
    def place(self, val):
        row, col = self.selected
        if self.cubes[row][col].value == 0:
            self.cubes[row][col].set(val)
            self.update_model()

            if validate(self.model, val, (row, col)) and solve(self.model):
                return True
            else:
                self.cubes[row][col].set(0)
                self.cubes[row][col].set_temp(0)
                self.update_model()
                return False