示例#1
0
 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)
示例#2
0
    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