我想弄清楚如何在我的纸牌游戏课中从牌组中处理一张牌

时间:2013-06-07 21:32:00

标签: java bluej

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

这个方法需要一个 卡片从牌组中添加到玩家手中。

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

甲板课程:

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

任何帮助表示赞赏!! p.s我知道如何从Card类中添加卡但不知道如何从Deck类中添加卡

3 个答案:

答案 0 :(得分: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();

作为一种初始化,为了看到你的异常可以被提出的地方(我非常怀疑),它完全回归:

甲板内容: 黑桃王牌 心之王

手的内容: 黑桃王牌

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

然后你应该创建ENUM以便处理每种类型的卡和那里的值,一个改组方法,一个处理方法(你可以用卡片中的一些卡进行参数化),......

答案 1 :(得分:0)

是的,既然我已经想过,“手”是空的,因为你从“甲板”中填充它,而甲板又是空的。也许你可以在deck类中定义一个方法“initDeck”,用随机卡填充牌组,并从游戏构造函数中调用它。或者你可能已经从游戏课程中做到了这一点,但在这种情况下你应该通过游戏套牌进行游戏!

答案 2 :(得分:0)

我没有看到一个控制器或什么会随机将卡片混合到卡座上,你只需在卡座上添加卡片阵列,但你不会添加卡片。