Hi all,
I'm working through the java tutorials on sun.com and trying to create a card class and a deck class which populates with card objects. Having trouble getting things running and I'm not sure where the problem is, pasted below are the card class and deck class, I'd appreciate if someone could take a look and let me know if they can see the problem.
The main methods in each class are just for testing purposes. SingleCard seems to work fine and my problem with the deck class is that it seems to default to returning the joker result(end of deck)
I'm just trying to accomplish a full draw through the deck at the moment with program ending at deck end(I am aware that the code as it stands draws only 1 card from the deck, I was waiting to get 1 card drawn successfully before putting in a loop to iterate through the deck)
Card class -
package cardDeck;
import java.util.*;
public class SingleCard {
private Random rnd = new Random();
private final String[] rank = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King","Joker"};
private final String[] suit = {"Diamonds","Hearts","Clubs","Spades"};
private static String pickedRank;
private static String pickedSuit;
protected SingleCard()
{
pickedRank = rank[rnd.nextInt(13)];
pickedSuit = suit[rnd.nextInt(4)];
}
protected SingleCard(int selectedSuit)
{
if (selectedSuit >= 0 && selectedSuit <=3)
{
pickedRank = rank[rnd.nextInt(13)];
pickedSuit = suit[selectedSuit];
}
else
{
pickedRank = rank[rnd.nextInt(13)];
pickedSuit = suit[0];
}
}
protected SingleCard(int selectedSuit, int selectedRank)
{
if (selectedRank >= 0 && selectedRank <=12)
{
pickedRank = rank[selectedRank];
pickedSuit = suit[selectedSuit];
}
else
{
pickedRank = rank[rnd.nextInt(13)];
pickedSuit = suit[0];
}
}
protected String getSuit()
{
return pickedSuit;
}
protected String getRank()
{
return pickedRank;
}
/**
* @param args
*/
public static void main(String[] args)
{
new SingleCard();
System.out.println("You have picked the " + pickedRank + " of " + pickedSuit + ".");
}
}
Deck Class
package cardDeck;
import java.util.*;
public class FullDeck
{
private int noOfCards = 52;
private SingleCard[][] deck = new SingleCard[4][13];
private int[][] shuffleDeckControlArray = new int[4][13];
private SingleCard[] shuffledDeck = new SingleCard[52];
private int topOfDeck = 0;
private Random rnd = new Random();
private static SingleCard joker = new SingleCard(0, 13);
/*randomise deck with second array of 1's and 0's to mark drawn and not, use local noofcards to load
* 52 slot 1d array called shuffleddeck then use the end of this array to deal the deck from ,
* shortening the number of cards as they are drawn using the noofcards static variable, if redealing same deck
* in order set no of cards to 52 again.Complete drawcard method
*/
protected FullDeck()
{
int noOfUnshuffledCards = 52;//control for randomly picking card order to add to deck
for(int i = 0; i < 4;i++) //creates a new 52 card deck, ordered
{
for(int j = 0; j < 13; j++)
{
deck[i][j] = new SingleCard(i,j);
}
}
for(int k = 0; k < 4; k++) //populate the control array with 0's
{
for(int l = 0; l < 13; l++)
{
shuffleDeckControlArray[k][l] = 0;
}
}
while(noOfUnshuffledCards != 0)
{
boolean cardPicked = false;
while(cardPicked == false)
{
int m = rnd.nextInt(4);
int n = rnd.nextInt(13);
if(shuffleDeckControlArray[m][n] == 0)
{
shuffledDeck[topOfDeck] = deck[m][n];
topOfDeck ++;
noOfUnshuffledCards --;
shuffleDeckControlArray[m][n] = 1;
cardPicked = true;
}
}
}
}
protected SingleCard drawCard()
{
if(noOfCards != 0)
{
noOfCards --;
SingleCard drawnCard = shuffledDeck[topOfDeck];
topOfDeck --;
return drawnCard;
}
else
{
return joker;
}
}
public static void main(String args[])
{
FullDeck myDeck = new FullDeck();
if(myDeck.drawCard() != joker)
{
System.out.println(myDeck.drawCard().getRank() + " of " + myDeck.drawCard().getSuit());
}
else
{
System.out.println("End of Deck");
System.exit(0);
}
}
}