public int Calculate(ANode node, ANode goal) { int result = 0; var currentNode = node as ANode; var goalNode = goal as ANode; List <double> list = new List <double>(); int currentNumber; int currentIndex; for (int i = 0; i < tilesNumber; i++) { NPuzzleState state = (NPuzzleState)currentNode.State; currentNumber = state.Tiles[i]; if (currentNumber == 0) { continue; } currentIndex = FindTileCurrentIndex(currentNumber, goalNode); result += GetDistanceToGoalTileForIndex(i, currentIndex); } return(result); }
public static IState RightMove(IState currentState, int N) { NPuzzleState state = (NPuzzleState)currentState; int EmptyTileIndex = state.EmptyTileIndex; var newTiles = state.Tiles.Clone() as byte[]; if (newTiles == null) { return(null); } newTiles[EmptyTileIndex] = newTiles[EmptyTileIndex + 1]; newTiles[EmptyTileIndex + 1] = 0; return(new NPuzzleState(newTiles)); }
private int FindTileCurrentIndex(int goalNumber, ANode current) { NPuzzleState state = (NPuzzleState)current.State; for (int j = 0; j < tilesNumber; j++) { if (state.Tiles[j] == goalNumber) { return(j); } } return(-1); }
private static byte GetEmptyTilePosition(NPuzzleState node) { byte emptyTilePos = byte.MaxValue; for (byte i = 0; i < node.Tiles.Length; i++) { if (node.Tiles[i] == 0) { emptyTilePos = i; break; } } return(emptyTilePos); }
public IEnumerable <IOperator> ApplicableOperators(IState currentState) { NPuzzleState state = (NPuzzleState)currentState; List <IOperator> applicableOperators = new List <IOperator>(); if (state.EmptyTileIndex < N * (N - 1)) { applicableOperators.Add(MoveOperator.Down); } if (state.EmptyTileIndex % N > 0) { applicableOperators.Add(MoveOperator.Left); } if (state.EmptyTileIndex % N < N - 1) { applicableOperators.Add(MoveOperator.Right); } if (state.EmptyTileIndex > N - 1) { applicableOperators.Add(MoveOperator.Up); } return(applicableOperators); }
public IOutcome act(IState currentState, IOperator op) { NPuzzleState state = (NPuzzleState)currentState; IState succ; if (op.Equals(MoveOperator.Up)) { if (state.EmptyTileIndex > N - 1) { succ = MoveOperator.UpMove(state, N); return(new Outcome(succ, cost)); } else { throw new NotApplicableOperatorException("not applicable operator"); } } if (op.Equals(MoveOperator.Down)) { if (state.EmptyTileIndex < N * (N - 1)) { succ = MoveOperator.DownMove(state, N); return(new Outcome(succ, cost)); } else { throw new NotApplicableOperatorException("not applicable operator"); } } if (op.Equals(MoveOperator.Left)) { if (state.EmptyTileIndex % N > 0) { succ = MoveOperator.LeftMove(state, N); return(new Outcome(succ, cost)); } else { throw new NotApplicableOperatorException("not applicable operator"); } } if (op.Equals(MoveOperator.Right)) { if (state.EmptyTileIndex % N < N - 1) { succ = MoveOperator.RightMove(state, N); return(new Outcome(succ, cost)); } else { throw new NotApplicableOperatorException("not applicable operator"); } } throw new NotApplicableOperatorException("Unknown operator"); }