samedi 31 janvier 2015

Deck of cards using a 3 dimensional array in Java

I'm making a deck of cards with a 3 dimensional array. But when I call the random card method, it says the card array has a null value even the I should be filling it with a double for loop?


I've looked up other posts on cards, but they are usually a 2 D or two different arrays, not 3D. Thanks for any of you time. If I'm way off in my coding/thoughts/post, please let me know, I'm pretty new to Java and stack.



import java.util.Random;
import java.util.Arrays;
public class Deck {

String Hearts, Diamonds, Spades, Clubs;
private String Suit;
private int Deck [][][];
private int card [][];


public void FreshDeck()
{
Deck = new int [][][]
{{{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1}}, //Hearts
{{1,2},{2,2},{3,2},{4,2},{5,2},{6,2},{7,2},{8,2},{9,2},{10,2},{11,2},{12,2}}, //Diamonds
{{1,3},{2,3},{3,3},{4,3},{5,3},{6,3},{7,3},{8,3},{9,3},{10,3},{11,3},{12,3}}, //Spades
{{1,4},{2,4},{3,4},{4,4},{5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{12,4}} //Clubs
};
}

private void setValue(int index, int value)
{
card[index][0] = value;
}
private void setSuit(int index, int suit)
{
card[index][1] = suit;
}
public int Value(int index)
{
return card[index][0];
}
public int Suit(int index)
{
return card[index][1];
}

public void setRandomCard()
{
Random randomCard = new Random();
int randomInt = randomCard.nextInt(51);
for (int i=0; i<12; i++)
{
for (int j=0; j<4; ++j)
{
card[i][j] = Deck[randomInt][i][j];
}
}

}
}




Aucun commentaire:

Enregistrer un commentaire