我正在尝试为纸牌游戏编写模拟。我写了我的类和函数。我将尝试将我的卡片对象保存在 ArrayLists 中。但是主要我不能使用 ArrayList 的方法。我想我在 Representation 类中创建了 ArrayList 类型的 PileOfCards 对象。因为在 PileOfCards 类中,我在构造函数中创建了一个 ArrayList。所以我想让每次我们从 PileOfCards 类创建一个对象时,它的类型都是 ArrayList。我的意思是我想在我的 PileOfCards 对象上使用 ArrayList 方法。但在我的主要内容中,我收到一个错误“对于 PileOfCards 类型的方法 size() 未定义”和 get(3)。我相信 PileOfCards 对象是 ArrayList 类型。为什么我有这样的错误。任何帮助将不胜感激。
// MY CARD CLASS
public class Card
{
/* I assigned variables to numbers because I can use them later easily.
* And they can not be changed
*/
public final static int red = 0;
public final static int black = 1;
public final static int heart = 0;
public final static int spade = 1;
public final static int diamond = 2;
public final static int club = 3;
public final static int ace = 0;
public final static int jack = 10;
public final static int queen = 11;
public final static int king = 12;
public static String values[] = {"A" , "2" , "3" , "4" , "5" , "6" , "7",
"8" , "9" , "10" , "J" , "Q" , "K"};
private int value;
private int suit;
boolean faceUp;
// Create card object with parameters.
Card(int value_in, int suit_in, boolean face_type)
{
value = value_in;
suit = suit_in;
faceUp = face_type;
}
// Make the card face up
public void faceUp()
{
faceUp = true;
}
// Make the card face down
public void faceDown()
{
faceUp = false;
}
// return card is face up or face down
public final boolean cardStatus()
{
return faceUp;
}
// return the value of the card
public final int cardValue()
{
return value;
}
// return the suit of the card
public final int cardSuit()
{
return suit;
}
// return the color of the card
public final int color()
{
if(cardSuit()==heart || cardSuit()==diamond)
{
return red;
}
else
{
return black;
}
}
public String toString() {
return ("Card Suit: "+this.cardSuit()+
" Card Value: "+ this.cardValue() +
" Card Status: "+ this.cardStatus() +
" Color : " + this.color());
}
}
//--------------------------------------------------------------------------------
// MY PILEOFCARDS CLASS
import java.util.*;
public class PileOfCards
{
/* I created pile as ArrayList because the elements of the list will change.
* So using ArrayList is more useful and easier.
*/
public ArrayList<Card> pile;
//create an empty pile
PileOfCards()
{
// Create pile with card objects
this.pile = new ArrayList<Card>();
}
//create a deck with 52 cards
public void makeDeck()
{
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
pile.add(new Card(a, b, false));
}
}
}
// shuffling the cards
public void shuffle()
{
Collections.shuffle(this.pile);
}
// get the top card and remove that. I think the first element of the list as top card
public Card takeTopCard()
{
if(pile.isEmpty())
{
return null;
}
else
{
Card top = pile.get(0);
pile.remove(0);
return top;
}
}
// add a specific card to the pile
public void addCardToPile(Card c)
{
pile.add(c);
}
// determines the pile can take card
public boolean canTakeSuit (Card aCard)
{ if (pile.isEmpty())
return aCard.cardValue() == 0;
Card topCard = pile.get(0);
return (aCard.cardSuit() == topCard.cardSuit()) &&
(aCard.cardValue() == 1 + topCard.cardValue());
}
public boolean canTakeValue (Card aCard)
{ if (pile.isEmpty())
return aCard.cardValue() == 12;
Card topCard = pile.get(0);
return (aCard.color() != topCard.color()) &&
(aCard.cardValue() == topCard.cardValue() - 1);
}
public void selectSuit ()
{ if (pile.isEmpty())
return;
// if face down flip the card
Card topCard = pile.get(0);
if ( topCard.cardStatus() != true)
{
topCard.faceUp();
}
pile.remove(0);
if(canTakeSuit(topCard))
{
pile.add(topCard);
}
else
{
addCardToPile(topCard);
}
}
public void selectValue ()
{ if (pile.isEmpty())
return;
// if face down flip the card
Card topCard = pile.get(0);
if ( topCard.cardStatus() != true)
{
topCard.faceUp();
}
pile.remove(0);
if(canTakeValue(topCard))
{
pile.add(topCard);
}
else
{
addCardToPile(topCard);
}
}
}
//--------------------------------------------------------------------------
// MY REPRESENTATION CLASS
public class Representation
{
// create the main pile. we will deal the cards from here
static PileOfCards mainPile = new PileOfCards();
// create a pile for opening cards. This pile can hold only one card.
static PileOfCards oneCardPile = new PileOfCards();
// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile1 = new PileOfCards();
// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile2 = new PileOfCards();
// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile3 = new PileOfCards();
// create a pile that holds the cards in one suit from ace to king
static PileOfCards oneSuitPile4 = new PileOfCards();
// create the pile that holds one card at the beginning - max. 13 cards
static PileOfCards firstPile = new PileOfCards();
// create the pile that holds 2 cards at the beginning - max. 13 cards
static PileOfCards secondPile = new PileOfCards();
// create the pile that holds 3 cards at the beginning - max. 13 cards
static PileOfCards thirdPile = new PileOfCards();
// create the pile that holds 4 cards at the beginning - max. 13 cards
static PileOfCards fourthPile = new PileOfCards();
// create the pile that holds 5 cards at the beginning - max. 13 cards
static PileOfCards fifthPile = new PileOfCards();
// create the pile that holds 6 cards at the beginning - max. 13 cards
static PileOfCards sixthPile = new PileOfCards();
// create the pile that holds 7 cards at the beginning - max. 13 cards
static PileOfCards seventhPile = new PileOfCards();
}
//-------------------------------------------------------------------------
// MY DEMO CLASS
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Representation.mainPile.makeDeck();
for(int i=0; i<Representation.mainPile.size(); i++)
System.out.println(Representation.mainPile.get(i));
}
}