[][src]Crate gridd

A generic, dirt-simple, two-dimensional grid.

Grids & Coordinates

The Grid structure represents a two-dimensional grid containing arbitrary data within its cells. Data is accessed via the Coord type, which is equivalent to the (usize, usize) type. Cell indices are of the form (column, row) where column and row are non-negative values.

Offset Vectors

Gridd offers Offsets for working with positional relationships. This allows the API to stay small while still offering a more convenient abstraction for relational methods and iterators. Here's how you might implement a knight_moves method using Gridd:

use gridd::{Coord, Grid, Offset};

struct ChessGame<T> {
    board: Grid<T>
}

impl<T> ChessGame<T> {
    pub(crate) fn rotate(os: &mut Offset) {
        let new_c = os.row_offset;

        os.row_offset = os.col_offset;
        os.col_offset = new_c * (-1);
    }

    pub fn knight_moves(&self, rook_pos: Coord) -> Vec<&T> {
        let mut coords = Vec::new();

        let mut move1 = Offset::from((2, 1));
        let mut move2 = Offset::from((1, 2));

        for _ in 0..4 {
            if let Some(square_data) = self.board.rget(rook_pos, move1) {
                coords.push(square_data);
            }
            if let Some(square_data) = self.board.rget(rook_pos, move2) {
                coords.push(square_data);
            }
            Self::rotate(&mut move1);
            Self::rotate(&mut move2);
        }

        coords
    }
}

It's also worth noting that Gridd's approach doesn't impose any order on the final knight_moves method, offering greater flexibility.

Implementations are provided for scalar multiplication, vector addition, and vector subtraction.

Structs

Grid

Two-dimensional, non-resizeable, zero-indexed grid.

Offset

A two-dimensional offset vector used to relate grid elements spatially.

Type Definitions

Coord

Coordinates of the form (column, row), where column >= 0 and row >= 0.