jorgeAChacon
jorgeAChacon

Reputation: 327

how to create a deck of cards constructor

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[] deck; //An implicit set of 52 Playing-Cards
private int cardsInDeck;//Number of cards currently in the deck
private Random dealer;  //Used to rendomly select a card to be dealt

//Constructor
public DeckOfCards()
{
        deck = new boolean[52];
        for (int j = 0; j<deck.length ; j ++)
            deck [j] = false;

Am I heading the right direction?... What do I do after this? I am not very familiar with boolean arrays

Upvotes: 2

Views: 2852

Answers (3)

twain249
twain249

Reputation: 5706

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

  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".

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

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];
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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).

Upvotes: 3

Related Questions