0

Deck クラスに配列を使用することは許可されていないため、代わりにオブジェクトを作成する必要があるため、これは難しいと思いprivate Deck deck;ます (コードで私が何を意味するかがわかります)。

このメソッドは、デッキからカードを 1 枚取り、プレイヤーの手札に追加します。

カード、デッキ、ゲームクラスはこちら

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

0

ええ、今考えてみると、「手札」が空だったのは、空の「山札」から埋めたからです。たぶん、デッキ クラスでメソッド「initDeck」を定義して、デッキをランダムなカードで満たし、ゲーム コンストラクターから呼び出すことができます。または、既にゲーム クラスの外でこれを行っているかもしれませんが、その場合はデッキをゲームに渡す必要があります。

于 2013-06-07T22:11:36.200 に答える
0

カードをデッキにランダムに混合するコントローラーや何かが表示されません。デッキでカードの配列を初期化するだけで、カードを追加しません。

于 2013-06-07T23:03:53.453 に答える