Separate the width and height of the grid

main
Clément FRÉVILLE 2 years ago
parent 2920e6614f
commit 7fd8d54b34

@ -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! {
<table class="board">
{ (0..board::BOARD_SIZE).map(|x| html! {
{ (0..board.height()).map(|y| html! {
<tr class="board-row">
{ (0..board::BOARD_SIZE).map(|y| html! {
{ (0..board.width()).map(|x| html! {
<PlacedTileView x={x} y={y} key={x} tile={board.get(x, y)} on_click={on_click.clone()} />
}).collect::<Html>() }
</tr>

@ -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,
}
}
}

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

Loading…
Cancel
Save