我正在尝试创建一副具有两种变化的牌: 它可以从标准的 52 副牌中选择并返回一张牌,并且没有重复的牌。 然后是另一副“无限”牌,其中可以重复牌。
public Deck(int a) {
if (a == 0) {
deckOf52();
} else {
infiniteDeck();
}
}
public void deckOf52() {
}
public void infiniteDeck() {
cards = new ArrayList<Card>(52);
for (int a = 0; a <= 3; a++) {
for (int b = 0; b <= 12; b++) {
cards.add(new Card(a, b));
}
}
}
public Card getCard() {
Random generator = new Random();
int index = generator.nextInt(cards.size());
return cards.remove(index);
}
infiniteDeck 可以完全按照我的需要进行操作,但我不确定如何让 DeckOf52 方法执行相同的操作,并且仅限于 52 张卡片,没有重复。
有人有任何想法或任何东西可以指引我正确的方向吗?
最佳答案
无限套牌中的每张卡都是唯一的,getCard 会删除它使用的每张卡,因此每张卡只会出现一次。
我不确定您要做什么,但这将满足您向我们提供的描述。
private int deckType;
public Deck(int a) {
this.deckType = a;
createDeck();
}
// create deck of 52 unique cards
public void createDeck() {
cards = new ArrayList<Card>(52);
for (int a = 0; a <= 3; a++) {
for (int b = 0; b <= 12; b++) {
cards.add(new Card(a, b));
}
}
}
// infinite deck never runs out of cards so do not remove card
public Card getCard() {
Random generator = new Random();
int index = generator.nextInt(cards.size());
if(this.deckType == 0){
return cards.remove(index);
} else {
return cards.get(index);
}
}
关于java - 创建一副具有两种变化的纸牌 : 52 and infinite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648364/