I am trying to program a card game where I must remove a random card from Linked List cardList
and return that card. I populated cardList
with a for loop
public Deck(){
for (int s = 0; s < 3; s++){
for (int n = 0; n < 13; n++){
cardList.add(new Card(numbers[n],suit[s]));
}
}
I am running into errors in my deal()
method
public Card deal() {
int random = (int)(cardList.size() * Math.random());
return cardList.remove(random);
}
public int getSize() {
return cardList.size();
}
These are the errors I'm getting
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at Deck.deal(Deck.java:30)
at Game.<init>(Game.java:31)
at Simulation.simulate(Simulation.java:45)
at Main.main(Main.java:4)
I know the problem is in deal()
because all the other locations lead to it. I also tried this way to make sure there aren't 0s but it still doesn't work.
numOfCards = 52;
public Card deal(){
if (numOfCards > 0){
int min = 0;
int max = numOfCards;
int randomNum = Math.random() * (max - min +1) + min;
numOfCards --;
return cardList.remove(randomNum);
} else{
return null;
}
}
Any help would be appreciated! I've been working on this for hours now ._.
Aucun commentaire:
Enregistrer un commentaire