-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_num.cpp
114 lines (108 loc) · 3.45 KB
/
guess_num.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
#include <iostream>
using namespace std;
void compare(int playerChoice, int secretNumber,int &choicesLeft)
{
//cout<<"Hey";
//int ch;
//ch = &choicesLeft;
//choicesLeft = ch;
if(playerChoice ==secretNumber)
{
cout<<"OHHHH"<<endl;
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!!"<< endl;
//counter++;
exit(0);
}
else
{
cout << "Sorry, your guess is not correct. Please try again!!!"<<endl;
cout<<"The secret number is = "<<secretNumber<<endl;
choicesLeft--;
cout << choicesLeft << " choices left..."<< endl;
if (choicesLeft == 0)
{
cout << "You couldn't find the secret number, it was "<< secretNumber<<endl;
cout << "Game Over!!\n\n";
cout << "Play the game again to win!!!\n\n";
}
}
}
int easy(int s_no)
{
srand(time(0));
int secretNumber = (rand() % 10)+1;
return secretNumber;
}
int medium(int s_no)
{
srand(time(0));
int secretNumber = (rand() % 25)+1;
return secretNumber;
}
int hard(int s_no)
{
srand(time(0));
int secretNumber = (rand() % 50)+1;
return secretNumber;
}
int main()
{
char again;
do{
cout<<"Welcome to guessing game!\n\n";
cout<<"*************************";
cout<<"\n"<<"Choose Game Mode to start:\n"<<endl;
cout<<"(E)asy"<<"\n"<<"(M)edium"<<"\n"<<"(H)ard"<<"\n"<<"E(x)it"<<endl;
string choice; int secretNumber,playerChoice;
cin>>choice;
if(choice=="E"||choice=="e")
{
cout<<"easy";
int choicesLeft = 10;
for (int i = 1; i <= 10; i++)
{
cout << "\n\nEnter the number: ";
cin >> playerChoice;
secretNumber = easy(secretNumber);
// prompting the player to guess the secret number
compare(playerChoice,secretNumber,choicesLeft);
}
}
else if(choice=="M"|| choice=="m")
{
cout<<"Medium";
int choicesLeft = 4;
for (int i = 1; i <= 4; i++)
{
cout << "\n\nEnter the number: ";
cin >> playerChoice;
secretNumber = medium(secretNumber);
// prompting the player to guess the secret number
compare(playerChoice,secretNumber,choicesLeft);
}
}
else if(choice=="H" || choice=="h")
{
cout<<"Hard";
int choicesLeft = 3;
for (int i = 1; i <= 3; i++)
{
cout << "\n\nEnter the number: ";
cin >> playerChoice;
secretNumber = hard(secretNumber);
// prompting the player to guess the secret number
compare(playerChoice,secretNumber,choicesLeft);
}
}
else if(choice=="X"||choice=="x")
{
cout<<"Exit game ";
return 0;
}
cout<<"Do you want to contiue? Press y to continue: ";
cin>>again;
} while(again=='y');
return 0;
}