private string Restitute(Pos start, PathCoord foundPath) { while (foundPath != null) { int distance = getDistance(start, foundPath.current); if (distance == 1) { return foundPath.previousDirection; } foundPath = foundPath.previous; } Console.WriteLine("Error with restitute"); return Direction.Stay; }
private List<PathCoord> getAvailableCoords(PathCoord current, GameState state, Pos goal) { PathCoord east = createPathCoord(new Pos() { x = current.current.x, y = current.current.y + 1 }, current, state, goal, Direction.East); PathCoord west = createPathCoord(new Pos() { x = current.current.x, y = current.current.y - 1 }, current, state, goal, Direction.West); PathCoord north = createPathCoord(new Pos() { x = current.current.x - 1, y = current.current.y }, current, state, goal, Direction.North); PathCoord south = createPathCoord(new Pos() { x = current.current.x + 1, y = current.current.y }, current, state, goal, Direction.South); var rc = new List<PathCoord>(); if (east != null) { rc.Add(east); } if (west != null) { rc.Add(west); } if (north != null) { rc.Add(north); } if (south != null) { rc.Add(south); } return rc; }
private PathCoord createPathCoord(Pos current, PathCoord previous, GameState state, Pos goal, string previousDirection) { try { if (!isValid(state, current)) { return null; } PathCoord pc = new PathCoord(); if (state.board[current.y][current.x] == Tile.IMPASSABLE_WOOD) { return null; } if (previous != null) { pc.weight = previous.weight; // to modify } pc.weight += getCost(state.board[current.y][current.x]); pc.current = current; pc.previous = previous; pc.previousDirection = previousDirection; pc.heuristic = pc.weight + getDistance(current, goal); return pc; } catch (Exception e) { Console.WriteLine("Error with bound"); throw e; } }
private PathCoord createPathCoord(Pos current, PathCoord previous, GameState state, Pos goal, string previousDirection) { try { if (!isValid(state, current)) { return null; } PathCoord pc = new PathCoord(); var currentTile = state.board.At(current); if ( !(currentTile == Tile.FREE || currentTile == Tile.SPIKES || areEqual(goal, current) || (currentTile >= Tile.HERO_1 && currentTile <= Tile.HERO_4))) { return null; } if (previous != null) { pc.weight = previous.weight; // to modify } pc.weight += getCost(state, current); pc.current = current; pc.previous = previous; pc.previousDirection = previousDirection; pc.heuristic = pc.weight + getDistance(current, goal); // Void path if it would kill us if (CanKill(currentTile) && state.myHero.life < pc.weight) { return null; } return pc; } catch (Exception e) { Console.WriteLine("Error with bound"); throw e; } }