File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes cards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cards" Watch "cards" New topic
Author

cards

doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
im trying to deal out a 52 card deck of cards, randomly.. They have their card number and th suit attached to them. Can someone direct me in a better direction..I dont feel like im getting anywhere.


[ November 14, 2004: Message edited by: Kenshin Himura ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a common way to solve the problem. Shuffle the cards by swapping each one with some other card picked at random:

This is so common it's in the JDK. See Collections.shuffle().

You might want to make a little object to represent one card:

Initialize 52 of those in a collection and shuffle them in a jiffy.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
I'm not sure how to use collection..it says varible not found when i put it in.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Kenshin,

I'm not sure how to use collection..it says varible not found when i put it in.
You might want to check out the Sun's Java Tutorial: Collections

Joyce
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
eh thank..Is there a way to do it without this though? I'm not really understanding this.
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
ok..this outputs random..but then there is the issue of repeats im stuck on.

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I highly recommend you follow Stan's advice to create a Card class. Then write a method createStandardDeck() that builds and returns a standard deck of 52 cards.

The way you have it, you are creating a deck of 52 random cards, thus the duplicates.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11302
    
  16

If you really don't want to use collections, you'll have to somehow keep track of which cards you've dealt to avoid duplicates. about the only way i can think of is make and arrayList of available cards. pick one at random, and put it in the first position (so you'll need another arrayList here). Then, remove the element selected from the first list, and chose an element from the remaining cards (note that you now have one less to chose from, so you need to pick an element between 0 and 50 now, instead of 0-51).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
this is what i have now



after the first loop runs, i have 4 of every card..but in the second loop somehow im getting duplicates. and card suit is just takin the remainder if its 0 and then that would be one suit, 1, 2, 3 would be the four suits.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
How about some psudocode:


Now you do the Java code.
[ November 15, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Kenshin Himura:
Why do you have a do-while loop here? Line 2 ensures that every card element is initialized to a value between 1 and 13, so why loop? I read this as "Initialize the card value; then if a randomly chosen number j happens to equal the current card number, initialize it again to the same value."

Now, the reason you're still seeing duplicates is that you're printing out the cards as you shuffle them. Thus, you aren't guaranteed to print each card, and you will likely print the same card more than once.

Pull out the println() from the last loop and make a third loop over all the cards to print them -- after shuffling. If you want to store the card suits and have them stick to the cards as they're shuffled, you'll need dto either create a Card class or use another array. If the latter, initialize it when initializing the cards and shuffle it along with the card array.
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
Thanks for anyone who posted!

I really liked how david explained it though, and it was pretty easy. I think im starting to understand arrays better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cards