-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnumber_guessing.cpp
207 lines (194 loc) · 5.05 KB
/
number_guessing.cpp
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
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
cout << "\n\t\t\tWelcome to GuessTheNumber game!"
<< endl;
cout << "You have to guess a number between 1 and 100. "
"You'll have limited choices based on the "
"level you choose. Good Luck!"
<< endl;
while (true) {
cout << "\nDifficulty levels: \n";
cout << "1 for easy!\t";
cout << "2 for medium!\t";
cout << "3 for difficult!\t";
cout << "0 for ending the game!\n" << endl;
// select the level of difficulty
int difficultyChoice;
cout << "Enter the difficulty level: ";
cin >> difficultyChoice;
// generating the secret number
srand(time(0));
int secretNumber = 1 + (rand() % 100);
int playerChoice;
// Difficulty Level:Easy
if (difficultyChoice == 1) {
cout << "\nYou have 10 choices for finding the "
"secret number between 1 and 100.";
int choicesLeft = 10;
for (int i = 1; i <= 10; i++) {
// prompting the player to guess the secret
// number
cout << "\n\nEnter the number: ";
cin >> playerChoice;
// determining if the playerChoice matches
// the secret number
if (playerChoice == secretNumber) {
cout << "Well played! You won, "
<< playerChoice
<< " is the secret number" << endl;
cout << "\t\t\t Thanks for playing...."
<< endl;
cout << "Play the game again with "
"us!!\n\n"
<< endl;
break;
}
else {
cout << "Nope, " << playerChoice
<< " is not the right number\n";
if (playerChoice > secretNumber) {
cout << "The secret number is "
"smaller than the number "
"you have chosen"
<< endl;
}
else {
cout << "The secret number is "
"greater than the number "
"you have chosen"
<< endl;
}
choicesLeft--;
cout << choicesLeft << " choices left. "
<< endl;
if (choicesLeft == 0) {
cout << "You couldn't find the "
"secret number, it was "
<< secretNumber
<< ", You lose!!\n\n";
cout << "Play the game again to "
"win!!!\n\n";
}
}
}
}
// Difficulty level : Medium
else if (difficultyChoice == 2) {
cout << "\nYou have 7 choices for finding the "
"secret number between 1 and 100.";
int choicesLeft = 7;
for (int i = 1; i <= 7; i++) {
// prompting the player to guess the secret
// number
cout << "\n\nEnter the number: ";
cin >> playerChoice;
// determining if the playerChoice matches
// the secret number
if (playerChoice == secretNumber) {
cout << "Well played! You won, "
<< playerChoice
<< " is the secret number" << endl;
cout << "\t\t\t Thanks for playing...."
<< endl;
cout << "Play the game again with "
"us!!\n\n"
<< endl;
break;
}
else {
cout << "Nope, " << playerChoice
<< " is not the right number\n";
if (playerChoice > secretNumber) {
cout << "The secret number is "
"smaller than the number "
"you have chosen"
<< endl;
}
else {
cout << "The secret number is "
"greater than the number "
"you have chosen"
<< endl;
}
choicesLeft--;
cout << choicesLeft << " choices left. "
<< endl;
if (choicesLeft == 0) {
cout << "You couldn't find the "
"secret number, it was "
<< secretNumber
<< ", You lose!!\n\n";
cout << "Play the game again to "
"win!!!\n\n";
}
}
}
}
// Difficulty level : Medium
else if (difficultyChoice == 3) {
cout << "\nYou have 5 choices for finding the "
"secret number between 1 and 100.";
int choicesLeft = 5;
for (int i = 1; i <= 5; i++) {
// prompting the player to guess the secret
// number
cout << "\n\nEnter the number: ";
cin >> playerChoice;
// determining if the playerChoice matches
// the secret number
if (playerChoice == secretNumber) {
cout << "Well played! You won, "
<< playerChoice
<< " is the secret number" << endl;
cout << "\t\t\t Thanks for playing...."
<< endl;
cout << "Play the game again with "
"us!!\n\n"
<< endl;
break;
}
else {
cout << "Nope, " << playerChoice
<< " is not the right number\n";
if (playerChoice > secretNumber) {
cout << "The secret number is "
"smaller than the number "
"you have chosen"
<< endl;
}
else {
cout << "The secret number is "
"greater than the number "
"you have chosen"
<< endl;
}
choicesLeft--;
cout << choicesLeft << " choices left. "
<< endl;
if (choicesLeft == 0) {
cout << "You couldn't find the "
"secret number, it was "
<< secretNumber
<< ", You lose!!\n\n";
cout << "Play the game again to "
"win!!!\n\n";
}
}
}
}
// To end the game
else if (difficultyChoice == 0) {
exit(0);
}
else {
cout << "Wrong choice, Enter valid choice to "
"play the game! (0,1,2,3)"
<< endl;
}
}
return 0;
}