如何洗牌用二维阵列制作的套牌

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


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


示例输出:


第1副牌中的两张完全匹配卡牌是:钻石王牌,俱乐部2张


第2副牌中的两张完全匹配卡牌是:钻石王牌,俱乐部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;

    }

}

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


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的两张精确匹配卡是:钻石王牌,俱乐部2卡组2中的两张精确匹配卡牌是:钻石王牌,俱乐部2


洗牌次数: 387


慕妹3242003
浏览 206回答 1
1回答

精慕HU

您的方法是正确的。无需执行另一个将接受 2D 数组的 shuffle 方法。您只需要在2D阵列的每个套牌中调用该方法即可。要做到这一点,只需:shuffleshuffledeck[0] = shuffle(deck[0]);deck[1] = shuffle(deck[1]);我还更新了您的代码,以便在出现其他要求时可以更轻松地进行修改。请参考以下代码:public class Main {&nbsp; &nbsp; public static String[] card; // The holder for all cards&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; card = generateCardSuits(); // generate the cards with their corresponding suits&nbsp; &nbsp; &nbsp; &nbsp; doPartOne();&nbsp; &nbsp; &nbsp; &nbsp; doPartTwo();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Part One&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void doPartOne() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("========== PART ONE ==========");&nbsp; &nbsp; &nbsp; &nbsp; int[] deck = generateAndShuffleInitialDeck();&nbsp; &nbsp; &nbsp; &nbsp; int ctr = 1;&nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (deck[0] % 13 == deck[1] % 13 && deck[1] % 13 == deck[2] % 13 && deck[2] % 13 == deck[3] % 13) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck = shuffle(deck);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctr++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Four-of-kind cards are: " + card[deck[0]] + " , " + card[deck[1]] + " , " + card[deck[2]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " and " + card[deck[3]]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Number of shuffled times: " + ctr);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("==============================");&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Part Two&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void doPartTwo() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("========== PART TWO ==========");&nbsp; &nbsp; &nbsp; &nbsp; int[][] deck = new int[2][52];&nbsp; &nbsp; &nbsp; &nbsp; deck[0] = generateAndShuffleInitialDeck();&nbsp; &nbsp; &nbsp; &nbsp; deck[1] = generateAndShuffleInitialDeck();&nbsp; &nbsp; &nbsp; &nbsp; int ctr = 1;&nbsp; &nbsp; &nbsp; &nbsp; while (deck[0][0] != deck[1][0] || deck[0][1] != deck[1][1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck[0] = shuffle(deck[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck[1] = shuffle(deck[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctr++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Two exact match cards from deck 1 are: " + card[deck[0][0]] + " and " + card[deck[0][1]]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Number of shuffled times: " + ctr);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("==============================");&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Generate an initial deck of cards and shuffle them&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return The initial and shuffled deck of cards&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static int[] generateAndShuffleInitialDeck() {&nbsp; &nbsp; &nbsp; &nbsp; int[] deck = new int[52];&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < deck.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck[j] = j;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return shuffle(deck);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Generate the cards with their corresponding suits&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return The deck that will serve as the mapper for the cards to their suits&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static String[] generateCardSuits() {&nbsp; &nbsp; &nbsp; &nbsp; String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };&nbsp; &nbsp; &nbsp; &nbsp; String[] suit = { "Spades", "Hearts", "Diamond", "Clubs" };&nbsp; &nbsp; &nbsp; &nbsp; String[] cards = new String[52];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, j = 0, s = 0; i < cards.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = rank[j] + " of " + suit[s];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == 13) { // Since each suit consist of 13 cards&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return cards;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Shuffle the deck of cards&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param deck&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the deck of cards to be shuffle&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return The shuffled deck of cards&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static int[] shuffle(int[] deck) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < deck.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = (int) (Math.random() * deck.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = deck[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck[i] = deck[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck[index] = temp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return deck;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java