Skip to content

Guess the Number Game

This example demonstrates how to create a simple yet engaging “Guess the Number” game in Ellex. The computer picks a random secret number, and the player tries to guess it, receiving hints if their guess is too high or too low. It’s a great way to practice loops, conditionals, and user input in a game context.

Below is the Ellex code for the Guess the Number game:

set secret to 7
set attempts to 0
set max_attempts to 5
tell "Welcome to Guess the Number!"
tell "I'm thinking of a number between 1 and 10."
tell "You have {max_attempts} tries to guess it!"
repeat while {attempts} < {max_attempts}:
ask "What's your guess?" → guess
set attempts to {attempts} + 1
when {guess} = {secret}:
tell "Congratulations! You guessed it in {attempts} tries!"
stop
when {guess} < {secret}:
tell "Too low! Try a higher number. ({max_attempts} - {attempts} tries left)"
when {guess} > {secret}:
tell "Too high! Try a lower number. ({max_attempts} - {attempts} tries left)"
when {attempts} = {max_attempts}:
tell "Game Over! You've used all {max_attempts} tries."
tell "The number was {secret}. Better luck next time!"
  • set secret to 7: Sets a secret number (7 in this case). In a full implementation, Ellex might support a random number generator, but for simplicity, it’s hardcoded here.
  • set attempts to 0 and set max_attempts to 5: Initializes the number of attempts taken and the maximum allowed guesses.
  • tell commands: Provide instructions and feedback to the player about the game rules and remaining tries.
  • repeat while {attempts} < {max_attempts}: Loops until the player runs out of attempts.
  • Inside the loop:
    • ask "What's your guess?" → guess: Gets the player’s guess.
    • set attempts to {attempts} + 1: Increments the attempt counter.
    • when conditions check:
      • If the guess equals the secret number, congratulates the player and stops the loop with stop.
      • If the guess is less than the secret, hints that it’s “too low.”
      • If the guess is greater, hints that it’s “too high.”
  • After the loop, a final when checks if all attempts are used up, revealing the secret number if the player didn’t guess it.

This game introduces game logic concepts like win/lose conditions, user feedback, and loop control, all using Ellex’s natural language syntax.

  1. Start the Ellex REPL by running make dev or use the web playground.
  2. Type or paste the code above and run it.
  3. Follow the prompts to guess the number (which is 7 in this example).
  4. See how the game responds with hints (“too high” or “too low”) until you guess correctly or run out of tries.
  • Random Numbers: If Ellex supports random number generation, modify set secret to 7 to use a random value between 1 and 10 for a different number each game.
  • Adjust Difficulty: Change max_attempts to give more or fewer tries, or adjust the range (1 to 10) to make guessing harder or easier.
  • Add Replay Option: After the game ends, ask if the player wants to play again and restart the loop if they say yes.
  • Track Best Score: Keep track of the fewest attempts needed to win across multiple games.

This game example shows how Ellex can be used to create interactive experiences beyond just drawing or calculations. It’s a stepping stone to more complex games or applications where user interaction drives the program!