I have two types of a custom Player
class, botPlayer
and humanPlayer
. I create a vector
of the base class Player
, and when using functions that are defined in the Player
class, the code runs fine.
Code is below:
Player.h
class Player
{
public:
explicit Player(string name){
this->name = name;
this->balance = 100;
}
Player() { };
int getBalance(){
return this->balance;
}
Hand getHand(){
return this->playerHand;
}
string getName(){
return this->name;
}
void fillHand(vector<Card> &deck){
for (unsigned int i = 0; i < 5; i++){
Card curr = deck[i];
playerHand.addCardToHand(curr, i);
deck.erase(deck.begin() + i);
}
playerHand.convertHandtoInt();
}
void updateBalance(int n){
this->balance += n;
}
void placeBet(int &ante, int &pool){
pool += ante;
updateBalance(-ante);
}
protected:
int balance;
Hand playerHand;
string name;
};
class botPlayer : public Player
{
public:
explicit botPlayer(string name){
this->name = name;
this->balance = 100;
}
int decide(){
return rand() % 2;
}
void tradeCards(vector<Card> &deck){
int decision = decide();
if(decision == 1){
int trades = rand() % 3 + 1;
cout << "Decided to trade " << trades << " cards" << endl;
for (unsigned int i = 0; i < trades; i++){
playerHand.addCardToHand(deck[i], i);
deck.erase(deck.begin() + i);
}
}else{
cout << "Decided not to trade cards" << endl;
}
}
};
class humanPlayer : public Player
{
public:
explicit humanPlayer(string name){
this->name = name;
this->balance = 100;
}
void tradeCards(vector<Card> &deck){
string input;
vector<int> trades;
int curr;
cout << "Please enter the indices you want to switch cards for. If you don't want a switch, simply type x" << endl;
cin >> input;
for(char& c : input){
if(c == 'x'){
return;
}else{
curr = (int)c - 48;
trades.push_back(curr);
}
}
for (unsigned int i = 0; i < trades.size(); i++){
playerHand.addCardToHand(deck[i], trades[i]);
deck.erase(deck.begin() + i);
}
}
};
Main.cpp
/** Include a bunch of stuff **/
using namespace std;
int main(){
srand(time(NULL));
int ante = 50;
int pool = 0;
Deck myDeck;
vector<Card> theDeck = myDeck.getDeck();
myDeck.shuffleDeck(theDeck);
humanPlayer playerOne("tony");
botPlayer playerTwo("comp");
vector<Player> players;
players.push_back(playerOne);
players.push_back(playerTwo);
while(players.size() > 1){
Hand bestHand = players[0].getHand();
checkPlayers(players);
for(unsigned int i = 0; i < players.size(); i++){
cout << "My name is: " << players[i].getName() << " and my balance is: " << players[i].getBalance() << endl;
players[i].fillHand(theDeck);
players[i].placeBet(ante, pool);
cout << " My current hand is: " << endl;
players[i].getHand().printHand();
cout << "\n" << endl;
players[i].tradeCards(theDeck);
//curr.getHand().printHand();
//cout << "\n" << endl;
} //end for
} //end while
cout << players[0].getName() << " wins! " << endl;
return 0;
} //end main
However, when using functions defined in the subclasses, I get an error:
Testing.cpp:43:24: error: '__gnu_cxx::__alloc_traits<std::allocator<Player>, Player>::value_type' {aka 'class Player'} has no member named 'tradeCards'
43 | players[i].tradeCards(theDeck);
I tried adding in a blank function call in the Player
class, like so:
void tradeCards(vector<Card> &deck){ };
The code then compiled, but produced incorrect results (i.e., the cards were not traded).
How can I access the specific inherited class' member functions? Isn't this supposed to be accomplished by polymorphism?