首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中使用in-shuffle和out-shuffle创建riffle shuffle

如何在java中使用in-shuffle和out-shuffle创建riffle shuffle
EN

Stack Overflow用户
提问于 2019-12-20 19:59:35
回答 1查看 763关注 0票数 0

我正在寻找创造一个即兴洗牌。我需要将我的牌组分成两个相等的部分(顶部和底部),然后通过交错来自两个一半的单张卡片将这两个一半合并在一起。我已经创建了牌面,但我不知道如何将其一分为二。

这是我到目前为止所拥有的代码:

代码语言:javascript
复制
class Deck
{
    private static String[] suit = {"\u2660", "\u2666", "\u2663", "\u2764"};
    private static String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
    private static String[] deck = new String[52];

    // create deck
    static
    {
        for (int i = 0; i < deck.length; i++)
        {
            deck[i] = rank[i % 13] + " of " + suit[i / 13];
        }
    }

    // un-shuffled deck
    static void deck()
    {
        for (int i = 0; i < deck.length; i++)
        {
            deck[i] = rank[i % 13] + " of " + suit[i / 13];
            System.out.println(deck[i]);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-12-20 20:52:39

因为你的牌组由52张牌组成,你必须将它分成两组,每组26张牌。正如约阿希姆所说,你不需要为此创建一个新的数组,但可以考虑从deck25的甲板1和甲板2开始。下一步,你需要一张一张地检查每一副牌,并保存新的洗牌牌。

代码语言:javascript
复制
static String[] shuffleDeck(String[] unshuffledDeck) {
    // Buffer for deck we want to return
    String[] shuffledDeck = new String[unshuffledDeck.length];
    // We start at index 0 for our first deck (lower half)
    int firstDeckIndex = 0;
    // We start at half of the maximum length of the total deck for our second deck (upper half)
    int secondDeckIndex = unshuffledDeck.length / 2;
    // We start going through the indexes of the new deck which we are going to return
    for(int shuffledDeckIndex = 0; shuffledDeckIndex < shuffledDeck.length; shuffledDeckIndex++) {
        // This is for alternating between the two decks. The modulo operator (%) returns the remainder of a division
        // 0 % 2 == 0 equals to true, 1 % 2 == 0 equals to false, 2 % 2 == 0 equals to true etc. 
        if(shuffledDeckIndex % 2 == 0) {
            // We put the current card of the first deck inside our new shuffledDeck
            shuffledDeck[shuffledDeckIndex] = unshuffledDeck[firstDeckIndex];
            // We advance the index to get the next card of the first deck
            firstDeckIndex++;
        } else {
            // Same as with the first deck
            shuffledDeck[shuffledDeckIndex] = unshuffledDeck[secondDeckIndex];
            secondDeckIndex++;
        }
    }
    // We return the new shuffled deck
    return shuffledDeck;
}

我建议你一旦掌握了概念本身,就自己尝试实现一个新的函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59424927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档