Quest 10: Number Game - Can You Guess It?

Build Your First Interactive Game

๐ŸŽฒ QUEST 10 | Difficulty: Beginner | Time: 5 minutes

๐Ÿ“Š Complexity Level: Intermediate โญโญ

Builds on fundamental concepts from earlier quests. Best for students who have completed Quests 1-6 or have some basic programming experience. This combines multiple concepts into a complete project.

๐Ÿ“– Introduction: The Guessing Challenge

A mysterious wizard thinks of a number between 1 and 100. Your job? Guess it! After each guess, the wizard gives you a hint: โ€œToo high!โ€ or โ€œToo low!โ€ How many guesses will you need?

This classic game combines everything youโ€™ve learned: variables, conditionals, loops, and functions!

๐ŸŽฎ Story Time: Youโ€™ve stumbled upon an ancient puzzle door. To open it, you must guess the secret number. The door whispers hints after each attempt. Will you solve it in time? This is your chance to build a real, playable game from scratch!

๐Ÿ’ก Explanation: Game Components

Every game needs these elements:

  1. Game State: Variables to track game data (secret number, guesses, etc.)
  2. Game Loop: Repeat until win condition is met
  3. User Input: Get playerโ€™s actions
  4. Logic: Process input and determine outcome
  5. Feedback: Tell player what happened
# Basic game structure
secret = 42
guesses_left = 5

while guesses_left > 0:
    # Get input
    guess = int(input("Guess: "))
    
    # Check win condition
    if guess == secret:
        print("You win!")
        break
    
    # Provide feedback
    if guess > secret:
        print("Too high!")
    else:
        print("Too low!")
    
    guesses_left -= 1

๐ŸŽฏ Game Design Principles:

  1. Clear Goal: Player knows what to accomplish (guess the number)
  2. Feedback: Player gets information after each action
  3. Challenge: Not too easy, not too hard
  4. Progression: Player gets better with each attempt
  5. Win/Lose Conditions: Clear ending states

Our game includes: - Random number generation - Limited attempts (optional difficulty) - Hints (hot/cold feedback) - Score tracking

๐ŸŽฎ Activity: Build the Number Guessing Game

Hereโ€™s a complete, playable version:

๐ŸŽฏ Challenge - Enhance the Game:

  1. Add difficulty levels (Easy: 1-50, Medium: 1-100, Hard: 1-500)
  2. Track high score (fewest attempts ever)
  3. Add a โ€œgive upโ€ option
  4. Implement a streak counter for consecutive wins

๐Ÿ‘จโ€๐Ÿ’ป Code Example: Game with Statistics

Letโ€™s add game statistics tracking:

๐Ÿ’ก Game Development Tips:

  1. Start Simple: Get basic version working first
  2. Test Often: Play your game frequently while developing
  3. Add Features Gradually: One feature at a time
  4. Handle Errors: What if player enters invalid input?
  5. Make it Fun: Add personality with messages and emojis!

Common enhancements: - Difficulty selection - Hint system - Time limits - Multiplayer mode - Sound effects (in web version)

๐Ÿงฉ Puzzle Time!

Whatโ€™s the optimal strategy? Figure it out:

๐Ÿ”‘ Solution Explained:

For a number between 1-100:

Linear Search (Guessing 1, 2, 3โ€ฆ): - Worst case: 100 attempts - Average case: 50 attempts - If number is 87: exactly 87 attempts

Binary Search (Dividing in half): - Attempts: ~logโ‚‚(100) โ‰ˆ 7 attempts maximum! - Works by eliminating half the possibilities each time

Example for secret = 87: 1. Guess 50 โ†’ Too low, search [51-100] 2. Guess 75 โ†’ Too low, search [76-100] 3. Guess 87 โ†’ FOUND! (or search [88-100] if too high)

Maximum attempts: With binary search, you can find any number 1-100 in at most 7 guesses!

The Math: Each guess halves the search space: - 100 โ†’ 50 โ†’ 25 โ†’ 12 โ†’ 6 โ†’ 3 โ†’ 1 - Thatโ€™s roughly logโ‚‚(100) โ‰ˆ 6.64 โ‰ˆ 7 guesses

Lesson: Smart algorithms make a HUGE difference!

๐ŸŽฏ Key Takeaways

โœจ Quest 10 Complete! โœจ

Youโ€™ve learned:

โœ… Games combine variables, loops, conditionals, and functions
โœ… Game loops run until a win/lose condition is met
โœ… Good games provide clear feedback to players
โœ… Binary search is much faster than linear search
โœ… Track statistics to make games more engaging
โœ… Start simple and add features gradually

Next Quest: Ready for mind-bending recursion? Try Quest 11: Recursion!

๐Ÿš€ Try This at Home!

Create variations of the game:

Rock, Paper, Scissors:

import random

choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
player = "rock"  # or get from input

print(f"You: {player}, Computer: {computer}")

if player == computer:
    print("Tie!")
elif (player == "rock" and computer == "scissors") or \
     (player == "paper" and computer == "rock") or \
     (player == "scissors" and computer == "paper"):
    print("You win!")
else:
    print("Computer wins!")

Basic Math Quiz Game:

import random

score = 0
for i in range(5):
    a = random.randint(1, 10)
    b = random.randint(1, 10)
    answer = a + b
    
    print(f"Question {i+1}: {a} + {b} = ?")
    # guess = int(input("Your answer: "))
    guess = answer  # Simulate correct answer
    
    if guess == answer:
        print("โœ… Correct!")
        score += 1
    else:
        print(f"โŒ Wrong! Answer was {answer}")

print(f"\nFinal score: {score}/5")

๐Ÿ“ฑ Youโ€™re a Game Developer Now! Keep building and experimenting! ๐ŸŽฎ