-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (97 loc) · 3.57 KB
/
main.cpp
File metadata and controls
127 lines (97 loc) · 3.57 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
#include <iostream>
#include <random>
#include <thread>
#include <chrono>
using namespace std;
string acceptableReplayInputs[2] = {"Y", "N"};
namespace defaults {
constexpr int minimumNumber = 1;
constexpr int maximumNumber = 10;
}
bool gameRunning = true;
string makeUpper(string stringToConvert) {
for (char &c : stringToConvert) {
// Cast it to unsigned char first to avoid negative value issues (toupper expects this)
const int upper = toupper(static_cast<unsigned char>(c));
c = static_cast<char>(upper); // Convert each character to uppercase (back to a normal char)
}
return stringToConvert;
}
int RandomNumber() {
random_device rd; // random_device gets a seed from the hardware of the pc every compilation
mt19937 gen(rd()); // Use the mersenne-twister engine to generate the number with the rd seed
uniform_int_distribution<> distribution(1,10); // Generate a range of ints, with an even distribution (equal amount of chance of being chosen)
return distribution(gen); // Generate the random number with our engine, within our range
}
int ValidateIntegerInput() {
int userInput;
// Loop until the input is valid for the type (int)
// Loop will only be entered if the type check fails
while (true) {
cout << "Enter a number between 1 and 10: ";
while (!(cin >> userInput)) {
// Input wasn't a valid int
cout << "Invalid input, please type a number: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// Input was an int, but outside the range
if (userInput < defaults::minimumNumber || userInput > defaults::maximumNumber) {
cout << "Number must be between " << defaults::minimumNumber << " and " << defaults::maximumNumber << "." << endl;
continue;
}
// If we got here, its valid and in range
break;
}
cout << "You entered: " << userInput << endl;
return userInput;
}
bool isValidAnswer(const size_t listSize, string acceptableInputs[], const string& userAnswer) {
for (int j=0; j < listSize; j++ ) {
if (acceptableInputs[j] == userAnswer) {
return true;
}
}
return false;
}
void GameplayLoop() {
cout << "I'm thinking of a number between " << defaults::minimumNumber << "and " << defaults::maximumNumber << "!" << endl;
const int numberToGuess = RandomNumber();
bool userHasGuessedCorrectly = false;
int amountOfGuesses {0};
while (!userHasGuessedCorrectly) {
const int userGuess = ValidateIntegerInput();
amountOfGuesses++;
if (userGuess == numberToGuess) {
cout << "Correct! You got it in " << amountOfGuesses << " guesses.";
userHasGuessedCorrectly = true;
} else {
cout << "Sorry, try again.\n";
}
}
string playAgainAnswer;
while (true) {
cout << "Do you want to play again? (Y/N)";
cin >> playAgainAnswer;
playAgainAnswer = makeUpper(playAgainAnswer);
if (isValidAnswer(size(acceptableReplayInputs), acceptableReplayInputs, playAgainAnswer)) {
// Valid response
break;
}
cout << "Please enter either Y or N.\n";
}
if (playAgainAnswer == "Y") {
GameplayLoop();
} else {
gameRunning = false;
}
}
int main() {
while (gameRunning) {
GameplayLoop();
}
cout << "Thanks for playing, goodbye!";
// Wait a couple seconds before exiting
this_thread::sleep_for(chrono::seconds(2));
return 0;
}