【发布时间】:2013-12-30 09:43:05
【问题描述】:
编辑:我在尝试将卡添加到我的 originalDecks 和 dealDecks 时遇到错误。
我想我已经知道如何创建单副牌[在方法 Decks() 中显示],但我的问题是我必须创建多副牌 [n 副牌数 = n*52 数卡]。我不知道如何利用 n 制作同一张卡的倍数。在创建了多个套牌之后,我想我可以弄清楚 shuffle() 和 reset(),但是现在获得多个套牌会给我带来麻烦。
注意:我正在考虑创建一个包含 n 个卡组的数组,但我什至不知道如何开始。
卡类
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank - valid values are 1 to 13
* Suit - valid values are 0 to 3
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws PlayingCardException {
if ((rank < 1) || (rank > 13))
throw new PlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new PlayingCardException("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try
{
Card c1 = new Card(1,3); // A Spades
System.out.println(c1);
c1 = new Card(10,0); // 10 Clubs
System.out.println(c1);
//c1 = new Card(10,5); // generate exception here
}
catch (PlayingCardException e)
{
System.out.println("PlayingCardException: "+e.getMessage());
}
}
}
套牌类
/** class Decks represents : n decks of playing cards
* Use class Card to construct n * 52 playing cards!
*
* Do not add new data fields!
* Do not modify any methods
* You may add private methods
*/
class Decks {
/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;
/* this starts with n*52 cards deck from original deck */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck */
private List<Card> dealDecks;
/* number of decks in this object */
private int numberDecks;
/**
* Constructor: Creates default one deck of 52 playing cards in originalDecks and
* copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
* @throws PlayingCardException
*/
public Decks() throws PlayingCardException
{
// implement this method!
int i, j;
for (i=0;i<4;i++)
{
for(j=1;j<14;j++)
{
Card orcard = new Card(i,j);
originalDecks.add(orcard);
dealDecks.add(orcard);
}
}
}
/**
* Constructor: Creates n decks (52 cards each deck) of playing cards in
* originalDecks and copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
* @throws PlayingCardException
*/
public Decks(int n) throws PlayingCardException
{
// implement this method!
int i, j;
for (i=0;i<4;i++)
{
for(j=1;j<14;j++)
{
Card orcard = new Card(i,j);
originalDecks.add(orcard);
dealDecks.add(orcard);
}
}
}
【问题讨论】:
-
我没有时间给出一个长的答案,但是创建一个 Deck 类。您可能根本不需要 Decks 类,您可以使用 List
的一个实例。