卡-甲板-Hand Java Project

时间:2018-06-26 02:59:59

标签: java

我知道每当有人在这里进行作业时,每个人都会对此表示怀疑,但是我已经没有选择余地了,可以真正使用一些指导。我有一个项目,我必须创建一副纸牌并允许用户选择手的大小,然后用随机纸牌填充该手并将其显示给用户。我已经找到了很多使用ArrayLists的答案,但是我需要一个数组,而且我已经尝试了所有已知的知识,并且我的代码要么完全错误,要么抛出了很多错误。 所以这是我遇到的问题:

1)Hand类中的addCard方法可用于一次将一个Card对象添加到hand数组中,直到其满为止。每次将Card对象添加到Hand时,只要有足够的空间可容纳到Hand中,它就应该递增cardsInHand计数器。

这是Hand类的代码:

public class Hand
{

   private int handSize;         //Holds the size of the hand
   private int cardsInHand;      //Holds the number of cards allowed in the hand
   private Card[] hand;          //Array of card objects

   public Hand()
   {
      this.handSize = 5;
      this.cardsInHand = 0;
      this.hand = new Card[52];
   }//end Default Constructor

   public Hand(int handSize)
   {
      this.handSize = handSize;
   }//end Parameterized Constructor

   public Hand(Hand handIn)
   {
      this.handSize = handIn.handSize;
      this.cardsInHand = handIn.cardsInHand;
      this.hand = handIn.hand;
   }//end Copy Constructor

   public void addCard(Card card)
   {
      hand[card]; //--> throws a type mismatch exception (change card param to an int) 

   }//end addCard()

   public int getHandSize()
   {
      return handSize;
   }

   public void setHandSize(int handSize)
   {
      this.handSize = handSize;
   }

   public int getCardsInHand()
   {
      return cardsInHand;
   }

   public void setCardsInHand(int cardsInHand)
   {
      this.cardsInHand = cardsInHand;
   }

   public Card[] getHand()
   {
      return hand;
   }

   public void setHand(Card[] hand)
   {
      this.hand = hand;
   }

   public String toString()
   {
      String msg = "";

      return msg;
   }//end toString()

}//end class

我的addCard方法非常简陋,我真的可以使用一些帮助来填充它,以便我的程序正常工作。任何帮助,甚至指向正确方向的指示,将不胜感激!

2 个答案:

答案 0 :(得分:1)

  

我的addCard方法非常简陋,我真的可以使用一些帮助来填充它,以便我的程序正常工作。任何帮助,甚至指向正确方向的指示都将不胜感激

有时候最好的办法是停下来,关闭屏幕,拿起笔和纸,然后不加任何代码地将其拧紧。尝试理解问题并弄清楚逻辑。

基本上,您有一系列可以在其中放入Card的存储桶。在将Card放入存储桶之前,您需要知道是否有可用的免费存储桶。

如果有,则需要将Card添加到下一个可用存储桶(应由cardsInHand指向)并递增cardsInHand

您的错误是因为您尝试使用Card引用“存储桶”,但是只能通过索引(数字)引用“存储桶”,所以...

hand[card];

应该更像是...

hand[cardsInHand] = card;

但只有在确定是否有可用的“存储桶”之后,您才应在此语句之后增加cardsInHand

我也会担心您的构造函数

public Hand(int handSize)
{
   this.handSize = handSize;
}//end Parameterized Constructor

不会初始化hand,因此将是null。更好的解决方案是在可能的情况下使用现有的构造函数来构建通用的“初始化”路径

public Hand() {
    this(5);
}//end Default Constructor

public Hand(int handSize) {
    this.handSize = handSize;
    this.cardsInHand = cardsInHand;
    this.hand = new Card[handSize];
}//end Parameterized Constructor

public Hand(Hand handIn)
{
   this.handSize = handIn.handSize;
   this.cardsInHand = handIn.cardsInHand;
   this.hand = handIn.hand;
}//end Copy Constructor

令人担忧,因为某些外部类可能会对handIn的{​​{1}}进行更改,并且该更改也将反映在此实例中(因为它们指向同一数组) )。

“复制”构造函数应该对数据进行“复制”。实际上,这可能应该是“深层”副本,因此对hand的任何更改也不会与Card混淆,但我将从一个简单的“浅层”副本开始开始

Hand

答案 1 :(得分:0)

@MadProgrammer已经给出了更好的答案,我只想简单一点。了解使用Java进行的OP项目分配之后,我想对Hand类的设计进行一些评论。

任务清楚地表明,用户可以选择具有自定义尺寸的手,然后用户将卡添加到手中,直到手满为止。因此,我将提出如下的Hand类设计。

public class Hand {

    private int size; // Hold the amount of card can be hold by hand.
    private int counter; // Count how many card added.
    private Card[] cards; // The card on hand.

    public Hand(int size) {
        this.counter = 0;
        this.size = size;
        this.cards = new Card[size];
    }

    public void addCard(Card card) {
        if (this.counter > this.size) {
            throw new IllegalStateArgument("The hand is full of card!");
        }
        this.cards[this.counter] = card;
        this.counter++;
    }

    public String show() {
        StringBuilder result = new StringBuilder("The card on hand is: \n");
        for (int i=0; i<this.size; i++) {
            result.append(this.cards[i].toString()).append("\n");
        }
        return result.toString();
    }
}

在我看来,Hand类更容易理解并从任务中实现Hand目的。但是,只需将其用作参考并编写您容易理解的代码即可。