how to create a deck of cards constructor

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:45

This 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:53

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)
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:09

public 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 is at deck[rank*4+suite] or deck[suite*13+rank] , it does not matter much.

Commented May 15, 2012 at 2:16

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

  1. Create a function drawACard() that either prints out or returns the card drawn
  2. Randomly choose a card from the deck (using the random generator dealer ) (save the index)
  3. Check to see if card is still in the deck (The true / false in the deck should help here)
  4. Once you have a card that's still in the deck you'll have to update that card location by setting the location in deck appropriately
  5. You'll also have to update the cardsInDeck variable appropriately
  6. Now you have the index of the card from which you can generate what card that is.
  7. Since you now the order of the cards in the deck you can determine from the index alone what card you drew
  8. Once you have the corresponding string something like "Ace of Clubs" you can do whatever you need to with that

So the main part of your implementation will be how to change something where you have index=5 into the String "Six of Clubs" .