-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGuess_game.cpp
169 lines (155 loc) · 8.9 KB
/
Guess_game.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
/*Name: Shaista Rehan
Program 11 Page 890
Sample Input/Output:
GUESSING GAME
*****************************
Press 1 for Human Player vs Human Player
Press 2 for Human Player vs Computer
Press 3 for Computer vs Computer
*****************************
3
Player 1's turn to guess.
You guess 29. Your guess is too low.
Player 2's turn to guess.
You guess 78. Your guess is too high.
Player 1's turn to guess.
You guess 55. Your guess is too high.
Player 2's turn to guess.
You guess 34. Your guess is too low.
Player 1's turn to guess.
You guess 54. Your guess is too high.
Player 2's turn to guess.
You guess 49. You're right! You win!
Do you want to play again? (y/n)
y
*****************************
Press 1 for Human Player vs Human Player
Press 2 for Human Player vs Computer
Press 3 for Computer vs Computer
*****************************
2
Player 1's turn to guess.
Enter a number
56
You guess 56. Your guess is too high.
Player 2's turn to guess.
You guess 10. Your guess is too low.
Player 1's turn to guess.
Enter a number
40
You guess 40. Your guess is too low.
Player 2's turn to guess.
You guess 47. Your guess is too high.
Player 1's turn to guess.
Enter a number
45
You guess 45. Your guess is too high.
Player 2's turn to guess.
You guess 43. You're right! You win!
Do you want to play again? (y/n)
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int high=100,low=-1;
class Player
{
public:
virtual int getGuess()
{
return 0;
}
};
class HumanPlayer: public Player //Child class of parent class player
{
public:
virtual int getGuess()
{
cout<<"Enter a number"<<endl;
int t;
cin>>t;
return t;
}
};
class ComputerPlayer: public Player //Child class
{
int guess_number=0; //number to store whether the guess is first or not and if its 0 then it makes a random guess
public:
virtual int getGuess()
{
int t=(rand()%(high-low+1))+low;
return t;
}
};
bool checkForWin(int guess, int answer)
{
cout<< "You guess " << guess << ". ";
if (answer == guess)
{
cout<< "You're right! You win!" <<endl;
return true;
}
else if (answer < guess)
{
cout<< "Your guess is too high."<<endl;
if(guess<high)
high=guess-1; //if the last guess was too high means the answer must be lower than that guess so fixing
//the upper value to the lowest guessed number
}
else
{
cout<< "Your guess is too low." <<endl;
if(guess>low) //if the last guess was too low means the answer must be highee than that guess so fixing
low=guess+1; //the lower value to the highest guessed number
}
return false;
}
void play(Player &player1, Player &player2)
{
int answer = 0, guess = 0;
answer = rand() % 100;
bool win = false;
while (!win)
{
cout<< "Player 1's turn to guess." <<endl;
guess = player1.getGuess();
win = checkForWin(guess, answer);
if (win) return;
cout<< "Player 2's turn to guess." <<endl;
guess = player2.getGuess();
win = checkForWin(guess, answer);
}
}
int main()
{
char ans;
cout<<"GUESSING GAME"<<endl;
do{
cout <<"*****************************\n";
cout<<"Press 1 for Human Player vs Human Player"<<endl;
cout<<"Press 2 for Human Player vs Computer"<<endl;
cout<<"Press 3 for Computer vs Computer"<<endl;
cout <<"*****************************\n";
int choice;
cin>>choice;
srand (time(NULL));
if(choice==1)
{
HumanPlayer player1,player2;
play(player1,player2);
}
if(choice==2)
{
HumanPlayer player1;
ComputerPlayer player2;
play(player1,player2);
}
if(choice==3)
{
ComputerPlayer player1,player2;
play(player1,player2);
}
cout <<"Do you want to play again? (y/n)" <<endl;
cin >> ans;
}while(ans == 'y' || ans == 'Y');
}