我正在寻找创造一个即兴洗牌。我需要将我的牌组分成两个相等的部分(顶部和底部),然后通过交错来自两个一半的单张卡片将这两个一半合并在一起。我已经创建了牌面,但我不知道如何将其一分为二。
这是我到目前为止所拥有的代码:
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]);
}
}
}发布于 2019-12-20 20:52:39
因为你的牌组由52张牌组成,你必须将它分成两组,每组26张牌。正如约阿希姆所说,你不需要为此创建一个新的数组,但可以考虑从deck25的甲板1和甲板2开始。下一步,你需要一张一张地检查每一副牌,并保存新的洗牌牌。
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;
}我建议你一旦掌握了概念本身,就自己尝试实现一个新的函数。
https://stackoverflow.com/questions/59424927
复制相似问题