Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "shuffle" an array? [duplicate]

I am having a tough time trying to create a "shuffleDeck()" method.

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

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);
        }
    }
}
like image 947
user2690972 Avatar asked Aug 27 '13 15:08

user2690972


People also ask

How can I shuffle an array?

Write the function shuffle(array) that shuffles (randomly reorders) elements of the array. Multiple runs of shuffle may lead to different orders of elements. For instance: let arr = [1, 2, 3]; shuffle(arr); // arr = [3, 2, 1] shuffle(arr); // arr = [2, 1, 3] shuffle(arr); // arr = [3, 1, 2] // ...

How do you find duplicate objects in an array?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.

How do I randomly shuffle an array in Python?

Using shuffle() method from Random library to shuffle the given array. Here we are using shuffle method from the built-in random module to shuffle the entire array at once.

How do you shuffle an array in C#?

OrderBy(c => random. Next()). ToArray(); In the previous code we generated a list of 0 to 9 numbers, then we assign every and each of them a random number and we order the list by that random number: this process will effectively gets us a shuffled array.


1 Answers

How about:

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

Or one-liner:

Collections.shuffle(Arrays.asList(deck));
like image 65
rocketboy Avatar answered Sep 22 '22 05:09

rocketboy