This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

cards

 
Ranch Hand
Posts: 31
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • 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
  • 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.
 
Ranch Hand
Posts: 1392
  • 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
  • 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
  • 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.

 
Ranch Hand
Posts: 1646
  • 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.
 
lowercase baba
Posts: 12907
63
Chrome Java Linux
  • 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
  • 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.
 
Ranch Hand
Posts: 1272
  • 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
  • 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
  • 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.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic