def act(self, action): """Run an action represented by string <action>. Return a string representing either the new state or an error message, and whether the program should end. @type self: Controller @type action: str @rtype: (str, bool) """ # TODO: Add to this method to handle different actions. if action == 'exit': return '', True elif action == "solve": return str(solve(self._puzzle)),True elif action == "solve-all": solution = solve_complete(self._puzzle) solution = list(map(lambda x: str(x), solution)) return "\n".join(solution),True elif "hint" in action: if action[5].isdigit() and int(action[5]) >= 1: return str(hint_by_depth(self._puzzle,int(action[5]))),False else: return "Please enter input again!" elif self._puzzle.check(action) == ValueError: return "Incorrect input", False elif self._puzzle.check(action): return self._puzzle.move(action), True else: return self.state(), False
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())
def to_solve_all(self): """ To give all solutions of the puzzle. @rtype: tuple """ result_all = solve_complete(self._puzzle) if len(result_all) == 0: message = 'The puzzle is unsolvable from the current state' return (message, True) else: return ('\n'.join(map(str, result_all)), True)
def act(self, action): """Run an action represented by string <action>. Return a string representing either the new state or an error message, and whether the program should end. @type self: Controller @type action: str @rtype: (str, bool) """ if action == ':EXIT': return ('', True) elif action == ':SOLVE': return (solve(self._puzzle), True) elif action == ':SOLVE-ALL': return (solve_complete(self._puzzle), True) elif list(action)[0] == '(' and list(action)[-6:-1] == list(') -> '): newp = self._puzzle.move(action) self._tree.put_item((newp, action), self._puzzle) self._puzzle = newp return (newp, newp.is_solved()) elif [x for x in list(abc) if x in list(action)]: newp = self._puzzle.move(action) self._tree.put_item((newp, action), self._puzzle) self._puzzle = newp return (newp, newp.is_solved()) elif action == ':UNDO': parent = self._tree.find_par(self._puzzle) if parent != None: self._puzzle = parent.root[0] else: print("The previous state does not exist.") return (self._puzzle, self._puzzle.is_solved()) elif action == ':ATTEMPTS': lst = self._tree.node_list() for i in range(len(lst)): if i != 0: print(i.root[1]) print('----------------') print(i.root[0]) return (self.state(), False) elif list(action)[:5] == list(':HINT'): num = '' for i in range(len(list(action)) - 6): num += action[6 + i] x = hint_by_depth(self._puzzle, int(num)) if x == 'No possible extensions!' or x == 'Already at a solution!': print(x) return (self.state(), False) return (x, self._puzzle.is_solved()) else: return (self.state(), False)
def _act_solve_all(self): """Returns all the solution of the puzzle if there exists any or 'There are no solutions.' if no solutions exist, and tells the program to end. @type self: Controller @rtype: (str, bool) """ all_solutions = '' solutions = solve_complete(self._puzzle) for solution in solutions: all_solutions += str(solution) + '\n' if all_solutions != '': return all_solutions, True else: return 'There are no solutions.', True
def act(self, action): """Run an action represented by string <action>. Return a string representing either the new state or an error message, and whether the program should end. @type self: Controller @type action: str @rtype: (str, bool) """ if action == 'exit': return '', True elif action == ':SOLVE': return solve(self._puzzle).__str__(), True elif action == ':SOLVE-ALL': return "\n".join(map(str, solve_complete(self._puzzle))), True # The remaining conditions use helpers for more complicated code. elif action == ':UNDO': return self._undo(), False elif action == ':ATTEMPTS': return self._state_tree.attempts(), False elif action[:6] == ":HINT ": return self._hint(action), False elif isinstance(action, str): # The action is some sort of move that depends on the puzzle. # rtype of _player_move is (str, bool), since a move could cause the # game to end. return self._player_move(action) # Nothing happens if input is somehow incorrect. else: return self.state(), False
def act(self, action): """Run an action represented by string <action>. Return a string representing either the new state or an error message, and whether the program should end. @type self: Controller @type action: str @rtype: (str, bool) """ # : Add to this method to handle different actions. if action == 'exit': return '', True elif action == ":SOLVE": solution = solve(self._puzzle) if solution is None: return "There is no solution for this Puzzle!", True else: return str(solution), True elif action == ":SOLVE-ALL": solutions = solve_complete(self._puzzle) if len(solutions) == 0: return "There are no solutions for this Puzzle!", True else: values = [str(solution) for solution in solutions] return "\n".join(values), True elif action == ":UNDO": value = self._tree.undo() if value is None: print("There is no previous state.") else: self._puzzle = value return str(self._puzzle), self._puzzle.is_solved() elif action == ":ATTEMPTS": self._tree.attempts() return str(self._puzzle), self._puzzle.is_solved() elif len(action) > 5 and action[0:6] == ":HINT ": n = action[6:] try: n = int(n) print(hint_by_depth(self._puzzle, n)) print() return str(self._puzzle), self._puzzle.is_solved() except: return "Invalid action, please try again.", False else: # Parse the action to the puzzle and make the move try: value = self._puzzle.move(action) self._puzzle = value # add the state into the tree self._tree.act(action, self._puzzle) # return str(value), value.is_solved() except: return "Invalid action, please try again.", False
def act(self, action): """Run an action represented by string <action>. Return a string representing either the new state or an error message, and whether the program should end. @type self: Controller @type action: str @rtype: (str, bool) """ # TODO: Add to this method to handle different actions. # if the action command is 'EXIT' if action == ':EXIT': # return empty string and the program should end return ('', True) # else if the action command is 'SOLVE' elif action == ':SOLVE': # solve the puzzle state = solve(self._puzzle) # if the state exists if state: # return the solution, and the program should end return (str(state), True) # otherwise return the error message, the program should end return ('Failed to solve from this point!', True) # else if the action command is 'SOLVE-ALL' elif action == ':SOLVE-ALL': # get the all possible solution states = solve_complete(self._puzzle) # if the states exist if states: # return the all solutions in the correct format, and the # program should end return ('\n'.join(map(str, states)), True) # otherwise return the error message, and program should end return ('Failed to solve from this point!', True) # else if the action command let the program give hint elif action.startswith(':HINT'): # split the action command to the list of parts parts = action.split(' ') # if the length of parts are 2 if len(parts) != 2: # return the error message for action, the program should # not end return ('Incorrect action format for hints!', False) # try the following firstly try: # get the index 1 of parts n = int(parts[1]) # else raise value error except ValueError: # return the error message for action, the program # should not end return ('Incorrect action format for hints!', False) # if the n is less than 1 if n < 1: # return the error message for action, the program should # not end return ('Incorrect action format for hints!', False) # otherwise get the hint by calling hint by depth function, # the program should not end return (hint_by_depth(self._puzzle, n), False) # if the action command is 'UNDO' elif action == ':UNDO': # return the string of the new current state and the current # state of puzzle result, self._puzzle = self._movetree.undo() # return the string of new current state and the program should # not end return (result, False) # else if the action command is 'ATTEMPTS' elif action == ':ATTEMPTS': # return the all resulting from the user made at the current state # and the program should not end return (self._movetree.attempts(), False) # if the action command is ':' elif action == ':': # return the current state of puzzle, and the program should # not end return (self.state(), False) # otherwise else: # try following try: # get the current state of puzzleafter given move state = self._puzzle.move(action) # else the value error except ValueError as e: # return the string of e, and the program should not end return (str(e), False) # update the puzzle after move self._puzzle = state # get the puzzle state by finding the node of move from tree self._movetree.do(state, action) # return the current puzzle state # and check whether it has been solved return (self.state(), state.is_solved())