The error occurs in the last line of this class where I'm trying to print the number of the first card in the deck and I'm not really sure why.
public class CardTricks {
public static void main (String[] args){
Deck newdeck = new Deck();
newdeck.construct();
newdeck.shuffle();
System.out.println(newdeck.deck[0].Number);
}
}
This is the class for the card with the main purpose to give the card a suite attribute and a number attribute.
public class Card {
String Suite;
int Number;
}
This is the deck class, functions in this class are used to create the deck and shuffle it.
public class Deck {
Card[] deck;
public void construct(){
deck = new Card[52];
String[] possuite = new String[4];
possuite[0] = "Hearts";
possuite[1] = "Diamonds";
possuite[2] = "Clubs";
possuite[3] = "Spades";
int x = 0;
while (x < 4){
String suite = possuite[x];
x++;
int number = 1;
System.out.println(suite);
while (number < 14){
deck[number-1] = new Card();
deck[number-1].Suite = suite;
deck[number-1].Number = number;
number++;
}
}
}
public void shuffle(){
int x;
int y;
int z = 0;
while (z < 10000){
x = (int)(Math.random()*52);
y = (int)(Math.random()*52);
Card a = deck[y];
Card b = deck[x];
deck[x] = a;
deck[y] = b;
z++;
}
}
}
number < 14-> only first 13 cards will be generated; after shuffling eventuallydeck[0]is one of the ones that was not created. Your loops are confusing, first one starts with index 0, the second with 1 - you should stick with one standard. Also I would suggest using a third index for thedeck, that is, one for suite, second for number and 3rd to access the deck...and maybe aforloop is more readable.