Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

3x3 Slider Puzzle 1D Array

843789Mar 1 2010 — edited Mar 2 2010
1. Acquire an image
2. Divide image into NINE separate images
a. You will have a TENTH image that will represent the ?removed? space
3. Create TWO 2-D Image arrays
a. One array will store the correct images as reference (use the array index as coordinates for the locations)
b. One array will store the images mixed up
4. Create ONE 2-D Boolean array
a. This array will store the location of where the empty tile is since you actually don?t delete the image
5. Create ONE 2-D Rectangle array
a. This array will store the information for the tiles (width, height, x-start, y-start) you are moving
6. Display the tiles in the right order for the correct image
7. Create a button that will remove a random image-tile
8. Allow the remaining pieces to be moved around (into the empty space) to mix up the picture
9. Keep track of number of moves made
10. Create a button that will indicate it?s the ?Solver?s? turn
11. Keep track of number of steps made to solve the puzzle
12. Check if the pieces have been moved into the right locations
13. If the puzzle is completed, add back in the removed piece
14. Play sounds at appropriate times

Hey gaise, im trying to make a 1d array 3X3 slider puzzle but the problem is it's only showing 3 images across the top row and nothing else. If you could, take a look at it and help me!!!!!!

/
*@(#)PictureSlide.java*

*PictureSlide Applet application*

*@author* 
@version 1.00 2010/2/17
*/*

*import java.awt.*;
import java.applet.*;*
*import java.awt.event.*;


public class PictureSlide extends Applet implements KeyListener {


Image[] pic;
Image[] spot;
Image tempPic;
int picX,picY,picW,picH;
int spotIndex;
int keyCode;
int emptyIndex;


public void init() {
pic=new Image[10];
pic[0]=getImage(getCodeBase(),"blank.jpg");
pic[1]=getImage(getCodeBase(),"00.jpg");
pic[2]=getImage(getCodeBase(),"01.jpg");
pic[3]=getImage(getCodeBase(),"02.jpg");
pic[4]=getImage(getCodeBase(),"0-1.jpg");
pic[5]=getImage(getCodeBase(),"0-2.jpg");
pic[6]=getImage(getCodeBase(),"1-1.jpg");
pic[7]=getImage(getCodeBase(),"1-2.jpg");
pic[8]=getImage(getCodeBase(),"2-1.jpg");
pic[9]=getImage(getCodeBase(),"2-2.jpg");


picX=10;
picY=10;
picW=109;
picH=109;
spot=new Image[10];
randomizeImageSpots();
addKeyListener(this);


}
public void randomizeImageSpots() {
for (int x=0;x<4;x++) {
spot[x]=null;
}
for (int y=0;y<4;y++) {
getNewSpot();
spot[spotIndex]=pic[y];
}
}

public void getNewSpot() {
do {
spotIndex=(int)(4*Math.random());
} while (spot[spotIndex]!=null);
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {

g.drawImage(spot[0],picX,picY,picW,picH,this);
/ *00* /
g.drawImage(spot[1],picX,picY,picW,picH,this);
/ *01* /
g.drawImage(spot[2],picX+119,picY,picW,picH,this);
/ *02* /
g.drawImage(spot[3],picX+228,picY,picW,picH,this);
/ *0-1* /
g.drawImage(spot[4],picX,picY+119,picW,picH,this);
/ *0-2* /
g.drawImage(spot[5],picX,picY+228,picW,picH,this);
/ *1-1* /
g.drawImage(spot[6],picX+119,picY+119,picW,picH,this);
/ *1-2* /
g.drawImage(spot[7],picX+119,picY+228,picW,picH,this);
/ *2-1* /
g.drawImage(spot[8],picX+228,picY+119,picW,picH,this);
/ *2-2* /
g.drawImage(spot[9],picX+228,picY+228,picW,picH,this);
requestFocus();


}

public void keyPressed(KeyEvent event) {
keyCode=event.getKeyCode();
findEmptySpot();
swapPieces();
repaint(); 
}

public void findEmptySpot() {
for (int z=0;z<4;z++) {
if (spot[z]==pic[0]) {
emptyIndex=z;
}
}
}


public void swapPieces() {
if (keyCode==38) {// up 
if ((emptyIndex==0)||(emptyIndex==1)) {
tempPic=spot[emptyIndex+2];
spot[emptyIndex+2]=spot[emptyIndex];
spot[emptyIndex]=tempPic;
}

}
else if (keyCode==40) {// down 
if ((emptyIndex==2)||(emptyIndex==3)) {
tempPic=spot[emptyIndex-2];
spot[emptyIndex-2]=spot[emptyIndex];
spot[emptyIndex]=tempPic;
}
}
else if (keyCode==37) {// left 
if ((emptyIndex==0)||(emptyIndex==2)) {
tempPic=spot[emptyIndex+1];
spot[emptyIndex+1]=spot[emptyIndex];
spot[emptyIndex]=tempPic;
}
}
else if (keyCode==39) {// right
if ((emptyIndex==1)||(emptyIndex==3)) {
tempPic=spot[emptyIndex-1];
spot[emptyIndex-1]=spot[emptyIndex];
spot[emptyIndex]=tempPic;
}
}


}

public void keyReleased(KeyEvent event) {
}

public void keyTyped(KeyEvent event) {
}



}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 30 2010
Added on Mar 1 2010
2 comments
766 views