Data
Structures
Through C
Project
Title:High Score
Updater
Name:S.Sai Krishna Reddy
Roll no:24B81A67B0
Dept:CSD-B
AIM:
The aim of this program is to design and implement a simple Dice Roll
Game using the C programming language. This game allows the user to
simulate the rolling of a dice, calculate points based on the result, and
maintain the highest score using file handling. It helps the student
understand and apply core C programming concepts such as:
Random number generation using rand() to simulate real dice
behavior
Conditional logic for score calculation based on the dice outcome
Loops and user interaction to create a continuous game experience
Character input handling using getch() for real-time gameplay
File handling to store and retrieve the highest score across sessions
This project demonstrates how fundamental programming concepts can
be used to build interactive games, and how C can be applied to real-
world logic such as scoring systems and state saving.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int getHighScore() {
FILE *f = fopen("highscore.txt", "r");
int high = 0;
if (f != NULL) {
fscanf(f, "%d", &high);
fclose(f);
}
return high;
}
void updateHighScoreIfNeeded(int score) {
int high = getHighScore();
if (score > high) {
FILE *f = fopen("highscore.txt", "w");
if (f != NULL) {
fprintf(f, "%d", score);
fclose(f);
printf("High score updated!\n");
}
} else {
printf("Score not higher. File not updated.\n");
}
}
int main() {
int score = 0, roll;
char key;
srand(time(0));
printf("Welcome to Dice Roll Game!\n");
printf("Press any key to roll the dice.\n");
printf("Press '0' to quit.\n\n");
while (1) {
printf("Press key to roll: ");
key = getch();
if (key == '0') break;
roll = (rand() % 6) + 1;
printf("You rolled: %d\n", roll);
if (roll == 6) {
score += 10;
printf("+10 points!\n");
} else if (roll >= 4) {
score += 5;
printf("+5 points!\n");
} else {
score += 2;
printf("+2 points.\n");
}
printf("Current Score: %d\n\n", score);
}
printf("Final Score: %d\n", score);
int high = getHighScore();
if (score > high) {
printf("New High Score! Previous was %d\n", high);
} else {
printf("High Score: %d\n", high);
}
updateHighScoreIfNeeded(score);
printf("Thanks for playing!\n");
return 0;
}
OUTPUT:
Welcome to Dice Roll Game!
Press any key to roll the dice.
Press '0' to quit.
Press key to roll: You rolled: 4
+5 points!
Current Score: 5
Press key to roll: You rolled: 6
+10 points!
Current Score: 15
Press key to roll: You rolled: 2
+2 points.
Current Score: 17
Press key to roll: (user presses '0')
Final Score: 17
New High Score! Previous was 10
High score updated!
Thanks for playing!
Highscore.txt:
17
OUTPUT - EXPLANATION:
Player presses a key → dice is rolled → random number
between 1 and 6 is shown.
Based on the roll:
o Roll = 6 → +10 points
o Roll = 4 or 5 → +5 points
o Roll = 1, 2, or 3 → +2 points
Game continues until user presses 0.
The final score is compared to the previous high score from
highscore.txt.
If the current score is higher, the file is updated.
Real-Time Applications / Similar Concepts:
Board games like Ludo, Monopoly use dice mechanics.
Mobile games with random outcomes use similar logic.
Casino or chance-based games use dice or random number
generation.
Score tracking using files is a basic idea used in game saves,
leaderboards, and high score storage.