Separate the width and height of the grid

This commit is contained in:
2023-02-10 20:39:13 +01:00
parent 2920e6614f
commit 7fd8d54b34
3 changed files with 30 additions and 12 deletions

View File

@@ -1,26 +1,28 @@
use crate::position::Position2d;
use crate::position::{Grid2d, Position2d};
use crate::tile::Tile;
pub const BOARD_SIZE: usize = 25;
const DEFAULT_BOARD_SIZE: usize = 25;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Board {
tiles: [Option<Tile>; BOARD_SIZE * BOARD_SIZE],
tiles: Vec<Option<Tile>>,
width: usize,
height: usize,
}
impl Board {
pub fn get(&self, x: usize, y: usize) -> Option<Tile> {
self.tiles[y * BOARD_SIZE + x]
self.tiles[y * self.width + x]
}
pub fn set(&mut self, x: usize, y: usize, tile: Tile) {
self.tiles[y * BOARD_SIZE + x] = Some(tile);
self.tiles[y * self.width + x] = Some(tile);
}
pub fn difference(&self, other: &Board) -> Vec<Position2d> {
let mut diff = Vec::new();
for x in 0..BOARD_SIZE {
for y in 0..BOARD_SIZE {
for y in 0..self.height {
for x in 0..self.width {
if self.get(x, y) != other.get(x, y) {
diff.push(Position2d::new(x, y));
}
@@ -55,10 +57,25 @@ impl Board {
}
}
impl Grid2d for Board {
fn width(&self) -> usize {
self.width
}
fn height(&self) -> usize {
self.height
}
}
impl Default for Board {
fn default() -> Self {
let size = DEFAULT_BOARD_SIZE * DEFAULT_BOARD_SIZE;
let mut tiles = Vec::with_capacity(size);
tiles.resize_with(size, || None);
Self {
tiles: [None; BOARD_SIZE * BOARD_SIZE],
tiles,
width: DEFAULT_BOARD_SIZE,
height: DEFAULT_BOARD_SIZE,
}
}
}

View File

@@ -3,5 +3,5 @@ pub mod expr;
pub mod game;
mod lexer;
mod parser;
mod position;
pub mod position;
pub mod tile;