class Menu(): def __init__(self): self.Controller = Controller() def MainMenu(self): while (True): print("Give size of matrix (if 0 the program will stop):") size = int(input()) if size != 0: self.Controller.ResizeAndRestart(size) print("Type of solution 1- dfs, 2-greedy") pick = input() if pick == '1': FinalStates = self.Controller.DFS() if len(FinalStates) != 0: print("Correct States:") for State in FinalStates: print(State) else: print("No Solution Found !") else: FinalState = self.Controller.Greedy() if FinalState[0].CheckCorrectState() == True: print("Correct Final State") else: print("Incorrect final state") print(FinalState[0]) else: return
class UI: def __init__(self): self.__initC = Configuration([-1] * 4) self.__p = Problem(self.__initC) self.__ctrl = Controller(self.__p) self.__n = 4 def printMenu(self): str = '' str += "0 - exit \n" str += "1 - read the number of queens \n" str += "2 - find a solution with DFS \n" str += "3 - find a soultion with Greedy \n" print(str) def readCommand(self): n = 4 try: print("Input the number of queens") n = int(input("n = ")) except: print("Invalid number, the implicit value is 4.") n = 4 self.__initC = Configuration([-1] * n) self.__p = Problem(self.__initC) self.__ctrl = Controller(self.__p) self.__n = n def findSolDFS(self): startClock = time() print(str(self.__ctrl.DFS(self.__p.getRoot(), self.__n))) print('execution time = ', time() - startClock, " seconds") def findSolGreedy(self): startClock = time() print(str(self.__ctrl.greedy(self.__p.getRoot(), self.__n))) print('execution time = ', time() - startClock, " seconds") def run(self): run = True self.printMenu() while run: try: command = int(input(">> ")) if command == 0: run = False elif command == 1: self.readCommand() elif command == 2: self.findSolDFS() elif command == 3: self.findSolGreedy() except: print("Invalid command!")
class UI: def __init__(self, size): self._initialConfig = Configuration(size) self._problem = Problem(self._initialConfig) self._controller = Controller(self._problem) def printMenu(self): print("\n---------------------\n" + "0 - exit\n" + "1 - solve with DFS\n" + "2 - solve with Greedy\n" + "3 - give new size\n") def run(self): runM = True self.printMenu() while runM: command = int(input(">")) if command == 0: runM = False elif command == 1: self.solveDFS() elif command == 2: self.solveBestFS() elif command == 3: self.reconfigure() self.printMenu() def solveDFS(self): startClock = time() print(self._controller.DFS(self._problem.getRoot())) print("execution time = ", time() - startClock, " seconds") def solveBestFS(self): startClock = time() print(self._controller.Greedy(self._problem.getRoot())) print("execution time = ", time() - startClock, " seconds") def reconfigure(self): size = 5 try: print( "Input the size of the board and number of queens (implicit 5)" ) size = int(input("size = ")) except: print("Invalid input, implicit value is still in place") self._initialConfig = Configuration(size) self._problem = Problem(self._initialConfig) self._controller = Controller(self._problem)
class UI: def __init__(self): try: n = int(input("Input the size of the board (implicit n=4)")) except: print("Invalid number. Size set to implict: 4.") n = 4 matrix = np.zeros((n, n), dtype=int).tolist() self.__initialState = Matrix(n, matrix) self.__problem = Problem(self.__initialState) self.__controller = Controller(self.__problem) def mainMenu(self): print("Please choose an option from below:") print("0.Exit") print("1.Solve with DFS") print("2.Solve with greedy") def runBFS(self): solution = self.__controller.DFS(self.__problem.getRoot()) if solution == []: print("No solution found") else: for s in solution: print(str(s)) def runGBFS(self): solution = self.__controller.Greedy(self.__problem.getRoot()) if solution == []: print("No solution found") else: print(str(solution)) def run(self): self.mainMenu() finished = False while not finished: try: choice = int(input(">>")) if choice == 0: finished = True elif choice == 1: self.runBFS() elif choice == 2: self.runGBFS() except ValueError: print("Invalid choice. Please choose one of 0,1 or 2")
class UI: def __init__(self): self.__config = Board([0, 0, 0, 0]) self.__p = Problem(self.__config) self.__cont = Controller(self.__p) def printMenu(self): print("N-Queens Problem") print("Current configuration is: n = " + str(self.__config.getSize())) print("0 - Exit") print("1 - Change Configuration") print("2 - DFS") print("3 - greedy") def changeConfig(self): self.__config = Board([0 for y in range(int(input("n = ")))]) self.__p = Problem(self.__config) self.__cont = Controller(self.__p) def findDFS(self): startClock = time() print(str(self.__cont.DFS(self.__p.getRoot()))) print('execution time = ', time() - startClock, " seconds") def findGreedy(self): startClock = time() print(str(self.__cont.greedy(self.__p.getRoot()))) print('execution time = ', time() - startClock, " seconds") def run(self): while True: self.printMenu() command = int(input(">>")) if command == 0: break elif command == 1: self.changeConfig() elif command == 2: self.findDFS() elif command == 3: self.findGreedy()
class UI: def __init__(self, cntrl): self.cntrl = Controller(4) def menu(self): s = '' s += "0. Exit\n" s += "1. Read Board\n" s += "2. Resolve the problem using DFS\n" s += "3. Resolve the problem using Greedy" print(s) def readConfig(self): try: n = int(input("Dimension of the board (implicit value is 4): ")) self.ctrl = Controller(n) except: print( "Invalid number, constructing the board with the implicit dimension 4" ) self.ctrl = Controller(4) def run(self): while True: try: self.menu() cmd = int(input(">")) if cmd == 0: return False elif cmd == 1: self.readConfig() elif cmd == 2: queue = self.cntrl.DFS() print(queue) elif cmd == 3: queue = self.cntrl.Greedy() print(queue) except: print("Invalid command")