class Director: """Director class engine of the program""" def __init__(self): """States the variables we will use""" self.lives = 4 self.console = Console() self.jumper = Jumper() self.word = Word() def start_game(self): """Starts the game""" self.console.write("Hello welcome to Jumper") # Get a word self.word.get_word() while self.lives >= 0: # Display word result = self.word.print_word() if result: self.console.write( "Congratulations you guessed the word correctly") break # Display the Jumper self.console.write(self.jumper.get_parachute(self.lives)) # Check if you lose if not self.lives: self.console.write("You killed him") break # Ask for a Guess guess = self.console.read("Guess a letter [a-z]: ") # Filters input result = self.word.check_guess(guess) if not result: continue # Saves guess and updates life result = self.word.save_guess(guess) if not result: self.lives -= 1
class Director: """A code template for a person who directs the game. The responsibility of this class of objects is to control the sequence of play. Stereotype: Controller Attributes: console (Console): An instance of the class of objects known as Console. keep_playing (boolean): Whether or not the game can continue. player (Player): An instance of the class of objects known as Player. word (Word): An instance of the class of objects known as Word. letter(Letter): """ def __init__(self): """The class constructor. Args: self (Director): an instance of Director. """ self.console = Console() self.player = Player() self.keep_playing = True self.word = Word() self.letter = "" def start_game(self): """Starts the game loop to control the sequence of play. Args: self (Director): an instance of Director. """ self.word.get_word() self.word.list_word() while self.keep_playing: self.get_inputs() self.do_updates() self.do_outputs() def get_inputs(self): """Gets the inputs at the beginning of each round of play. In this case, that means moving the hunter to a new location. Args: self (Director): An instance of Director. """ letter = self.player.get_letter() self.letter def do_updates(self): """Updates the important game information for each round of play. In this case, that means the rabbit watches the hunter. Args: self (Director): An instance of Director. """ self.console.check_letter(self.letter) def do_outputs(self): """Outputs the important game information for each round of play. In this case, that means the rabbit provides a hint. Args: self (Director): An instance of Director. """ self.word.secretWord self.console.create_parachute
class Guess: # ********************************** INIZIALITED *********************************** def __init__(self): """ This method will inizialied the two list the one for the gues and the one with the word """ self.word = Word() self.random_word = [] self.guessed_letters = [] self.tries = 0 self.random_word = self.word.get_word() self.word_completion = "_" * len(self.random_word) self.play = True # ********************************** COMPUTE GUESS *********************************** def can_play(self): if self.tries == 5: self.play = False return self.play # ********************************** COMPUTE GUESS *********************************** def compute_guess(self, guess): """ This Method will return the progress that the user have made so far """ if len(guess) == 1 and guess.isalpha(): if guess in self.guessed_letters: print("You already guessed the letter", guess) elif guess not in self.random_word: print(guess, "is not in the word.") self.tries += 1 self.guessed_letters.append(guess) else: print("Good job, ", guess, " is in the word!.") self.guessed_letters.append(guess) word_as_list = list(self.word_completion) indices = [ i for i, letter in enumerate(self.random_word) if letter == guess ] for index in indices: word_as_list[index] = guess self.word_completion = "".join(word_as_list) if "_" not in self.word_completion: print("Congreatulations You guessed the word! " + self.random_word) print("The Jumper is still alive Good Job! \n") self.play = False elif len(guess) == len(self.random_word) and guess.isalpha(): if guess in self.guessed_letters: print("You already guessed the word", guess) elif guess != self.random_word: print(guess, " Is not not the word.") self.tries += 1 self.guessed_letters.append(guess) else: self.play = False self.word_completion = guess print("Congreatulations You guessed the word! " + self.random_word) print("The Jumper is still alive Good Job! \n") else: print("Not a valid guess") pass # ********************************** USER PROGRESS *********************************** def user_progress(self): """ This will return the progress of the user how far is he with the word """ return self.word_completion # ********************************** MYSTERIOUS WORD *********************************** def mysterious_word(self): """ This funcgtion will return the random word """ return self.random_word # ********************************** NUMBER OF TRIES *********************************** def number_of_tries(self): """ This function will return the random word """ return self.tries