public void FindPath(Grid _end, Maze _maze) { if (this == _end) { this.nextPos = _end; this.nextPos.nextPos = null; return; } List <Grid> grids = new List <Grid>() { _maze.GetGrid(this.X, this.Y - 1), _maze.GetGrid(this.X, this.Y + 1), _maze.GetGrid(this.X - 1, this.Y), _maze.GetGrid(this.X + 1, this.Y), }; for (int i = grids.Count - 1; i <= 0; i++) { if (grids[i] == null) { grids.RemoveAt(i); } } if (grids.Count == 0) { return; } foreach (var grid in grids) { grid.Refish(this, _end, _maze); } grids.Sort((x, y) => x.F.CompareTo(y.F)); grids[0].lastPos = this; this.nextPos = grids[0]; this.nextPos.FindPath(_end, _maze); }