我正在寻找创建浅滩混洗。我需要将套牌分成相等的两半(上半部分和下半部分),然后通过将两个半部分中的单张纸交错插入来将两个半部分合并在一起。我已经创建了套牌,但是我不知道如何将其分成两个相等的部分。
这是我到目前为止的代码:
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]);
}
}
}
答案 0 :(得分:0)
由于卡组包含52张卡,因此您必须将其“拆分”为两个卡组,每个卡组26张卡。如Joachim所说,您无需为此创建新的数组,但可以想到从1 [0]和2 [25]开始的1甲板。接下来,您将需要一段时间后遍历每个卡座,并保存新的随机播放卡座。
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;
}
一旦您掌握了概念本身,我建议您尝试自己实现一个新功能。