#ifndef Guess_h
#define Guess_h
#include <iostream>
#include <string>
#include <algorithm> // used for sort
#include <vector> // for vector
#include <sstream>
#include <fstream>
#include <limits>
#include <cstdlib>
usingnamespace std;
/////////////////////////
// Player Class
/////////////////////////
class Player
{
public:
virtualint getGuess() = 0;
virtualvoid lastGuess(int result) = 0;
virtualvoid lastGuess1(int result) = 0;
virtualvoid hGuess2(double result) = 0;
virtualvoid hGuess3(double result) = 0;
};
/////////////////////////
// ComputerPlayer Class
/////////////////////////
class ComputerPlayer : public Player
{
public:
ComputerPlayer();
int getGuess();
virtualvoid lastGuess(int result);
virtualvoid lastGuess1(int result);
virtualvoid hGuess2(double result) {};
virtualvoid hGuess3(double result) {};
void setmin(int mi) {min = mi;};
void setmax(int ma) {max = ma;};
int returnGuess() {cout << guess << endl;return guess;}
private:
int min = 0, max = 99;
// Bounds to guess within
int guess;
// The guess we made
};
ComputerPlayer::ComputerPlayer() : Player()
{
min = 0;
max = 99;
guess = 0;
}
int ComputerPlayer::getGuess()
{
// Make guess where min < guess <= max
int guess = (rand() % (max -min)) + min;
cout << "The computer guesses " << guess << endl;
// Remember the guess
this->guess = guess;
return guess;
}
// Tells the computer player if the last guess was
// too low or too high. This version only gets feedback
// on our own guess, not on the opponent's guess. It would
void ComputerPlayer::lastGuess(int result)
{
// implement your code here
cout << "last guess function high " << endl;
//max = result;
setmax(result);
//cout << max << end;
guess = result;
}
void ComputerPlayer::lastGuess1(int result)
{
// implement your code here
cout << "last guess function low " << endl;
//min = result;
setmin(result);
cout << min << endl;
guess = result;
}
/////////////////////////
// HumanPlayer Class
/////////////////////////
class HumanPlayer : public Player
{
public:
HumanPlayer();
int getGuess();
virtualvoid lastGuess(int result) {};
virtualvoid lastGuess1(int result) {};
virtualvoid hGuess2(double result);
virtualvoid hGuess3(double result);
private:
int guess;
};
HumanPlayer::HumanPlayer() : Player()
{
}
int HumanPlayer::getGuess()
{
cout << "Please enter your guess: " << endl;
cin >> guess;
//implement your code here
// Ask user to input guess
// get guess from the human player
return guess;
}
void HumanPlayer::hGuess2(double result)
{
cout << "I want to change the max " << endl;
max = result; // ***************** This is where error
// Do nothing, human can see the screen
}
void HumanPlayer::hGuess3(double result)
{
cout << "I want to change the min " << endl;
min = result; // ***************** This is where error
// Do nothing, human can see the screen
}
//////////////////////////////////////
// Global functions
//////////////////////////////////////
bool checkForWin(int guess, int answer, Player &p)
{
bool win = false;
if (guess > answer)
{
cout << "Your guess was to high " << endl;
p.lastGuess(guess);
p.hGuess2(guess);
win = false;
}
elseif(guess < answer)
{
cout << "Your guess was to low " << endl;
p.lastGuess1(guess);
p.hGuess3(guess);
win = false;
}
if ( guess == answer)
{
cout << "You win!!!" << endl;
win = true;
}
// if the guess is same as answer. The player wins.
// if the guess is not the same as answer
// inform the palyer if the guess is too high or too low
// and keep track of last guess
//
return win;
}
void play(Player &player1, Player &player2)
{
int answer = 0, guess = 0;
answer = rand() % 100;
cout << answer << endl;
// get an ramdom answer for play to guess
bool win = false;
while (!win)
{
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess();
win = checkForWin(guess, answer, player1);
if (win) return;
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess();
win = checkForWin(guess, answer, player2);
}
}
This might be because of line 11. std::min and std::max are defined in the algorithm header.
usingnamespace std; is a bad habit, now might be the time to break it :+)
If you look at the code provided by many of the experienced users here, they always put std:: before each std thing. It's the best way. There are other alternatives, but doing that is the best in the end.
Some other things to help you out:
When overriding a pure virtual function, use the override keyword. C++11
Consider using the member initilization list rather than assignment in the constructor.
Use const more, for member functions that don't change the state of the object, in function parameters, and basically where ever you can.
What are the down sides? and what are the benefits to using std::
Well the main downside you have already encountered: name clashes. That is the whole point of namespaces - to avoid name clashes, usingnamespace std; subverts that in a big way by bringing in all of the STL (as defined by whatever include files there are) into the global namespace and polluting it. There are lots of examples of normal words used in the STL: left, right, distance to name just a few. Ideally one should put their own code into it's own namespaces.
Also, when one uses std:: , it specifies which thing one is using. For example, std::copy , not boost::copy , or copy from some other library.
Another thing I noticed in your code: Avoid using std::endl , it flushes the buffer every time, so could be inefficient. Just use "\n" instead: