我正在尝试创建一种处理一副牌的交易方法。
import java.util.List;
import java.util.ArrayList;
/**
* The Deck class represents a shuffled deck of cards.
* It provides several operations including
* initialize, shuffle, deal, and check if empty.
*/
public class Deck {
/**
* cards contains all the cards in the deck.
*/
private List<Card> cards;
/**
* size is the number of not-yet-dealt cards.
* Cards are dealt from the top (highest index) down.
* The next card to be dealt is at size - 1.
*/
private int size;
/**
* Creates a new <code>Deck</code> instance.<BR>
* It pairs each element of ranks with each element of suits,
* and produces one of the corresponding card.
* @param ranks is an array containing all of the card ranks.
* @param suits is an array containing all of the card suits.
* @param values is an array containing all of the card point values.
*/
public Deck(String[] ranks, String[] suits, int[] values) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
cards = new ArrayList<Card>();
int n = 0;
for(int i = 0; i < ranks.length - 1; i++)
for(int j = 0; j < suits.length - 1; j++)
for(int k = 0; k < values.length - 1; k++)
{
if(n < cards.size())
cards.add(n + 1, new Card(ranks[i],
suits[j], values[k]));
}
this.size = 52 - cards.size();
shuffle();
}
/**
* Determines if this deck is empty (no undealt cards).
* @return true if this deck is empty, false otherwise.
*/
public boolean isEmpty() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if(size() == 0)
return true;
else
return false;
}
/**
* Accesses the number of undealt cards in this deck.
* @return the number of undealt cards in this deck.
*/
public int size() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
return this.size;// the number of undealt cards in the deck
}
/**
* Randomly permute the given collection of cards
* and reset the size to represent the entire deck.
*/
public void shuffle() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
}
/**
* Deals a card from this deck.
* @return the card just dealt, or null if all the cards have been
* previously dealt.
*/
public Card deal() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if(isEmpty())
return null;
else
{
this.size++;
return cards.get(size - 1);
}
}
/**
* Generates and returns a string representation of this deck.
* @return a string representation of this deck.
*/
@Override
public String toString() {
String rtn = "size = " + size + "\nUndealt cards: \n";
for (int k = size - 1; k >= 0; k--) {
rtn = rtn + cards.get(k);
if (k != 0) {
rtn = rtn + ", ";
}
if ((size - k) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}
rtn = rtn + "\nDealt cards: \n";
for (int k = cards.size() - 1; k >= size; k--) {
rtn = rtn + cards.get(k);
if (k != size) {
rtn = rtn + ", ";
}
if ((k - cards.size()) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}
rtn = rtn + "\n";
return rtn;
}
----------
}
答案 0 :(得分:1)
else
{
this.size++;
return cards.get(size - 1);
}
看起来对我很怀疑。您似乎正在尝试访问nth
数组的n-size
元素。
一旦你有了一个大小,你就不能在没有向列表添加新元素的情况下增加它。
答案 1 :(得分:0)
这条线看起来很可疑:
this.size = 52 - cards.size();
由你自己的Javadoc:
尺寸是未发牌的数量
所以说在for循环之后,List
中有52张牌。您的尺寸设置为52 - 52 = 0
,这意味着零牌都不存在,或者更确切地说所有牌都是在做任何事情之前处理的。
this.size
应初始化为:
this.size = cards.size();
我还建议将其重命名,因为它不是非常具有描述性,但可能超出了您“允许”做的范围*(它会查看您是否填写了骨架代码)。我会称之为:
private int cardsRemaining;
因为此值是尚未处理卡的数量,您需要在交易时减少。 已发送的数量已经向上, undealt 卡的数量已经向下。
public Card deal() {
if(isEmpty()) {
return null;
}
else
{
this.size--;
return cards.get(size - 1);
}
}
*众所周知,教育工作者的代码总是100%完美无瑕,在任何情况下都不能被质疑或改进!