0

我发现这很难,因为我不允许对 Deck 类使用数组,而是我必须为它创建一个对象private Deck deck;(你会在代码中看到我的意思)

此方法从牌库中取出一张牌并将其添加到玩家的手上。

继承人的卡片,甲板,游戏类

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

甲板等级:

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();
    }

}

这是我有点卡住的地方。也和我的

public void showHand() 

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

游戏类:

         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());
            }
}
}

任何帮助表示赞赏!ps 我知道如何从 Card 类添加卡片,但不知道如何从 Deck 类添加卡片

4

3 回答 3

1

你的代码似乎工作正常,我认为你应该看看你是如何初始化甲板的。

我试过了,添加了这个:

    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();

作为一种初始化,为了看看你的异常可以在哪里引发(我很怀疑),它完美地返回了:

牌组内容:黑桃王红桃A

手牌内容:黑桃A

所以我建议你首先检查你的输入。

然后,您应该创建 ENUM 以处理每种类型的卡片及其值、洗牌方法、发牌方法(您可以将其参数化为从牌堆中取出的卡片数量),...

于 2013-06-07T23:38:06.727 回答
0

是的,现在我已经考虑过了,“手”是空的,因为您从“甲板”中填充它,而“甲板”又是空的。也许你可以在牌组类中定义一个方法“initDeck”,用随机牌填充牌组,并从游戏构造函数中调用它。或者你可能已经在游戏课外这样做了,但在这种情况下,你应该把套牌传给游戏!

于 2013-06-07T22:11:36.200 回答
0

我没有看到控制器或将卡片随机混合到甲板上的东西,你只是在甲板上初始化卡片阵列,但你没有向其中添加卡片。

于 2013-06-07T23:03:53.453 回答