Malvin Malvin
Malvin Malvin

Reputation: 3

How to shuffle deck in Java?

I have been trying to shuffle the deck, but there is error in the output. I want it to show a random card like Ace spade, two heart three king and etc. However, the outcome is this as shown below. How do i change my code?

Card@b5f53a
Card@1f6f0bf
Card@137c60d
Card@ab853b
Card@b82368
Card@11c8a71
Card@c53dce
Card@15cda3f
Card@fc9944
Card@1b26af3
Card@8b819f
Card@eb017e
Card@aeffdf
Card@120a47e
Card@f73c1
Card@789144
Card@b5f53a
Card@1893efe
Card@186c6b2
Card@15ee671
Card@eb017e
Card@137c60d
Card@16b13c7
Card@11c8a71
Card@df8f5e
Card@13d93f4
Card@1bca5f1
Card@b82368
Card@329f3d
Card@1749757

The code:

import java.util.Random;

public class deck3 {

    //public int TOTALCARD;
    public Card[] deck;

    public deck3() {
        this.deck = new Card[52];
        String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};// Array for
        // suit
        int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Array

        for (int i = 0; i < suit.length; i++) {
            for (int n = 0; n < number.length; n++) {
                Card c = new Card(suit[i], number[n]); // Card instantiate

                // Using (i*number.length + n) will get you from index 0 to 51
                if (deck[(i * number.length + n)] == null) {
                    deck[(i * number.length + n)] = c; // Accessing index 0 to
                    // 51
                }
            }
        }
    }

    public void CreateDeck() {}

    //int[] TOTALCARD={deck};
    public void shuffleDeck() {

        Random generator = new Random();
        for (int i = 0; i < deck.length; i++) {
            int thisplace = generator.nextInt(50);
            int thatplace = generator.nextInt(50);

            Card savednumber = deck[thisplace];
            deck[thisplace] = deck[thatplace];
            deck[thatplace] = savednumber;
            System.out.println(savednumber);
        }

    }

    public void displayDeck() {
        for (int i = 0; i < deck.length; i++) {
            this.deck[i].display();
            //shuffleDeck();// Calling the display method from my card
            // class.
            //System.out.println(randomNumbers);
        }
    }
}

Upvotes: 0

Views: 941

Answers (3)

MKZ
MKZ

Reputation: 11

There's an error in your function deck.display() what you print is the class id of your cards, and not their data fields. (I'm suggesting you are making something like that System.out.print(card[i]) that is wrong because the program doesn't know what value you want to print from the class so it prints the class id)

You may want to implement a function display() in your card class as follows:

public void display()
{
    System.out.println("Card Number = "+number+", Card Suit = "+suit);
}

Then when you call deck.display() you can call card[i].display() for all of your cards. Also consider using int values for the card's Suit, this is more lightweight.

Upvotes: 0

Andrey Pushin
Andrey Pushin

Reputation: 196

You need override public String toString() method in class Card for pretty print (instead of Card@b5f53a, ...). for example:

class Card {
    private String value;
    private Long value2;

    @Override
    public String toString() {
        return "Card{" +
                "value='" + value + '\'' +
                ", value2=" + value2 +
                '}';
        }
    }

And if you need shuffle - better way it use Collections.shuffle(). for example:

    Card[] cards = new Card[10];
    .... // you cards array (may be you can use List?)
    List<Card> cardList = new ArrayList<Card>();
    Collections.addAll(cardList, cards);
    Collections.shuffle(cardList);
    //transform to array (if you need array)
    cards = cardList.toArray(new Card[cardList.size()]);

As you see you can use list instead of array

Upvotes: 3

peter.petrov
peter.petrov

Reputation: 39457

Override the toString method in your Card class.
Then use the same code to print out the Card objects.
That should do it.

Object.toString

Upvotes: 0

Related Questions