I have this shuffled card deck and I'm supposed to make it so that it hands out 5 cards to four players. I've been sitting here like an idiot for hours and I'm stuck.
public class Deck {
public static void main(String[] args)
{
String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
// initialize deck
int n = SUITS.length * RANKS.length;
String[] deck = new String[n];
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}
// shuffle
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
// print shuffled deck
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++)
System.out.println(deck[i])
}
}
I am stuck on that last part. I am getting five similar cards for four players. It looks like this:
- Queen of Hearts
- Queen of Hearts
- Queen of Hearts
- Queen of Hearts
- Queen of Hearts
- 10 of Diamonds
- 10 of Diamonds
- 10 of Diamonds
- 10 of Diamonds
- 10 of Diamonds
- 6 of Hearts
- 6 of Hearts
- 6 of Hearts
- 6 of Hearts
- 6 of Hearts
- 10 of Spades
- 10 of Spades
- 10 of Spades
- 10 of Spades
- 10 of Spades
How am I supposed to go at it if my intent is to deal five different cards to four players?
I'm coding in Java, doing arrays and I can't use any java utils.