-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomNumberGame.cpp
More file actions
129 lines (104 loc) · 3.33 KB
/
randomNumberGame.cpp
File metadata and controls
129 lines (104 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <cmath> // For abs()
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
#include <fstream> // For file I/O
#include <iostream> // For cin, cout
using namespace std; // Using the standard namespace
// Function to read high score from file
int getHighScore() {
// Open file for reading
ifstream inFile("highscore.txt");
int score = 999; // Default high score if file doesn't exist
if (inFile.is_open()) {
inFile >> score;
inFile.close();
}
return score;
}
// Function to save high score to file
void saveHighScore(int score) {
// Open file for writing
ofstream outFile("highscore.txt");
if (outFile.is_open()) {
outFile << score;
outFile.close();
}
}
// Function to give hot/cold hint based on difference
string getTemperatureHint(int difference, int lastDifference) {
// Define temperature ranges
if (difference == 0)
return "You got it!";
string hint;
// Determine how hot/cold based on absolute distance
if (difference <= 5)
hint = "Very Hot!";
else if (difference <= 10)
hint = "Hot!";
else if (difference <= 20)
hint = "Warm";
else if (difference <= 40)
hint = "Cold";
else
hint = "Very Cold!";
// Add getting warmer/colder if not first guess
if (lastDifference != -1) { // -1 indicates first guess
// Compare current difference to last difference
if (difference < lastDifference) {
hint += " (Getting Warmer!)";
} else if (difference > lastDifference) {
hint += " (Getting Colder!)";
}
}
return hint;
}
int main() {
// Variables
int number, guess, attempts;
char playAgain;
int highScore = getHighScore(); // Get high score from file
int lastDifference = -1; // Track last guess difference
// Seed the random number generator
srand(time(0));
// Main game loop (including messages)
do {
number = rand() % 100 + 1; // Generate random number between 1 and 100
attempts = 0; // Reset attempts
lastDifference = -1; // Reset for new game
// Display welcome message
cout << "\n=== Welcome to the Number Guessing Game! ===\n";
cout << "Current High Score: " << highScore << " attempts\n";
cout << "I have chosen a number between 1 and 100.\n";
cout << "I'll tell you if you're hot or cold!\n";
// Game loop
do {
// Get player's guess
cout << "\nEnter your guess: ";
cin >> guess;
attempts++; // Increment attempts
int difference = abs(guess - number); // Calculate difference from target
// Get temperature hint
string hint = getTemperatureHint(difference, lastDifference);
cout << hint << "\n";
// Store current difference for next comparison
lastDifference = difference;
// If correct guess
if (difference == 0) {
// Display number of attempts
cout << "\nCongratulations! You guessed it in " << attempts << " attempts!\n";
// Update high score
if (attempts < highScore) {
highScore = attempts; // Update high score
cout << "New High Score: " << highScore << " attempts!\n";
saveHighScore(highScore); // Save high score to file
}
}
} while (guess != number); // Repeat until correct guess
// Ask to play again
cout << "\nWould you like to play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y'); // Repeat if 'y' or 'Y'
// Display goodbye message
cout << "\nThanks for playing!\nBest score: " << highScore << " attempts\n";
return 0;
}