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 8 of 8

Thread: Shuffling a deck

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

    Default Shuffling a deck

    So I have all this made.. and I need to shuffle the deck, but it's not working and it's getting on my nerves...

     
    import java.util.*;
     
    class Hand { 
        static Card[] deck = new Card[52];
        Card[] hand = new Card[10];
        static int index;
     
     
     
     
        public static void createDeck () {
            int m = 0;
            for (int j = 0; j < 4; j++) {
                for (int k = 1; k < 14; k++) {
                   deck[m] = new Card(j,k);
                   m++;
                }
            }
            index = 0;
        } //end createDeck
     
        public static Card deal () {
            index++;
            return deck[index-1];
        } //end deal
     
     
     
        public static int compareCard (Card c1, Card c2) {     
            if (c1.getSuit() > c2.getSuit()) return 1; 
            if (c1.getSuit() < c2.getSuit()) return -1;
            if (c1.getRank() > c2.getRank()) return 1; 
            if (c1.getRank() < c2.getRank()) return -1;
            return 0;
        } //end compareCard
     
        public void printDeck (int size) {
            int j = 0;
            while (j < size) {
                deck[j].printCard();
                j++;
            }
        }//end printDeck
     
        public void shuffleDeck () {
             Random randomNumber = new Random ();
     
            for (int i =0; i < deck.length; i++) {
                int randomInt = randomNumber.nextInt (53);
              Card temp = hand [i];
               hand[i] = hand [randomInt];
               hand[randomInt] = temp;
     
        }
    }//end shuffleDeck
     
    } //end class
    Last edited by Darkzama; June 11th, 2013 at 04:38 PM. Reason: I edited the shuffle.. works a little better. still an error when run


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Shuffling a deck

    it's not working
    So, what is the problem? Does the code compile? If it does compile, does it do something unwanted or unexpected at runtime? If so, what?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Shuffling a deck

    It compiles, but when I run the Driver, it gives me an array exception...
    Untitled.jpg

    there's a screen shot of my error

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Shuffling a deck

    Sorry, I can't read the image.

    What is the stack trace? (the thing that says what the exception is, and then gives a list of lines of your code to go looking at)

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Shuffling a deck

    it says "Java.lang.ArrayIndexOutofboundsexcetion: ## (the number changes each time it's run)
    at hand.shuffleDeck (hand.java:52)
    at Dealer.<init>(Dealer.java:11)
    at Driver.Main(Driver.java:3)

    and then in my code it highlights "Hand[I] = hand [randomInt];"

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Shuffling a deck

    OK.

    You read these stack traces from the top down, until you reach a line of your code (which happens rather soon here!). It is saying that the problem is an ArrayIndexOutOfBoundsException. In other words you are trying to access an array at a crazy place (say a negative index, or an index that is too big.)

    That ## number is the crazy index that the Java runtime is complaining about. Not all exception messages are as helpful.

    Now the problem is evidently occurring at the line "hand[i] = hand [randomInt];" which is line 52 of the file Hand.java.

    The first thing to do is have a look where the hand array is initialised. That's up near the top:

    Card[] hand = new Card[10];

    So hand is an array that holds exactly ten cards. But back where the problem is occuring you say:

    int randomInt = randomNumber.nextInt (53);
    Card temp = hand [i];
    hand[i] = hand [randomInt];

    If randomInt is ever bigger than nine (and it very well may be) then you will get an ArrayIndexOutOfBoundsException.

    Basically the hand is too small (ten elements) for what you are shuffling (a random thing 0->52). I wonder if you mean to shuffle the hand or the deck...
    Last edited by pbrockway2; June 11th, 2013 at 05:17 PM. Reason: too many typos :( and I was trying hard to get things precise...

  7. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Darkzama (June 11th, 2013)

  8. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Shuffling a deck

    Okay... so, I reduced the random int to 9... and I still got the same error, but on the line above where it was before... and I mean to shuffle the deck....
    *edit*
    I got it.. thanks very much pbrockway!

  9. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Shuffling a deck

    You're welcome.

Similar Threads

  1. shuffling help
    By ronimacarroni in forum Algorithms & Recursion
    Replies: 9
    Last Post: April 15th, 2012, 11:23 PM
  2. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  3. [SOLVED] array not shuffling
    By captain in forum Collections and Generics
    Replies: 2
    Last Post: February 27th, 2012, 08:32 AM
  4. Shuffling a 2D array
    By Buzzins in forum Object Oriented Programming
    Replies: 6
    Last Post: January 16th, 2012, 01:47 PM
  5. Shuffling elements in a linked list.
    By xecure in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 01:25 PM