ai-22 / src / am / aua / npuzzle / TilesGoalTest.java
TilesGoalTest.java
Raw
package am.aua.npuzzle;

import am.aua.search.GoalTest;
import am.aua.search.State;

public class TilesGoalTest implements GoalTest {

	public final int[] GOAL = new int[]{1, 2, 3, 4, 5, 6, 7, 8, Tiles.EMPTY_TILE};

	public boolean isGoal(State state) {
		Tiles tiles = (Tiles)state;
		int lastTileIndex = tiles.width * tiles.width - 1;
		for (int index = lastTileIndex - 1; index >=0; --index)
			if (tiles.tiles[index] != GOAL[index])
				return false;
		return tiles.tiles[lastTileIndex] == Tiles.EMPTY_TILE;
	}
}