findThePath method

Iterable<Point<int>> findThePath({
  1. ValueChanged<List<Point<int>>>? doneList,
})

Method that starts the search

Implementation

Iterable<Point<int>> findThePath({ValueChanged<List<Point<int>>>? doneList}) {
  _doneList.clear();
  _waitList.clear();

  if (barriers.contains(end)) {
    return [];
  }

  _addNeighbors(grid);

  Tile startTile = grid[start.x.toInt()][start.y.toInt()];
  Tile endTile = grid[end.x.toInt()][end.y.toInt()];

  Tile? winner = _getTileWinner(
    startTile,
    endTile,
  );

  List<Point<int>> path = [end];

  if (winner != null) {
    Tile? tileAux = winner.parent;
    for (int i = 0; i < winner.g - 1; i++) {
      path.add(tileAux!.position);
      tileAux = tileAux.parent;
    }
  }
  path.add(start);
  doneList?.call(_doneList.map((e) => e.position).toList());

  if (winner == null && !_isNeighbors(start, end)) {
    path.clear();
  }
  return path.reversed;
}