我发现这很难,因为我不允许对 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 类添加卡片