How to shuffle a deck made with two dimensional arrayHow can I concatenate two arrays in Java?How can I create a two dimensional array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to Sort Multi-dimensional Array by Value?Syntax for creating a two-dimensional arraySorting a hand of pokerHow to switch card positions in Card Deck shuffle() for Bridge gameShuffling an array of objects in java not working properlyUnable to make iterate through to get four of a kind

Why Is Sojdlg123aljg a Common Password?

How do I use NEC PC-6001 .p6 or .cas files?

Draw the ☣ (Biohazard Symbol)

Do I need to declare engagement ring bought in UK when flying on holiday to US?

Why would image resources loaded from different origins triggering HTTP authentication dialogs be harmful?

How do I delete cookies from a specific site?

Fantasy Military Arms and Armor: the Dwarven Grand Armory

Dissuading my girlfriend from a scam

If I have an accident, should I file a claim with my car insurance company?

What is the purpose of the rotating plate in front of the lock?

Is Sanskrit really the mother of all languages?

What is the justification for Dirac's large numbers hypothesis?

What are some countries where you can be imprisoned for reading or owning a Bible?

Does the Giant Toad's Swallow acid damage take effect only at the start of its next turn?

I won a car in a poker game. How is that taxed in Canada?

What quests do you need to stop at before you make an enemy of a faction for each faction?

Matlab fmincon for a problem with many nonlinear constraints

Balm of the Summer Court fey energy dice usage limits

Looking for the comic book where Spider-Man was [mistakenly] addressed as Super-Man

Remaining in the US beyond VWP admission period

Are there mathematical concepts that exist in the fourth dimension, but not in the third dimension?

"syntax error near unexpected token" after editing .bashrc

Can you fix a tube with a lighter?

Translate English to Pig Latin | PIG_LATIN.PY



How to shuffle a deck made with two dimensional array


How can I concatenate two arrays in Java?How can I create a two dimensional array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to Sort Multi-dimensional Array by Value?Syntax for creating a two-dimensional arraySorting a hand of pokerHow to switch card positions in Card Deck shuffle() for Bridge gameShuffling an array of objects in java not working properlyUnable to make iterate through to get four of a kind






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am having an issue shuffling two decks. I created two decks using a two dimensional array. Keep in mind that those were the requirements. I am also required to shuffle them using a method called shuffle() that takes in a 1D array and returns a 1D array.



After shuffling both decks I need to pick the first two card from the first deck and the first two cards from the second deck and check if they match. As well as the number of shuffles it took to get the result.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs



Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387



This is part two of a project we worked on and below is what I tried to solve this problem.



I tried creating decks using the code below which worked



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;




And I wrote the shuffle method but it doesn't seem to work.



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;




The code below is the original code from the first part of this project where we were required to print four of a kind from a single deck after shuffling it and counting how many shuffles it took to get four of a kind



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



Once again result should look like this.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs
Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387










share|improve this question


























  • Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

    – KevinO
    Mar 28 at 5:41












  • Your shuffle method work not sure what your problem is

    – Mark
    Mar 28 at 6:31











  • hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

    – Abdallah Alsharmani
    Mar 28 at 22:52

















1















I am having an issue shuffling two decks. I created two decks using a two dimensional array. Keep in mind that those were the requirements. I am also required to shuffle them using a method called shuffle() that takes in a 1D array and returns a 1D array.



After shuffling both decks I need to pick the first two card from the first deck and the first two cards from the second deck and check if they match. As well as the number of shuffles it took to get the result.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs



Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387



This is part two of a project we worked on and below is what I tried to solve this problem.



I tried creating decks using the code below which worked



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;




And I wrote the shuffle method but it doesn't seem to work.



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;




The code below is the original code from the first part of this project where we were required to print four of a kind from a single deck after shuffling it and counting how many shuffles it took to get four of a kind



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



Once again result should look like this.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs
Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387










share|improve this question


























  • Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

    – KevinO
    Mar 28 at 5:41












  • Your shuffle method work not sure what your problem is

    – Mark
    Mar 28 at 6:31











  • hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

    – Abdallah Alsharmani
    Mar 28 at 22:52













1












1








1








I am having an issue shuffling two decks. I created two decks using a two dimensional array. Keep in mind that those were the requirements. I am also required to shuffle them using a method called shuffle() that takes in a 1D array and returns a 1D array.



After shuffling both decks I need to pick the first two card from the first deck and the first two cards from the second deck and check if they match. As well as the number of shuffles it took to get the result.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs



Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387



This is part two of a project we worked on and below is what I tried to solve this problem.



I tried creating decks using the code below which worked



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;




And I wrote the shuffle method but it doesn't seem to work.



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;




The code below is the original code from the first part of this project where we were required to print four of a kind from a single deck after shuffling it and counting how many shuffles it took to get four of a kind



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



Once again result should look like this.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs
Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387










share|improve this question
















I am having an issue shuffling two decks. I created two decks using a two dimensional array. Keep in mind that those were the requirements. I am also required to shuffle them using a method called shuffle() that takes in a 1D array and returns a 1D array.



