< Previous | Contents | Next >
The Player class represents a human player. It’s derived from GenericPlayer.
class Player : public GenericPlayer
{
public:
Player(const string& name = ""); virtual ~Player();
//returns whether or not the player wants another hit virtual bool IsHitting() const;
//announces that the player wins void Win() const;
//announces that the player loses void Lose() const;
//announces that the player pushes void Push() const;
};
Player::Player(const string& name): GenericPlayer(name)
{}
Player::~Player()
{}
bool Player::IsHitting() const
{
cout << m_Name << ", do you want a hit? (Y/N): "; char response;
cin >> response;
return (response = = ’y’ || response = = ’Y’);
}
void Player::Win() const
{
cout << m_Name << " wins.\n";
}
Introducing the Blackjack Game 369
void Player::Lose() const
{
cout << m_Name << " loses.\n";
}
void Player::Push() const
{
cout << m_Name << " pushes.\n";
}
The class implements the IsHitting() member function that it inherits from GenericPlayer. Therefore, Player isn’t abstract. The class implements the member function by asking the human whether he wants to keep hitting. If the human enters y or Y in response to the question, the member function returns true, indicating that the player is still hitting. If the human enters a different character, the member function returns false, indicating that the player is no longer hitting.
The Win(), Lose(), and Push() member functions simply announce that a player has won, lost, or pushed, respectively.