user2368486
user2368486

Reputation: 1

Im trying to figure out how to deal a card from the deck in my card game class

Im finding this hard since im not allowed to use arrays for the Deck class instead i have to create an object for it like private Deck deck;(you 'll see what i mean in the code)

This method takes a card from the deck and adds it to the player's hand.

Heres the Card, Deck, Game Class

public class Card
{
    public static final String HEARTS = "hearts";
    public static final String DIAMONDS = "diamonds";
    public static final String CLUBS = "clubs";
    public static final String SPADES = "spades";

    private String description;   
    private String suit;      
    private int value;

    public Card()
    {
        description = "Joker";
        suit        = "Spades";
        value       = 0;
    }

    /**
     * Constructor for objects of class Card
     * @param description e.g. "Ten"
     * @param suit e.g. "Hearts"
     * @param value e.g. 10
     */
    public Card(String description, String suit, int value)
    {
        setDescription(description);
        setSuit(suit);
        setValue(value);
    }

    /**
     * Gets the suit.
     * @return suit as a String
     */
    public String getSuit()
    {
        return suit;
    }

    /**
     * Gets the value.
     * @return value as an int
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Gets the description.
     * @return description as a String
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * Sets the suit
     * @param suit e.g."Hearts"
     */
    public final void setSuit(String suit)
    {
        if((suit != null)&& 
            (suit.equalsIgnoreCase(HEARTS)) ||
            (suit.equalsIgnoreCase(DIAMONDS)) ||
            (suit.equalsIgnoreCase(CLUBS))||
            (suit.equalsIgnoreCase(SPADES))){
            this.suit = suit;
        }
        else {
            this.suit = "unknown suit";
        }
    }

    /**
     * Sets the description
     * @param description e.g. "Ten"
     */
    public final void setDescription(String description)
    {
       if(description != null) {
          this.description = description; 
       }
       else {
          this.description = "unknown description";
       }
    }

    /**
     * Sets the value
     * @param value of this card e.g. 10
     */
    public final void setValue(int value)
    {
        if(value > 0 && value <= 10) {
            this.value = value;
        }
        else {
            this.value = 0;
        }
    }

    /**
     * @return string containing description and suit
     */
    public String getInfo()
    {
        return description + " of " + suit;
    }
}

Deck Class:

import java.util.ArrayList;

/**
 * Deck of cards.
 * 
 * 
 *
 */
public class Deck
{
    private ArrayList<Card> deck;

    /**
     * Constructor for objects of class Deck
     * Creates a new container for Card objects
     */
    public Deck()
    {
        deck = new ArrayList<Card>();
    }

    /**
     * Add a card to the deck.
     * @param Card to be added
     */
    public void addCard(Card cardToAdd)
    {
        deck.add(cardToAdd);
    }

    /**
     * Take the first card from the deck.
     * @return Card or null
     */
    public Card takeCard()
    {
        if(deck.isEmpty()) {
            return null; 
        }
        else {    
            Card top = deck.get(0);
            deck.remove(0);
            return top;
            //return  deck.remove(0); // get the top card
        }
    }

    /**
     * Show the contents of the deck.
     */
    public void showDeck()
    {
        for(Card eachCard : deck) {
            System.out.println(eachCard.getDescription()+ 
                            " of " + eachCard.getSuit());
        }
    }

    /**
     * Get the size of the deck
     * @return deck size
     */
    public int getDeckSize()
    {
        return deck.size();
    }

}

Heres where im a little stuck. also with my

public void showHand() 

    is getting a java.lang.NullPointerException
at Game.showHand(Game.java:31)

Game Class:

         import java.util.ArrayList;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game
{
    private InputReader reader;
    private Deck deck;
    private ArrayList<Card> hand;
    private static final int MAX = 5;

    public Game()
    {
        reader = new InputReader();
        deck = new Deck();
        hand = new ArrayList<Card>();
    }

    public void dealCard()
    {
       //<---- not sure what to do with this one
        }
    }

    public void showHand()//<----- not sure if this one is even right
    {                       //compiles fine but when i call the method it gets
        //
        for(Card eachCard: hand){
            System.out.println(eachCard.getDescription()+
                " of " + eachCard.getSuit());
            }
}
}

Any help appreciated!! p.s i know how to add a card from the Card class but not sure how to do it from the Deck class

Upvotes: 0

Views: 2494

Answers (3)

skoll
skoll

Reputation: 232

You're code seems to work fine, I think you should look at how you initilize the deck.

I tried it, adding this :

    deck = new Deck();
    Card myCard = new Card();
    myCard.setDescription("Ace");
    myCard.setSuit("spades");
    myCard.setValue(11);
    Card mySecondCard = new Card();
    mySecondCard.setDescription("king");
    mySecondCard.setSuit("hearts");
    mySecondCard.setValue(4);
    hand = new ArrayList<Card>();

    deck.addCard(myCard);
    deck.addCard(mySecondCard);

    System.out.println("content of deck :");
    deck.showDeck();

    hand.add(deck.takeCard());
    System.out.println("content of hand after taking top card:");
    showHand();

As a kind of initilalizing, in order to see where your exception could be raised (I was quite sceptical), and it perfectly returned:

content of deck : Ace of spades king of hearts

content of hand : Ace of spades

So I suggest you to check your input first of all.

Then you should create ENUM in order to handle each type of card and there values, A shuffling method, a dealing method (you may parametrized it with a number of card to take from the deck), ...

Upvotes: 1

ddarellis
ddarellis

Reputation: 3960

I dont see a controller or something which will randomly mix the cards to the deck, you just init the array of card at the deck but you dont add cards to it.

Upvotes: 0

Carmine Ingaldi
Carmine Ingaldi

Reputation: 976

yeah, now that i've thinked about, "hand" was empty because you fill it from "deck" that is in turn empty. Maybe you can define a method "initDeck" in deck class that fills the deck with random cards, and call it from game constructor. or maybe you already do this out of the game class, but in that case you should pass the deck to game!

Upvotes: 0

Related Questions