You need to have a 'Deck' of cards or somesuch, and randomly shuffle them, and then deal them out to Players by removing them from the Deck.
Otherwise you can deal the same card twice, which is not possible in real life. (Though larger decks can be used.)
public class Card {
public enum Suit {HEART, DIAMOND, CLUB, SPADE};
public int getValue(); // Ace, Jack, Queen, King encoded as numbers also.
}
public class Deck {
protected List<Card> cardList = new ArrayList();
public void newDeck() {
// clear & add 52 cards..
Collections.shuffle( cardList);
}
public Card deal() {
Card card = cardList.remove(0);
return card;
}
}
If/when you do need to generate random integers, you should use truncation, not rounding. Otherwise the bottom value will have only half its desired probability..
int y = Math.round( x)
0 - 0.49 -> 0 // only half the probability of occurrence!
0.5 - 1.49 -> 1
1.5 - 2.49 -> 2
..
There's no Math
function to truncate, just cast to int
.
int faceValue = (int) ((Math.random() * 13)) + 1;
Or, you can use the Random.nextInt( n) function to do this.
Random rand = new Random();
int faceValue = rand.nextInt( 13) + 1;
Fill in the blanks.