Link to home
Start Free TrialLog in
Avatar of MichaelNew31
MichaelNew31

asked on

Objects referencing Objects

I am writing a card game program.  I am having difficulty with getting one object to reference another.  I suppose the problem is that I am unsure of who asks what.  THis is my first attempt at writing a real program, and I have the majority of teh code written.  i have:
Card.java - defines all the cards in a deck
Deck.java - deals out cards
Hand.java - a single hand of cards
Player.java - a single player
Game.java - creates a share deck and controls events

When I create a shared deck, is that what will be in my main code?  If so, how do I each player's hand to draw a different card from that shared deck?  I pretty much have all I need to display the cards in the hand, but my biggest problem is this deck issue.  So far, in main, when I get my initial 5 cards, I am getting the same sequence of cards for each player, and each individual player will have two of the same cards.  Anyone have any ideas?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You must randomly select Hands from the Deck. Those Hands must of course be subtracted from the Deck.
Avatar of MichaelNew31
MichaelNew31

ASKER

Thank you.  I knew that was the theory.  My problem is how you actually do that.  Do you have any insight?
Michael, a lot of how code is organized is programmer preference. I would make a slight modification that should give you some ideas. Use this if you are comfortable with it.

Card - represents a single Card
Deck - represents a deck of Cards
Dealer - deals the Cards from Deck


As CEHJ pointed out, the Cards from the Hands that have been dealt must be subtracted from the deck. So, in the Deck, the collection of Cards should be reduced somehow. This can be done many ways, here's some suggestions.

Each time a Card is dealt, the Deck sets a boolean flag for that particular Card to mark that it's no longer in the deck and should not be dealt by the Dealer again. Or the program could just literally remove the instance of the Card object from the collection.
Just build it up bit by bit. Start with the card class. Have you got that already?
Another suggestion is to performe a random shuffle on an initial array of card (which is actually the Deck), and then each dealing returns a clone(!!!) of the Card to the player and moves a deck pointer forward.
You need to decide what to do when you dealed all the cards in the deck.
I'd keep it simple and *realistic*. When a dealer deals from a deck, the cards are *removed* not virtualized ;-)
All are great suggestions.  FIVESIGMAEVENT - helpful solution.  Of course, I have spent much time getting where I am, I would hate to start over!   CEHJ - yes, I have all classes built. I am just trying to get it all to work together.   SHJI1 - another helpful comment.  Sometimes it just seems simpler to heave someone look at your code to critique it and offer suggestions with what you have already done.  Looks like I will be hard at task for a bit!  Thanks guys for the ideas.
ASKER CERTIFIED SOLUTION
Avatar of fivesigmaevent
fivesigmaevent

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Funny you mention vectors!  Vectors are our friends; at least in the C world they are!  Very good stuff, Five.  Yes, I have FINALLY put it all together and, after only 6 hours or so, I have been able to get some interaction with my objects.  Deal is good, players get cards through Hand objects, players can draw, discard, swap cards with other players, etc, etc etc.  Thanks for the good advice.
Congrats! I'm glad to have discussed it with you. :-)