Solving Approach

How do you plan to solve it?

  • The microcontroller randomly selects a number between 1 and 100.
  • The user guesses the number through the Serial Monitor.
  • For every guess:
    • If guess > number → show “Too High”
    • If guess < number → show “Too Low”
    • If guess == number → user wins
  • Count how many attempts the user takes to guess correctly.
  • Store this attempt count in EEPROM so it is not lost after reset.
  • Maintain a Leaderboard of Top 5 lowest attempt scores in EEPROM:
    • Insert new score in sorted order (smallest first).
  • Display the Leaderboard after each completed game.
  • Start a new game again with a new random number.

 

Code

/*Paste your code here*/
#include <EEPROM.h>

#define MAX_SCORES 5
int scores[MAX_SCORES];

int targetNumber;
int attempts;

void loadScores() {
    for (int i = 0; i < MAX_SCORES; i++)
        EEPROM.get(i * sizeof(int), scores[i]);

    // If uninitialized EEPROM, fill with max int
    for (int i = 0; i < MAX_SCORES; i++)
        if (scores[i] <= 0 || scores[i] > 10000)
            scores[i] = 9999;
}

void saveScores() {
    for (int i = 0; i < MAX_SCORES; i++)
        EEPROM.put(i * sizeof(int), scores[i]);
}

void insertScore(int newScore) {
    for (int i = 0; i < MAX_SCORES; i++) {
        if (newScore < scores[i]) {
            for (int j = MAX_SCORES - 1; j > i; j--)
                scores[j] = scores[j - 1];
            scores[i] = newScore;
            break;
        }
    }
    saveScores();
}

void printLeaderboard() {
    Serial.println("\n📌 Leaderboard (Least Attempts = Best)");
    for (int i = 0; i < MAX_SCORES; i++) {
        if (scores[i] == 9999)
            Serial.println(String(i + 1) + ". ---");
        else
            Serial.println(String(i + 1) + ". " + String(scores[i]) + " attempts");
    }
    Serial.println("----------------------------------\n");
}

void startNewGame() {
    targetNumber = random(1, 101);
    attempts = 0;
    Serial.println("\n🎮 New Game Started!");
    Serial.println("Guess a number between 1 and 100:\n");
}

void setup() {
    Serial.begin(9600);
    randomSeed(analogRead(0));
    loadScores();
    startNewGame();
}

void loop() {
    if (Serial.available()) {
        int userGuess = Serial.parseInt();
        if (userGuess <= 0) return;

        attempts++;

        if (userGuess > targetNumber) {
            Serial.println("⬆ Too High! Try again...");
        } else if (userGuess < targetNumber) {
            Serial.println("⬇ Too Low! Try again...");
        } else {
            Serial.println("\n🎉 Correct Guess!");
            Serial.println("🔢 Number = " + String(targetNumber));
            Serial.println("📌 Attempts = " + String(attempts));

            // Save and show Leaderboard
            insertScore(attempts);
            printLeaderboard();

            delay(1000);
            startNewGame();
        }
    }
}

 

Output

Video

Add a video of the output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

 

Screenshot of Serial Terminal 

Add a Screenshot of the serial terminal showing the output.


 

Upvote
Downvote

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!