Posted: 3/25/2013 5:06:57 PM EDT
|
We are doing a bit of a bigger project for class and I have 1 more week to finish it. We are creating a video poker game and so far I have it to where I deal a hand of 5 cards. My problem in this is I can have the same card pop up and I need to get it so that doesn't happen.
I have two programs setup one is running an array string with all 52 cards ex... "A Spades", "2 Spades", "3 Spades" etc.... My other program is ran with two arrays one is an ArrayList<String> with the 4 suit values and the other is an integer array with numbers 1-13. I'll post both tell me which would be easiest one to work with. Later in the program I need to allow the user to choose if he wants to discard any cards, if he does how many he wants to, and deal a final hand. I would like to fix this issue of double cards before I move on. 52 card Array public class PokerHand {
Two array's public class PokerHand {
|
|
I would use a single array method.
The single array would contain the deck. You need to make sure when you are dealing cards they are being removed from the deck array and inserted into the player array. ETA - Instead of having a variable for each card have a single array for each player that contains the cards. |
|
I'd probably have two enums, once for suit and one for card value. Class Card has two instance variables, one for suit and one for face value.
Initialize a deck of cards by looping through suit and face value. probably use an array of cards, length 52 or whatever if using jokers. "shuffle" the deck you just initialized by randomizing the order of the cards. "deal" from the top of the deck and maintain an index to the current top card. |
|
Your program doesn't deal out 5 cards. It "looks" at 5 cards from random locations in the deck. Since you didn't remove the cards, it is always possible to look at the same card again. When you deal a hand, you actually remove the card from the pile to put it into the hand. If you are going to pick a random card as you are currently doing, you need to remember if you already dealt it or not. If it was already dealt, you have to pick again (possibly picking an already dealt card yet again). Another way to look at this problem is to not randomly select cards, but to shuffle the cards in some way. Store the shuffled cards in an array, and simply pull cards off of the end of the array. |
|
I would go with suits x face value. Generate an array using a for loop to keep track of which cards have been "used".
After that, generate a deck array by generating a random suit and random face. If the card is not used, put it in the deck and mark it used. If it's used, just do it again until you have a random deck with all 52 cards "shuffled". Now you can "deal cards" until you are done with the deck. |
|
Here's a function which you may find useful: java.util.Collections.shuffle
What I would do is make a List<String> from your array of card names, call shuffle on it, then whenever you need to "draw" a card, take the first element of the list, then remove element 0 of the list (so that it now represents the remaining deck). HTH. |
|
1. Generate deck. - c[i] = i (Where i = 0..51) (You can use this number as an index into an constant array of card names.)
2. Shuffle deck. - http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle 3. Deal cards. - "pop" cards off the top of the array |
I'm writing this in the Arfcom editor, so you tell me if it even compiles properly. public class Card {
|
|
Quoted:
Here's a function which you may find useful: java.util.Collections.shuffle What I would do is make a List<String> from your array of card names, call shuffle on it, then whenever you need to "draw" a card, take the first element of the list, then remove element 0 of the list (so that it now represents the remaining deck). HTH. If you're doing that, just use a Stack and pop() it off the top. |
