How to "shuffle" an array?

I have a hard time trying to create the "shuffleDeck ()" method.

What I'm trying to do is create a method that takes an array parameter (which will be a deck of cards) shuffle the cards and return a list of shuffled arrays.

This is the code:

class Card { int value; String suit; String name; public String toString() { return (name + " of " + suit); } } public class PickACard { public static void main( String[] args) { Card[] deck = buildDeck(); // display Deck(deck); int chosen = (int)(Math.random()* deck.length); Card picked = deck[chosen]; System.out.println("You picked a " + picked + " out of the deck."); System.out.println("In Blackjack your card is worth " + picked.value + " points."); } public static Card[] buildDeck() { String[] suits = {"clubs", "diamonds", "hearts", "spades" }; String[] names = {"ZERO", "ONE", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Jack", "Queen", "King", "Ace" }; int i = 0; Card[] deck = new Card[52]; for ( String s: suits ) { for ( int v = 2; v<=14; v++) { Card c = new Card(); c.suit = s; c.name = names[v]; if ( v == 14) c.value = 11; else if ( v>10) c.value = 10; else c.value = v; deck[i] = c; i++; } } return deck; } public static String[] shuffleDeck( Card[] deck) { /** I have attempted to get two index numbers, and swap them. I tried to figure out how to loop this so it kind of simulates "shuffling". */ } public static void displayDeck( Card[] deck) { for ( Card c: deck) { System.out.println(c.value + "\t" + c); } } } 
+6
source share
4 answers

What about:

 List<Card> list = Arrays.asList(deck); Collections.shuffle(list); 

Or single line:

 Collections.shuffle(Arrays.asList(deck)); 
+10
source

One way is to convert the array to a list and use java.util.Collections.shuffle(array) to shuffle it:

 Card[] deck = ...; List<Card> list = Arrays.asList(deck); Collections.shuffle(list); 

If you still need an array instead of a list, you can add:

 list.toArray(deck); 

Here is a TIO (Try-it-online) link to see how the array will enumerate the conversion and shuffling in action.

TIO code copied below as a reference:

 import java.util.Arrays; import java.util.Collections; import java.util.List; class M{ public static void main(String[] a){ // Original array Integer[] array = new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; System.out.println("before: " + Arrays.toString(array)); // Convert array to list List<Integer> list = Arrays.asList(array); // And shuffle that list Collections.shuffle(list); System.out.println("after as list: " + list); // (Optional) then convert the list back to an array, // and save it in its initial variable (`array` in this case) list.toArray(array); System.out.println("after as array: " + Arrays.toString(array)); } } 
+2
source

I see two ways to do this:

-> You can use a shuffle algorithm, such as the Fisher-Yates shuffle algorithm , if you want to implement the method itself.

-> You can use the shuffle method from collections

0
source

If this is for a school project (as I think it is), you are not allowed to use built-in functions like Collections :: shuffle (). If so, then you should try to simulate randomness (which in programming can be surprisingly complex).

The most common way to create a sense of randomness is to use RNG (random number generator) . As you said

I tried to get two index numbers and swap them.

Right. One way to shuffle is to select one card at a time and randomly select another card to swap places.

  • You know that there are always 52 cards on the deck.
  • You have a random number generator to select a random index.
  • You have a programming language with a loop structure.

With these tools, you can easily implement your own shuffle function.

0
source

Source: https://habr.com/ru/post/952602/


All Articles