-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++.cpp
More file actions
645 lines (549 loc) · 19.5 KB
/
C++.cpp
File metadata and controls
645 lines (549 loc) · 19.5 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <iostream>
#include <limits>
#include <string>
#include <cmath>
#include <random>
#include <vector>
#include <algorithm>
#include <chrono>
#include <thread>
#include <fstream>
// ANSI color codes
const std::string RESET = "";
const std::string RED = "";
const std::string GREEN = "";
const std::string YELLOW = "";
const std::string BLUE = "";
const std::string MAGENTA = "";
const std::string CYAN = "";
const std::string WHITE = "";
// Game constants
const std::string HIGH_SCORE_FILE = "highscores.txt";
const int MAX_HIGH_SCORES = 5;
// Forward declarations
void clearScreen();
void printHeader();
int getValidatedChoice(int min, int max);
void provideHint(int guess, int secret, int range);
void playSingleRound();
void playTournament();
void howToPlay();
void viewHighScores();
void waitForEnter();
void loadHighScores();
bool saveHighScore(int score, const std::string& name);
void displayStats();
void settingsMenu();
void printScoreboard();
void printTrophy();
void printFireworks();
void quickPlay();
void extremeMode();
void multiplayerMode();
// High score structure
struct HighScore {
std::string name;
int score;
HighScore(const std::string& n, int s) : name(n), score(s) {}
bool operator<(const HighScore& other) const {
return score > other.score;
}
};
// Global variables
std::vector<HighScore> highScores;
bool enableAnimations = true;
bool enableColor = true;
// Fast random number generator
int getRandomNumber(int min, int max) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
// Clears the console screen
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// Prints header
void printHeader() {
std::cout << "=========================================" << std::endl;
std::cout << " GUESS THE NUMBER GAME " << std::endl;
std::cout << "=========================================" << std::endl;
}
// Gets validated input
int getValidatedChoice(int min, int max) {
int choice;
while (!(std::cin >> choice) || choice < min || choice > max) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number between "
<< min << " and " << max << ": ";
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return choice;
}
// Provides hint
void provideHint(int guess, int secret, int range) {
int diff = std::abs(secret - guess);
if (diff == 0) return;
double percentDiff = static_cast<double>(diff) / range;
if (percentDiff < 0.02) {
std::cout << "Boiling hot!";
} else if (percentDiff < 0.05) {
std::cout << "Very hot!";
} else if (percentDiff < 0.1) {
std::cout << "Hot!";
} else if (percentDiff < 0.2) {
std::cout << "Warm";
} else if (percentDiff < 0.3) {
std::cout << "Cool";
} else if (percentDiff < 0.4) {
std::cout << "Cold";
} else {
std::cout << "Freezing!";
}
std::cout << std::endl;
}
// Wait for user input
void waitForEnter() {
std::cout << "\nPress Enter to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
}
// Load high scores
void loadHighScores() {
highScores.clear();
std::ifstream in(HIGH_SCORE_FILE);
if (in) {
std::string name;
int score;
while (in >> name >> score) {
highScores.emplace_back(name, score);
}
in.close();
}
std::sort(highScores.begin(), highScores.end());
if (highScores.size() > MAX_HIGH_SCORES) {
highScores.resize(MAX_HIGH_SCORES);
}
}
// Save high score
bool saveHighScore(int score, const std::string& playerName) {
if (highScores.size() < MAX_HIGH_SCORES || score > highScores.back().score) {
highScores.emplace_back(playerName, score);
std::sort(highScores.begin(), highScores.end());
if (highScores.size() > MAX_HIGH_SCORES) {
highScores.pop_back();
}
std::ofstream out(HIGH_SCORE_FILE);
if (out) {
for (const auto& hs : highScores) {
out << hs.name << " " << hs.score << std::endl;
}
out.close();
}
return true;
}
return false;
}
// Print trophy
void printTrophy() {
std::cout << " .-=========-." << std::endl;
std::cout << " \\'-=======-'/" << std::endl;
std::cout << " _| .=. |_" << std::endl;
std::cout << " ((| {{1}} |))" << std::endl;
std::cout << " \\| /|\\ |/" << std::endl;
std::cout << " \\__ '`' __/" << std::endl;
std::cout << " _`) (`_" << std::endl;
std::cout << " _/_______\\_" << std::endl;
std::cout << " /___________\\" << std::endl;
}
// Print fireworks
void printFireworks() {
std::cout << "* * * *" << std::endl;
std::cout << " * * *" << std::endl;
std::cout << " * * *" << std::endl;
std::cout << "* * * *" << std::endl;
std::cout << " * * *" << std::endl;
}
// Print scoreboard
void printScoreboard() {
std::cout << "High Scores:" << std::endl;
std::cout << "====================" << std::endl;
if (highScores.empty()) {
std::cout << "No high scores yet!" << std::endl;
} else {
for (size_t i = 0; i < highScores.size(); ++i) {
std::cout << (i + 1) << ". "
<< highScores[i].name << " - "
<< highScores[i].score << std::endl;
}
}
std::cout << "====================" << std::endl;
}
// View high scores
void viewHighScores() {
clearScreen();
printHeader();
std::cout << std::endl;
printScoreboard();
waitForEnter();
}
// Display statistics
void displayStats() {
clearScreen();
printHeader();
std::cout << "\nGame Statistics:" << std::endl;
std::cout << "High scores stored: " << highScores.size() << std::endl;
if (!highScores.empty()) {
std::cout << "Top score: " << highScores[0].score << " by " << highScores[0].name << std::endl;
}
waitForEnter();
}
// Settings menu
void settingsMenu() {
int choice;
do {
clearScreen();
printHeader();
std::cout << "\nSettings Menu:" << std::endl;
std::cout << "1. Animations: " << (enableAnimations ? "ON" : "OFF") << std::endl;
std::cout << "2. Colors: " << (enableColor ? "ON" : "OFF") << std::endl;
std::cout << "3. Back to Main Menu" << std::endl;
std::cout << "Enter your choice: ";
choice = getValidatedChoice(1, 3);
switch (choice) {
case 1:
enableAnimations = !enableAnimations;
break;
case 2:
enableColor = !enableColor;
break;
}
} while (choice != 3);
}
// Quick play mode
void quickPlay() {
clearScreen();
printHeader();
int secretNumber = getRandomNumber(1, 100);
std::cout << "\nQuick Play Mode: Guess between 1 and 100" << std::endl;
int guess;
int tries = 0;
bool guessedCorrectly = false;
std::vector<int> previousGuesses;
while (!guessedCorrectly) {
std::cout << "\nEnter your guess (1 - 100): ";
guess = getValidatedChoice(1, 100);
tries++;
previousGuesses.push_back(guess);
if (guess < secretNumber) {
std::cout << "Too low!" << std::endl;
provideHint(guess, secretNumber, 100);
} else if (guess > secretNumber) {
std::cout << "Too high!" << std::endl;
provideHint(guess, secretNumber, 100);
} else {
std::cout << "\nCongratulations! You guessed the number in "
<< tries << " tries." << std::endl;
if (enableAnimations) {
printFireworks();
}
std::cout << "\nYour guesses: ";
for (size_t i = 0; i < previousGuesses.size(); ++i) {
std::cout << previousGuesses[i];
if (i < previousGuesses.size() - 1) {
std::cout << " -> ";
}
}
std::cout << std::endl;
guessedCorrectly = true;
}
}
waitForEnter();
}
// Extreme mode
void extremeMode() {
clearScreen();
printHeader();
int secretNumber = getRandomNumber(1, 1000);
std::cout << "\nExtreme Mode: Guess between 1 and 1000 (only 10 attempts!)" << std::endl;
int guess;
int tries = 0;
const int maxTries = 10;
bool guessedCorrectly = false;
while (tries < maxTries && !guessedCorrectly) {
std::cout << "\nAttempt " << (tries + 1) << " of " << maxTries << ": ";
guess = getValidatedChoice(1, 1000);
tries++;
if (guess < secretNumber) {
std::cout << "Too low!" << std::endl;
provideHint(guess, secretNumber, 1000);
} else if (guess > secretNumber) {
std::cout << "Too high!" << std::endl;
provideHint(guess, secretNumber, 1000);
} else {
std::cout << "\nCongratulations! You guessed the number in "
<< tries << " tries." << std::endl;
if (enableAnimations) {
printFireworks();
}
guessedCorrectly = true;
}
if (!guessedCorrectly && tries < maxTries) {
std::cout << "Remaining attempts: " << (maxTries - tries) << std::endl;
}
}
if (!guessedCorrectly) {
std::cout << "\nGame Over! The number was: " << secretNumber << std::endl;
}
waitForEnter();
}
// Multiplayer mode
void multiplayerMode() {
clearScreen();
printHeader();
std::cout << "\nMultiplayer Mode" << std::endl;
std::cout << "Enter the number for the other player to guess: ";
int secretNumber = getValidatedChoice(1, 1000);
clearScreen();
printHeader();
std::cout << "\nPlayer 2: Guess the number (1-1000)" << std::endl;
int guess;
int tries = 0;
bool guessedCorrectly = false;
while (!guessedCorrectly) {
std::cout << "\nEnter your guess: ";
guess = getValidatedChoice(1, 1000);
tries++;
if (guess < secretNumber) {
std::cout << "Too low!" << std::endl;
provideHint(guess, secretNumber, 1000);
} else if (guess > secretNumber) {
std::cout << "Too high!" << std::endl;
provideHint(guess, secretNumber, 1000);
} else {
std::cout << "\nCongratulations! You guessed the number in "
<< tries << " tries." << std::endl;
if (enableAnimations) {
printFireworks();
}
guessedCorrectly = true;
}
}
waitForEnter();
}
// Single round game mode
void playSingleRound() {
clearScreen();
printHeader();
std::cout << "\nChoose a difficulty level:" << std::endl;
std::cout << "1. Easy (1 - 50)" << std::endl;
std::cout << "2. Medium (1 - 100)" << std::endl;
std::cout << "3. Hard (1 - 200)" << std::endl;
std::cout << "4. Expert (1 - 1000)" << std::endl;
std::cout << "5. Custom (Enter your own range)" << std::endl;
std::cout << "Enter your choice: ";
int diffChoice = getValidatedChoice(1, 5);
int minNumber = 1;
int maxNumber = 100;
if (diffChoice == 5) {
std::cout << "Enter the minimum number: ";
minNumber = getValidatedChoice(1, std::numeric_limits<int>::max() - 1);
std::cout << "Enter the maximum number: ";
maxNumber = getValidatedChoice(minNumber + 1, std::numeric_limits<int>::max());
} else {
switch (diffChoice) {
case 1: maxNumber = 50; break;
case 2: maxNumber = 100; break;
case 3: maxNumber = 200; break;
case 4: maxNumber = 1000; break;
}
}
int secretNumber = getRandomNumber(minNumber, maxNumber);
int range = maxNumber - minNumber;
std::cout << "\nI've picked a number between " << minNumber << " and " << maxNumber
<< ". Try to guess it!" << std::endl;
int guess;
int tries = 0;
bool guessedCorrectly = false;
std::vector<int> previousGuesses;
while (!guessedCorrectly) {
std::cout << "\nEnter your guess (" << minNumber << " - " << maxNumber << "): ";
guess = getValidatedChoice(minNumber, maxNumber);
tries++;
previousGuesses.push_back(guess);
if (guess < secretNumber) {
std::cout << "Too low!" << std::endl;
provideHint(guess, secretNumber, range);
} else if (guess > secretNumber) {
std::cout << "Too high!" << std::endl;
provideHint(guess, secretNumber, range);
} else {
std::cout << "\nCongratulations! You guessed the number in "
<< tries << " tries." << std::endl;
if (enableAnimations) {
printFireworks();
}
std::cout << "\nYour guesses: ";
for (size_t i = 0; i < previousGuesses.size(); ++i) {
std::cout << previousGuesses[i];
if (i < previousGuesses.size() - 1) {
std::cout << " -> ";
}
}
std::cout << std::endl;
guessedCorrectly = true;
}
}
waitForEnter();
}
// Tournament mode
void playTournament() {
clearScreen();
printHeader();
std::cout << "\nWelcome to Tournament Mode!" << std::endl;
std::cout << "You will face 3 rounds with increasing difficulty. Try to score as many points as possible." << std::endl;
std::string playerName;
std::cout << "\nEnter your name for the high score board: ";
std::getline(std::cin, playerName);
if (playerName.empty()) playerName = "Anonymous";
int totalScore = 0;
const int rounds = 3;
int ranges[rounds] = {50, 200, 500};
int maxAttempts[rounds] = {10, 8, 6};
std::string roundNames[rounds] = {"Easy", "Medium", "Hard"};
for (int round = 0; round < rounds; round++) {
std::cout << "\nRound " << round + 1 << " (" << roundNames[round]
<< ") - Guess the number between 1 and "
<< ranges[round] << std::endl;
int secretNumber = getRandomNumber(1, ranges[round]);
int roundScore = maxAttempts[round] * 100;
int guess;
int attempts = 0;
bool success = false;
while (attempts < maxAttempts[round]) {
std::cout << "\nAttempt " << (attempts + 1)
<< " of " << maxAttempts[round]
<< ". Your guess: ";
guess = getValidatedChoice(1, ranges[round]);
attempts++;
if (guess < secretNumber) {
std::cout << "Too low!" << std::endl;
provideHint(guess, secretNumber, ranges[round]);
roundScore -= 100;
} else if (guess > secretNumber) {
std::cout << "Too high!" << std::endl;
provideHint(guess, secretNumber, ranges[round]);
roundScore -= 100;
} else {
std::cout << "Correct! You guessed it in "
<< attempts << " attempt"
<< (attempts > 1 ? "s" : "") << "."
<< std::endl;
success = true;
break;
}
std::cout << "Current round score: " << roundScore << std::endl;
}
if (success) {
if (roundScore < 0) roundScore = 0;
std::cout << "You earned "
<< roundScore << " points in this round."
<< std::endl;
totalScore += roundScore;
} else {
std::cout << "Out of attempts! The correct number was "
<< secretNumber << "." << std::endl;
}
std::cout << "Total score so far: " << totalScore << std::endl;
if (round < rounds - 1) {
std::cout << "\nNext round starting..." << std::endl;
if (enableAnimations) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
}
std::cout << "\nTournament Over! Your total score is: "
<< totalScore << std::endl;
if (enableAnimations && totalScore > 0) {
printTrophy();
}
bool isNewHighScore = saveHighScore(totalScore, playerName);
if (isNewHighScore) {
std::cout << "New high score! You've made it to the leaderboard!" << std::endl;
}
waitForEnter();
}
// How to play
void howToPlay() {
clearScreen();
printHeader();
std::cout << "\nHow to Play:" << std::endl;
std::cout << "1. In Single Round mode, select a difficulty level and try to guess the secret number." << std::endl;
std::cout << "2. After each wrong guess, you'll get feedback and a temperature hint on closeness." << std::endl;
std::cout << "3. In Tournament mode, play through 3 rounds with increasing difficulty and limited attempts." << std::endl;
std::cout << "4. Each wrong guess in Tournament reduces your round score. Total score is summed across rounds." << std::endl;
std::cout << "5. Quick Play is a fast version with fixed range (1-100)." << std::endl;
std::cout << "6. Extreme Mode challenges you to guess between 1-1000 in just 10 attempts." << std::endl;
std::cout << "7. Multiplayer Mode lets one player set a number and another guess it." << std::endl;
waitForEnter();
}
int main() {
loadHighScores();
int choice;
do {
clearScreen();
printHeader();
std::cout << "\nMain Menu:" << std::endl;
std::cout << "1. Quick Play (1-100)" << std::endl;
std::cout << "2. Single Round Game" << std::endl;
std::cout << "3. Tournament Mode" << std::endl;
std::cout << "4. Extreme Mode (1-1000, 10 tries)" << std::endl;
std::cout << "5. Multiplayer Mode" << std::endl;
std::cout << "6. How to Play" << std::endl;
std::cout << "7. View High Scores" << std::endl;
std::cout << "8. Statistics" << std::endl;
std::cout << "9. Settings" << std::endl;
std::cout << "10. Quit" << std::endl;
std::cout << "Enter your choice: ";
choice = getValidatedChoice(1, 10);
switch (choice) {
case 1:
quickPlay();
break;
case 2:
playSingleRound();
break;
case 3:
playTournament();
break;
case 4:
extremeMode();
break;
case 5:
multiplayerMode();
break;
case 6:
howToPlay();
break;
case 7:
viewHighScores();
break;
case 8:
displayStats();
break;
case 9:
settingsMenu();
break;
case 10:
std::cout << "\nThank you for playing! Goodbye!" << std::endl;
break;
}
} while (choice != 10);
return 0;
}