diff --git a/board-frontend/src/app.rs b/board-frontend/src/app.rs index c431968..97fe667 100644 --- a/board-frontend/src/app.rs +++ b/board-frontend/src/app.rs @@ -1,10 +1,11 @@ use crate::hand_view::HandView; use crate::tile_view::PlacedTileView; -use board_shared::board::{self, Board}; +use board_shared::board::Board; use board_shared::expr::is_valid_guess; use board_shared::game::Game; use board_shared::tile::Tile; use gloo_dialogs::alert; +use board_shared::position::Grid2d; use yew::prelude::*; enum SelectedTile { @@ -23,9 +24,9 @@ struct BoardViewProps { fn board_view(BoardViewProps { board, on_click }: &BoardViewProps) -> Html { html! { - { (0..board::BOARD_SIZE).map(|x| html! { + { (0..board.height()).map(|y| html! { - { (0..board::BOARD_SIZE).map(|y| html! { + { (0..board.width()).map(|x| html! { }).collect::() } diff --git a/board-shared/src/board.rs b/board-shared/src/board.rs index 7f4cca5..8fa433d 100644 --- a/board-shared/src/board.rs +++ b/board-shared/src/board.rs @@ -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; BOARD_SIZE * BOARD_SIZE], + tiles: Vec>, + width: usize, + height: usize, } impl Board { pub fn get(&self, x: usize, y: usize) -> Option { - 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 { 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, } } } diff --git a/board-shared/src/lib.rs b/board-shared/src/lib.rs index de5a1b5..d524a51 100644 --- a/board-shared/src/lib.rs +++ b/board-shared/src/lib.rs @@ -3,5 +3,5 @@ pub mod expr; pub mod game; mod lexer; mod parser; -mod position; +pub mod position; pub mod tile;