All submissions

Number Guessing Game

Solving Approach

How do you plan to solve it?

 

 

Code

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


// EEPROM Structure
const int HISTORY_ADDR = 0;      // Starting address for score history
const int MAX_HISTORY = 10;      // Track last 10 scores
const int MAX_NUMBER = 100;      // Max number to guess
int randomNumber;                // Randomly generated number
int guesses;                     // Number of guesses in the current session


void setup() {
 Serial.begin(9600);
 randomSeed(analogRead(A0));    // Seed for random number generation
 randomNumber = random(1, MAX_NUMBER + 1);
 guesses = 0;


 Serial.println("Welcome to the Number Guessing Game!");
 Serial.println("Guess the number between 1 and 100.");
 Serial.println();
}


void loop() {
 if (Serial.available()) {
   int guess = Serial.parseInt();


   if(guess == 0)
   {
    /*
     * Handling the NL and CR here
     */
     return;
   }
   else if (guess < 1 || guess > MAX_NUMBER) {
     Serial.println("Invalid input. Please guess a number between 1 and 100.");
     return;
   }


   guesses++;
   if (guess < randomNumber) {
     Serial.println("Too Low!");
   } else if (guess > randomNumber) {
     Serial.println("Too High!");
   } else {
     Serial.print("Correct! You guessed it in ");
     Serial.print(guesses);
     Serial.println(" attempts!");


     saveScore(guesses);
     displayLeaderboard(guesses);
     displayHistory();


     Serial.println("Game Over! Resetting...");
     delay(2000);
     resetGame();
   }
 }
}


void saveScore(int score) {
 // Update history log by shifting older scores
 for (int i = (MAX_HISTORY - 1); i > 0; i--) {
   EEPROM.write(HISTORY_ADDR + i, EEPROM.read(HISTORY_ADDR + (i - 1)));
 }
 // Save the latest score
 EEPROM.write(HISTORY_ADDR, score);


 Serial.println("Saving your score...");
}


void displayLeaderboard(int currentScore) {
 Serial.println("\nLeaderboard:");


 // Fetch and sort the scores
 int scores[MAX_HISTORY];
 int validScores = 0;
 for (int i = 0; i < MAX_HISTORY; i++) {
   int score = EEPROM.read(HISTORY_ADDR + i);
   if (score > 0 && score != 255) { // Ignore invalid or uninitialized scores
     scores[validScores++] = score;
   }
 }


 // Sort scores for ranking
 for (int i = 0; i < validScores - 1; i++) {
   for (int j = i + 1; j < validScores; j++) {
     if (scores[j] < scores[i]) { // Bubble Sort Algorithm
       int temp = scores[i];
       scores[i] = scores[j];
       scores[j] = temp;
     }
   }
 }


 // Display sorted leaderboard with "You" tag for the current score
 bool currentScoreTagged = false;
 for (int i = 0; i < validScores; i++) {
   Serial.print(i + 1);
   Serial.print(". ");
   Serial.print(scores[i]);
   Serial.print(" Guesses");
   if (!currentScoreTagged && scores[i] == currentScore) {
     Serial.print(" (You)");
     currentScoreTagged = true;
   }
   Serial.println();
 }


 if (validScores == 0) {
   Serial.println("No scores available.");
 } else if (!currentScoreTagged) {
   Serial.println("\nYour score is not in the top 10.");
 }
}


void displayHistory() {
 Serial.println("\nLast 10 Scores:");
 for (int i = 0; i < MAX_HISTORY; i++) {
   int score = EEPROM.read(HISTORY_ADDR + i);
   if (score > 0 && score != 255) {
     Serial.print(score);
     Serial.print(" Guesses");
   } else {
     Serial.print("-");
   }
   if (i < MAX_HISTORY - 1) Serial.print(", ");
 }
 Serial.println();
}


void resetGame() {
 randomNumber = random(1, MAX_NUMBER + 1);
 guesses = 0;
 Serial.println("New game started! Guess the number between 1 and 100.");
}

 

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.


 

Submit Your Solution

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