patterncppMinor
Accessing multiple data members in Blackjack classes
Viewed 0 times
membersmultipleclassesdataaccessingblackjack
Problem
In my Blackjack game so far, I have multiple classes that access each other frequently. For example, this is my
First, a new
-
The specified player's
-
In that one
-
That aforementioned card is pushed onto the player's hand card vector.
Again, I'm using a
What would be a good solution to this? I'm okay with either method (preferably the one with
Here are snippets of the relevant class' headers:
Game.h
Player.h
Hand.h
```
class Hand
{
private:
std::vector c
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 players;
Deck deck;
public:
Game();
~Game();
};Player.h
class Player
{
private:
std::vector playerHand;
public:
Player();
~Player();
std::vector& getPlayerHand() {return playerHand;}
};Hand.h
```
class Hand
{
private:
std::vector c
Solution
This code:
should read:
The thing is, your classes shouldn't access each other like that. You shouldn't have getter methods that return the internal vectors. You should have methods that perform operations. If you do that, I think your question won't even come up.
void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].getPlayerHand()[0].getHandCards().push_back(newCard);
}should read:
void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].hit(newCard);
}The thing is, your classes shouldn't access each other like that. You shouldn't have getter methods that return the internal vectors. You should have methods that perform operations. If you do that, I think your question won't even come up.
Code Snippets
void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].getPlayerHand()[0].getHandCards().push_back(newCard);
}void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].hit(newCard);
}Context
StackExchange Code Review Q#24732, answer score: 2
Revisions (0)
No revisions yet.