• Post Reply Bookmark Topic Watch Topic
  • New Topic

implementation question  RSS feed

 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All -

Just starting out in Java, and have decided to make a simple game for myself, but have a problem:

In my game, I'd like to have a deck of cards that players draw from, hold in their "hand", and discard. When played, the card would move a piece on the board. (Not exactly groundbreaking stuff!)

So, I envision each card as being in one of four "locations" (DrawDeck, Player1Hand, Player2Hand, DiscardDeck).

The card objects would initially be in the DrawDeck, and 3 would be randomly dealt into each player hand. Players push a button to play a card, which moves it to the DiscardDeck. When the DrawDeck is empty, the Discard Deck is copied back to the DrawDeck.

And, finally(!), some questions:

Q: Would it make sense to implement these locations as 4 separate ArrayLists?

Q: Is there a simple way to move an object in an ArrayList to another ArrayList?

Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Someone else is likely to suggest something clever, but offhand the ArrayList approach makes sense to me. For moving cards, I think I would write a method like moveCard(Card c, List from, List to).
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,

Thanks for your insight (and your welcome to JavaRanch)! I'm glad that my approach made sense to somebody else!

Now that I'm writing the code, I have a new problem:

Once I generate the random index for the card to "deal" to the player, it's easy to get rid of that reference from the original ArrayList, but I'm stuck for how to get a reference to that object into the Player1Hand ArrayList.

I originally thought that I'd just use

to get the name of the object at that index, but it gives me back things like "MovementCard@60aeb0" when I expected the objects name (i.e. I initially created the card with

and expected to get back "redFive" from the .get), so I don't know how to refer to it when I'm trying to add the reference to the new list.

I'm sure there's a way to do this, but I can't figure out what I'm missing. Any ideas?

Thanks
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't identify cards in the deck by the variable name you gave them when you created them, but by unique data that is inside of them. For instance, it could be a single String object (could also be two ints, or 1 String and 1 int, ...) :




which allows:






I would recommend only having one Class to store card values and calling it Card, not MovementCard.
[ August 12, 2007: Message edited by: Red Smith ]
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Yes, this is exactly what I needed. Many thanks for removing this stumbling block!

Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!