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));
}
}
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.
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.
[ 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.
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.