import random

# List of words to choose from
words = ["python", "programming", "computer", "hangman", "challenge"]

# Hangman stages (visual representation of incorrect guesses)
hangman_stages = [
    """
      --------
      |      |
      |
      |
      |
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      O
      |
      |
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      O
      |      |
      |
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      O
      |     /|\
      |
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      O
      |     /|\
      |     /
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      O
      |     /|\
      |     / \
      |
      |
     ----
    """,
    """
      --------
      |      |
      |      X
      |     /|\
      |     / \
      |
      |
     ----
    """
]


def choose_word():
  """
  This function randomly chooses a word from the word list.
  """
  return random.choice(words)


def display_word(word, guessed_letters):
  """
  This function displays the word with placeholders for unguessed letters and revealed letters.
  """
  displayed_word = ""
  for letter in word:
    if letter in guessed_letters:
      displayed_word += letter
    else:
      displayed_word += "-"
  return displayed_word


def play_game():
  """
  This function is the main game loop that handles word selection, guess tracking, and game flow.
  """
  # Choose a word and initialize variables
  word = choose_word().lower()
  guessed_letters = set()
  wrong_guesses = 0

  print("Welcome to Hangman!")

  # Game loop
  while True:
    # Display current state
    print(hangman_stages[wrong_guesses])
    print(display_word(word, guessed_letters))

    # Check win or lose condition
    if display_word(word, guessed_letters) == word:
      print("Congratulations! You guessed the word.")
      break
    if wrong_guesses == len(hangman_stages) - 1:
      print("Oh no! You ran out of guesses. The word was:", word)
      break

    # Get user guess
    guess = input("Guess a letter: ").lower()

    # Validate guess
    if len(guess) != 1 or not guess.isalpha():
      print("Invalid guess. Please enter a single letter.")
      continue

    # Check if letter is already guessed
    if guess in guessed_letters:
      print("You already guessed that letter. Try again.")
      continue

    # Update guesses
    guessed_letters.add(guess)

    # Check if guess is correct
    if guess not in word:
      wrong_guesses += 1


def main():
  """
  This function starts the game and offers replay option.
  """
  play_game()

  # Ask for replay
  while True:
    play_again = input("Do you want to play again? (yes/no): ").lower()
    if play_again == "yes":
      main()
      break
    elif play_again == "no":
      print("Thanks for playing!")
      break
    else:
      print("Invalid input. Please enter 'yes' or 'no'.")


if __name__ == "__main__":
  main()
