def startApp(self): """ Main function for starting the app :return: """ # # with open(self.fileAddresses, "w") as f: # for i in range(10): # f.write(str(Address(i, "Address" + str(i), str(i*3), str(i*4)))) # with open(self.fileDrivers, "w") as f: # for i in range(10): # f.write(str(Driver("Name" + str(i), str(i*5), str(i*6)))) repoAddresses = Repository(self.fileAddresses, Address) repoDrivers = Repository(self.fileDrivers, Driver) while True: UI.printMenu() try: cmd = UI.readCmd() if cmd == 1: UI.printAll(repoAddresses, repoDrivers) if cmd == 2: address = UI.readAddress() listOfDrivers = self.calculateDistance( address, repoDrivers) UI.printList(listOfDrivers) if cmd == 3: pairs = self.getPairOfDrivers(repoDrivers) UI.printPairs(pairs) except Exception as ex: print(ex)
def runProgram(self): """ This method runs the command-based part of the game Receive commands from UI and execute them :return: nothing Catches any exceptions raised during the execution, mostly from UI """ UI.printMenu([]) while True: try: cmd, args = UI.getCommand() self.__cmdDict[cmd](args) except Exception as ex: UI.printException(ex)
def startGame(self, args): """ Starts the game as soon as the `start` command is entered to console :param args: not used, just for compatibility with cmdDict :return: True if the player wins, false if Not """ sentence = self.sentenceController.getRandomSentence() hangman = "hangman" indexHangman = 0 while indexHangman < 7: print(sentence.toHangMan() + " -> " + "\033[91m" + hangman[:indexHangman] + "\033[0m") char = "" try: char = UI.getChar() except Exception as ex: UI.printException(ex) if char in sentence.toTxt(): if char not in sentence.chars: sentence.chars.append(char) else: indexHangman += 1 UI.printException(Exception("This character was already introduced!")) else: indexHangman += 1 if "_" not in sentence.toHangMan(): UI.printException(Exception("YOU WON!")) UI.printMenu([]) return True if indexHangman == 7: print("\033[92m" + hangman[:indexHangman].upper() + "\033[0m") UI.printException(Exception("You lost!")) UI.printMenu([]) return False