Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Shuffle method error

Thread view: 
j0ecanad1an80 - 19 Jun 2006 04:29 GMT
I am relatively new to java programming and I am working on a card
project for school. I had finished and tested every method with error
in my Deck class. However, in the time it took to pick up dinner it is
now producing errors. Nothing changed, so I am clueless as to why
this no longer works. I would greatly appreciate anyone input and help.
This is the error that is produce when invoking the shuffle method.

java.lang.IndexOutOfBoundsException: Index: 235, Size: 51
       at java.util.ArrayList.RangeCheck(ArrayList.java:546)
       at java.util.ArrayList.get(ArrayList.java:321)
       at Deck.swap(Deck.java:176)
       at Deck.shuffle(Deck.java:129)

public class Deck
{
   /** The number of cards in a deck */
   public static final int DECK_SIZE = 52;
   /** The number of times to shuffle */
   public static final int TIMES_TO_SHUFFLE = 1000;

  private ArrayList deck;                   // a deck of cards

   /**
    * Constructor for objects of class Deck
    */
   public Deck()
   {
      deck = new ArrayList();
      newDeck();
   }

   /**
    * Load a new deck with all DECK_SIZE cards
    */
   public void newDeck()
   {
       if (deck.size() > 0){
           deck = null;
       }
       else{
       //Hearts
       deck.add(new Card("Ace","Hearts",11));
       deck.add(new Card("Two","Hearts",2));
       deck.add(new Card("Three","Hearts",3));
       deck.add(new Card("Four","Hearts",4));
       deck.add(new Card("Five","Hearts",5));
       deck.add(new Card("Six","Hearts",6));
       deck.add(new Card("Seven","Hearts",7));
       deck.add(new Card("Eight","Hearts",8));
       deck.add(new Card("Nine","Hearts",9));
       deck.add(new Card("Ten","Hearts",10));
       deck.add(new Card("Jack","Hearts",10));
       deck.add(new Card("Queen","Hearts",10));
       deck.add(new Card("King","Hearts",10));

       //Diamonds
       deck.add(new Card("Ace","Diamonds",11));
       deck.add(new Card("Two","Diamonds",2));
       deck.add(new Card("Three","Diamonds",3));
       deck.add(new Card("Four","Diamonds",4));
       deck.add(new Card("Five","Diamonds",5));
       deck.add(new Card("Six","Diamonds",6));
       deck.add(new Card("Seven","Diamonds",7));
       deck.add(new Card("Eight","Diamonds",8));
       deck.add(new Card("Nine","Diamonds",9));
       deck.add(new Card("Ten","Diamonds",10));
       deck.add(new Card("Jack","Diamomds",10));
       deck.add(new Card("Queen","Diamonds",10));
       deck.add(new Card("King","Diamonds",10));

       //Spades
       deck.add(new Card("Ace","Spades",11));
       deck.add(new Card("Two","Spades",2));
       deck.add(new Card("Three","Spades",3));
       deck.add(new Card("Four","Spades",4));
       deck.add(new Card("Five","Spades",5));
       deck.add(new Card("Six","Spades",6));
       deck.add(new Card("Seven","Spades",7));
       deck.add(new Card("Eight","Spades",8));
       deck.add(new Card("Nine","Spades",9));
       deck.add(new Card("Ten","Spades",10));
       deck.add(new Card("Jack","Spades",10));
       deck.add(new Card("Queen","Spades",10));
       deck.add(new Card("King","Spades",10));

       //Clubs
       deck.add(new Card("Ace","Clubs",11));
       deck.add(new Card("Two","Clubs",2));
       deck.add(new Card("Three","Clubs",3));
       deck.add(new Card("Four","Clubs",4));
       deck.add(new Card("Five","Clubs",5));
       deck.add(new Card("Six","Clubs",6));
       deck.add(new Card("Seven","Clubs",7));
       deck.add(new Card("Eight","Clubs",8));
       deck.add(new Card("Nine","Clubs",9));
       deck.add(new Card("Ten","Clubs",10));
       deck.add(new Card("Jack","Clubs",10));
       deck.add(new Card("Queen","Clubs",10));
       deck.add(new Card("King","Clubs",10));
       }
     }
   /**
    * Add a single card to the deck.
    * @param a Card object
    */
   public void addCard(Card newCard)
   {
      // add a card to the deck
      deck.add(newCard);
   }

