OOP Design - passing data down through classes

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
This is a pretty good question.

My thought process brings me to the question "what does the table need to know?". Here, does the table really need to know about the player?

In my mind, the table consists of "slots" (for lack of a better word). Each slot has a few things:
1) A hand that is being played
2) A bet that has been wagered
3) An owner (player)

So I would probably have the flow go Table->Slot->Player rather than Table->Player->Hand. This way the table doesn't need to care about which player has how many hands. It just treats every hand the same way.

As for "create a 2nd function to call the first" vs. "Get the interface and call the first function directly" -- I wish I had a clear cut answer for you. I've done it both ways myself.


Hi Disch,

Thanks for your reply and for the slots suggestion - I think I do need to revisit my class relationships - I see what you mean about the table not needing to know about the player.

I think I will run with the "Get the interface and call the first function directly" approach for now - it seems a bit tidier.

Thanks,

Jimbot
Topic archived. No new replies allowed.