private void CheckForCollisions() { if (!(Mathf.Abs(transform.position.x) > LeftBorder)) { return; } IsGoal?.Invoke(transform.position.x > 0); BallReset(); }
public IReadOnlyList <Route> FindRoutes(BoardPoint start, IsGoal isGoal, bool stopOnFirstGoal = false, int?maxLength = null) { var result = new List <Route>(); var frontier = new Queue <BoardPoint>(); frontier.Enqueue(start); var cameFrom = new Dictionary <BoardPoint, BoardPoint> { [start] = new BoardPoint() }; while (frontier.Count > 0) { var current = frontier.Dequeue(); var currentElement = _board.GetElementAt(current); var lazyRouteLength = new Lazy <int>(() => GetRouteLength(cameFrom, start, current)); if (isGoal(currentElement, current, () => lazyRouteLength.Value)) { result.Add(CreateRoute(cameFrom, start, current, currentElement)); if (stopOnFirstGoal) { break; } } if (maxLength.HasValue && lazyRouteLength.Value > maxLength) { break; } foreach (var next in GetNeighbors(current)) { if (!cameFrom.ContainsKey(next)) { frontier.Enqueue(next); cameFrom[next] = current; } } } return(result); }
/* * Breadth First Search along Connections */ private Queue <Vector2> BFS(Vector2 from, IsGoal isGoal) { Queue <Vector2> frontier = new Queue <Vector2>(); HashSet <Vector2> set = new HashSet <Vector2>(); Dictionary <Vector2, Vector2> dict = new Dictionary <Vector2, Vector2>(); dict.Add(from, Vector2.one * int.MinValue); Vector2 curr; frontier.Enqueue(from); while (frontier.Count != 0) { curr = frontier.Dequeue(); if (isGoal(curr)) { return(ConstructPath(curr, dict)); } foreach (Connection con in ConnectionsWith(curr)) { Vector2 vec = con.left == curr ? con.right : con.left; if (set.Contains(vec)) { continue; } if (!frontier.Contains(vec)) { dict[vec] = curr; frontier.Enqueue(vec); } } set.Add(curr); } return(null); }