Originally Posted by
Skynet928
I have a deck of cards...
I do it with numbers.
Here's what I mean:
Let the 52 card values be integers 0, 1, 2, ... , 51
Suppose a given card has an integer value of, say, x.
A number value for the
rank is obtained by the following:
int rank = x % 13
You could make the following correspondences between integer value of rank and a card. (I usually consider Ace to be the highest rank, so I will give it the largest integer value.)
0 => Deuce
1 => Trey
2 => Four
.
.
.
8 => Ten
9 => Jack
10 => Queen
11 => King
12 => Ace
Next, a number value for the
suit is obtained by the following:
int suit = x / 13
There are four possible values for suit, and you might make the following correspondences:
0 => Clubs
1 => Diamonds
2 => Hearts
3 => Spades
By having each card represented by a single integer, lots of things become "easy."
For example a Card is an integer having value 0..51
A Deck for Poker or Bridge can be simply an array of 52 Cards, each of which has a different integer value.
You will need methods that can convert a given card to text or graphical representations for your application. For simple (not particularly attractive) text applications, you only need an array of chars for suits and another array for ranks.
Maybe something like:
char suits[] = {'c', 'd', 'h', 's'}; // For Clubs, Diamonds, Hearts, Spades
char ranks[] = {'2', '3', '4', '5', '6', '7', '8', '9', 't', 'j', 'q', 'k', 'a'}; // Ace-high
Then you make a String representation of a card by appending the char from ranks[] to the char from suits[] for the given card. So, for example a card with an integer value of 24 gives suit=1, rank = 9 so, by indexing into the corresponding arrays, it becomes "jd" for the Jack of Diamonds. (You could use arrays of Strings instead of Chars to make the representation a little more attractive if you have a mind to.)
Or some such thing.
Now: Start with a deck that consists of an array of 52 integer values that has contents 0, 1, 2, ... ,50, 51 (easily assigned in a single loop, since you are only dealing with integer variables)..
You can shuffle the deck by performing a "random shuffle" on the array of 52 integer values of the cards. It's really easy. Look it up.
Fisher-Yates shuffle Scroll down to the pseudo-code in the section titled "The modern algorithm."
I mean, it's OK to try to figure out how to "randomize" the order of elements in an array, but most people's initial efforts don't do it "right" from the point of view of "how random is random" as well as requiring an unknown number of calls to a random function.
Bottom line: Don't do something naive; look it up. It takes something like five lines of Java, and consists of a single pass that always calls a random function exactly 51 times to shuffle a 52 card deck, and has mathematically proven properties of unpredictableness (depending only on the distribution of variates from the random function that gets called).
Or...
Instead of making your own random shuffle method, consider using the
shuffle() method that is conveniently part of the Collections class. You can't plug an array into the
Collections.shuffle() method, but you can shuffle a List of Integers corresponding to a an array of Integers (using Arrays.asList()) with the result is it will shuffle the underlying array. It just flabbers my gast every time I go over it again.
Cheers!
Z