Person1: AS KD 3H 8S 6S 4C 6H TS JD AC 5C JH 5S
Person2: 4D 7H 8C 3C QD 2S TH JC 5H 7D QS 9H KS
etc.
The only way that I thought I could get it to work doesn't. I have no errors in code but get an error on runtime:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at DeckOfCards.deal(DeckOfCards.java:57)
at DeckOfCards.main(DeckOfCards.java:13)
public class DeckOfCards {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"S", "H", "D", "C"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] playerOne = new String[13];
String[] playerTwo = new String[13];
String[] playerThree = new String[13];
String[] playerFour = new String[13];
//Shuffle the cards
shuffle(deck);
deal(deck, suits, ranks, playerOne, playerTwo, playerThree, playerFour);
//Print out the hands
for(int i = 0; i < 52; i++) {
System.out.print("Person 1: " + playerOne[i] + " ");
//}
}
}
//Method for shuffling cards
public static void shuffle(int[]deck) {
//Initialize cards
for (int i = 0; i < deck.length; i++)
deck[i] = i;
//Shuffle the cards for 2-51
for (int i = 51; i > 2; i--) {
//Generate and index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck [i] = deck[index];
deck[index] = temp;
}
//Shuffle card for 0-1
for (int i = 1; i < 2; i++) {
int index = 0 + (int)(Math.random() * 2);
int temp = deck[i];
deck [i] = deck[index];
deck[index] = temp;
}
}
//Deal the cards
public static void deal(int[] deck, String[] suits, String[] ranks, String[] playerOne, String[] playerTwo, String[] playerThree, String[] playerFour) {
for (int i = 0; i < 52; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
String card = rank + suit;
if (i < 13) {
playerOne[i] = card;
}
else if (i < 26) {
playerTwo[i] = card;
}
else if (i < 39) {
playerThree[i] = card;
}
else if (i <52) {
playerFour[i] = card;
}
}
}
}
Advice please? Thank you in advance.

New Topic/Question
Reply



MultiQuote



|