How to Approach a Number Guessing Game (with a Twist) Algorithm?

Game Overview

The classic number guessing game has a simple premise: a computer generates a random number, and the player tries to guess it within a limited number of attempts. Our twist? The computer will provide hints about the relationship between the guessed number and the secret number, but the hints won’t be straightforward “higher” or “lower.”

Algorithm Breakdown

1. Generate a Secret Number


// Randomly generate a secret number within a range (e.g., 1 to 100)
secretNumber = random.randint(1, 100)

2. Initialize Variables

  • attempts: Counter for the number of guesses allowed.
  • guessedNumbers: List to store the player’s guesses.

3. Game Loop

  • Prompt the player to enter a guess.
  • Check if the guess is valid (within the range and not already guessed).
  • If valid, add the guess to the guessedNumbers list.
  • Provide a hint about the relationship between the guess and the secret number:
    • “Warmer”: The guessed number is closer to the secret number than the previous guess.
    • “Colder”: The guessed number is farther from the secret number than the previous guess.
    • “Same Distance”: The guessed number is the same distance from the secret number as the previous guess.
  • Decrement the attempts counter.
  • Repeat until attempts reach zero or the player guesses correctly.

4. Determining Hints


def get_hint(guess, previous_guess, secret_number):
  # Calculate distances from the secret number
  current_distance = abs(guess - secret_number)
  previous_distance = abs(previous_guess - secret_number)

  if current_distance < previous_distance:
    return "Warmer"
  elif current_distance > previous_distance:
    return "Colder"
  else:
    return "Same Distance"

5. Game End

  • If the player guesses correctly, display a winning message.
  • If the player runs out of attempts, display the secret number and a losing message.

Example Implementation (Python)


import random

def number_guessing_game():
  secret_number = random.randint(1, 100)
  attempts = 7
  guessed_numbers = []

  print("Welcome to the Number Guessing Game!")
  print("I've chosen a secret number between 1 and 100.")

  while attempts > 0:
    print(f"\nYou have {attempts} attempts left.")
    try:
      guess = int(input("Enter your guess: "))
    except ValueError:
      print("Invalid input. Please enter a number.")
      continue

    if guess in guessed_numbers:
      print("You already guessed that number. Try again.")
      continue

    guessed_numbers.append(guess)
    attempts -= 1

    if len(guessed_numbers) > 1:
      hint = get_hint(guess, guessed_numbers[-2], secret_number)
      print(f"Hint: {hint}")

    if guess == secret_number:
      print(f"\nCongratulations! You guessed the number {secret_number} in {7 - attempts} attempts!")
      break

  if guess != secret_number:
    print(f"\nYou ran out of attempts. The secret number was {secret_number}.")

def get_hint(guess, previous_guess, secret_number):
  # ... (Same code as before)

if __name__ == "__main__":
  number_guessing_game()

Conclusion

The twisted number guessing game adds an element of strategy and deduction to the classic gameplay. By carefully analyzing the provided hints, players can refine their guesses and increase their chances of victory.

Leave a Reply

Your email address will not be published. Required fields are marked *