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.