4 min read

CS50 in Arabic Session 3&4 Project: Guess the Number Game

CS50 in Arabic Session 3&4 Project: Guess the Number Game

Problem We Want to Solve

A small entertainment app wants to create a simple number guessing game for users.

Right now, there is no interactive way for users to challenge themselves or practice logical thinking.

We will build a program that:

  • Generates a random secret number
  • Lets the user keep guessing until they find it
  • Tells the user if each guess is too high or too low
  • Shows a final summary when the correct number is guessed

What You Will Build

A simple Guess the Number Game in C that behaves like a small interactive console game — the kind you might find on a calculator or old-school handheld device.


Step-by-Step Development (Phases)


Phase 1: Generate a Random Number

Goal: Generate a secret random number that the game will use.

Starting Code Template

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Random integer in range [min, max]
int rand_int(int min, int max) {
    srand(time(NULL));
    return (rand() % (max - min + 1)) + min;
}

int main(void) {
  
    // Example usage (uncomment to test)
    // int value = rand_int(1, 100);
    // printf("Random number: %d\n", value);

    return 0;
}

Requirements:

  • Include the correct header files (stdio.h, stdlib.h, time.h)
  • Seed the random number generator using srand(time(NULL))
  • Generate a number between 1 and 100 using rand()
  • Store it in a variable (do not print it to the user)

Expected Output Example:

(No output — the number is secret!)

Phase 2: User Input

Goal: Ask the user to enter a guess and store it correctly.

Requirements:

  • Print a prompt asking the user to enter a guess
  • Read the guess
  • Store it in an integer variable

Expected Output Example:

Enter your guess: 50

Phase 3: Guess Checking Logic

Goal: Compare the user's guess to the secret number and give feedback.

Rules:

  • If guess < number → print Too low! Try again.
  • If guess > number → print Too high! Try again.
  • If guess == number → print Correct!

Expected Output Example:

Enter your guess: 30
Too low! Try again.

Enter your guess: 80
Too high! Try again.

Enter your guess: 73
Correct!

Phase 4: Loop Until Correct

Goal: Keep asking the user to guess until they get the right answer.

Requirements:

  • Wrap your input and checking logic in a while or do-while loop
  • Count how many attempts the user has made
  • Stop the loop only when the guess matches the secret number

Expected Output Example:

Enter your guess: 50
Too low! Try again.

Enter your guess: 75
Too high! Try again.

Enter your guess: 63
Too low! Try again.

Enter your guess: 73
Correct!

Phase 5: Difficulty Levels

Goal: Allow the player to choose a difficulty level before the game starts.

Requirements

Before generating the random number:

  • Ask the player to choose:
    • Easy → numbers between 1–50
    • Medium → numbers between 1–100
    • Hard → numbers between 1–500
  • Store the selected difficulty
  • Set the maximum random number range based on the choice
  • Generate the secret number using the selected range

Example Output

Choose Difficulty:
1. Easy   (1 - 50)
2. Medium (1 - 100)
3. Hard   (1 - 500)

Enter choice: 2

Phase 6: Attempt Limit

Goal: Limit the number of guesses the player can make.

Requirements

  • Give the player only 7 attempts
  • Count every guess
  • Stop the game if attempts reach 7
  • If the player loses:
    • Print Game over!
    • Reveal the secret number

Example Output

Enter your guess: 20
Too low!

Attempts remaining: 2

Enter your guess: 90
Too high!

Attempts remaining: 1

Enter your guess: 50
Too low!

Game over! The number was 73.

Phase 7: Score System

Goal: Reward players for solving the game in fewer attempts.

Requirements

  • Create a score variable
  • Calculate score based on attempts
  • Fewer attempts should give a higher score
  • Print the final score in the game report

Suggested Formula

score = 100 - (attempts * 10);

You may adjust the formula however you like.

Example Output

===============================
         GAME RESULT
===============================
Correct Number : 73
Total Attempts : 4
Final Score    : 60
-------------------------------
Result         : You guessed it!
===============================

Final Feature: Game Report

Instead of ending silently, show a clean summary when the player wins.

Example Output:

===============================
         GAME RESULT
===============================
Correct Number : 73
Total Attempts : 5
-------------------------------
Result         : You guessed it!
===============================

Functions You Should Implement

// Returns -1 if guess is too low, 1 if too high, 0 if correct
int checkGuess(int guess, int number);

// Prints the final game summary after the player wins or loses.
void printGameReport(int number, int attempts, int score, int won);

// Allows the player to choose a difficulty level and returns the maximum number range.
int chooseDifficulty(void);

Learning Goals

After this assignment, you should understand:

  • How loops work in real interactive applications
  • How to use functions to organize and reuse your code
  • How to generate random numbers in C
  • How to use conditions (if / else if / else) to make decisions
  • How to count iterations and track program state
  • How to control when a loop starts and stops
  • How to format clean, readable output for the user

Note

  • Start with small tests — guess a number you already know by printing it temporarily
  • Calculate the expected feedback manually before running the program
  • Compare your expected output with what the program actually prints
  • If something is wrong, isolate the step: is it the input, the comparison, or the loop?

Session Recording

If you need to review any part of the explanation while working on your project, you can watch the full recording here: Watch the Session on YouTube

Remember: "The best programs are built one small, working step at a time."

Can't wait to see what you build ❤️