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:
- Game State: Variables to track game data (secret number, guesses, etc.)
- Game Loop: Repeat until win condition is met
- User Input: Get playerโs actions
- Logic: Process input and determine outcome
- 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:
- Clear Goal: Player knows what to accomplish (guess the number)
- Feedback: Player gets information after each action
- Challenge: Not too easy, not too hard
- Progression: Player gets better with each attempt
- 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:
- Add difficulty levels (Easy: 1-50, Medium: 1-100, Hard: 1-500)
- Track high score (fewest attempts ever)
- Add a โgive upโ option
- Implement a streak counter for consecutive wins
๐จโ๐ป Code Example: Game with Statistics
Letโs add game statistics tracking:
๐ก Game Development Tips:
- Start Simple: Get basic version working first
- Test Often: Play your game frequently while developing
- Add Features Gradually: One feature at a time
- Handle Errors: What if player enters invalid input?
- 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! ๐ฎ