Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥 Use exisiting function for finding neighbor positions and remove ex…
Browse files Browse the repository at this point in the history
…tra code
bal7hazar committed Oct 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7c7452c commit 711c743
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions crates/map/src/helpers/bfs.cairo
Original file line number Diff line number Diff line change
@@ -56,9 +56,7 @@ pub impl BFS of BFSTrait {
while directions != 0 {
let direction = DirectionTrait::pop_front(ref directions);
if Astar::check(grid, width, height, current.position, direction, ref visited) {
let neighbor_position = Self::get_neighbor_position(
current.position, direction, width
);
let neighbor_position = direction.next(current.position, width);
parents.insert(neighbor_position.into(), current.position);
let neighbor = NodeTrait::new(neighbor_position, current.position, 0, 0);
queue.append(neighbor);
@@ -74,18 +72,6 @@ pub impl BFS of BFSTrait {
Self::path(parents, start, target)
}

/// Calculates the position of a neighbor in the given direction
#[inline]
fn get_neighbor_position(position: u8, direction: Direction, width: u8) -> u8 {
match direction {
Direction::North => position + width,
Direction::East => position + 1,
Direction::South => position - width,
Direction::West => position - 1,
_ => 0,
}
}

/// Reconstructs the path from start to target using the parents dictionary
#[inline]
fn path(mut parents: Felt252Dict<u8>, start: Node, target: Node) -> Span<u8> {
@@ -149,7 +135,7 @@ mod test {
let from = 0;
let to = 14;
let path = BFS::search(grid, width, height, from, to);
assert_eq!(path, array![14, 15, 11, 7, 6, 5, 1].span());
assert_eq!(path, array![14, 15, 11, 7, 6, 5, 4].span());
}

#[test]

0 comments on commit 711c743

Please sign in to comment.