Shuffle method error

J

j0ecanad1an80

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));
}
}
 
L

Lionel

j0ecanad1an80 said:
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()
{
//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);
}
}

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
*/
public void swap(int index1, int index2) {
Card temp = (Card)deck.get(index1);
deck.set(index1, deck.get(index2));
deck.set(index2, temp);

}

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.
 
L

Lionel

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

Now, your error:


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.



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.

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.
 
J

j0ecanad1an80

Lionel said:
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.

You are absolutely correct. I see the problem now. Thanks for the help.
 
B

blmblm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top