Im trying to create a deck of cards using enums. I already have my enum declared, but I'm stuck in how exactly to create the deck of cards using a boolean array.

So far i tried to initialize my constructor, but I don't know what direction to take now. Any help would be greatly appreciated it. Thank you.

package Cards;

//Class to represent a standard Deck of 52 Playing-Cards
//  The following functionality is provided
//      Default Constructor - creates a complete deck of cards
//      shuffle() - collects all 52 cards into the deck
//      deal() - returns a randomly selected card from the deck
//
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 randomly select a card to be dealt

    //Constructor
    public DeckOfCards()
    {  

            deck = new boolean[ DECK_SIZE ];
            for(PlayingCard.CardSuit suit : PlayingCard.CardSuit.values())
                for(PlayingCard.CardRank rank : PlayingCard.CardRank.values())
                    deck[ cardsInDeck++ ] = true;

    }

    //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()
    {

            return null;
    }

How exactly do you intend to use the boolean array? What is the logical proposition that will be true ort false for each element?
Most people define a Card class, and have an array of Cards.

The boolean is gonna help me when dealing the card from the array. It will be set to true for all of the cards, once i deal a card it will be set to false. Im just kinda confused on where should i crdate the deck of cards.

Most people define a Card class, and have an array of Cards. Then when a Card is dealt you simply remove it from the array. Using an ArrayList<Card> rather than an array of Cards handles removing and resizing for you.

Edited 4 Years Ago by JamesCherrill

Thank you for your feedback, but we have to implement an array rather than an array list. But i could create a new class, and that class will have the array of cards?

That would make perfect Object-Oriented sense - a "Deck" class that has a (private) array of Cards, and provides public methods like :

Deck deck = new Deck();
deck.shuffle();
Card nextCard = deck.dealOneCard();

That way all the mechanisms for keeping track of what cards remain etc will be properly hidden inside that class.

This article has been dead for over six months. Start a new discussion instead.