Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

card shuffling error

0 views
Skip to first unread message

j0eca...@hotmail.com

unread,
Jun 18, 2006, 11:07:56 PM6/18/06
to
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. 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));
}
}

}

IchBin

unread,
Jun 18, 2006, 11:28:58 PM6/18/06
to
j0eca...@hotmail.com wrote:
> 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. 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 */
[snip code]

It's hard debugging it with out the Card class.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Patricia Shanahan

unread,
Jun 19, 2006, 12:11:44 AM6/19/06
to
j0eca...@hotmail.com wrote:
> 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. I would greatly appreciate anyone input and help.
> This is the error that is produce when invoking the shuffle method.
...

There is an API method, Collections.shuffle, that shuffles a List, so
don't implement your own shuffle unless you are doing it as an exercise.

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

You seem to have DECK_SIZE and TIMES_TO_SHUFFLE switched around. This
will shuffle 52 times, while choosing the index value from 1 through
999, which I am sure is not what you meant.

Also, this not a very good shuffle algorithm. See the
java.util.Collections source code.

Patricia

0 new messages