• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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?
0
MichaelNew31
Asked:
MichaelNew31
  • 3
  • 3
  • 3
  • +1
1 Solution
 
CEHJCommented:
You must randomly select Hands from the Deck. Those Hands must of course be subtracted from the Deck.
0
 
MichaelNew31Author Commented:
Thank you.  I knew that was the theory.  My problem is how you actually do that.  Do you have any insight?
0
 
fivesigmaeventCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
Just build it up bit by bit. Start with the card class. Have you got that already?
0
 
shji1Commented:
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.
0
 
CEHJCommented:
I'd keep it simple and *realistic*. When a dealer deals from a deck, the cards are *removed* not virtualized ;-)
0
 
MichaelNew31Author Commented:
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.
0
 
fivesigmaeventCommented:
Michael, consider using a Vector (http://java.sun.com/j2se/1.4.1/docs/api/java/util/Vector.html) or some other collection class to hold the Deck of Cards. It depends on what/how you choose to implement it, but with Vector's remove(), removeElement() and removeElementAt() a Card can be pulled from the Deck. add() and addElement() can be used to refill the Deck for the next game.

I'm assuming your design is setup something like so:

Game would have Players and Deck as members. Game also defines main and a startGame method.
Player would Hand as member.
Deck would have a collection of Cards as members.
Hand would have a collection of Cards as members.

>> I am having difficulty with getting one object to reference another.  I suppose the problem is that I am unsure of who asks what.

The Game could implement a startGame(), which is called from main() and game play begins. Deck deals the cards (since your code does not use a Dealer object) with a dealCard() method. This dealCard() pulls a card from the Decks collection. A Player recieves a card with a receiveCard() method and adds it to its Hand collection.

That should get you started. Good luck.
0
 
MichaelNew31Author Commented:
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.
0
 
fivesigmaeventCommented:
Congrats! I'm glad to have discussed it with you. :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now