aspose file tools*
The moose likes Beginning Java and the fly likes Some direction required... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some direction required..." Watch "Some direction required..." New topic
Author

Some direction required...

Matt Konnor
Greenhorn

Joined: Mar 13, 2005
Posts: 1
Hopefully this is in the right section.

I'm trying to write a short program which deals 20 random playing cards. At the moment I just have a card class, containing a randomCard method, which generates two random numbers and assigns them to a specific suit and value (using switch statements). I then return the card as a string and run a for loop in the main class to print out the 20 cards. I'm struggling to think of a way to check for duplication. How would I virtually take a card out the deck once it is delt so that it isnt generated again?

Hopefully I want to turn this into a full version of the game 'pontoon'. What classes should I use and how should they interact with each other?

Many thanks,

Matt
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Have each card in the deck have a unique identifier (an integer number will do) and store all retrieved identifiers in some data structure (probably an array).
Then all you have to do is check whether a new card already exists in that array and generate another one if it does.


42
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeroen Wenting:
Have each card in the deck have a unique identifier (an integer number will do) and store all retrieved identifiers in some data structure (probably an array).
Then all you have to do is check whether a new card already exists in that array and generate another one if it does.

This has one slight disadvantage. Say you are dealing all 52 cards in a standard deck. Once you deal the first 51 cards, theoretically you can infinitely try to generate a new card to deal and always generate one that was already previously dealt. An alternative to this is to create a data structure, either an array or ArrayList would do nicely, that holds all the possible unique cards. Then you pick a random card from the structure and remove it. The next time you deal a card, only the remaining cards in the deck are in the data structure. When you get to the last card, you can easily deal it without having to double-check if your randomly generated card has been dealt already.

HTH

Layne


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Another common technique is to make a collection of the 52 possible card values (maybe just ints 1 through 52) and mix them up (shuffle). Then you can take the first "n" and get a random set. See if you can think of a neat way to mix up a bunch of card numbers.


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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Along the same lines. You have an array of 52 cards. You randomly pick a number from the size of the array. Take that Card and remove it form the array. Now the array has 51 elements, so the next time you randomly pick a number from the size of the array, it will be from 1 to 51, or actually 0 to 50, subtract one from the randomizer.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Oh, I forgot to ask. Are you from UEA, as in the country?

Also please remove those letters from your display name, since it doesn't meet our naming policy of using a real first and last name. Unless you real first name is UEAMatt, which is unlikely.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

And I should look at the Javadocs becfore answering sometimes.

Collections.shuffle(List)

or Collections.shuffle(List, Random)


Then just go through the List, and remove as you go along, if you want to.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some direction required...