Slider Puzzle

<< Space Invader Game | FinalProjectsTrailIndex | Stone Game >>

GIF files Tile0.gif through Tile.15.gif for each Tile class

Tile.java

import info.gridworld.actor.Actor;


public class Tile extends Actor {
	public int getRow(){
		return getLocation().getRow();
	}
	public int getCol(){
		return getLocation().getCol();
	}
	public void act(){
		//do nothing
	}
}

Tile0.java

The classes Tile0 through Tile15 are pretty much the same, and only exist so that the correct gif file is drawn.


public class Tile0 extends Tile {

}

SliderPuzzle.java

import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;


public class SliderPuzzle extends ActorWorld {
	private Grid gr;
	private Tile0 t0=new Tile0();
	private Tile1 t1=new Tile1();
	private Tile2 t2=new Tile2();
	private Tile3 t3=new Tile3();
	private Tile4 t4=new Tile4();
	private Tile5 t5=new Tile5();
	private Tile6 t6=new Tile6();
	private Tile7 t7=new Tile7();
	private Tile8 t8=new Tile8();
	private Tile9 t9=new Tile9();
	private Tile10 t10=new Tile10();
	private Tile11 t11=new Tile11();
	private Tile12 t12=new Tile12();
	private Tile13 t13=new Tile13();
	private Tile14 t14=new Tile14();
	private Tile15 t15=new Tile15();
	public SliderPuzzle() {
	    gr=new BoundedGrid(4,4);
	    System.setProperty("info.gridworld.gui.selection", "hide");
		System.setProperty("info.gridworld.gui.frametitle", "Slider Puzzle");
		System.setProperty("info.gridworld.gui.tooltips", "hide");
	    setMessage("Click on a tile to move them in order. \nTry to do it in as few moves as necessary");
		Tile[] tiles={t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15};
	    tiles=shuffle(tiles);
	    this.setGrid(gr);
	    for(int row=0; row<4;row++)
	    		for (int col=0; col<4;col++)
	    			add(new Location(row,col), tiles[4*row+col] );
	}
	public boolean locationClicked(Location loc){
		if(isValidMove(loc)){
			if (t0.getRow()==loc.getRow()){//slide horz
				int offset=t0.getCol()-loc.getCol();
				if(offset<0){//slide left
					setMessage("sliding left "+offset);
					slide(loc,offset,Location.EAST);
				}else{
					setMessage("sliding right "+offset);
					slide(loc,offset,Location.WEST);
				}
			}else { // slide vert
				int offset=t0.getRow()-loc.getRow();
				if(offset<0){//slide up
					setMessage("sliding up "+offset);
					slide(loc,offset,Location.SOUTH);
				}else{
					setMessage("sliding down "+offset);
					slide(loc,offset,Location.NORTH);
				}
			}
		}
		return true;
	}
	public void slide(Location lastLoc, int offset, int dir){

		Location[] slots=new Location[1+Math.abs(offset)];
		slots[0]=t0.getLocation();
		for (int i=1; i<slots.length;i++){
			slots[i]=slots[i-1].getAdjacentLocation(dir);
		}
		t0.removeSelfFromGrid();
		for (int i=1; i<slots.length;i++){
			Tile next=(Tile)gr.get(slots[i]);
			next.moveTo(slots[i-1]);
		}
		t0.putSelfInGrid(gr, slots[slots.length-1]);

	}
	public boolean isValidMove(Location loc){
		boolean result=false;
		Location space = t0.getLocation();
		if (space.getRow()==loc.getRow())
			result=true;
		if (space.getCol()==loc.getCol())
			result=true;
		if (loc.equals(space))
			result=false;
		setMessage("move is valid: "+result);
		return result;
	}
	public Tile[] shuffle(Tile[] t){
		for (int i=0; i<100; i++){
			int x = 1+(int)(15.0*Math.random());
			int y = 1+(int)(15.0*Math.random());
			Tile buffer=t[x];
			t[x]=t[y];
			t[y]=buffer;
		}
		return t;
	}
	public static void main(String[] args)
	{
        SliderPuzzle game=new SliderPuzzle();
        game.show();
	}

}

Attach:SliderPuzzle.zip