This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cards

 
doburomirushii nikku
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
doburomirushii nikku
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to use collection..it says varible not found when i put it in.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eh thank..Is there a way to do it without this though? I'm not really understanding this.
 
doburomirushii nikku
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..this outputs random..but then there is the issue of repeats im stuck on.

 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
doburomirushii nikku
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about some psudocode:


Now you do the Java code.
[ November 15, 2004: Message edited by: Mike Gershman ]
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic