java - 创建一副具有两种变化的纸牌 : 52 and infinite

标签 java arrays playing-cards

我正在尝试创建一副具有两种变化的牌: 它可以从标准的 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/

相关文章:

Java 接口(interface)多态性

java - 在java中将字符串转换为java.Security.Publickey

C++ - 这个双指针/数组结构的真正含义是什么?我很难想象

java - 纸牌游戏类的 OOP 设计

c# - 在 C# 中反向 Fisher-Yates Shuffle

java - 我不断收到错误 : int cannot be dereferenced. 如何修复它?

使用 Jsoup 进行 Java 网页抓取

python - 在 jinja2 中使用数组

java - 在排列的文本文件中搜索字符串

java - 如何在 time4j 库中获取当前日期?