Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Shuffling a deck of cards

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Shuffling a deck of cards

    I'm not sure why my original deck of cards is shuffling.

    public class Cards {
     
        public static void main(String[] agrs){
     
            final String[] standardDeck = {"AC","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC",
                    "AD","2D","3D","4D","5D","6D","7D","8D","9D","10D","JD","QD","KD",
                    "AH","2H","3H","4H","5H","6H","7H","8H","9H","10H","JH","QH","KH",
                    "AS","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS",
            };
     
            String[] shuffledDeck=standardDeck;
     
            int r =0;
            for(int i=0;i<shuffledDeck.length;i++){
               r=(int)(Math.random()*shuffledDeck.length);
               String tempString=shuffledDeck[i];
               shuffledDeck[i]=shuffledDeck[r];
               shuffledDeck[r]=tempString;
            }
     
            for(int i=0;i<shuffledDeck.length;i++){
                System.out.println(standardDeck[i]+"  "+shuffledDeck[i]);
            }
        }
    }

    I believe that there is something wrong with this section of the code but I don't see it.

    int r =0;
            for(int i=0;i<shuffledDeck.length;i++){
               r=(int)(Math.random()*shuffledDeck.length);
               String tempString=shuffledDeck[i];
               shuffledDeck[i]=shuffledDeck[r];
               shuffledDeck[r]=tempString;
            }

    My output is:
    10D  10D
    3C  3C
    5D  5D
    8S  8S
    2H  2H
    4S  4S
    9S  9S
    6S  6S
    7S  7S
    KC  KC
    7C  7C
    QH  QH
    QD  QD
    4H  4H
    9D  9D
    3D  3D
    JD  JD
    QC  QC
    6C  6C
    KD  KD
    10H  10H
    7H  7H
    3H  3H
    6D  6D
    6H  6H
    AD  AD
    JS  JS
    8D  8D
    9H  9H
    2C  2C
    4C  4C
    5H  5H
    9C  9C
    5S  5S
    5C  5C
    JC  JC
    8C  8C
    KH  KH
    AS  AS
    2S  2S
    QS  QS
    10C  10C
    2D  2D
    AH  AH
    3S  3S
    4D  4D
    10S  10S
    AC  AC
    KS  KS
    7D  7D
    8H  8H
    JH  JH

    The first column should be not be shuffled but it is. What am I doing wrong?
    Last edited by aterrorbite; October 5th, 2013 at 07:12 PM. Reason: correctlly defining the problem.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Shuffling a deck of cards

    String[] shuffledDeck=standardDeck;
    I believe that this statement points shuffledDeck to standardDeck and therefore whatever changes in shuffled deck changes in standard. Notice how when you print the columns it should come out in the order you intended for the first column.

    Edit: Try this instead
    String[] shuffledDeck = standardDeck.clone();

  3. The Following User Says Thank You to camel-man For This Useful Post:

    aterrorbite (October 5th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Shuffling a deck of cards

    Quote Originally Posted by camel-man View Post
    String[] shuffledDeck=standardDeck;
    I believe that this statement points shuffledDeck to standardDeck and therefore whatever changes in shuffled deck changes in standard. Notice how when you print the columns it should come out in the order you intended for the first column.

    Edit: Try this instead
    String[] shuffledDeck = standardDeck.clone();
    Not sure if that'll work either. I'm not sure what class, if any, handles arrays, but I would think that having an array call the clone method, unless arrays have some special override to Object's clone method that I'm not aware of, would make a shallow copy like Object's clone method does, which is basically what the OP already had. A shallow copy refers to the same memory address and hence would change both when you change one of them.


    This code definitely makes a deep, or separate, copy:
    String[] shuffledDeck = new String[standardDeck.length];
     
    for (int i =0; i < shuffledDeck.length; i++)
    {
    shuffledDeck[i] = standardDeck[i];
     
     
    }

  5. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    aterrorbite (October 5th, 2013)

  6. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Shuffling a deck of cards

    Quote Originally Posted by camel-man View Post
    String[] shuffledDeck=standardDeck;
    I believe that this statement points shuffledDeck to standardDeck and therefore whatever changes in shuffled deck changes in standard. Notice how when you print the columns it should come out in the order you intended for the first column.

    Edit: Try this instead
    String[] shuffledDeck = standardDeck.clone();
    Thanks, that solved it.

    I also got it to work with the code shown below.
     String[] shuffledDeck= new String[standardDeck.length];
            for(int i =0;i<standardDeck.length;i++){
                shuffledDeck[i] = new String(standardDeck[i]);
            }

Similar Threads

  1. Shuffling a deck
    By Darkzama in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 11th, 2013, 05:58 PM
  2. Please help with unwrap, deck, shuffle cards
    By pots in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2013, 12:46 PM
  3. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  4. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM
  5. Adding cards back into deck
    By sp4ce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2011, 01:21 AM