Reputation: 1
Making a deck of cards in Java for a blackjack clone with OOP, When populating the deck it breaks and I cant see why. There are no suits for the game. I'm trying to get the get it to properly populate the deck but it keeps getting stuck with 48 cards in the deck. Can't figure out why so any help would be greatly appreciated
import java.util.ArrayList;
import java.util.Random;
public class Deck {
ArrayList<Integer> deckList = new ArrayList<Integer>();
Deck(){
newDeck();
}
public boolean duplicateNum(int CardType){
int counter = 0;
if (deckList.size() < 4){
return false;
}
for (int x = 0; x < deckList.size(); x++){
if (deckList.get(x) == CardType){
counter++;
}
}
if (counter >= 4){
return true;
}
return false;
}
public void newDeck(){
for (int x = 1; x <= 52; x++){
addCard();
}
}
public void addCard(){
Random deckFiller = new Random();
boolean x = true;
while (x){
int cardAdder = deckFiller.nextInt(12) + 1;
if (!duplicateNum(cardAdder)){
deckList.add(cardAdder);
} else {
continue;
}
System.out.print(deckList.size() + "-");
System.out.println(cardAdder);
}
}
}
Upvotes: 0
Views: 233
Reputation: 1648
There are several problems in your addCard() method:
while(){}
loop never ends as x
is never set to false
and there's no break
clause in itnextInt(int bound) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
, so you have to set the bound to 13
instead of 12
Here's the fixed method:
public void addCard() {
Random deckFiller = new Random();
while (true) {
int cardAdder = deckFiller.nextInt(13) + 1;
if (!duplicateNum(cardAdder)) {
deckList.add(cardAdder);
System.out.print(deckList.size() + "-");
System.out.println(cardAdder);
break;
}
}
}
If you want to improve the deck initialization, you could add all 52 cards to it and shuffle it with Collections.shuffle() method:
public void newDeck() {
for (int i = 1; i <= 13 ; i++) {
for (int j = 0; j < 4; j++) {
deckList.add(i);
}
}
Collections.shuffle(deckList);
// for (int i = 0; i < deckList.size(); i++) {
// System.out.println(i+1 + "-" + deckList.get(i));
// }
}
Upvotes: 2
Reputation: 1460
First off, I would suggest using Suit/Face, or at a minimum (0-3, 0-12) loops to populate the 52 cards. I get that blackjack has no suits, but if you're working on an OOP project, you should probably have a List<Card> cards
instead of a List<Integer> deckList
.
Looking at your code, there are a couple things:
while(x)
even exists. You're doing a continue and nothing is actually setting x = false
in what you've posted.while(x)
then this is definitely what is happening, as you're letting i++
happen regardless of an added card or a failure.With regards to creating the deck:
If your'e set on not using Suit/Face to create cards, you could probably just:
for (i = 0..3)
for (j = 0..12)
position = random(0-deck.size)
deck.add(position,j); // Randomize where its put
which is pretty much doing the same thing as randomly picking numbers and putting them in order, although it still isn't actually what shuffling does, where you can shuffle a deck of cards multiple times (split them in half and randomly insert groups from one half into another half.
Upvotes: 1