Java 卡片构造器
我对如何正确地安排这门课感到困惑。我不知道如何创建一个具有所有这些混合值的ArrayList…任何帮助都将不胜感激Java 卡片构造器,java,constructor,Java,Constructor,我对如何正确地安排这门课感到困惑。我不知道如何创建一个具有所有这些混合值的ArrayList…任何帮助都将不胜感激 public StandardCardImpl(Suit suit, int rank, StandardCardImpl deck) { face_up = false; List<Card> cards = new ArrayList<Card>(DeckSize); cards.add(suit,rank,deck);
public StandardCardImpl(Suit suit, int rank, StandardCardImpl deck) {
face_up = false;
List<Card> cards = new ArrayList<Card>(DeckSize);
cards.add(suit,rank,deck);
if (rank < 1 || rank > KING) {
throw new RuntimeException("Rank is out of range for standard card");
}
Iterator iterator = cards.iterator();
this.suit = suit;
this.rank = rank;
this.deck = deck;
}
public StandardCardImpl(西服、国际军衔、StandardCardImpl甲板){
正面朝上=错误;
列表卡=新阵列列表(甲板尺寸);
卡片。添加(套装、军衔、牌组);
如果(等级<1 | |等级>国王){
抛出新的RuntimeException(“等级超出标准卡的范围”);
}
迭代器迭代器=cards.Iterator();
这套衣服;
这个.等级=等级;
this.deck=甲板;
}
也许您需要以下内容
cards.add(new Card(suit,rank,deck));
i、 e使用适当的成员创建一个新的
卡对象,然后将其添加到数组列表中。重新开始。什么是卡
?它是一个有等级和套装的物体。因此,您需要:
名为套装的enum
等级的一系列数字。大概2到14岁(10岁,杰克,女王,国王,王牌)。
- 或者一个枚举:2,3,…,A。这可能有点过分
有西装和军衔的一种等级卡
牌
不应该有二传手,因为你不希望玩家在有二传手的时候换牌。所以
public class Card {
private final Suit suit;
private final int rank;
public Card(final Suit suit, final int rank) {
this.suit = suit;
this.rank = rank;
}
// Getters, toString, equals, hashCode, formatting for humans, etc.
}
现在,你需要建造一个甲板。一副牌应该包含一张牌的列表,你必须能够洗牌。您可以将其设置为ArrayList
,但我会将其设置为包含此类列表的对象,因为甲板具有行为deck.deal()
比内联做deal
更清晰
public class Deck {
private List<Card> cards = new ArrayList<>();
public Deck() {
for (Suit suit: Suit.values()) {
for (int rank = 2; rank <= ACE; rank ++) {
cards.add(new Card(suit, rank));
}
}
Collections.shuffle(cards); // Perhaps use your own Random too.
}
public synchronized Card deal() {
Card dealt = cards.get(0);
cards.remove(0);
return dealt;
}
}
公共类甲板{
私有列表卡=新的ArrayList();
公共甲板(){
for(Suit:Suit.values()){
对于(int rank=2;排列什么是卡
?它不能容纳套装
,排列
,牌组
?