#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.");
}
Display Leaderboard - displayLeaderboard(int currentScore)
Retrieve Scores:
Sort Scores:
Highlight Current Session:
Edge Case Handling:
void displayLeaderboard(int currentScore) {
Serial.println("\nLeaderboard:");
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) {
scores[validScores++] = score;
}
}
// Sort scores in ascending order
for (int i = 0; i < validScores - 1; i++) {
for (int j = i + 1; j < validScores; j++) {
if (scores[j] < scores[i]) {
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
// Display leaderboard
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 (!currentScoreTagged) {
Serial.println("\nYour score is not in the top 10.");
}
}
Display History - void displayHistory()
Retrieve History:
Display Scores
Handling Empty Entries
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) {
Serial.print(score);
Serial.print(" Guesses");
if (i < MAX_HISTORY - 1) Serial.print(", ");
}
}
Serial.println();
}