aima.core.environment.eightpuzzle.EightPuzzleFunctionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.environment.eightpuzzle;
import java.util.LinkedHashSet;
import java.util.Set;
import aima.core.agent.Action;
import aima.core.search.framework.problem.ActionsFunction;
import aima.core.search.framework.problem.ResultFunction;
/**
* @author Ravi Mohan
* @author Ciaran O'Reilly
*/
public class EightPuzzleFunctionFactory {
private static ActionsFunction _actionsFunction = null;
private static ResultFunction _resultFunction = null;
public static ActionsFunction getActionsFunction() {
if (null == _actionsFunction) {
_actionsFunction = new EPActionsFunction();
}
return _actionsFunction;
}
public static ResultFunction getResultFunction() {
if (null == _resultFunction) {
_resultFunction = new EPResultFunction();
}
return _resultFunction;
}
private static class EPActionsFunction implements ActionsFunction {
public Set actions(Object state) {
EightPuzzleBoard board = (EightPuzzleBoard) state;
Set actions = new LinkedHashSet();
if (board.canMoveGap(EightPuzzleBoard.UP)) {
actions.add(EightPuzzleBoard.UP);
}
if (board.canMoveGap(EightPuzzleBoard.DOWN)) {
actions.add(EightPuzzleBoard.DOWN);
}
if (board.canMoveGap(EightPuzzleBoard.LEFT)) {
actions.add(EightPuzzleBoard.LEFT);
}
if (board.canMoveGap(EightPuzzleBoard.RIGHT)) {
actions.add(EightPuzzleBoard.RIGHT);
}
return actions;
}
}
private static class EPResultFunction implements ResultFunction {
public Object result(Object s, Action a) {
EightPuzzleBoard board = (EightPuzzleBoard) s;
if (EightPuzzleBoard.UP.equals(a)
&& board.canMoveGap(EightPuzzleBoard.UP)) {
EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
newBoard.moveGapUp();
return newBoard;
} else if (EightPuzzleBoard.DOWN.equals(a)
&& board.canMoveGap(EightPuzzleBoard.DOWN)) {
EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
newBoard.moveGapDown();
return newBoard;
} else if (EightPuzzleBoard.LEFT.equals(a)
&& board.canMoveGap(EightPuzzleBoard.LEFT)) {
EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
newBoard.moveGapLeft();
return newBoard;
} else if (EightPuzzleBoard.RIGHT.equals(a)
&& board.canMoveGap(EightPuzzleBoard.RIGHT)) {
EightPuzzleBoard newBoard = new EightPuzzleBoard(board);
newBoard.moveGapRight();
return newBoard;
}
// The Action is not understood or is a NoOp
// the result will be the current state.
return s;
}
}
}