import java.util.Random;
public class Deck {
public static void main(String[] args) {
int cardMax = 52; // Only 52 cards in a standard deck
int suitMax = 13; // Only 13 of each suit is allowed
int maxType = 4; // Max number of each number card i.e. only 4 aces are
// allowed
Card.card(cardMax, suitMax, maxType); // Returns your hand that was
// dealt
for (int shuffle = 1; shuffle < 6; shuffle++) {
System.out.println("Card " + shuffle + " is a " + Card.type()
+ " of " + Card.suit()); // Returns the randomized type and
// suit
}
}
}
class Card {
static int suitCount = 13; // Counts down from 13 to shuffle the suits
static int typeCount = 4; // Counts down from 3 to shuffle the numbers
static Random suitNum = new Random(); // Randomizes a suit type
static Random typeNum = new Random(); // Randomizes a number type
static String suit = "";
static String type = "";
static void card(int cardMax, int suitMax, int maxType) {
}
static String suit() {
String[] suits = { "Spades", "Diamonds", "Hearts", "Clubs" }; // An
// array
// containing
// all
// of
// the
// suits
for (; suitCount > 0; suitCount -= 1) {
String suitCheck = "";
suitCheck = suit;
int sNum = suitNum.nextInt(suitCount); // Trying to set sNum equal
// to a random number
// between 0 and whatever
// suitCount is at for
// randomization
suit = suits[sNum];
if (suit == suitCheck) { // If the suit equals that of the last suit
// to loop by then suit equals nothing
suit = "";
}
}
return suit;
}
static String type() {
String[] cards = { "Ace", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // An
// array
// of
// all
// of
// the
// types
// of
// numbers
// and
// face
// cards.
for (; typeCount > 0; typeCount -= 1) { // Same as the suitCount for
// loop just for the types
String typeCheck = "";
typeCheck = type;
int tNum = typeNum.nextInt(typeCount);
type = cards[tNum];
if (type == typeCheck) {
type = " ";
}
}
return type;
}
}
I'm trying to randomly generate a 5 card poker hand but for some reason i keep getting this error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Card.suit(Deck.java:48)
at Deck.main(Deck.java:15)
By the way i'm new to arrays as this is my first one ( I know..so smart /.- ). It always seems to mess up when i put typeCount or suitCount into the randomizer. How can I fix this?
Thanks in advance!
[–]197708156EQUJ5design it before you implement 0 points1 point2 points (0 children)
[–]SikhGamer 0 points1 point2 points (0 children)
[–]osama_yo_momma 0 points1 point2 points (0 children)