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).
answered May 15, 2012 at 0:43 Hovercraft Full Of Eels Hovercraft Full Of Eels 285k 25 25 gold badges 263 263 silver badges 381 381 bronze badges@user1166061: you might want to let us know the details of your assignment then because that stipulation confuses me.
Commented May 15, 2012 at 0:45This is what I am asked to do:The required representation stores the cards implicitly; the cards are never explicitly stored. A boolean array of 52 elements corresponding to the 52 playing-cards is used. An element value of true indicates that the corresponding card is in the deck; a value of false indicates that the corresponding card has been dealt, and is not in the deck. All the cards in any suit are represented contiguously, thus, elements 0 – 12 correspond to CLUBS, elements 13 – 25 correspond to DIAMONDS, elements 26 – 38 correspond to HEARTS, and elements 39 – 51 correspond to SPADES.
Commented May 15, 2012 at 0:47@user1166061: OK, then if you want to "fill" the deck in your constructor, what should you be setting all of the boolean values held by the array to?
Commented May 15, 2012 at 0:49 can you rephrase your question please Commented May 15, 2012 at 0:52@user1166061 After what you have done, you have to mark the cards you want to have in the deck as true in the deck array. You should pass the number of the cards you want to have in the deck in the constructor. If cardsInDeck is for example 12, you could use the dealer to generate 12 distinctive integers in the range of 0 to DECK_SIZE-1 and mark the deck variables as true in those positions.
Commented May 15, 2012 at 0:53Considering 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)answered May 15, 2012 at 0:56 Sergey Kalinichenko Sergey Kalinichenko 724k 85 85 gold badges 1.1k 1.1k silver badges 1.6k 1.6k bronze badges
I agree that this is a better approach, but I don't think it's within the bounds of the assignment (based on what the OP said in the comments).
Commented May 15, 2012 at 1:09public DeckOfCards() < deck = new boolean[52]; for (int j = 0; j//Collect all 52 Playing-Cards into the deck public void shuffle() < >//Simulate dealing a randomly selected card from the deck //Dealing from an empty deck results in a RuntimeException public PlayingCard deal()
Commented May 15, 2012 at 1:30@user1166061 Instead of using a 2-D array you can use a regular array: you'd need to work out the math there, but it is doable. For example, you can say that a card
Based on your comments you have to use a boolean array where true indicates that the card is there.
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?
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.
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)
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.
To draw a card you'll have to
So the main part of your implementation will be how to change something where you have index=5 into the String "Six of Clubs" .