public Algorithm(TileWorld.TileWorld world) { this.world = world; // Find the start and goalNode. int startX = 0; int startY = 0; int goalX = 0; int goalY = 0; for (int x = 0; x < world.getWidth(); x++) { for (int y = 0; y < world.getHeight(); y++) { if (world.getTileType(x, y) == eTileType.START) { startX = x; startY = y; } if (world.getTileType(x, y) == eTileType.END) { goalX = x; goalY = y; } } } startNode = GetNode(startX, startY, world.getTileType(startX, startY)); goalNode = GetNode(goalX, goalY, world.getTileType(goalX, goalY)); startNode.cost = 0; startNode.heuristic = CalculateHeuristic(startNode); open.Add(startNode); notDiscoveredYet.Add(goalNode); }
/// <summary> /// Returns a list with all the neighbours of the given node. /// </summary> /// <param name="node"></param> /// <returns>List of neighbouring nodes</returns> protected ArrayList GetNeighbourNodes(Node node) { ArrayList neighbours = new ArrayList(); /* Straight neightbours */ // If the node has a left neighbour. if (node.x > 0) { int neighbourX = node.x - 1; int neighbourY = node.y; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has a right neighbour. if (node.x < world.getWidth() - 1) { int neighbourX = node.x + 1; int neighbourY = node.y; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has a top neighbour. if (node.y > 0) { int neighbourX = node.x; int neighbourY = node.y - 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has a bottom neighbour. if (node.y < world.getHeight() - 1) { int neighbourX = node.x; int neighbourY = node.y + 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } /* Diagonal neighbours */ // If the node has an upper left neighbour. if (node.x > 0 && node.y > 0) { int neighbourX = node.x - 1; int neighbourY = node.y - 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has an upper right neighbour. if (node.x < world.getWidth() - 1 && node.y > 0) { int neighbourX = node.x + 1; int neighbourY = node.y - 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has a bottom left neighbour. if (node.x > 0 && node.y < world.getHeight() - 1) { int neighbourX = node.x - 1; int neighbourY = node.y + 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } // If the node has a bottom right neighbour. if (node.x < world.getWidth() - 1 && node.y < world.getHeight() - 1) { int neighbourX = node.x + 1; int neighbourY = node.y + 1; if (world.getTileType(neighbourX, neighbourY) != eTileType.NONWALKABLE) { neighbours.Add(GetNode(neighbourX, neighbourY, world.getTileType(neighbourX, neighbourY))); } } return(neighbours); }