void RevealNeighbors(Tile tile) { var neighbors = grid.GetNeighbors(tile); foreach (var neighbor in neighbors) { Reveal(neighbor); } }
private IEnumerator DisplayDepthFirstSearch(Tile start, Tile end) { Assert.IsNotNull(m_grid); Assert.IsNotNull(start); Assert.IsNotNull(end); start.Color = Color.green; end.Color = Color.red; var visited = new Dictionary <Tile, Tile>(); visited.Add(start, null); var frontier = new Stack <Tile>(); frontier.Push(start); while (frontier.Count > 0) { var current = frontier.Pop(); if (current != start && current != end) { current.Color = m_visitedTilesColor; } if (current == end) { this.DisplayPath(start, end, visited); yield break; } var neighbors = ShuffleTiles(m_grid.GetNeighbors(current).Where(tile => tile.IsPassable).ToList()); foreach (var tile in neighbors) { if (!visited.ContainsKey(tile)) { frontier.Push(tile); visited.Add(tile, current); if (tile != end) { tile.Color = m_frontierColor; } } } yield return(new WaitForSeconds(DEFAULT_WAIT)); } }
public Node FindPath(Vector3 startPos, Vector3 finishPos) { Node startNode = grid.NodeFromPos(startPos); Node finishNode = grid.NodeFromPos(finishPos); List <Node> nextNodes = null; if (startNode != finishNode) { List <Node> openSet = new List <Node>(); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); bool temp = true; while (openSet.Count > 0 && temp) { Node currentNode = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].fCost < currentNode.fCost || (openSet[i].fCost == currentNode.fCost)) { if (openSet[i].hCost < currentNode.hCost) { currentNode = openSet[i]; } } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == finishNode) { nextNodes = RetracePath(startNode, finishNode); temp = false; } foreach (Node neighbor in grid.GetNeighbors(currentNode)) { if (neighbor.walkable && !closedSet.Contains(neighbor)) { int newMoveCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor); if (newMoveCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor)) { neighbor.gCost = newMoveCostToNeighbor; neighbor.hCost = GetDistance(neighbor, finishNode); neighbor.parent = currentNode; if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } } } } } if (nextNodes != null) { return(nextNodes[0]); } } return(null); }