コード例 #1
0
def test_sudoku_fail_fast_doctest() -> None:
    """Test SudokuPuzzle.fail_fast on the provided doctest."""
    s = SudokuPuzzle(4, [["A", "B", "C", "D"], ["C", "D", " ", " "],
                         [" ", " ", " ", " "], [" ", " ", " ", " "]],
                     {"A", "B", "C", "D"})

    assert s.fail_fast() is False

    s = SudokuPuzzle(4, [["B", "D", "A", "C"], ["C", "A", "B", "D"],
                         ["A", "B", " ", " "], [" ", " ", " ", " "]],
                     {"A", "B", "C", "D"})
    assert s.fail_fast() is True
コード例 #2
0
def test_dfs_solver_example() -> None:
    """Test DfsSolver.solve on a SudokuPuzzle."""
    # This SudokuPuzzle is a more filled-in version of the one in the
    # example from the handout.
    s = SudokuPuzzle(4, [["C", "D", "B", "A"], ["B", "A", "D", "C"],
                         ["D", " ", "A", " "], ["A", " ", "C", " "]],
                     {"A", "B", "C", "D"})

    solver = DfsSolver()
    actual = solver.solve(s)[-1]

    expected = SudokuPuzzle(4, [["C", "D", "B", "A"], ["B", "A", "D", "C"],
                                ["D", "C", "A", "B"], ["A", "B", "C", "D"]],
                            {"A", "B", "C", "D"})

    assert actual == expected
コード例 #3
0
def test_has_unique_solution_doctest() -> None:
    """Test has_unique_solution on a SudokuPuzzle with a non-unique solution."""
    s = SudokuPuzzle(4, [["D", "C", "B", "A"], ["B", "A", "D", "C"],
                         ["C", " ", "A", " "], ["A", " ", "C", " "]],
                     {"A", "B", "C", "D"})

    assert s.has_unique_solution() is False
コード例 #4
0
    def test_solve_complete(self):
        s = SudokuPuzzle([['A', 'B', '', ''], ['C', 'D', '', ''],
                          ['B', '', '', ''], ['D', '', 'A', '']])

        solutions = solve_complete(s)
        self.assertEqual(len(solutions), 2)
        for solution in solutions:
            self.assertTrue(solution.is_solved())
コード例 #5
0
 def test_solve_one(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', 'A', 'B'],
                       ['B', 'A', '', ''], ['D', 'C', '', '']])
     solved = solve(s)
     # Note: when we run our tests, we will replace
     # this with our own "is_solved" method
     # to make sure the puzzle is correctly solved.
     # This means you should *not* change the internal state
     # of the Sudoku Puzzle class!
     self.assertTrue(solved.is_solved())
コード例 #6
0
def main():
    """Prompt the user to configure and play the game.

    @rtype: None
    """
    game = input("Do you want to play Sudoku (s) or Word Ladder (w)? ")
    while game != "s" and game != "w":
        print("That is not a valid input.")
        game = input(
            "Do you want to play Sudoku (s) or Word Ladder (w)? ").lower()

    if game == "s":
        view_type = "text"
        g = SudokuPuzzle([['', '', '', 'A'], ['D', '', 'B', ''],
                          ['C', '', '', ''], ['', 'B', '', 'D']])
        print(
            "\n\nTo make a move: use the format (<row>, <column>) -> letter.")
    elif game == "w":
        view_type = input(
            "Would you like to play in text mode or web mode? ").lower()
        if view_type != "web" and view_type != "text":
            print(
                "That is not a valid mode, you will be playing in text mode.")
            view_type = "text"
        choice = input(
            "Do you want to choose your start and end words? (y/n) ")
        if choice == "y":
            print(
                "Your starting and ending words should have the same length.")
            start = input("What would you like your starting word to be? ")
            end = input("What would you like your ending word to be? ")
            while len(start) != len(end):
                print(
                    "Your starting and ending words don't have the same length. Please try again."
                )
                start = input("What would you like your starting word to be? ")
                end = input("What would you like your ending word to be? ")
        else:
            start = "rock"
            end = "taco"
        g = WordLadderPuzzle(start, end)
        print(
            "\n\nTo make a move, type a word which does not differ by more than one letter from the current word."
        )

    if view_type == "text":
        print("To quit the game, type exit.")
        print("To ask for a solution, type :SOLVE.")
        print("To ask for a hint, type :HINT.")
        print("To undo a move, type :UNDO.")
        print(
            "To look at your past moves from this current game state, type :ATTEMPTS.\n"
        )

    c = Controller(g, mode=view_type)
