import random
import time
def read_word_list(file_path):
# Read the word list from the text file
with open(file_path, 'r') as file:
return [word.strip() for word in file.readlines()]
def choose_word(words):
# Choose a random word from the word list
return random.choice(words)
def display_word(word, guessed_letters):
# Display the word with guessed letters filled in and the rest as underscores
display = ""
for letter in word:
if letter in guessed_letters:
display += letter
else:
display += "_"
return display
def draw_hangman(attempts_left):
# Draw the hangman based on the number of attempts left
stages = [
"""
--------
| |
|
|
|
|
""",
"""
--------
| |
| O
|
|
|
""",
"""
--------
| |
| O
| |
|
|
""",
"""
--------
| |
| O
| /|
|
|
""",
"""
--------
| |
| O
| /|\\
|
|
""",
"""
--------
| |
| O
| /|\\
| /
|
""",
"""
--------
| |
| O
| /|\\
| / \\
|
"""
]
return stages[len(stages) - attempts_left - 1]
def get_hint(word, guessed_letters):
# Provide a hint by revealing a letter that hasn't been guessed yet
hint_indices = [i for i, letter in enumerate(word) if letter not in guessed_letters]
hint_index = random.choice(hint_indices) if hint_indices else None
return hint_index
def calculate_score(word):
# Calculate the score based on the length of the word
return len(word) * 10 # Each letter is worth 10 points
def play_hangman(name, words):
word_to_guess = choose_word(words)
guessed_letters = []
attempts_left = 6
word_guessed = False
print(f"Welcome to Hangman, {name}!")
print(display_word(word_to_guess, guessed_letters))
print(draw_hangman(attempts_left))
# Calculate time limit based on word length
time_limit = max(15, len(word_to_guess) * 5)
start_time = time.time()
while attempts_left > 0 and not word_guessed:
elapsed_time = time.time() - start_time
remaining_time = max(0, time_limit - elapsed_time)
if remaining_time <= 0:
print("Time's up! You've run out of time.")
break
print(f"Time remaining: {remaining_time:.1f} seconds")
guess = input("Guess a letter or the entire word (type 'hint' for a hint): ").lower()
if guess == "hint":
hint_index = get_hint(word_to_guess, guessed_letters)
if hint_index is not None:
guessed_letters.append(word_to_guess[hint_index])
print(f"Hint: Letter '{word_to_guess[hint_index]}' is in the word.")
else:
print("No hint available.")
continue
if len(guess) > 1 and guess != word_to_guess:
attempts_left -= 1
print(f"Incorrect guess! You have {attempts_left} attempts left.")
elif guess == word_to_guess:
word_guessed = True
break
elif len(guess) == 1 and guess.isalpha():
if guess in guessed_letters:
print("You've already guessed that letter.")
continue
guessed_letters.append(guess)
if guess not in word_to_guess:
attempts_left -= 1
print(f"Incorrect guess! You have {attempts_left} attempts left.")
else:
print("Correct guess!")
else:
print("Please enter a single letter or the entire word.")
continue
print(display_word(word_to_guess, guessed_letters))
print(draw_hangman(attempts_left))
if "_" not in display_word(word_to_guess, guessed_letters):
word_guessed = True
if word_guessed:
score = calculate_score(word_to_guess)
print(f"Congratulations! You guessed the word '{word_to_guess}'!")
print(f"Score: {score}")
return True, score
else:
print(f"Sorry, you're out of attempts. The word was '{word_to_guess}'.")
return False, 0
def play_again(name, total_score, words):
while True:
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again == "yes":
success, score = play_hangman(name, words)
if success:
total_score += score
elif play_again == "no":
print(f"Thanks for playing Hangman, {name}!")
print(f"Total Score: {total_score}")
break
else:
print("Please enter 'yes' or 'no'.")
name = input("Enter your name: ")
file_path = "word_list.txt" # Path to word list file
words = read_word_list(file_path)
total_score = 0
success, score = play_hangman(name, words)
if success:
total_score += score
play_again(name, total_score, words)