Search code examples
javapoker

I am having difficulty with my deal method


In my PokerDeck class I cannot get my deal method to run without producing duplicates. Can anyone tell me what I am doing incorrectly?

import java.util.*;

public class PokerDeck {
    public static final int NUMBER_OF_CARDS = PokerCard.NUMBER_OF_SUITS * PokerCard.NUMBER_OF_RANKS;
    // Instance Variables
    private boolean[] deck;
    private int numberOfCardsInDeck;

    // Constructor
    public PokerDeck()

    {
        deck = new boolean[NUMBER_OF_CARDS];
        for (int j = 0; j < deck.length; j++)
            for (PokerCard.CardSuit suit : PokerCard.CardSuit.values())
                for (PokerCard.CardRank rank : PokerCard.CardRank.values())
                    deck[j] = true;

    }

    // Accessor
    public int getNumberOfCardsInDeck() {
        numberOfCardsInDeck = NUMBER_OF_CARDS;
        return this.numberOfCardsInDeck;

    }

    // Mutator:
    // Return all 52 PokerCards to this PokerDeck
    public void shuffle() {
        for (int i = 0; i < deck.length; i++) {
            int index = (int) (Math.random() * deck.length);
            deck[i] = deck[index];
        }

    }

    // Mutator:
    // Return a randomly selected PokerCard from this PokerDeck
    // Update the state of this PokerDeck to reflect removal of
    // the selected PokerCard
    // Exception thrown if this PokerDeck is "empty"

    public PokerCard deal() {
        Random dealer = new Random();
        int ran = dealer.nextInt(deck.length);
        if (numberOfCardsInDeck == 0)
            throw new RuntimeException("Empty Deck");

        if (deck[ran] == false)
            ran = dealer.nextInt(deck.length);

        deck[ran] = false;
        int suit = ran / 13;
        int rank = ran % 13;

        return new PokerCard(PokerCard.CardSuit.values()[suit], PokerCard.CardRank.values()[rank]);
    }
}

I tried at least five different code and all of them either do not run or they run and return duplicates.


Solution

  • There is a lot of things wrong with this code. Your problem is, because you are randoming more and more instead of using te ran variable you declared at the beggining of deal method. After you declared your ran variable, don't do dealer.nextInt(deck.length), replace it with ran.

    Your shuffling is not really shuffling, check this: Random shuffling of an array.

    Comparing (like if(something == false)) should be done with ==, you are doing it with = (but you probably just pasted it wrong).

    In your getter this numberOfCardsInDeck = NUMBER_OF_CARDS; is wrong, unless you wanted to 'refresh' a deck instead of just get a number of cards, but I don't think so, remove that line.

    Triple for in your constructor is wrong, you need only the first one, it doesn't make a difference here (except performance), but fix it.

    Also I suggest rewriting all this, do a Card object (class) with suit and rank enums, make a Deck object with List of Card objects, make a deck.getCard() method that removes the last card from the deck, make the deck.shuffle() method that shuffles deck (you will have a List of Cards now, not an array, so you will be able to do Collections.shuffle(cardList), simple).

    Also, change this:

    if (deck[dealer.nextInt(deck.length)] == false)
                dealer.nextInt(deck.length);
    

    To something like this:

    while (deck[ran] == false)
                ran = dealer.nextInt(deck.length);