def execute(self): """ Solves a the Sudoku puzzle loaded in 'game.initial_sudoku' and loads the results in the 'game.solved_sudoku' SudokuBoard object. If the solving algorithm is None, the puzzle is solved using the default algorithm specified in the Settings object. """ if self.game == None: raise InvalidCmdParametersException("The command needs a game.") if self.game.initial_sudoku == None: raise InvalidCmdParametersException("The Sudoku was not loaded.") if not (isinstance(self.readconfig_parameters[self.ALGORITHM_PARAM], str)): raise InvalidCmdParametersException("The command needs an algorithm.") algorithm_factory = AlgorithmFactory(self.readconfig_parameters[self.ALGORITHM_PARAM]) if (algorithm_factory.getAlgorithm()) is None: algorithm_factory.settings = self.game.settings_manager.settings.getAlgorithmName() solving_algorithm = algorithm_factory.getAlgorithm() solution = solving_algorithm.solve(self.game.initial_sudoku.to_dictionary()) self.game.solved_sudoku = SudokuBoard() self.game.solved_sudoku.from_dictionary(solution, True) self.game.user_sudoku = SudokuBoard() self.game.user_sudoku.from_dictionary(solution, True) if self.game.is_started(): self.game.stop_game_timer()
def execute(self): """ execute the command taking account the parameters of the command """ if self.game == None: raise InvalidCmdParametersException("The command needs a game.") elif self.game.user_sudoku == None: raise InvalidCmdParametersException("The Sudoku was not loaded.") if not self.game.is_started(): raise InvalidCmdParametersException("It is not possible to hint if the game is not started.") if self.game.solved_sudoku is None: algorithm_factory = AlgorithmFactory(self.game.settings_manager.settings.getAlgorithmName()) solving_algorithm = algorithm_factory.getAlgorithm() solution = solving_algorithm.solve(self.game.initial_sudoku.to_dictionary()) self.game.solved_sudoku = SudokuBoard() self.game.solved_sudoku.from_dictionary(solution, True) if self.game.user_sudoku.is_editable( self.readconfig_parameters[self.ROW_PARAM], int(self.readconfig_parameters[self.COLUMN_PARAM]) ): value = self.game.solved_sudoku.get_value( self.readconfig_parameters[self.ROW_PARAM], int(self.readconfig_parameters[self.COLUMN_PARAM]) ) self.game.user_sudoku.set_value( self.readconfig_parameters[self.ROW_PARAM], int(self.readconfig_parameters[self.COLUMN_PARAM]), value )
class TestAlgFactory(unittest.TestCase): def setUp(self): # create a setting with algorithm type PETER self.factoryPeter = AlgorithmFactory(AlgorithmType.PETER_NORVIG) self.factoryBacktracking = AlgorithmFactory(AlgorithmType.BACK_TRACKING) self.factoryRecursive = AlgorithmFactory(AlgorithmType.RECURSIVE) def test_having_setttings_for_peter_the_factory_should_return_peter_algorithm(self): alg = self.factoryPeter.getAlgorithm() self.assertEqual(PeterNorvigAlgorithm, type(alg)) def test_having_setttings_for_peter_the_factory_should_return_backtracking_algorithm(self): alg = self.factoryBacktracking.getAlgorithm() self.assertEqual(BackTrackingAdapter, type(alg)) def test_having_setttings_for_peter_the_factory_should_return_recursive_algorithm(self): alg = self.factoryRecursive.getAlgorithm() self.assertEqual(Recursive, type(alg))