-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[example] Array example using a grid #74
Open
VietnameseRick
wants to merge
8
commits into
dojoengine:main
Choose a base branch
from
VietnameseRick:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3da6d5c
Grid Map
32a0559
Fix scarb format
VietnameseRick 37610cb
Merge branch 'dojoengine:main' into main
VietnameseRick 64670cb
Array example using grid map
VietnameseRick 89e3c62
Merge branch 'dojoengine:main' into main
VietnameseRick 98e871b
Update examples/grid_map/src/actions.cairo
VietnameseRick 5e6d9b0
Update examples/grid_map/src/actions.cairo
VietnameseRick 0804c8c
Merge branch 'dojoengine:main' into main
VietnameseRick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,8 @@ mod map { | |
mod hex; | ||
mod types; | ||
} | ||
mod grid { | ||
mod grid; | ||
mod types; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
use core::array::ArrayTrait; | ||
use origami::map::grid::{types::{GridTile, Direction, DirectionIntoFelt252}}; | ||
|
||
trait IGridTile { | ||
fn new(col: u32, row: u32) -> GridTile; | ||
fn neighbor(self: GridTile, direction: Direction) -> GridTile; | ||
fn neighbors(self: GridTile) -> Array<GridTile>; | ||
fn is_neighbor(self: GridTile, other: GridTile) -> bool; | ||
fn tiles_within_range(self: GridTile, range: u32) -> Array<GridTile>; | ||
} | ||
|
||
impl ImplGridTile of IGridTile { | ||
fn new(col: u32, row: u32) -> GridTile { | ||
GridTile { col, row } | ||
} | ||
|
||
fn neighbor(self: GridTile, direction: Direction) -> GridTile { | ||
match direction { | ||
Direction::East(()) => GridTile { col: self.col + 1, row: self.row }, | ||
Direction::North(()) => GridTile { col: self.col, row: self.row - 1 }, | ||
Direction::West(()) => GridTile { col: self.col - 1, row: self.row }, | ||
Direction::South(()) => GridTile { col: self.col, row: self.row + 1 }, | ||
} | ||
} | ||
|
||
fn neighbors(self: GridTile) -> Array<GridTile> { | ||
return array![ | ||
self.neighbor(Direction::East(())), | ||
self.neighbor(Direction::North(())), | ||
self.neighbor(Direction::West(())), | ||
self.neighbor(Direction::South(())) | ||
]; | ||
} | ||
|
||
fn is_neighbor(self: GridTile, other: GridTile) -> bool { | ||
let mut neighbors = self.neighbors(); | ||
|
||
loop { | ||
if (neighbors.len() == 0) { | ||
break false; | ||
} | ||
|
||
let curent_neighbor = neighbors.pop_front().unwrap(); | ||
|
||
if (curent_neighbor.col == other.col) { | ||
if (curent_neighbor.row == other.row) { | ||
break true; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
fn tiles_within_range(self: GridTile, range: u32) -> Array<GridTile> { | ||
let mut queue = array![self]; | ||
let mut visited = array![self]; | ||
let mut moves = 0; | ||
|
||
loop { | ||
if moves == range { | ||
break; | ||
} | ||
let mut next_queue = array![]; | ||
loop { | ||
if queue.len() == 0 { | ||
break; | ||
} | ||
let tile = queue.pop_front().unwrap(); | ||
let mut neighbors = tile.neighbors(); | ||
|
||
loop { | ||
if neighbors.len() == 0 { | ||
break; | ||
} | ||
let neighbor = neighbors.pop_front().unwrap(); | ||
let mut is_visited = false; | ||
let mut index = 0; | ||
let visited_span = visited.span(); | ||
|
||
loop { | ||
if index == visited_span.len() || is_visited == true { | ||
break; | ||
} | ||
let curr = *visited_span.at(index); | ||
if (curr.col == neighbor.col && curr.row == neighbor.row) { | ||
is_visited = true; | ||
} | ||
index = index + 1; | ||
}; | ||
if !is_visited { | ||
next_queue.append(neighbor); | ||
visited.append(neighbor); | ||
} | ||
}; | ||
}; | ||
queue = next_queue.clone(); | ||
moves = moves + 1; | ||
}; | ||
return visited; | ||
} | ||
} | ||
|
||
|
||
// tests ----------------------------------------------------------------------- // | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{IGridTile, ImplGridTile, Direction, GridTile}; | ||
#[test] | ||
fn test_row_col() { | ||
let mut grid_tile = ImplGridTile::new(5, 5); | ||
|
||
assert(grid_tile.col == 5, 'col should be 5'); | ||
assert(grid_tile.row == 5, 'row should be 5'); | ||
} | ||
|
||
|
||
#[test] | ||
fn test_grid_tile_neighbors() { | ||
let mut grid_tile = ImplGridTile::new(5, 5); | ||
|
||
let east_neighbor = grid_tile.neighbor(Direction::East(())); | ||
|
||
assert(east_neighbor.col == 6, 'col should be 6'); | ||
assert(east_neighbor.row == 5, 'row should be 5'); | ||
|
||
let north_neighbor = grid_tile.neighbor(Direction::North(())); | ||
|
||
assert(north_neighbor.col == 5, 'col should be 5'); | ||
assert(north_neighbor.row == 4, 'row should be 4'); | ||
|
||
let west_neighbor = grid_tile.neighbor(Direction::West(())); | ||
|
||
assert(west_neighbor.col == 4, 'col should be 4'); | ||
assert(west_neighbor.row == 5, 'row should be 5'); | ||
|
||
let south_neighbor = grid_tile.neighbor(Direction::South(())); | ||
|
||
assert(south_neighbor.col == 5, 'col should be 4'); | ||
assert(south_neighbor.row == 6, 'row should be 6'); | ||
} | ||
|
||
#[test] | ||
fn test_is_neighbor() { | ||
let mut grid_tile = ImplGridTile::new(5, 5); | ||
|
||
assert( | ||
grid_tile.is_neighbor(GridTile { col: grid_tile.col + 1, row: grid_tile.row }), 'east' | ||
); | ||
|
||
assert( | ||
grid_tile.is_neighbor(GridTile { col: grid_tile.col, row: grid_tile.row + 1 }), 'south' | ||
); | ||
|
||
assert( | ||
grid_tile.is_neighbor(GridTile { col: grid_tile.col, row: grid_tile.row - 1 }), 'north' | ||
); | ||
|
||
assert( | ||
grid_tile.is_neighbor(GridTile { col: grid_tile.col - 1, row: grid_tile.row }), 'west' | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_tiles_within_range() { | ||
let grid_tile = ImplGridTile::new(5, 5); | ||
let tiles_range_one = grid_tile.tiles_within_range(1); | ||
let tiles_range_two = grid_tile.tiles_within_range(2); | ||
let tiles_range_three = grid_tile.tiles_within_range(3); | ||
// Including the center tile | ||
assert_eq!(tiles_range_one.len(), 5, "should be 5"); | ||
assert_eq!(tiles_range_two.len(), 13, "should be 13"); | ||
assert_eq!(tiles_range_three.len(), 25, "should be 25"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#[derive(Copy, Drop, Serde, Introspect)] | ||
struct GridTile { | ||
col: u32, | ||
row: u32, | ||
} | ||
|
||
#[derive(Drop, Copy, Serde)] | ||
enum Direction { | ||
East: (), | ||
North: (), | ||
West: (), | ||
South: (), | ||
} | ||
|
||
impl DirectionIntoFelt252 of Into<Direction, felt252> { | ||
fn into(self: Direction) -> felt252 { | ||
match self { | ||
Direction::East => 0, | ||
Direction::North => 1, | ||
Direction::West => 2, | ||
Direction::South => 3, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "grid_map" | ||
version = "0.0.0" | ||
description = "Example Grid Map using Arrays" | ||
homepage = "https://github.com/dojoengine/origami/tree/examples/grid_map" | ||
|
||
[dependencies] | ||
cubit.workspace = true | ||
dojo.workspace = true | ||
origami.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Grid map using Array | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a brief explanation in the readme |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use:
#[generate_trait]
to make code cleaner