简体   繁体   中英

Java: Shuffle Deck using math.random()

I have a code that is based on generating a deck and then shuffling it. Here is what I currently have for my code.

import java.lang.Math;
import java.util.Arrays;


public class Program_9 {
    public static void main(String[] args) {            
        int[] deck = new int[36];
        int n = 1;
        
    initDeck(deck);
    
    System.out.println("\nDeck");
    System.out.println("#############\n");
    
    displayDeck(deck);
    
    System.out.println("\nShuffled Deck");
    System.out.println("#############\n");
    
    shuffleDeck(deck, 10);
    
    
    
}

public static int cardValue(int card) {
    //Return the integers value 1 - 9 of card
    
    int cardValue = card % 9 + 1;
    
    return cardValue;
}

public static String cardSuit(int card) {
    //return the suit club, spade, heart, diamond of card   
    
    String cardSuit = "";
    int value = card / 9;
    
    if (value == 0) {
        cardSuit = "Club";
    }
    
    if (value == 1) {
        cardSuit = "Spade";
    }
    
    if (value == 2) {
        cardSuit = "Heart";
    }
    
    if (value == 3) {
        cardSuit = "Diamond";
    }
    
    return cardSuit;        
}

public static void displayCard(int card) {
    //prints card value and suit in some reasonable report
    System.out.print(cardValue(card) + " of " + cardSuit(card));
    
}

public static void initDeck(int[] deck) {
    //assign the elements of deck, such that each elements value is the same as it index
    
    deck[0] = 1;
    deck[1] = 2;
    deck[2] = 3;
    deck[3] = 4;    
    deck[4] = 5;
    deck[5] = 6;
    deck[6] = 7;
    deck[7] = 8;
    deck[8] = 9;
    deck[9] = 10;
    deck[10] = 11;
    deck[11] = 12;
    deck[12] = 13;
    deck[13] = 14;
    deck[14] = 15;
    deck[15] = 16;
    deck[16] = 17;
    deck[17] = 18;
    deck[18] = 19;
    deck[19] = 20;
    deck[20] = 21;
    deck[21] = 22;
    deck[22] = 23;
    deck[23] = 24;
    deck[24] = 25;
    deck[25] = 26;
    deck[26] = 27;
    deck[27] = 28;
    deck[28] = 29;
    deck[29] = 30;
    deck[30] = 31;
    deck[31] = 32;
    deck[32] = 33;
    deck[33] = 34;
    deck[34] = 35;
    deck[35] = 36;

}



public static void shuffleDeck(int[] deck, int n) {
    //the following performed exactly n times:
    // 1. generate two random numbers j and k - each in range of [0 , 35]
    // 2. swap the values of the deck array at indices j and k
    
    for (int i = 0; i < deck.length; i++) {
        
        int k = (int)((Math.random() * 35) + 0);
        int j = (int)((Math.random() * 35) + 0);
        
        
        
    }
}

public static void displayDeck(int[] deck) {
    //prints the cards in deck in some reasonable report format
    
    for (int i = 0; i < deck.length; ++i) {
        int card = i;
        
        displayCard(card);
        System.out.println();
        
    }
    
    
}

}

I am having issues understand how to write the shuffleDeck() method. This is my first time taking Java so I am fairly new. What I don't understand is the second commend in the shuffleDeck() method. Note this is a homework assignment so I would prefer if hints were provided instead of answers

Here is some code i have lying around that does just that.

    char[] someArray= ...;
    
    //shuffle it up
    for (int i = 0; i < someArray.length; i++) {
        int randomPosition = rand.nextInt(someArray.length);
        char temp = someArray[i];
        someArray[i] = someArray[randomPosition];
        someArray[randomPosition] = temp;
    } 

I basically goes through all the positions in the array and does a swap with a random position in the array.

Since I cannot comment because of < 50 reputation, I will add it here.

You are creating int k = (int) ((Math.random() * 35) + 0);

and int j = (int) ((Math.random() * 35) + 0);

twice inside the same method. That doesn't work.

You should get an error as

variable "k" is already defined in the scope

Please try to use a unique name for every variable to avoid this.

Here is all you need to do to shuffle an array.

int a[] = IntStream.range(0, 36).toArray();
System.out.println("New deck");
display(a);
shuffleDeck(a);
System.out.println("\nShuffled deck");
display(a);

prints

New deck
1C 2C 3C 4C 5C 6C 7C 8C 9C 
1D 2D 3D 4D 5D 6D 7D 8D 9D 
1H 2H 3H 4H 5H 6H 7H 8H 9H 
1S 2S 3S 4S 5S 6S 7S 8S 9S 

Shuffled deck
6S 3S 7H 2H 1C 1H 8C 3D 4S 
7S 6D 4H 9S 1D 9H 7C 7D 5D 
8D 6C 5S 4D 8S 5H 3H 2S 4C 
2C 1S 2D 9D 9C 3C 6H 8H 5C 

Shuffle method


public static void shuffleDeck(int[] deck) { 
      for (int i = deck.length-1; i >= 0; i--) {
          // get card to be swapped index.
          int loc = (int)(Math.random()*i);
          // and swap it with the one at i.
          // note that that card will never be moved again.
          int temp = deck[i];
          deck[i] = deck[loc];
          deck[loc] = temp;
      }
}

Display method

static String suits = "CDHS";
public static void display(int[] deck) {
    for (int k = 0; k < deck.length; k++) {
        int card = deck[k];
        System.out.printf("%s%s ", card % 9 + 1,
                suits.charAt(card / 9));
        if ((k + 1) % 9 == 0) {
            System.out.println();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM