what is wrong with this shuffle method??
Hey guys!!! for some reason my program is displaying stuff i dont want it to display so if you could help determine what is wrong with this method that would be great. thanks!!!
Code:
public void shuffle() {
for (int i = 0; i < 200; i++) {
int randomCard = (int)(Math.random() * 52);
int randomCard2 = (int)(Math.random() * 52);
Image temp = cards[randomCard2];
cards[randomCard2] = cards[randomCard];
cards[randomCard] = temp;
}
}
Re: what is wrong with this shuffle method??
What do you mean by "stuff"? We cannot see your screen so you need to post it here.
{Prediction} Are you getting something like Classname@9ed84a? If so that is the output from the toString method in the Object class. All classes inherit this method and you need to override it in any class you write to return meaningful data.
Re: what is wrong with this shuffle method??
Hi! sorry for not being clear. I am trying to display two rows of 5 cards but it appears as though some blank cards are showing up. Here is the whole code if you need it:
Code:
import java.awt.*;
import java.applet.Applet;
public class Unit12CTYassignment extends Applet {
private Image[] cards = new Image[52];
public void init(){ // Create a card array
String images = "images/";
for (int i = 0; i < 52; i++) {
int j = i + 1;
if ( i < 13 ) {
if (i < 10)
cards[i] = getImage( getDocumentBase(), images + "c" + j + ".gif" );
else if (i == 10)
cards[i] = getImage( getDocumentBase(), images + "c" + "j.gif" );
else if (i == 11)
cards[i] = getImage( getDocumentBase(), images + "c" + "k.gif" );
else if (i == 12)
cards[i] = getImage( getDocumentBase(), images + "c" + "q.gif" );
}
else if ( i < 26 ) {
if (i < 23)
cards[i] = getImage( getDocumentBase(), images + "d" + j + ".gif" );
else if (i == 23)
cards[i] = getImage( getDocumentBase(), images + "d" + "j.gif" );
else if (i == 24)
cards[i] = getImage( getDocumentBase(), images + "d" + "k.gif" );
else if (i == 25)
cards[i] = getImage( getDocumentBase(), images + "d" + "q.gif" );
}
else if ( i < 39 ) {
if (i < 36)
cards[i] = getImage( getDocumentBase(), images + "h" + j + ".gif" );
else if (i == 36)
cards[i] = getImage( getDocumentBase(), images + "h" + "j.gif" );
else if (i == 37)
cards[i] = getImage( getDocumentBase(), images + "h" + "k.gif" );
else if (i == 38)
cards[i] = getImage( getDocumentBase(), images + "h" + "q.gif" );
}
else {
if (i <= 49)
cards[i] = getImage( getDocumentBase(), images + "s" + j + ".gif" );
else if (i == 49)
cards[i] = getImage( getDocumentBase(), images + "s" + "j.gif" );
else if (i == 50)
cards[i] = getImage( getDocumentBase(), images + "s" + "k.gif" );
else if (i == 51)
cards[i] = getImage( getDocumentBase(), images + "s" + "q.gif" );
}
}
shuffle();
}
public void paint(Graphics screen) {
for (int i = 0; i < 10; i++) {
if (i < 5)
screen.drawImage( cards[i], 5 + (i*75) , 5, this);
else
screen.drawImage( cards[i], 5 + ((i-5)*75) , 105, this);
}
}
public void shuffle() {
for (int i = 0; i < 200; i++) {
int randomCard = (int)(Math.random() * 52);
int randomCard2 = (int)(Math.random() * 52);
Image temp = cards[randomCard2];
cards[randomCard2] = cards[randomCard];
cards[randomCard] = temp;
}
}
}
Re: what is wrong with this shuffle method??
Please post the output from the program so we can see what you are seeing.
Since we don't have the images you are using we can't execute the code to see it.
Also print out the contents of the cards array before and after calling shuffle. You can use the Arrays class's toString() method to format the array for printing.