diff --git a/__pycache__/images.cpython-39.pyc b/__pycache__/images.cpython-39.pyc new file mode 100644 index 0000000..f2ba1eb Binary files /dev/null and b/__pycache__/images.cpython-39.pyc differ diff --git a/__pycache__/words.cpython-39.pyc b/__pycache__/words.cpython-39.pyc new file mode 100644 index 0000000..aabd30a Binary files /dev/null and b/__pycache__/words.cpython-39.pyc differ diff --git a/hangman.py b/hangman.py index 7bc94b7..87e3f77 100644 --- a/hangman.py +++ b/hangman.py @@ -8,14 +8,11 @@ ''' def is_word_guessed(secret_word, letters_guessed): - ''' - secret_word: word guess by the user - letters_guessed: list hold all the word guess by the user - returns: - return True (if user guess the world correctly ) - return False (wrong selection) - ''' - return False + n = len(secret_word) + for i in range(n): + if secret_word[i] not in letters_guessed: + return False + return True # if you want to test this function please call function -> get_guessed_word("kindness", [k, n, d]) @@ -51,7 +48,31 @@ def get_available_letters(letters_guessed): return sting is -> `bcdfghijklmnopqrstuvwxyz` ''' letters_left = string.ascii_lowercase - return letters_left + word = "" + for ch in letters_left: + if ch in letters_guessed: + continue + else: + word += "" + + return word + + +def is_valid(letter): + if len(letter) == 1: + chars = string.ascii_lowercase + if letter in chars: + return True + return False + + +def hang_the_man(count): + print(IMAGES[count - 1]) + +def give_hint(letters_guessed, secret_word): + for ch in letters_guessed: + if ch not in secret_word: + print("Hint: {}".format(ch)) def hangman(secret_word): @@ -70,28 +91,55 @@ def hangman(secret_word): * Display partial word guessed by the user and use underscore in place of not guess word ''' print("Welcome to the game, Hangman!") + print("I am thinking of a word that is {} letters long.".format( str(len(secret_word))), end='\n\n') letters_guessed = [] + flag = True + remaining_lives = 8 + count = 0 available_letters = get_available_letters(letters_guessed) print("Available letters: {} ".format(available_letters)) - guess = input("Please guess a letter: ") - letter = guess.lower() - - if letter in secret_word: - letters_guessed.append(letter) - print("Good guess: {} ".format( - get_guessed_word(secret_word, letters_guessed))) - if is_word_guessed(secret_word, letters_guessed) == True: - print(" * * Congratulations, you won! * * ", end='\n\n') - else: - print("Oops! That letter is not in my word: {} ".format( - get_guessed_word(secret_word, letters_guessed))) - letters_guessed.append(letter) - print("") + while remaining_lives > 0: + + # available_letters = get_available_letters(letters_guessed) + # print("Available letters: {} ".format(available_letters)) + + guess = input("Please guess a letter: ") + letter = guess.lower() + + if len(letter) == 4 and letter == "hint" and flag: + give_hint(letters_guessed, secret_word) + flag = False + + if is_valid(letter): + remaining_lives -= 1 + count += 1 + + hang_the_man(count) + + print(remaining_lives) + + if letter in secret_word: + letters_guessed.append(letter) + print("Good guess: {} ".format(get_guessed_word(secret_word, letters_guessed))) + + if is_word_guessed(secret_word, letters_guessed) == True: + print(" * * Congratulations, you won! * * ", end='\n\n') + break + else: + print("Oops! That letter is not in my word: {} ".format( + get_guessed_word(secret_word, letters_guessed))) + letters_guessed.append(letter) + print("") + + + if remaining_lives == 0: + print("You lost", end = '\n') + hang_the_man(count) # Load the list of words into the variable wordlist