?
Solved

Objects referencing Objects

Posted on 2003-03-04
10
Medium Priority
?
230 Views
Last Modified: 2010-03-31
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
Comment
Question by:MichaelNew31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8067681
You must randomly select Hands from the Deck. Those Hands must of course be subtracted from the Deck.
0
 

Author Comment

by:MichaelNew31
ID: 8069105
Thank you.  I knew that was the theory.  My problem is how you actually do that.  Do you have any insight?
0
 

Expert Comment

by:fivesigmaevent
ID: 8069225
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8070963
Just build it up bit by bit. Start with the card class. Have you got that already?
0
 
LVL 1

Expert Comment

by:shji1
ID: 8071092
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071110
I'd keep it simple and *realistic*. When a dealer deals from a deck, the cards are *removed* not virtualized ;-)
0
 

Author Comment

by:MichaelNew31
ID: 8072200
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
 

Accepted Solution

by:
fivesigmaevent earned 200 total points
ID: 8073877
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
 

Author Comment

by:MichaelNew31
ID: 8078535
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
 

Expert Comment

by:fivesigmaevent
ID: 8080402
Congrats! I'm glad to have discussed it with you. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question