In my Blackjack game so far, I have multiple classes that access each other frequently. For example, this is my hit()
function for the Game
class:
void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].getPlayerHand()[0].getHandCards().push_back(newCard);
}
First, a new Card
object is assigned a new card from the Deck
object. However, the next line gets more complicated. Essentially:
The specified player's
Hand
object vector is accessed. As such, it needs an index. I'm doing this so that I can easily destroy hands without looping.players[playerNum].getPlayerHand()[0]
In that one
Hand
object in the vector, its card vector is accessed.players[playerNum].getPlayerHand()[0].getHandCards()
That aforementioned card is pushed onto the player's hand card vector.
players[playerNum].getPlayerHand()[0].getHandCards().push_back(newCard);
Again, I'm using a Hand
object so that I can easily destroy a player's cards at the end of each turn. In the game, different players can end up with a different number of cards at the end of each turn. If I assign each player a vector of cards instead, I would have to loop through the vector and pop each card. With a Hand
, I can just pop the Hand
and push a new one. However, using a Hand
class here means more accessing, which complicates the code's flow.
What would be a good solution to this? I'm okay with either method (preferably the one with Hand
), but they do have their pros and cons. If there is a better solution, I'd like to know about it.
Here are snippets of the relevant class' headers:
Game.h
class Game
{
private:
std::vector<Player> players;
Deck deck;
public:
Game();
~Game();
};
Player.h
class Player
{
private:
std::vector<Hand> playerHand;
public:
Player();
~Player();
std::vector<Hand>& getPlayerHand() {return playerHand;}
};
Hand.h
class Hand
{
private:
std::vector<Card> cards;
public:
Hand();
~Hand();
std::vector<Card>& getHandCards() {return cards;}
};
Card.h
class Card
{
private:
int rankValue;
char rank;
char suit;
std::string card;
public:
Card();
Card(char, char);
~Card();
};