After shuffling both decks I need to pick the first two card from the first deck and the first two cards from the second deck and check if they match. As well as the number of shuffles it took to get the result.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs



Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387



This is part two of a project we worked on and below is what I tried to solve this problem.



I tried creating decks using the code below which worked



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;




And I wrote the shuffle method but it doesn't seem to work.



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;




The code below is the original code from the first part of this project where we were required to print four of a kind from a single deck after shuffling it and counting how many shuffles it took to get four of a kind



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



Once again result should look like this.



Sample output:



Two exact match cards from deck 1 are: Ace of Diamonds, 2 of Clubs
Two exact match cards from deck 2 are: Ace of Diamonds, 2 of Clubs



Number of shuffled times: 387







java multidimensional-array iteration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 22:54







Abdallah Alsharmani

















asked Mar 28 at 4:59









Abdallah AlsharmaniAbdallah Alsharmani

103 bronze badges




103 bronze badges















  • Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

    – KevinO
    Mar 28 at 5:41












  • Your shuffle method work not sure what your problem is

    – Mark
    Mar 28 at 6:31











  • hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

    – Abdallah Alsharmani
    Mar 28 at 22:52

















  • Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

    – KevinO
    Mar 28 at 5:41












  • Your shuffle method work not sure what your problem is

    – Mark
    Mar 28 at 6:31











  • hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

    – Abdallah Alsharmani
    Mar 28 at 22:52
















Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

– KevinO
Mar 28 at 5:41






Out of curiosity, and perhaps I missed something in the question, why do you assign the suitX using [0],[1],[3],[4], but the rankX using [0],[1],[2],[3]?

– KevinO
Mar 28 at 5:41














Your shuffle method work not sure what your problem is

– Mark
Mar 28 at 6:31





Your shuffle method work not sure what your problem is

– Mark
Mar 28 at 6:31













hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

– Abdallah Alsharmani
Mar 28 at 22:52





hahahahaha...omg you have no idea how that got me Kevin. I didn't even realize how dumb of a mistake that is until now. I kept getting flashbacks. Anyway that was a mistake. Fixing it now back to the correct order. Thank you

– Abdallah Alsharmani
Mar 28 at 22:52












1 Answer
1






active

oldest

votes


















0
















You shuffle method is correct. No need to do another shuffle method that will accept a 2D array. You will just have to call the shuffle method in each deck of your 2D array. To do this just :



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


I have also updated your code so that it can be easier to modify incase additional requirements arises. Refer to the code below:



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]

/**
* 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;







share|improve this answer




















  • 1





    Thank you so much mark. I really appreciate the explanation.

    – Abdallah Alsharmani
    Mar 28 at 22:22










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390464%2fhow-to-shuffle-a-deck-made-with-two-dimensional-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0
















You shuffle method is correct. No need to do another shuffle method that will accept a 2D array. You will just have to call the shuffle method in each deck of your 2D array. To do this just :



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


I have also updated your code so that it can be easier to modify incase additional requirements arises. Refer to the code below:



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]

/**
* 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;







share|improve this answer




















  • 1





    Thank you so much mark. I really appreciate the explanation.

    – Abdallah Alsharmani
    Mar 28 at 22:22















0
















You shuffle method is correct. No need to do another shuffle method that will accept a 2D array. You will just have to call the shuffle method in each deck of your 2D array. To do this just :



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


I have also updated your code so that it can be easier to modify incase additional requirements arises. Refer to the code below:



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]

/**
* 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;







share|improve this answer




















  • 1





    Thank you so much mark. I really appreciate the explanation.

    – Abdallah Alsharmani
    Mar 28 at 22:22













0














0










0









You shuffle method is correct. No need to do another shuffle method that will accept a 2D array. You will just have to call the shuffle method in each deck of your 2D array. To do this just :



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


I have also updated your code so that it can be easier to modify incase additional requirements arises. Refer to the code below:



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]

/**
* 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;







share|improve this answer













You shuffle method is correct. No need to do another shuffle method that will accept a 2D array. You will just have to call the shuffle method in each deck of your 2D array. To do this just :



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


I have also updated your code so that it can be easier to modify incase additional requirements arises. Refer to the code below:



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]

/**
* 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;








share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 7:28









MarkMark

1,3691 gold badge6 silver badges24 bronze badges




1,3691 gold badge6 silver badges24 bronze badges










  • 1





    Thank you so much mark. I really appreciate the explanation.

    – Abdallah Alsharmani
    Mar 28 at 22:22












  • 1





    Thank you so much mark. I really appreciate the explanation.

    – Abdallah Alsharmani
    Mar 28 at 22:22







1




1





Thank you so much mark. I really appreciate the explanation.

– Abdallah Alsharmani
Mar 28 at 22:22





Thank you so much mark. I really appreciate the explanation.

– Abdallah Alsharmani
Mar 28 at 22:22








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390464%2fhow-to-shuffle-a-deck-made-with-two-dimensional-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript