2 Replies - 615 Views - Last Post: 18 November 2015 - 01:58 AM Rate Topic: -----

#1 Nguyen13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-November 15

Shuffling a Deck of Cards

Posted 17 November 2015 - 07:45 PM

I am working on a java experiment dealing with making a deck of cards. So far I believe I have a deck set up and now need to shuffle it. The way I am supposed to shuffle it is by by repeatedly (200 times) picking 2 cards randomly from the deck and switching them. I am still relatively new to Java and have never encountered anything like this before so I don't even know where to begin. Can someone please help?

This is the code I have so far:

public class DeckOfCards
{
    public static final int number = 52;
    public Card[] deck = new Card[number];

    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};



    public Deck()
    {
        int i = 0;
        for(String rank : ranks) 
        {
             for(String suit : suits) 
             {
                deck[i] = new Card(rank, suit);
                i++;
             }
        }

    }




} 




Thanks!
Paul Nguyen

Is This A Good Question/Topic? 0
  • +

Replies To: Shuffling a Deck of Cards

#2 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6644
  • View blog
  • Posts: 27,191
  • Joined: 12-December 12

Re: Shuffling a Deck of Cards

Posted 17 November 2015 - 11:59 PM

Quote

The way I am supposed to shuffle it is by by repeatedly (200 times) picking 2 cards randomly from the deck and switching them.

Study the Random class to generate random numbers.

You have a deck of 52 cards, so you need two random numbers between 0 and 51. Swap these two cards. Repeat. (You might decide to include a check if the same number is drawn.)

Quote

and have never encountered anything like this before so I don't even know where to begin.

Break it down into steps. Use pseudocode to help you. This is what programming is all about!

This post has been edited by andrewsw: 18 November 2015 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,497
  • Joined: 20-September 08

Re: Shuffling a Deck of Cards

Posted 18 November 2015 - 01:58 AM

http://technojeeves....hout-duplicates

A variable called 'number' that's an int is not self-documenting enough
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1