   /**
    * Shuffle the deck. This involves selecting random pairs of
    * cards and swapping them, the number of times to swap determined
    * by the constant TIMES_TO_SHUFFLE.
    */
   public void shuffle()
   {
        //Zero the deck vector
        deck.removeAllElements();

        int index1 ,index2;
        for (int i = 0; i < DECK_SIZE  ; i++) {
            index1 = (int)(Math.random()*TIMES_TO_SHUFFLE);
            index2 = (int)(Math.random()*TIMES_TO_SHUFFLE);
            swap (index1, index2);
          }
   }

   /**
    * Display the entire contents of the deck. Not used in the
    * game but useful for debugging.
    */

   public void showDeck()
   {
      Iterator it = deck.iterator();
      while(it.hasNext()) {
           Card currentCard = (Card) it.next();
           System.out.println(currentCard);
           }
   }

   /**
    * Remove the top card from the deck.
    * @return the Card object removed or null if there is nothing in
the deck.
    */
   public Card takeCard()
   {
       int index = 0;
       if (index >= deckSize()) {
           return null;
       }
       else{
           return (Card) deck.remove(0);
       }
    }

   /**
    * Return size of deck
    */
   public int deckSize()
   {
       return deck.size();
   }

   /**
    * Card Swap method
    */
   public void swap(int index1, int index2) {
   Card temp = (Card)deck.get(index1);
   deck.set(index1, deck.get(index2));
   deck.set(index2, temp);

}

   /**
    * Methods returns card index and card description - testing
purpose
    */
   public void displayCardIndex()
   {
       for (int index = 0; index < deckSize(); index++)
       {
           System.out.println("Index: " + index + "  Card : " +
deck.get(index));
       }
   }
Lionel - 19 Jun 2006 04:43 GMT
> I am relatively new to java programming and I am working on a card
> project for school. I had finished and tested every method with error
> in my Deck class. However, in the time it took to pick up dinner it is
> now producing errors. Nothing changed, so I am clueless as to why
> this no longer works. I would greatly appreciate anyone input and help.
> This is the error that is produce when invoking the shuffle method.

First of all, what do you mean "tested every method"? Running the
program is not testing.

Now, your error:

>     public void shuffle()
>     {
[quoted text clipped - 8 lines]
>            }
>     }

the lines
index1 = (int)(Math.random()*TIMES_TO_SHUFFLE);
index2 = (int)(Math.random()*TIMES_TO_SHUFFLE);

Generate numbers between 0 and 1000 (TIMES_TO_SHUFFLE is 1000).

these are passed as parameters to the swap method.

>     /**
>      * Card Swap method
[quoted text clipped - 5 lines]
>
> }

I think we are missing some code, but I assume that deck.get is going to
access an ArrayList somewhere and it is going to pass a value of up to
1000. The size of a deck is defined to be 52, so, well have a look at
that and see how you go.

Lionel.
Lionel - 19 Jun 2006 04:46 GMT
>> I am relatively new to java programming and I am working on a card
>> project for school. I had finished and tested every method with error
[quoted text clipped - 43 lines]
> 1000. The size of a deck is defined to be 52, so, well have a look at
> that and see how you go.

The gist of what I said is right.

Card temp = (Card)deck.get(index1);

You're potentially trying to get a card at position 999, where as the
size of deck which is an ArrayList is only going to be as big as the
number of Card instances you added to it.

Lionel.
j0ecanad1an80 - 19 Jun 2006 04:55 GMT
> >> I am relatively new to java programming and I am working on a card
> >> project for school. I had finished and tested every method with error
[quoted text clipped - 53 lines]
>
> Lionel.

You are absolutely correct. I see the problem now. Thanks for the help.
blmblm@myrealbox.com - 19 Jun 2006 19:58 GMT
>I am relatively new to java programming and I am working on a card
>project for school. I had finished and tested every method with error
>in my Deck class. However, in the time it took to pick up dinner it is
>now producing errors. Nothing changed, so I am clueless as to why
>this no longer works. I would greatly appreciate anyone input and help.
>This is the error that is produce when invoking the shuffle method.

[ snip ]

I notice that you posted the same question to comp.lang.java.help.
If you had cross-posted rather than multi-posting, people in one
group would find it easier to follow replies in the other group,
which would seem to me to be a good thing for people who only follow
one of the groups.

IMO, anyway.  I trust that any Respected Regulars here who disagree
with me will say so.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.