Пример #1
0
    def test_isValidBoard(self):

        self.assertTrue(Solve.is_valid_board(self.validBoard1))
        self.assertTrue(Solve.is_valid_board(self.validBoard2))
        self.assertTrue(Solve.is_valid_board(self.validBoard3))
        self.assertTrue(Solve.is_valid_board(self.solvedBoard1))
        self.assertTrue(Solve.is_valid_board(self.solvedBoard2))
        self.assertFalse(Solve.is_valid_board(self.invalidBoard1))
        self.assertFalse(Solve.is_valid_board(self.invalidBoard2))
        self.assertFalse(Solve.is_valid_board(self.invalidBoard3))
        self.assertFalse(Solve.is_valid_board(self.invalidBoard4))
        self.assertFalse(Solve.is_valid_board(self.invalidBoard5))
Пример #2
0
def brute_force(raw_board):
    # Return true if solved.  This will bring the correct board up through the chain of calls.
    if Solve.is_solved_board(raw_board):
        return raw_board, True
    # If board is not valid, move up the call chain once.
    if not Solve.is_valid_board(raw_board):
        return raw_board, False

    # Find the first blank space from the top left
    x, y = Solve.find_blank(raw_board)

    # Create a copy of board to be possed down the chain
    new_board = [[raw_board[i][j] for j in range(0, 9)] for i in range(0, 9)]

    for value in range(1, 10):

        # Place a value in the empty spot and go to the next spot.
        new_board[x][y] = value
        result_board, result = brute_force(new_board)

        # If the call below this found a solved board, bring the solved board up to the next call
        if result is True:
            return result_board, result
            # If the call below found an invalid board, replace the current spot with the next value

    # If all values were found to be invalid, go up a call and check the next value for the previous blank spot
    return raw_board, False