C++ The computer player in Programming Project 15.5 does not play the number guessing game very well, since it makes onl
Posted: Mon Jun 06, 2022 5:55 pm
C++
The computer player in Programming Project 15.5 does not play
the number
guessing game very well, since it makes only random guesses. Modify
the program
so that the computer plays a more informed game. The specific
strategy is up to
you, but you must add function(s) to the Player and ComputerPlayer
classes
so that the play(Player &player1, Player &player2) function
can send
the results of a guess back to the computer player. In other words,
the computer
must be told if its last guess was too high or too low, and it also
must be told if
its opponent’s last guess was too high or too low. The computer can
then use this
information to revise its next guess.
#include <iostream>
using namespace std;
class Player{ //1.Player base Class
public:virtual int getGuess(int guess,int answer); //virtual
getGuess function of base class
};
class HumanPlayer :public Player{ //2.HumanPlayer derived
class
public:int getGuess(int guess,int answer); //getGuess function of
derived class
};
class ComputerPlayer :public Player{ //3.ComputerPlayer derived
class
public:int getGuess(int guess,int answer); //getGuess function of
derived class
};
int Player::getGuess(int guess,int answer){
return 0;
}
int HumanPlayer::getGuess(int guess,int answer){
int gues;
cout << "Enter your guess number: ";
cin >> gues;
return gues;
}
int ComputerPlayer::getGuess(int guess,int answer){
if(guess<=answer)
return rand()%100+guess; //generate random no. between guess to
100
else
return rand()%guess; //generate random no. between 0 to guess
}
bool checkForWin(int guess, int answer) { //checkForWin
function
if (answer == guess) {
cout << "You're right! You win!" << endl;
return true;
}
else if (answer < guess)
cout << "Your guess is too high." << endl;
else
cout << "Your guess is too low." << endl;
return false;
}
void play(Player& player1, Player& player2){ //play
function
int guess = 0;
int answer = rand() % 100;
bool win = false;
while (!win) {
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess(guess,answer); //call getGuess function by
player1 object
win = checkForWin(guess,answer); //call checkForWin function
if (win)
return;
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess(guess,answer); //call getGuess function by
player2 object
win = checkForWin(guess, answer); //call checkForWin function
}
}
int main()
{
HumanPlayer h1, h2; //2 objects h1,h2 of class HumanPlayer
ComputerPlayer c1, c2;//2 objects c1,c2 of class
ComputerPlayer
cout << "=============Human vs Human==============\n";
cout << "Press a key to continue..........\n";
char ch;
cin >> ch;
play(h1, h2); //call play function
cout << "=============Human vs Computer============\n";
cout << "Press a key to continue..........\n";
cin >> ch;
play(h1,c1); //call play function
cout <<"=============Computer vs
Computer============\n";
cout << "Press a key to continue............\n";
cin >> ch;
play(c1,c1); //call play function
return 0;
}
The computer player in Programming Project 15.5 does not play
the number
guessing game very well, since it makes only random guesses. Modify
the program
so that the computer plays a more informed game. The specific
strategy is up to
you, but you must add function(s) to the Player and ComputerPlayer
classes
so that the play(Player &player1, Player &player2) function
can send
the results of a guess back to the computer player. In other words,
the computer
must be told if its last guess was too high or too low, and it also
must be told if
its opponent’s last guess was too high or too low. The computer can
then use this
information to revise its next guess.
#include <iostream>
using namespace std;
class Player{ //1.Player base Class
public:virtual int getGuess(int guess,int answer); //virtual
getGuess function of base class
};
class HumanPlayer :public Player{ //2.HumanPlayer derived
class
public:int getGuess(int guess,int answer); //getGuess function of
derived class
};
class ComputerPlayer :public Player{ //3.ComputerPlayer derived
class
public:int getGuess(int guess,int answer); //getGuess function of
derived class
};
int Player::getGuess(int guess,int answer){
return 0;
}
int HumanPlayer::getGuess(int guess,int answer){
int gues;
cout << "Enter your guess number: ";
cin >> gues;
return gues;
}
int ComputerPlayer::getGuess(int guess,int answer){
if(guess<=answer)
return rand()%100+guess; //generate random no. between guess to
100
else
return rand()%guess; //generate random no. between 0 to guess
}
bool checkForWin(int guess, int answer) { //checkForWin
function
if (answer == guess) {
cout << "You're right! You win!" << endl;
return true;
}
else if (answer < guess)
cout << "Your guess is too high." << endl;
else
cout << "Your guess is too low." << endl;
return false;
}
void play(Player& player1, Player& player2){ //play
function
int guess = 0;
int answer = rand() % 100;
bool win = false;
while (!win) {
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess(guess,answer); //call getGuess function by
player1 object
win = checkForWin(guess,answer); //call checkForWin function
if (win)
return;
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess(guess,answer); //call getGuess function by
player2 object
win = checkForWin(guess, answer); //call checkForWin function
}
}
int main()
{
HumanPlayer h1, h2; //2 objects h1,h2 of class HumanPlayer
ComputerPlayer c1, c2;//2 objects c1,c2 of class
ComputerPlayer
cout << "=============Human vs Human==============\n";
cout << "Press a key to continue..........\n";
char ch;
cin >> ch;
play(h1, h2); //call play function
cout << "=============Human vs Computer============\n";
cout << "Press a key to continue..........\n";
cin >> ch;
play(h1,c1); //call play function
cout <<"=============Computer vs
Computer============\n";
cout << "Press a key to continue............\n";
cin >> ch;
play(c1,c1); //call play function
return 0;
}