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)
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_given_cmd_stop_then_should_get_the_stop_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("stop", None) self.assertTrue(isinstance(cmd, StopCommand))
def test_given_cmd_print_then_should_get_the_print_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("print", None) self.assertTrue(isinstance(cmd, PrintBoardCommand))
def test_given_cmd_hint_then_should_get_the_hint_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("hint", self.hint_parameters) self.assertTrue(isinstance(cmd, HintCommand))
def test_given_cmd_setValue_then_should_get_the_setValue_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("setValue", self.set_value_parameters) self.assertTrue(isinstance(cmd, SetValueCommand))
def test_given_cmd_restart_then_should_get_the_restart_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("restart", {}) self.assertTrue(isinstance(cmd, RestartGameCommand))
def test_given_cmd_readconfigration_then_should_get_the_readconfiguration_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("readConfig", self.readconfig_parameters) self.assertTrue(isinstance(cmd, ReadConfigurationCommand))
def test_given_cmd_about_then_should_get_the_about_cmd_properly(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("about", {}) self.assertTrue(isinstance(cmd, About))
def test_an_OpenCommand_instance_should_be_returned_when_the_command_is_requested_to_the_factory(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("open", self.open_game_parameter) self.assertTrue(isinstance(cmd, OpenGameCommand))
def test_a_SaveCommand_instance_should_be_returned_when_the_command_is_requested_to_the_factory(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("save", self.save_game_parameter) self.assertTrue(isinstance(cmd, SaveGameCommand))
def test_an_ExportCommand_instance_should_be_returned_when_the_command_is_requested_to_the_factory(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("export", self.export_parameter) self.assertTrue(isinstance(cmd, ExportCommand))
def test_a_SolveGameCommand_instance_should_be_returned_when_the_command_is_requested_to_the_factory(self): game = Game() factory = CommandFactory(game) cmd = factory.getCommand("solve", None) self.assertTrue(isinstance(cmd, SolveGameCommand))