Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
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 {

   final private String[] handCards = {"A Spades", "2 Spades", "3 Spades",
       "4 Spades", "5 Spades", "6 Spades", "7 Spades", "8 Spades", "9 Spades",  
       "10 Spades", "J Spades", "Q Spades", "K Spades", "A Clubs", "2 Clubs",
       "3 Clubs", "4 Clubs", "5 Clubs", "6 Clubs", "7 Clubs", "8 Clubs",
       "9 Clubs", "10 Clubs", "J Clubs", "Q Clubs", "K Clubs", "A Hearts",
       "2 Hearts", "3 Hearts", "4 Hearts", "5 Hearts", "6 Hearts", "7 Hearts",
       "8 Hearts", "9 Hearts", "10 Hearts", "J Hearts", "Q Hearts", "K Hearts",
       "A Diamonds", "2 Diamonds", "3 Diamonds", "4 Diamonds", "5 Diamonds",
       "6 Diamonds", "7 Diamonds", "8 Diamonds", "9 Diamonds", "10 Diamonds",
       "J Diamonds", "Q Diamonds", "K Diamonds"};
   

   final private Random deal = new Random();
   
   private String card1 = handCards[deal.nextInt(handCards.length)];
   private String card2 = handCards[deal.nextInt(handCards.length)];
   private String card3 = handCards[deal.nextInt(handCards.length)];
   private String card4 = handCards[deal.nextInt(handCards.length)];
   private String card5 = handCards[deal.nextInt(handCards.length)];
 
   public PokerHand(){
       
}
       public void getHand(){

           System.out.println(card1);
           System.out.println(card2);
           System.out.println(card3);
           System.out.println(card4);
           System.out.println(card5);



Two array's
public class PokerHand {

   final private ArrayList<String> suits = new ArrayList<>();
   final private int[] cardNum = {1,2,3,4,5,6,7,8,9,10,11,12,13};
   final private Random deal = new Random();
   final private Random cardSuit = new Random();
   
   //creates randnom card numbers
   private int card1 = deal.nextInt(cardNum.length) + 1;
   private int card2 = deal.nextInt(cardNum.length) + 1;
   private int card3 = deal.nextInt(cardNum.length) + 1;
   private int card4 = deal.nextInt(cardNum.length) + 1;
   private int card5 = deal.nextInt(cardNum.length) + 1;
   
   public PokerHand(){
       suits.add("Spades");
       suits.add("Clubs");
       suits.add("Hearts");
       suits.add("Diamonds");
}
       public void getHand(){
           //creates random card suits
           int suit1 = cardSuit.nextInt(suits.size());
           int suit2 = cardSuit.nextInt(suits.size());
           int suit3 = cardSuit.nextInt(suits.size());
           int suit4 = cardSuit.nextInt(suits.size());
           int suit5 = cardSuit.nextInt(suits.size());
           //random hand cards
           String playerCard1 = card1 + " " + suits.get(suit1);
           String playerCard2 = card2 + " " + suits.get(suit2);
           String playerCard3 = card3 + " " + suits.get(suit3);
           String playerCard4 = card4 + " " + suits.get(suit4);
           String playerCard5 = card5 + " " + suits.get(suit5);
           
           
           System.out.println(playerCard1);
           System.out.println(playerCard2);
           System.out.println(playerCard3);
           System.out.println(playerCard4);
           System.out.println(playerCard5);

3/25/2013 5:12:02 PM EDT
[#1]
3/25/2013 5:15:12 PM EDT
[#2]
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.
3/25/2013 5:15:14 PM EDT
[#3]
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.
3/25/2013 5:17:10 PM EDT
[#4]
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.
3/25/2013 5:18:08 PM EDT
[#5]
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.

3/25/2013 5:20:07 PM EDT
[#6]
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.

3/25/2013 5:22:07 PM EDT
[#7]
Make another array and call it playerHand. When you pick a card, move it from the deck to playerHand.

This will make your discard logic easier too, as it will come from playerHand.
3/25/2013 5:24:31 PM EDT
[#8]
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
3/25/2013 5:27:58 PM EDT
[#9]
I'm writing this in the Arfcom editor, so you tell me if it even compiles properly.  

public class Card {

private String number;
private String suit;
public Card(String number, String suit) {
this.number = number;
this.suit = suit;
}
//Getters
}

import java.util.Random;
public class Driver {
ArrayList<Card> deck;
public static void main(String[] args) {
String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
String[] suits = {"Hearts", "Stars", "Clovers", "Horseshoes", "Blue Moons", "Pots", "Red Balloons"};
deck = new ArrayList<Card>();
for(int i = 0; i < numbers.length; i++) {
for(int j = 0; j < suits.length; j++) {
deck.add(new Card(number[i], suit[j]));
}
}
}

public Card drawCard() {
if(deck.isEmpty()) {
return null;
}

Random gen = new Random();
int posit = gen.nextInt(deck.size());
Card result = deck.getElementAt(posit);
deck.removeElementAt(posit);
return result;
}

}
3/25/2013 5:30:40 PM EDT
[#10]
If they've shown you enumerations, use those instead of strings for the suit and face value.
3/25/2013 5:32:23 PM EDT
[#11]
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.