コード例 #7
0
    def test_nine_extensions(self):
        big = SudokuPuzzle([['E', 'C', '', '', 'G', '', '', '', ''],
                            ['F', '', '', 'A', 'I', 'E', '', '', ''],
                            ['', 'I', 'H', '', '', '', '', 'F', ''],
                            ['H', '', '', '', 'F', '', '', '', 'C'],
                            ['D', '', '', 'H', '', 'C', '', '', 'A'],
                            ['G', '', '', '', 'B', '', '', '', 'F'],
                            ['', 'F', '', '', '', '', 'B', 'H', ''],
                            ['', '', '', 'D', 'A', 'I', '', '', 'E'],
                            ['', '', '', '', 'H', '', '', 'G', 'I']])

        self.assertEqual(big._possible_letters(4, 4), ['E'])
        self.assertEqual(big._possible_letters(3, 3), ['E', 'G', 'I'])
コード例 #8
0
ファイル: solver.py プロジェクト: stroudgr/UofT
                return [(puzzle.generate_hint(extension), True)]

            extension_hints = possible_hints(extension, n - 1)

            for hint in extension_hints:
                if hint[1]:  # if this hint will lead to a solution
                    return [(puzzle.generate_hint(extension), True)]

            # Else all the hints in extension do not lead to a solution
            # Only adds if lst is empty, since only one hint is necessary
            if len(lst) == 0 and len(extension_hints) > 0:
                lst.append((puzzle.generate_hint(extensions[0]), False))

    return lst


if __name__ == '__main__':
    from sudoku_puzzle import SudokuPuzzle
    from word_ladder_puzzle import WordLadderPuzzle
    s = SudokuPuzzle([['A', 'D', 'C', ''], ['B', 'C', '', ''],
                      ['C', 'B', 'A', 'D'], ['D', 'A', 'B', 'C']])
    print(s.extensions()[0])
    print('The puzzle:')
    print(s)
    print('SOLVE')
    solve(s, True)
    print('\nSOLVE-ALL')
    solve_complete(s, True)
    print('\nHINT 1')
    print(hint_by_depth(s, 1))
コード例 #9
0
    hard_sudoku.append(
        '000570030100000020700023400000080004007004000490000605042000300000700900001800000'
    )
    hard_sudoku.append(
        '700152300000000920000300000100004708000000060000000000009000506040907000800006010'
    )
    hard_sudoku.append(
        '100007090030020008009600500005300900010080002600004000300000010040000007007000300'
    )
    hard_sudoku.append(
        '100034080000800500004060021018000000300102006000000810520070900006009000090640002'
    )
    hard_sudoku.append(
        '000920000006803000190070006230040100001000700008030029700080091000507200000064000'
    )
    hard_sudoku.append(
        '060504030100090008000000000900050006040602070700040005000000000400080001050203040'
    )
    hard_sudoku.append(
        '700000400020070080003008079900500300060020090001097006000300900030040060009001035'
    )
    hard_sudoku.append(
        '000070020800000006010205000905400008000000000300008501000302080400000009070060000'
    )

    for sudoku in easy_sudoku:
        SudokuSolver(SudokuPuzzle(sudoku)).solve()

    for sudoku in hard_sudoku:
        SudokuSolver(SudokuPuzzle(sudoku)).solve()
コード例 #10
0
 def test_bad_format(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', 'A', 'B'],
                       ['B', 'A', '', ''], ['D', 'C', '', '']])
     with self.assertRaises(ValueError):
         s.move('2 2 C')
コード例 #11
0
 def test_invalid_move(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', 'A', 'B'],
                       ['B', 'A', '', ''], ['D', 'C', '', '']])
     with self.assertRaises(ValueError):
         s.move('(2, 2) -> C')
コード例 #12
0
 def test_sample3(self):
     s = SudokuPuzzle([['A', 'B', '', 'D'], ['C', 'D', '', 'B'],
                       ['B', '', '', ''], ['D', '', 'A', '']])
     self.assertEqual(s._possible_letters(2, 3), ['C'])
コード例 #13
0
 def test_hint_valid_state(self):
     s = SudokuPuzzle([['', 'B', 'C', 'D'], ['C', 'D', '', 'B'],
                       ['B', '', 'D', 'C'], ['D', 'C', 'B', '']])
     self.assertTrue(
         hint_by_depth(s, 3) in
         ['(0, 0) -> A', '(1, 2) -> A', '(2, 1) -> A', '(3, 3) -> A'])
コード例 #14
0
 def test_hint_can_reach_solution(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', '', 'B'],
                       ['B', 'A', 'D', 'C'], ['D', 'C', 'B', 'A']])
     for p in s.extensions():
         print(p)
     self.assertEqual(hint_by_depth(s, 10), '(1, 2) -> A')
コード例 #15
0
 def test_hint_no_possible_extensions(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', '', 'B'],
                       ['B', 'A', 'D', 'C'], ['C', '', 'A', 'A']])
     self.assertEqual(hint_by_depth(s, 10), 'No possible extensions!')
コード例 #16
0
 def test_hint_already_solved(self):
     s = SudokuPuzzle([['A', 'B', 'C', 'D'], ['C', 'D', 'A', 'B'],
                       ['B', 'A', 'D', 'C'], ['D', 'C', 'B', 'A']])
     self.assertEqual(hint_by_depth(s, 10), 'Already at a solution!')