so far this is what I have
import java.util.Random;
public class DeckOfCards
{
public static final int DECK_SIZE = 52;
//Instance Variables
private boolean[] de
Considering the assignment, you should be storing true
rather than false
in the deck
array. Moreover, I would make it a 2-D array of Booleans - a dimension for the suite, and a dimension for the rank.
private boolean deck[][] = new boolean[13][4];
public DeckOfCards() {
for (int rank = 0 ; rank != 13 ; rank++)
for (int suite = 0 ; suite != 4 ; suite++)
deck[rank][suite] = true;
}
boolean containsCard(int rank, int suite) {
return deck[rank][suite];
}
Myself, I'd use an array of Card objects for my deck variable, not an array of booleans. The primitive boolean variable can only be in one of two states, true or false, whereas a Card must have both a suit (1 of 4 states) and a rank (1 of 13 states).
Based on your comments you have to use a boolean array where true
indicates that the card is there.
First Point:
So when you construct the deck is it filled or empty?
I would assume it would be full, so what should the value of each cell be?
Second Point:
In the constructor you use:
deck = new boolean[52];
Which is perfectly valid but you also have
public static final int DECK_SIZE = 52;
declared so I would assume you should be using DECK_SIZE
where applicable.
Third Point:
You have two fields:
private int cardsInDeck;//Number of cards currently in the deck
private Random dealer; //Used to rendomly select a card to be dealt
Which are not initialized in your constructor (at least not in the part you posted)
Final Point:
The constructor method should not do anything except setup your local fields. So if you fix your deck
initialization and initialize your other fields you should be good for the constructor. The majority of the work in this case will be done in the function that draws a card.
EDIT:
To draw a card you'll have to
drawACard()
that either prints out or returns the card drawndealer
) (save the index)true
/false
in the deck should help here)deck
appropriately"Ace of Clubs"
you can do whatever you need to with thatSo the main part of your implementation will be how to change something where you have index=5
into the String
"Six of Clubs"
.