I am trying to deal an x amount of cards to a player depending on how many they already have in their hand (max of 5). But to insure that you don't get dealt the same card twice, or dealt a card that has already been played I've used an if Statement.
Card[] hand = new Card[5];
public void dealCard() {
int cardCount = 0;
Random ran = new Random();
if (cardCount < 5) {
int times = 5 - cardCount;
for (int l = 0; l < times; l++) {
int index = ran.nextInt(deck.length);
if (deck[index] != null) {
hand[cardCount] = deck[index];
deck[index] = null;
cardCount++;
}
}
}
}
It seemed to work at first but every so often the last card, and its always the last card, is null. I was told that it would probably be better to use a while loop instead, but just for the purpose of learning what have I done wrong here.
Aucun commentaire:
Enregistrer un commentaire