Expand description
Basic representation of Grid, Cell, and assosiated mathematical operations. Helpful in CLI-based gamedev.
This module contains the Cell type, representing the basic unit of Grid,
the Grid type, representing a two-dimentional field of Cells,
the Cells type, representing an iterator over every Cell on the Grid,
the Rows and the Columns types, representing iterators over subgrids of Grid,
and the GridMap<V> type, representing a wrapper around the HashMap<Cell, V>
§Usecases
One of the best usecases of this crate is for developing CLI based games:
Cell has two fields representing position on the Grid, which are both u8,
and the Grid consists of the start and the end Cells,
making the largest possible Grid to be 255x255, which is enough for most terminal games.
§Note
Cell’s global position currently represented in theu8for simplicity, and because this is enough for most terminal games. This may be changed to be a scalar generic in the future.- Error handling is currently rather stupid (just checks with panic!), but this helps to prevent scary logical bugs.
- Crate is in the “work in progress” state, so the public API may change in the future. Feel free to contribute!
§Examples
Perform some basic calculations for Cell:
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let start = grid.start();
let next = start.saturating_right(grid, 5).wrapping_down(grid, 15).strict_left(grid, 1);
assert!(next.within(grid));
assert_eq!(next, Cell::new(4, 5));Map every Cell on the Grid to the custom String representation:
use grid_math::{Cell, Grid};
let grid = Grid::new(3, 3);
let grid_string = grid
.rows()
.map(|row| {
row.cells().map(|_| " [#]")
.chain(std::iter::once("\n\n"))
.collect::<String>()
})
.collect::<String>();
assert_eq!(grid_string,
" \
[#] [#] [#]
[#] [#] [#]
[#] [#] [#]
"
);Store some actual data on the Grid, using GridMap:
use grid_math::{Cell, Grid, GridMap};
let grid = Grid::new(5, 5);
let mut map: GridMap<char> = GridMap::from(grid);
map.insert(map.grid().start(), '#');
map.insert(map.grid().end(), '@');
assert_eq!(map.len(), 2);
assert_eq!(map.get(&Cell::new(0, 0)).unwrap(), &'#');Structs§
- Cell
Cellrepresents the basic unit ofGrid.- Cells
Cellsrepresents an iterator over everyCellon theGrid- Columns
Columnsrepresents an iterator over every column ofCellon theGrid- Grid
Gridrepresents the field ofCell- GridMap
GridMap<V>represents a wrapper around theHashMap<Cell, V>- Rows
Rowsrepresents an iterator over every row ofCellon theGrid