【问题标题】:How to shuffle a deck made with two dimensional array如何洗牌用二维数组制作的牌组
【发布时间】:2019-08-18 19:16:14
【问题描述】:

我在洗牌两副牌时遇到了问题。我使用二维数组创建了两个卡组。请记住,这些是要求。我还需要使用名为 shuffle() 的方法对它们进行随机播放,该方法接受一维数组并返回一维数组。

洗完两副牌后,我需要从第一副牌中挑选前两张牌,从第二副牌中挑选前两张牌,并检查它们是否匹配。以及获得结果所需的洗牌次数。

样本输出:

套牌 1 中的两张完全匹配的牌是:方块 A,梅花 2

套牌 2 中的两张完全匹配的牌是:方块 A,梅花 2

洗牌次数:387

这是我们参与的项目的第二部分,下面是我试图解决这个问题的方法。

我尝试使用下面的代码创建套牌

int[][] deck = new int[2][52];

for (int i = 0; i <= deck.length - 1; i++) {
    for (int j = 0; j < deck[i].length; j++) {
        deck[i][j] = j;
    }
}

我写了 shuffle 方法,但它似乎不起作用。

public static int[] shuffle(int[] deck) {
  for(int i=0; i<deck.length; i++) {  
  int index = (int)(Math.random()* deck.length); 
  int temp = deck[i];  
  deck[i] = deck[index];
  deck[index] = temp;
  }
return deck;    

}

下面的代码是该项目第一部分的原始代码,我们需要在洗牌后从一个牌堆中打印四个,并计算需要多少次洗牌才能得到四个

class Main {
  public static void main(String[] args) {

  String[] suit = { "Spades", "Hearts", "Diamond", "Clubs" };

  String[] rank = { "Ace", "1", "2", "3", "4", 
                 "5", "6", "7", "8", "9",
                 "10", "Jack", "Queen", "King" };

  int rank1, rank2, rank3, rank4, suit1, suit2, suit3, suit4, count = 0; 

  int[] deck = new int[52];

  for (int i = 0; i < deck.length; i++) {
     deck[i] = i;
  }

  do {

    count++;
    for (int i = 0; i < deck.length; i++) {

       int index = (int) (Math.random() * deck.length);
       int temp = deck[i];
       deck[i] = deck[index];
       deck[index] = temp;
    }

    suit1 = deck[0] / 13;
    suit2 = deck[1] / 13;
    suit3 = deck[2] / 13;
    suit4 = deck[3] / 13;

    rank1 = deck[0] % 13;
    rank2 = deck[1] % 13;
    rank3 = deck[2] % 13;
    rank4 = deck[3] % 13;

  } while (rank1 != rank2 || rank2 != rank3 || rank3 != rank4);

    System.out.println(" Four-of-kind cards: " + suit[suit1] + " of "
        + rank[rank1] + ", " + suit[suit2] + " of " + rank[rank2]
        + ", " + suit[suit3] + " of " + rank[rank3] + ", "
        + suit[suit4] + " of " + rank[rank4]
        + "\n Number of shuffled times: " + count);

    }
}

再一次,结果应该是这样的。

样本输出:

套牌 1 中的两张完全匹配的牌是:方块 A、梅花 2 甲板 2 中的两张完全匹配的牌是:方块 A,梅花 2

洗牌次数:387

【问题讨论】:

  • 出于好奇,也许我错过了问题中的某些内容,为什么您使用 [0]、[1]、[3]、[4] 分配 suitX,但 @987654325 @ 使用 [0],[1],[2],[3]?
  • 你的随机播放方法工作不知道你的问题是什么
  • hahahahaha...天哪,你不知道凯文是怎么吸引我的。直到现在我才意识到这是多么愚蠢的错误。我一直在闪回。无论如何,这是一个错误。现在将其修复为正确的顺序。谢谢

标签: java multidimensional-array iteration


【解决方案1】:

shuffle的方法是正确的。无需执行另一个接受 2D 数组的 shuffle 方法。您只需在 2D 数组的每个卡组中调用 shuffle 方法。要做到这一点:

deck[0] = shuffle(deck[0]);
deck[1] = shuffle(deck[1]);

我还更新了您的代码,以便在出现其他要求时更容易修改。参考以下代码:

public class Main {
    public static String[] card; // The holder for all cards

    public static void main(String[] args) {
        card = generateCardSuits(); // generate the cards with their corresponding suits
        doPartOne();
        doPartTwo();
    }

    /**
     * Part One
     *
     */
    public static void doPartOne() {
        System.out.println("========== PART ONE ==========");
        int[] deck = generateAndShuffleInitialDeck();
        int ctr = 1;
        while (true) {
            if (deck[0] % 13 == deck[1] % 13 && deck[1] % 13 == deck[2] % 13 && deck[2] % 13 == deck[3] % 13) {
                break;
            }
            deck = shuffle(deck);
            ctr++;
        }
        System.out.println("Four-of-kind cards are: " + card[deck[0]] + " , " + card[deck[1]] + " , " + card[deck[2]]
                + " and " + card[deck[3]]);
        System.out.println("Number of shuffled times: " + ctr);
        System.out.println("==============================");
    }

    /**
     * Part Two
     *
     */
    public static void doPartTwo() {
        System.out.println("========== PART TWO ==========");
        int[][] deck = new int[2][52];
        deck[0] = generateAndShuffleInitialDeck();
        deck[1] = generateAndShuffleInitialDeck();

        int ctr = 1;
        while (deck[0][0] != deck[1][0] || deck[0][1] != deck[1][1]) {
            deck[0] = shuffle(deck[0]);
            deck[1] = shuffle(deck[1]);
            ctr++;
        }

        System.out.println("Two exact match cards from deck 1 are: " + card[deck[0][0]] + " and " + card[deck[0][1]]);
        System.out.println("Number of shuffled times: " + ctr);
        System.out.println("==============================");
    }

    /**
     * Generate an initial deck of cards and shuffle them
     *
     * @return The initial and shuffled deck of cards
     */
    public static int[] generateAndShuffleInitialDeck() {
        int[] deck = new int[52];

        for (int j = 0; j < deck.length; j++) {
            deck[j] = j;
        }
        return shuffle(deck);
    }

    /**
     * Generate the cards with their corresponding suits
     *
     * @return The deck that will serve as the mapper for the cards to their suits
     */
    public static String[] generateCardSuits() {
        String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
        String[] suit = { "Spades", "Hearts", "Diamond", "Clubs" };

        String[] cards = new String[52];

        for (int i = 0, j = 0, s = 0; i < cards.length; i++) {
            cards[i] = rank[j] + " of " + suit[s];
            j++;
            if (j == 13) { // Since each suit consist of 13 cards
                j = 0;
                s++;
            }
        }
        return cards;
    }

    /**
     * Shuffle the deck of cards
     *
     * @param deck
     *            the deck of cards to be shuffle
     *
     * @return The shuffled deck of cards
     */
    public static int[] shuffle(int[] deck) {
        for (int i = 0; i < deck.length; i++) {
            int index = (int) (Math.random() * deck.length);
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }
        return deck;
    }
}

【讨论】:

  • 非常感谢你的标记。我非常感谢您的解释。
猜你喜欢
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多