def test_if_execute_printcommand_doesnt_raise_exception_when_a_good_formater_dictionary_is_given(self): dict = {} puzzle = { "A1":'.', "A2":'6', "A3":'7', "A4":'.', "A5":'4', "A6":'.', "A7":'.', "A8":'.', "A9":'2', "B1":'9', "B2":'.', "B3":'.', "B4":'7', "B5":'3', "B6":'.', "B7":'4', "B8":'5', "B9":'.', "C1":'4', "C2":'.', "C3":'5', "C4":'.', "C5":'.', "C6":'.', "C7":'.', "C8":'.', "C9":'.', "D1":'6', "D2":'.', "D3":'.', "D4":'.', "D5":'8', "D6":'.', "D7":'.', "D8":'.', "D9":'.', "E1":'.', "E2":'.', "E3":'.', "E4":'9', "E5":'.', "E6":'.', "E7":'3', "E8":'2', "E9":'.', "F1":'2', "F2":'8', "F3":'3', "F4":'5', "F5":'.', "F6":'.', "F7":'6', "F8":'4', "F9":'9', "G1":'.', "G2":'7', "G3":'.', "G4":'.', "G5":'.', "G6":'.', "G7":'.', "G8":'.', "G9":'3', "H1":'.', "H2":'.', "H3":'.', "H4":'6', "H5":'5', "H6":'7', "H7":'8', "H8":'.', "H9":'4', "I1":'.', "I2":'.', "I3":'6', "I4":'4', "I5":'2', "I6":'3', "I7":'5', "I8":'9', "I9":'7' } try: game=Game() game.user_sudoku= SudokuBoard() game.user_sudoku.from_dictionary(puzzle,True) print_command = PrintBoardCommand(dict) print_command.game=game #print_command.game.user_sudoku.from_dictionary(puzzle) result = print_command.execute() self.assertTrue(True) except Exception as e: self.fail("Exception has been raised",e)
def test_an_InvalidCmdParametersException_should_be_raised_if_parameter_has_int_value(self): with self.assertRaises(InvalidCmdParametersException): game = Game() game.initial_sudoku = SudokuBoard() cmd = SolveGameCommand(self.int_param) cmd.set_game(game) cmd.execute()
def test_game_attributes_should_be_copied_when_a_game_is_copied(self): puzzle = { "A1":'.', "A2":'6', "A3":'7', "A4":'.', "A5":'4', "A6":'.', "A7":'.', "A8":'.', "A9":'2', "B1":'9', "B2":'.', "B3":'.', "B4":'7', "B5":'3', "B6":'.', "B7":'4', "B8":'5', "B9":'.', "C1":'4', "C2":'.', "C3":'5', "C4":'.', "C5":'.', "C6":'.', "C7":'.', "C8":'.', "C9":'.', "D1":'6', "D2":'.', "D3":'.', "D4":'.', "D5":'8', "D6":'.', "D7":'.', "D8":'.', "D9":'.', "E1":'.', "E2":'.', "E3":'.', "E4":'9', "E5":'.', "E6":'.', "E7":'3', "E8":'2', "E9":'.', "F1":'2', "F2":'8', "F3":'3', "F4":'5', "F5":'.', "F6":'.', "F7":'6', "F8":'4', "F9":'9', "G1":'.', "G2":'7', "G3":'.', "G4":'.', "G5":'.', "G6":'.', "G7":'.', "G8":'.', "G9":'3', "H1":'.', "H2":'.', "H3":'.', "H4":'6', "H5":'5', "H6":'7', "H7":'8', "H8":'.', "H9":'4', "I1":'.', "I2":'.', "I3":'6', "I4":'4', "I5":'2', "I6":'3', "I7":'5', "I8":'9', "I9":'7' } game = Game() game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(puzzle) game.user_sudoku = SudokuBoard() game.user_sudoku.from_dictionary(puzzle) game.solved_sudoku = SudokuBoard() game.solved_sudoku.from_dictionary(puzzle) game.currentTime = 1.0 game.started = True game.paused = True other_game = Game() other_game.copy(game) self.assertEqual(game.initial_sudoku.to_dictionary(), other_game.initial_sudoku.to_dictionary()) self.assertEqual(game.user_sudoku.to_dictionary(), other_game.user_sudoku.to_dictionary()) self.assertEqual(game.solved_sudoku.to_dictionary(), other_game.solved_sudoku.to_dictionary()) self.assertEqual(game.currentTime, other_game.currentTime) self.assertEqual(game.started, other_game.started) self.assertEqual(game.paused, other_game.paused)
def test_a_game_file_should_be_saved(self): game = Game() game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) game_writer = SudokuGameWriter() game_writer.write(game, 'test_game.sgf') game_file = open('test_game.sgf', 'rb') read_game = pickle.load(game_file) self.assertEqual(game.initial_sudoku.to_dictionary(), read_game.initial_sudoku.to_dictionary())
def test_given_a_valid_parameters_then_should_set_the_value_in_the_soduko_board(self): game = Game() game.started = True game.user_sudoku = SudokuBoard(9) cmd = SetValueCommand(self.valid_parameters) cmd.set_game(game) cmd.execute() self.assertEqual(self.valid_value, game.user_sudoku.dic[self.cell].value)
def test_if_level_is_moderate_then_a_moderate_level_sudoku_puzzle_should_be_generated(self): settings_manager = SettingsManager("mySettings.xml") settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.start_game_timer() cmd = GenerateGameCommand(None) cmd.set_game(game) cmd.execute() self.assertIsNotNone(game.initial_sudoku)
def test_if_level_name_is_easy_then_an_easy_level_sudoku_puzzle_should_be_generated(self): valid_params = {"level": "EASY"} settings_manager = SettingsManager("mySettings.xml") settings_manager.load() game = Game() game.set_settings_manager(settings_manager) cmd = GenerateGameCommand(valid_params) cmd.set_game(game) cmd.execute() self.assertIsNotNone(game.initial_sudoku)
def test_a_game_file_should_be_read(self): game = Game() game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) with open('test_game_file.sgf', 'wb') as output_file: pickle.dump(game, output_file, pickle.HIGHEST_PROTOCOL) game_reader = SudokuGameReader() read_game_file = game_reader.read_game('test_game_file.sgf') self.assertEqual(game.initial_sudoku.to_dictionary(), read_game_file.initial_sudoku.to_dictionary())
def test_a_game_should_be_imported(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.start_game_timer() import_command = ImportCommand(self.valid_params) import_command.set_game(game) import_command.execute() self.assertEqual((game.initial_sudoku.to_dictionary())['A1'], '.')
def test_given_a_valid_parameters_and_none_user_sudoku_then_should_raise_an_exception(self): game = Game() game.started = True cmd = PrintBoardCommand(None) cmd.set_game(game) try: cmd.execute() self.fail("Expected InvalidCmdParametersException was not raised.") except InvalidCmdParametersException: pass
def test_when_valid_parameters_and_valid_game_and_a_valid_user_sudoku_then_should_the_game_be_started(self): user_sudoku = SudokuBoard(3) game = Game() game.started = False game.user_sudoku = user_sudoku cmd = StartCommand(None) cmd.set_game(game) cmd.execute() self.assertTrue(game.started)
def test_given_a_valid_parameters_and_none_initial_sudoku_then_should_raise_an_exception(self): game = Game() game.user_sudoku = SudokuBoard(3) cmd = RestartGameCommand(None) cmd.set_game(game) try: cmd.execute() self.fail("Expected InvalidCmdParametersException was not raised.") except InvalidCmdParametersException: pass
def test_a_game_should_be_exported(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) export_command = ExportCommand(self.valid_params) export_command.set_game(game) export_command.execute() exported_file = open('puzzle_file.txt') self.assertEqual(self.exported_content, exported_file.readline())
def test_when_valid_parameters_and_valid_game_and_the_game_is_not_started_then_an_exception_should_be_trown_saying_the_game_is_not_starting(self): cmd = HintCommand(self.hint_parameters) game = Game() game.user_sudoku = SudokuBoard(3) cmd.set_game(game) try: cmd.execute() self.fail("Expected InvalidCmdParametersException was not raised.") except InvalidCmdParametersException: pass
def test_when_valid_parameters_and_valid_game_and_the_game_is_not_started_then_an_exception_should_be_trown_saying_the_game_is_not_starting(self): dict = {} game = Game() game.started = False cmd = StopCommand(dict) cmd.set_game(game) try: cmd.execute() self.fail("Expected InvalidCmdParametersException was not raised.") except InvalidCmdParametersException: pass
def test_sudoku_puzzle_should_be_solved_when_no_solving_algorithm_is_specified(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) cmd = SolveGameCommand() cmd.set_game(game) cmd.execute() solving_algorithm = Recursive() expected_results = solving_algorithm.solve(self.puzzle) self.assertEqual(expected_results, game.solved_sudoku.to_dictionary())
def test_given_a_valid_parameters_and_none_user_sudoku_then_should_raise_an_exception(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.started = True cmd = HintCommand(self.hint_parameters) cmd.set_game(game) try: cmd.execute() self.fail("Expected InvalidCmdParametersException was not raised.") except InvalidCmdParametersException: pass
def test_when_valid_parameters_and_valid_game_and_the_game_is_started_then_should_the_game_be_stopped(self): user_sudoku = SudokuBoard(3) game = Game() game.started = True game.startTime = time.clock() game.currentTime = 0 game.user_sudoku = user_sudoku cmd = StopCommand(None) cmd.set_game(game) timee = cmd.execute() self.assertFalse(game.started) self.assertTrue(timee != 0.0)
def test_when_restart_game_is_executed_then_the_user_table_should_be_restarted_properly(self): sudoku = SudokuBoard(3) user_sudoku = SudokuBoard(3) sudoku.from_dictionary(self.solved_sudoku, True) user_sudoku.from_dictionary(self.user_sudoku, True) game = Game() game.initial_sudoku = sudoku game.user_sudoku = user_sudoku cmd = RestartGameCommand(None) cmd.set_game(game) cmd.execute() self.assertDictEqual(game.initial_sudoku.to_dictionary(), game.user_sudoku.to_dictionary())
def __init__(self): ''' Constructor ''' self.settings_manager = SettingsManager('mySettings.xml') self.settings_manager.load() self.game = Game() self.game.set_settings_manager(self.settings_manager) self.factory = CommandFactory(self.game)
def test_given_a_valid_parameters_then_should_set_the_hint_value_in_the_soduko_board(self): initial_sudoku = SudokuBoard(3) user_sudoku = SudokuBoard(3) solved_sudoku = SudokuBoard(3) initial_sudoku.from_dictionary(self.solved_sudoku) user_sudoku.from_dictionary(self.user_sudoku) solved_sudoku.from_dictionary(self.solved_sudoku) game = Game() game.started = True game.initial_sudoku = initial_sudoku game.user_sudoku = user_sudoku game.solved_sudoku = solved_sudoku cmd = HintCommand(self.hint_parameters) cmd.set_game(game) cmd.execute() self.assertEqual(game.solved_sudoku.dic[self.cell].value, game.user_sudoku.dic[self.cell].value)
def test_solution_is_load_in_solved_sudoku_when_solved_sudoku_is_None(self): puzzle = { "A1":'.', "A2":'6', "A3":'7', "A4":'.', "A5":'4', "A6":'.', "A7":'.', "A8":'.', "A9":'2', "B1":'9', "B2":'.', "B3":'.', "B4":'7', "B5":'3', "B6":'.', "B7":'4', "B8":'5', "B9":'.', "C1":'4', "C2":'.', "C3":'5', "C4":'.', "C5":'.', "C6":'.', "C7":'.', "C8":'.', "C9":'.', "D1":'6', "D2":'.', "D3":'.', "D4":'.', "D5":'8', "D6":'.', "D7":'.', "D8":'.', "D9":'.', "E1":'.', "E2":'.', "E3":'.', "E4":'9', "E5":'.', "E6":'.', "E7":'3', "E8":'2', "E9":'.', "F1":'2', "F2":'8', "F3":'3', "F4":'5', "F5":'.', "F6":'.', "F7":'6', "F8":'4', "F9":'9', "G1":'.', "G2":'7', "G3":'.', "G4":'.', "G5":'.', "G6":'.', "G7":'.', "G8":'.', "G9":'3', "H1":'.', "H2":'.', "H3":'.', "H4":'6', "H5":'5', "H6":'7', "H7":'8', "H8":'.', "H9":'4', "I1":'.', "I2":'.', "I3":'6', "I4":'4', "I5":'2', "I6":'3', "I7":'5', "I8":'9', "I9":'7' } settings_manager = SettingsManager('settings.xml') settings_manager.load() game = Game() game.started = True game.set_settings_manager(settings_manager) game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(puzzle, True) game.user_sudoku = SudokuBoard() game.user_sudoku.from_dictionary(puzzle, True) cmd = HintCommand(self.hint_parameters) cmd.set_game(game) cmd.execute() self.assertEqual(game.solved_sudoku.dic[self.cell].value, game.user_sudoku.dic[self.cell].value)
def test_a_game_should_be_saved(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) game.start_game_timer() save_game_command = SaveGameCommand(self.valid_params) save_game_command.set_game(game) save_game_command.execute() game_file = open('game.sgf', 'rb') read_game = pickle.load(game_file) self.assertEqual(game.initial_sudoku.to_dictionary(), read_game.initial_sudoku.to_dictionary())
class Console(object): ''' Console class is defining console to receive an input from user ''' def __init__(self): ''' Constructor ''' self.settings_manager = SettingsManager('mySettings.xml') self.settings_manager.load() self.game = Game() self.game.set_settings_manager(self.settings_manager) self.factory = CommandFactory(self.game) def read_command(self): ''' Read_command function reads the user command input ''' cmd = input("PY-SUDOKU #") return cmd def parse_parameter(self, paramStr, params): ''' Parse_parameter function verifies if a parameter and its value is correct. If the parameter is correct then it is saved in params dictionary with the paramName as key and paramValue as value. ''' #verify is correct if paramStr[0] != '/': raise InvalidCmdParametersException("The parameter doesn't contain / character.") paramSplit = paramStr.split("=") if len(paramSplit) != 2: raise InvalidCmdParametersException("The parameter doesn't contain = character.") paramName = paramSplit[0][1:] paramValue = paramSplit[1] params[paramName] = paramValue return params def parse_command(self, cmd): ''' Parse_command function verifies if a command is correct. The entire string of user is split in commands and parameters. Only the Command is returned. ''' cmdSplit = cmd.split(" ") if len(cmdSplit) > 0: cmdName = cmdSplit[0] params = {} for param in cmdSplit[1:]: try: self.parse_parameter(param, params) except: return None if params=={}: params=None #generate command try: cmd = self.factory.getCommand(cmdName, params) except : raise InvalidCmdParametersException("The command is not valid.") #return None return cmd def execute_command(self, cmd): ''' Execute_command function execute a command given by user ''' if cmd: response = cmd.execute() if response: print(response) else: print("The command is incorrect, please try again") def run(self): ''' Run function calls the execute_command function if command previously given by user has been parsed in a correct way. The sudoku game is being print every time a value or hint has been set. ''' while True: cmdLine = self.read_command() print_cmd = self.parse_command("print") if not cmdLine=="": cmd = self.parse_command(cmdLine) try: os.system('cls') self.execute_command(cmd) self.execute_command(print_cmd) except CellNotEditableException: print("Cell is not editable") except Exception as e: print("Ooops unexpected Exception ", e)
def test_a_game_should_be_opened(self): settings_manager = SettingsManager('mySettings.xml') settings_manager.load() game = Game() game.set_settings_manager(settings_manager) game.initial_sudoku = SudokuBoard() game.initial_sudoku.from_dictionary(self.puzzle, True) game.start_game_timer() save_game_command = SaveGameCommand(self.valid_params) save_game_command.set_game(game) save_game_command.execute() other_game = Game() other_game.set_settings_manager(settings_manager) other_game.start_game_timer() open_game_command = OpenGameCommand(self.valid_params) open_game_command.set_game(other_game) open_game_command.execute() self.assertEqual(game.initial_sudoku.to_dictionary(), other_game.initial_sudoku.to_dictionary())