def run_single_game(words_list): """A function that receives a list of words, randomly choosing one and runs the game 'hangman' on the chosen word:""" word = hangman_helper.get_random_word(words_list) pattern = "_" * len(word) wrong_guess_list = [] msg = hangman_helper.DEFAULT_MSG num_errors = 0 while num_errors < hangman_helper.MAX_ERRORS and pattern != word: # a loop that will run until the gamer had manged to discover the word # or had made too many mistakes hangman_helper.display_state(pattern, num_errors, wrong_guess_list, msg) input1 = hangman_helper.get_input() chosen_letter = input1[1] if input1[0] == hangman_helper.LETTER: # checks if the gamer entered a letter if len(chosen_letter) != 1 or not chosen_letter.islower(): # checks if the chosen letter is valid msg = hangman_helper.NON_VALID_MSG continue elif chosen_letter in wrong_guess_list or chosen_letter in pattern: # checks if the chosen letter has already been used msg = hangman_helper.ALREADY_CHOSEN_MSG + chosen_letter elif chosen_letter in word: # checks if the chosen letter is in the word, which means we # want to put her in the pattern pattern = update_word_pattern(word, pattern, chosen_letter) msg = hangman_helper.DEFAULT_MSG else: # or else the chosen letter is wrong wrong_guess_list.append(chosen_letter) num_errors += 1 msg = hangman_helper.DEFAULT_MSG elif input1[0] == hangman_helper.HINT: # checks if the gamer asked for a hint and gives a hint new_list_words = filter_words_list(words_list, pattern, wrong_guess_list) letter = choose_letter(new_list_words, pattern) msg = hangman_helper.HINT_MSG + letter if pattern == word: hangman_helper.display_state(pattern, num_errors, wrong_guess_list, hangman_helper.WIN_MSG, True) else: hangman_helper.display_state(pattern, num_errors, wrong_guess_list, hangman_helper.LOSS_MSG + word, True)
def run_single_game(words_list): """recieves list of words, chooses randomly one of them, and runs a single game of hangman, words must contain english alphabet only and only lowercase letters""" random_word = hangman_helper.get_random_word(words_list) pattern_starter = ['_'] * len(random_word) pattern = concat_list(pattern_starter) wrong_guess_list = [] msg = hangman_helper.DEFAULT_MSG while len(wrong_guess_list) < GAME_OVER and pattern != random_word: hangman_helper.display_state(pattern, len(wrong_guess_list), wrong_guess_list, msg, ask_play=False) user_input = hangman_helper.get_input() guess = user_input[LETTER] if user_input[MODE] == hangman_helper.HINT: msg = give_hint(words_list, pattern, wrong_guess_list) hangman_helper.display_state(pattern, len(wrong_guess_list), wrong_guess_list, msg, ask_play=False) elif type(guess) == str: if len(guess) != 1 or str.isalpha(guess) == False or str.islower( guess) == False: msg = hangman_helper.NON_VALID_MSG elif guess in wrong_guess_list or guess in pattern: msg = hangman_helper.ALREADY_CHOSEN_MSG + guess elif guess in random_word: pattern = update_word_pattern(random_word, pattern, guess) msg = hangman_helper.DEFAULT_MSG else: wrong_guess_list.append(guess) msg = hangman_helper.DEFAULT_MSG msg = winner_winner_chicken_dinner(random_word, pattern) hangman_helper.display_state(pattern, len(wrong_guess_list), wrong_guess_list, msg, ask_play=True)
def run_single_game(words_list): """A function that runs the game of hangman""" word = hangman_helper.get_random_word(words_list) wrong_guess_lst = list() error_count = 0 pattern = '_' * len(word) msg = hangman_helper.DEFAULT_MSG words = words_list while word != pattern and error_count < hangman_helper.MAX_ERRORS: hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, False) options, letter = hangman_helper.get_input() word_lst = list(word) pattern_lst = list(pattern) if options == hangman_helper.HINT: # user asks for a hint words = filter_words_list(words, pattern, wrong_guess_lst) hint_letter = choose_letter(words, pattern) msg = hangman_helper.HINT_MSG + hint_letter elif options == hangman_helper.LETTER: # normel game play if letter_check (letter) == False: msg = hangman_helper.NON_VALID_MSG elif letter in pattern_lst or letter in wrong_guess_lst: msg = hangman_helper.ALREADY_CHOSEN_MSG + letter elif letter in word_lst: pattern = update_word_pattern(word, pattern, letter) msg = hangman_helper.DEFAULT_MSG else: wrong_guess_lst.append(letter) error_count += 1 msg = hangman_helper.DEFAULT_MSG if word == pattern: msg = hangman_helper.WIN_MSG hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, True) else: msg = hangman_helper.LOSS_MSG + word hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, True)
def display_current(error_count, msg, pattern, wrong_guess_lst, ask_play): """ present the current display. """ hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, ask_play)
def run_single_game(words_list): """ A function that starts the game with given words list. """ # Initialize parameters wrong_guess_count = 0 wrong_guess_words = [] already_chosed = [] msg = "" # Get random name from the words list. random_word = hangman_helper.get_random_word(words_list) # Initialize the pattern pattern = len(random_word) * HIDDEN_SIGN # Print default message to user msg = hangman_helper.DEFAULT_MSG # the game wont stop until the pattern will be revealed or guess number # will cross the max errors available. while wrong_guess_count < hangman_helper.MAX_ERRORS and \ pattern != random_word: # display the current state in each iteration of the loop hangman_helper.display_state(pattern, wrong_guess_count, wrong_guess_words, msg) # Get input from user request = hangman_helper.get_input() # Check if the input is a guess if request[INPUT_TYPE] == hangman_helper.LETTER: # Check parameter validation if len(request[INPUT_VALUE]) != 1 or \ not request[INPUT_VALUE].islower(): msg = hangman_helper.NON_VALID_MSG continue # Check if the letter already was chosen before. elif request[INPUT_VALUE] in already_chosed: msg = hangman_helper.ALREADY_CHOSEN_MSG + request[INPUT_VALUE] # If the guessed letter does exist in the word elif request[INPUT_VALUE] in random_word: # Updating the the word pattern accordingly pattern = update_word_pattern(random_word, pattern, request[INPUT_VALUE]) msg = hangman_helper.DEFAULT_MSG already_chosed.append(request[INPUT_VALUE]) else: wrong_guess_count += 1 wrong_guess_words.append(request[INPUT_VALUE]) msg = hangman_helper.DEFAULT_MSG already_chosed.append(request[INPUT_VALUE]) elif request[INPUT_TYPE] == hangman_helper.HINT: # Call the filter words function sort = filter_words_list(words_list, pattern, wrong_guess_words) # Call the choose letter function chosen_letter = choose_letter(sort, pattern) # Initialize the msg variable msg = hangman_helper.HINT_MSG + chosen_letter # Initialise the display function in case winning if pattern == random_word: msg = hangman_helper.WIN_MSG # Initialise the display function in case of losing elif wrong_guess_count == hangman_helper.MAX_ERRORS: msg = hangman_helper.LOSS_MSG + random_word # Calling the display function hangman_helper.display_state(pattern, wrong_guess_count, wrong_guess_words, msg, ask_play=True)
def run_single_game(words_list): """ This function runs the game, with any word from the given words_list """ # Initialise game word = h.get_random_word(words_list) pattern = "" error_count = 0 wrong_guess_lst = [] won = False game_over = False for letter in word: pattern += "_" h.display_state(pattern, error_count, wrong_guess_lst, h.DEFAULT_MSG) # get first input user_input = list(h.get_input()) # The Game is ON!!! while True: # player wants a hint if user_input[0] == h.HINT: hint = choose_letter( filter_words_list(words_list, pattern, wrong_guess_lst), pattern) h.display_state(pattern, error_count, wrong_guess_lst, h.HINT_MSG + hint) # player hazards a guess if user_input[0] == h.LETTER and len(user_input[1]) == 1: input_letter = user_input[1] # check if input is in lowercase if INDEX_A <= letter_to_index(input_letter) <= INDEX_Z: # check if letter was guessed before if input_letter in wrong_guess_lst \ or input_letter in pattern: h.display_state(pattern, error_count, wrong_guess_lst, h.ALREADY_CHOSEN_MSG + input_letter) # if correct guess of letter in word elif input_letter in word: pattern = update_word_pattern(word, pattern, input_letter) if pattern == word: won = True game_over = True break h.display_state(pattern, error_count, wrong_guess_lst, h.DEFAULT_MSG) # wrong guess else: wrong_guess_lst.append(input_letter) error_count += 1 if error_count == h.MAX_ERRORS: game_over = True break h.display_state(pattern, error_count, wrong_guess_lst, h.DEFAULT_MSG) # human error else: h.display_state(pattern, error_count, wrong_guess_lst, h.NON_VALID_MSG) elif game_over is True: break # wait for next input user_input = list(h.get_input()) if won is True: # We've got a WINNER! h.display_state(pattern, error_count, wrong_guess_lst, h.WIN_MSG, True) else: # Pa! They done gone and haaaaanged that man!! h.display_state(pattern, error_count, wrong_guess_lst, h.LOSS_MSG + word, True) user_input = list(h.get_input()) # loop until the user makes up their darn mind while user_input[0] != h.PLAY_AGAIN: user_input = list(h.get_input()) # run new game if requested, otherwise end. if user_input[1] is True: run_single_game(words_list)
def run_single_game(words_list): """ Running the game :param words_list: list of words """ # Initialising word = hangman_helper.get_random_word(words_list) # Getting a word errors_list = [] error_count = 0 pattern = HIDDEN_LETTER_SYMBOL * len(word) # Creating default pattern message = hangman_helper.DEFAULT_MSG while len(errors_list) < hangman_helper.MAX_ERRORS and pattern != word: # While the user has not reach maximum number of error # and he did not found the word # Displaying state hangman_helper.display_state(pattern, error_count, errors_list, message, ask_play=False) user_input = hangman_helper.get_input() # Getting the input if user_input[COMMAND_INDEX] == hangman_helper.LETTER: # If the input # is a guess letter = user_input[VALUE_INDEX] # Get the letter # Testing the letter if len(letter) != LENGTH_ONE_LETTER or not letter.islower(): # If # there is more than a letter or it's uppercase message = hangman_helper.NON_VALID_MSG continue elif letter in errors_list or letter in pattern: # If we already # choose this letter message = hangman_helper.ALREADY_CHOSEN_MSG + letter elif letter in word: # If he guess a good letter pattern = update_word_pattern(word, pattern, letter) # Updating the pattern with it message = hangman_helper.DEFAULT_MSG else: # His letter not in the word errors_list.append(letter) # Adding it to errors list error_count += INCREASE # Count an error message = hangman_helper.DEFAULT_MSG # Giving a hint elif user_input[COMMAND_INDEX] == hangman_helper.HINT: # If he want # a hint # Updating words list to keep only that's correspond words_list = filter_words_list(words_list, pattern, errors_list) # Choosing letters hint_letter = choose_letter(words_list, pattern) # Updating the message (it will display with the display_state # at the beginning of the while) message = hangman_helper.HINT_MSG + hint_letter # If he win if pattern == word: # If he found the entire word message = hangman_helper.WIN_MSG # If he loose else: # If he reach the limit message = hangman_helper.LOSS_MSG + word # Displaying state and asking if he want to play again hangman_helper.display_state(pattern, error_count, errors_list, message, ask_play=True)
def run_single_game(words_list): global msg """get a words list and choose one random word for the game""" random_word = hangman_helper.get_random_word(words_list) pattern = '' """create a pattern that is full hide, meaning the string '_' times as""" """the word length""" pattern = len(random_word) * '_' wrong_guess = 0 """making it a set as requested no repeats""" wrong_guess_lst = [] """The message that the user gets when he start the game""" msg = hangman_helper.DEFAULT_MSG """This while loop is till the game is over""" chosen_letter_lst = [] while wrong_guess < hangman_helper.MAX_ERRORS and gameover(pattern): hangman_helper.display_state(pattern, wrong_guess, wrong_guess_lst, msg, ask_play=False) status, input_t = hangman_helper.get_input() if status == hangman_helper.LETTER: """filter_words_list(random_word, pattern, wrong_guess_lst)""" """Checking if the input is correct, the input must be""" """length 1 only letters and only small letter""" if input_t.isupper() is True or len(input_t) != 1 or \ input_t.isalpha() is False: msg = hangman_helper.NON_VALID_MSG elif already_chosen(input_t, chosen_letter_lst): """The letter already been chosen""" msg = hangman_helper.ALREADY_CHOSEN_MSG + input_t chosen_letter_lst += str(input_t) elif letter_in_word(input_t, random_word): """if the letter is in the pattern""" pattern = update_word_pattern(random_word, pattern, input_t) msg = hangman_helper.DEFAULT_MSG chosen_letter_lst += str(input_t) else: """if the letter is not in the pattern""" wrong_guess += 1 wrong_guess_lst += input_t msg = hangman_helper.DEFAULT_MSG chosen_letter_lst += str(input_t) elif status == hangman_helper.HINT: """if you the user want hint""" wrong_guess_lst = list(wrong_guess_lst) words = filter_words_list(words_list, pattern, wrong_guess_lst) letter = choose_letter(words, pattern) msg = hangman_helper.HINT_MSG + letter else: msg = hangman_helper.DEFAULT_MSG if gameover(pattern): """The function gets the current pattern at the end of that game""" """and according to that determine if to display win or lose message""" """If the game is over and you lose, display a lose message and""" """ask if u want to play again""" msg = hangman_helper.LOSS_MSG + random_word hangman_helper.display_state(pattern, wrong_guess, wrong_guess_lst, msg, ask_play=True) else: """if you win display a win message and ask if you want to play""" """again or not""" msg = hangman_helper.WIN_MSG hangman_helper.display_state(pattern, wrong_guess, wrong_guess_lst, msg, ask_play=True)
def run_single_game(words_list, score): """operating one game of hangman""" word = hangman_helper.get_random_word(words_list) wrong_guess_list = [] correct_guess_list = [] pattern = len(word) * '_' if score > 0: hangman_helper.display_state(pattern, wrong_guess_list, score, GOOD_LUCK) input_num = 0 while pattern != word and score > 0 and input_num < 26: user_input = hangman_helper.get_input() if hangman_helper.LETTER == user_input[0]: check_let = check_letter(user_input[1], wrong_guess_list, pattern, word) if check_let == 1: hangman_helper.display_state(pattern, wrong_guess_list, score, INVALID_INPUT) elif check_let == 2: wrong_guess_list.append(user_input[1]) score -= 1 if score == 0: break input_num += 1 hangman_helper.display_state(pattern, wrong_guess_list, score, LETTER_CHOSEN) elif check_let == 3: score -= 1 pattern = update_word_pattern(word, pattern, user_input[1]) count_letter = pattern.count(user_input[1]) score += count_letter * (count_letter + 1) // 2 correct_guess_list.append(user_input[1]) input_num += 1 if pattern == word: break hangman_helper.display_state(pattern, wrong_guess_list, score, YOUR_TURN) elif check_let == 0: hangman_helper.display_state(pattern, wrong_guess_list, score, INVALID_INPUT) elif hangman_helper.WORD == user_input[0]: score -= 1 if user_input[1] == word: missing_letters = pattern.count('_') score += missing_letters * (missing_letters + 1) // 2 pattern = word break elif score == 0: break else: hangman_helper.display_state(pattern, wrong_guess_list, score, SORRY + YOUR_TURN) elif hangman_helper.HINT == user_input[0]: score -= 1 matches = filter_words_list(words_list, pattern, wrong_guess_list) tat_matches = [matches[0]] tat_matches = check_list(matches, tat_matches) hangman_helper.show_suggestions(tat_matches) if score == 0: break else: score -= 1 if score == 0: break hangman_helper.display_state(pattern, wrong_guess_list, score, INVALID_INPUT) if score == 0: hangman_helper.display_state(pattern, wrong_guess_list, score, LOSE + word) else: hangman_helper.display_state(pattern, wrong_guess_list, score, WIN) return score
def run_single_game(words_list): """The function that sets the rules, laws and the center backend of the game itself""" random_word = hangman_helper.get_random_word(words_list) # choose the word randomly from the index wrong_guess_lst = set() pattern = UNDERSCORE * len(random_word) msg = hangman_helper.DEFAULT_MSG # mapping helper func to more comfortable variables i determined ask_play = False # i wont ask the player to play again as default, only in specific # Circumstances while (len(wrong_guess_lst) < hangman_helper.MAX_ERRORS) and ( pattern.find(UNDERSCORE) != -1): error_count = len(wrong_guess_lst) hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, ask_play) # the main function that sends data to the player, the "game" itself key, value = hangman_helper.get_input() if key == hangman_helper.HINT: filtered_words = filter_words_list(words_list, pattern, wrong_guess_lst) hint = (choose_letter(filtered_words, pattern))[1] msg = hangman_helper.HINT_MSG + hint continue # every time we understand what has the player asked for, there is no # need to check all of the other options, that's why we will continue elif key == hangman_helper.LETTER: letter_now = str(value) if (len(letter_now) != 1) or ((ord(letter_now) < 97) or (ord( letter_now) > 122)): # checks if the input is a valid letter msg = hangman_helper.NON_VALID_MSG elif pattern.find(letter_now) != (-1): # checks if we have already chosen the letter before hand msg = hangman_helper.ALREADY_CHOSEN_MSG + letter_now continue elif letter_now in wrong_guess_lst: msg = hangman_helper.ALREADY_CHOSEN_MSG + letter_now continue elif random_word.find(letter_now) == (-1): # sets what happens to wrong guess wrong_guess_lst.add(letter_now) msg = hangman_helper.DEFAULT_MSG continue else: # the proper game where the pattern updates pattern = update_word_pattern(str(random_word), pattern, letter_now) msg = hangman_helper.DEFAULT_MSG if len(wrong_guess_lst) == hangman_helper.MAX_ERRORS: # ending the game if we have reached max errors and asks # the player to play again while showing him loss msg msg = hangman_helper.LOSS_MSG + random_word error_count = hangman_helper.MAX_ERRORS ask_play = True if pattern.find(UNDERSCORE) == -1: # if the world is revealed - win! msg + end game msg = hangman_helper.WIN_MSG ask_play = True hangman_helper.display_state(pattern, error_count, wrong_guess_lst, msg, ask_play)
def display(data): """Calls display_state by passing a dict, for readability""" h.display_state(data["pattern"], data["error_count"], data["wrong_guess_lst"], data["msg"], data["ask_play"]) return None
def run_single_game(words_list): """chooses one word out of the word list in random and starts a game of hangman with said word. in the end, wait for input from the user as to weather start a new game or not. for hangman see: https://en.wikipedia.org/wiki/Hangman_(game) """ # variable list with starting conditions random_word = hangman_helper.get_random_word(words_list) chosen_letters = [] wrong_guesses_list = [] list_of_the_word = list(random_word) pattern = PATTERN_SPACE * len(random_word) printed_massage = hangman_helper.DEFAULT_MSG errors = 0 # the smart part while errors < hangman_helper.MAX_ERRORS: hangman_helper.display_state( pattern, errors, wrong_guesses_list, printed_massage, ) user_choice, user_input = hangman_helper.get_input() # if user wants a hint if user_choice == hangman_helper.HINT: filtered_words = filter_words_list(words_list, pattern, wrong_guesses_list) hint = choose_letter(filtered_words, pattern) printed_massage = hangman_helper.HINT_MSG + hint continue # if user inputs a letter if user_choice == hangman_helper.LETTER: # checking if valid if checking_valid_input(user_input): if user_input not in chosen_letters: # if input is correct, update pattern or add mistake if user_input in list_of_the_word: pattern = update_word_pattern(random_word, pattern, user_input) chosen_letters.append(user_input) printed_massage = hangman_helper.DEFAULT_MSG else: errors += 1 wrong_guesses_list.append(user_input) chosen_letters.append(user_input) printed_massage = hangman_helper.DEFAULT_MSG else: printed_massage = hangman_helper.ALREADY_CHOSEN_MSG \ + user_input continue else: printed_massage = hangman_helper.NON_VALID_MSG continue # if guessed the whole word, end the game. if pattern == random_word: hangman_helper.display_state(pattern, errors, wrong_guesses_list, hangman_helper.WIN_MSG, True) return None # if got out of the loop, user must have lost. hangman_helper.display_state(pattern, errors, wrong_guesses_list, hangman_helper.LOSS_MSG + random_word, True) return None
def run_single_game(words_list): ''' randomly picks a word from words_list, and plays a game of hangman. asks player each turn for a letter. if guessed correct letter, adds to pattern displayed. if guessed incorrect letter, add to list of wrong guess and draws another body part on hangman. :param words_list: a list of words (strings). :return: None. ''' word_to_guess = hangman_helper.get_random_word(words_list) num_of_guesses = 0 letters_guessed = [] pattern = '_' * len(word_to_guess) DEFAULT_MSG = hangman_helper.DEFAULT_MSG curr_msg = DEFAULT_MSG while check_end_game(pattern, num_of_guesses) == 2: #game not over #displays game state and gets input from player hangman_helper.display_state(pattern, num_of_guesses, letters_guessed, curr_msg, ask_play=False) current_letter_guess = hangman_helper.get_input() #player asks for hint if current_letter_guess[0] == hangman_helper.HINT: hint_list = filter_words_list(words_list, pattern, letters_guessed) hint_let = choose_letter(hint_list, pattern) curr_msg = hangman_helper.HINT_MSG + hint_let continue #player enters letter else: current_letter_guess = str(current_letter_guess[1]) #guess is invalid if (str.islower(current_letter_guess) == False) or \ (len(current_letter_guess) > 1): curr_msg = hangman_helper.NON_VALID_MSG #letter entered already guessed elif current_letter_guess in letters_guessed or \ current_letter_guess in pattern: curr_msg = hangman_helper.ALREADY_CHOSEN_MSG + current_letter_guess #letter guessed correctly, updates pattern and checks if game ended elif current_letter_guess in word_to_guess: pattern = update_word_pattern(word_to_guess, pattern, current_letter_guess) if check_end_game(pattern, num_of_guesses) != 2: break curr_msg = DEFAULT_MSG #letter guessed not in word, #updates wrong guesses and checks if game ended else: letters_guessed.append(current_letter_guess) num_of_guesses += 1 if check_end_game(pattern, num_of_guesses) != 2: break curr_msg = DEFAULT_MSG #WIN, asks to play again if check_end_game(pattern, num_of_guesses) == 1: hangman_helper.display_state(pattern, num_of_guesses, letters_guessed, hangman_helper.WIN_MSG, ask_play=True) #LOSS, asks to play again elif check_end_game(pattern, num_of_guesses) == 0: hangman_helper.display_state(pattern, num_of_guesses, letters_guessed, hangman_helper.LOSS_MSG + word_to_guess, ask_play=True)