Hello,
I'm after some object-orientated design advice.....
I'm making a BlackJack game. Up until now my player class contained a member called Hand which was a vector of Card objects. So the code to take a card from the dealer went something like this:
1 2 3 4 5 6
|
for(PlayerIndex = 0; PlayerIndex < BlackJackTable.CountPlayers(); PlayerIndex++)
{
Player& CurrentPlayer = BlackJackTable.GetPlayer(PlayerIndex);
DealtCard = Dealer.Deal();
CurrentPlayer.TakeCard(DealtCard);
}
|
with the TakeCard function looking like this:
1 2 3 4 5
|
void Player::TakeCard(Card& DealtCard)
{
Hand.push_back(DealtCard);
HandValue += DealtCard.GetValue();
}
|
This works fine, although now I wish to expand the game so that the player can hold multiple hands. So my plan was to create a Hand class.
So the Player class would have a member called Hands which would be a vector of Hand objects, and the Hand class would have a member called Cards which would be a vector of Card objects.
The Hand class would then contain a TakeCard function:
1 2 3 4 5
|
void Hand::TakeCard(Card& DealtCard)
{
Cards.push_back(DealtCard);
HandValue += DealtCard.GetValue();
}
|
I can think of two ways of now working with this design:
1)
Working with everything in the main function like this:
1 2 3 4 5 6 7
|
for(PlayerIndex = 0; PlayerIndex < BlackJackTable.CountPlayers(); PlayerIndex++)
{
Player& CurrentPlayer = BlackJackTable.GetPlayer(PlayerIndex);
DealtCard = Dealer.Deal();
Hand& CurrentHand = CurrentPlayer.GetHand(HandIndex)
CurrentHand.TakeCard(DealtCard);
}
|
2)
Or to effectively pass the information down through the classes so both the Player class and the Hand class would have a TakeCard function but all the Player class version would do is call the Hand class version, like:
1 2 3 4
|
void Player::TakeCard(Card& DealtCard, int HandIndex)
{
Hands[HandIndex].TakeCard(DealtCard);
}
|
Is one method preferred over the other?
Is there a better approach?
Any guidance on this would be greatly appreciated!
Jimbot