From 5dc11de37de4aa974f19242eaa8dc8c0f6c56d34 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Wed, 9 Aug 2023 20:00:14 -0700 Subject: [PATCH] WIP Add Grid --- src/board.rs | 8 +++--- src/game.rs | 12 ++++++--- src/wasm.rs | 11 +++++++++ ui/src/elements.tsx | 60 +++++++++++++++++++++++++++++++++++++++++++-- ui/src/index.tsx | 3 +++ ui/src/style.less | 38 ++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/board.rs b/src/board.rs index 79a0257..b75eaf3 100644 --- a/src/board.rs +++ b/src/board.rs @@ -91,7 +91,7 @@ impl Letter { } } -#[derive(Debug)] +#[derive(Debug, Copy, Clone, Serialize)] pub enum CellType { Normal, DoubleWord, @@ -104,13 +104,13 @@ pub enum CellType { #[derive(Debug)] pub struct Cell { pub value: Option, - cell_type: CellType, - coordinates: Coordinates, + pub cell_type: CellType, + pub coordinates: Coordinates, } #[derive(Debug)] pub struct Board { - cells: Vec, + pub cells: Vec, } pub struct Word<'a> { diff --git a/src/game.rs b/src/game.rs index a3b0beb..108cd71 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,7 +1,7 @@ use rand::rngs::SmallRng; use rand::SeedableRng; use wasm_bindgen::prelude::wasm_bindgen; -use crate::board::Letter; +use crate::board::{Board, Letter}; use crate::constants::{standard_tile_pool, TRAY_LENGTH}; use crate::player_interaction::ai::Difficulty; use crate::player_interaction::Tray; @@ -21,7 +21,8 @@ pub struct PlayerState { } pub struct Game { - tiles: Vec, + tile_pool: Vec, + board: Board, player: PlayerState, } @@ -51,7 +52,8 @@ impl Game { }; Game { - tiles: letters, + tile_pool: letters, + board: Board::new(), player, } } @@ -63,5 +65,7 @@ impl Game { pub fn get_tray_mut(&mut self) -> &mut Tray { &mut self.player.tray } - + + pub fn get_board(&self) -> &Board {&self.board} + } \ No newline at end of file diff --git a/src/wasm.rs b/src/wasm.rs index 0f8571c..4682e2a 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -1,6 +1,7 @@ use serde_wasm_bindgen::Error; use wasm_bindgen::JsValue; use wasm_bindgen::prelude::wasm_bindgen; +use crate::board::CellType; use crate::game::Game; #[wasm_bindgen] @@ -19,4 +20,14 @@ impl GameWasm { serde_wasm_bindgen::to_value(tray) } + + pub fn get_board_cell_types(&self) -> Result { + let board = self.0.get_board(); + + let cell_types: Vec = board.cells.iter().map(|cell| -> CellType { + cell.cell_type.clone() + }).collect(); + + serde_wasm_bindgen::to_value(&cell_types) + } } \ No newline at end of file diff --git a/ui/src/elements.tsx b/ui/src/elements.tsx index 8405994..5e8065e 100644 --- a/ui/src/elements.tsx +++ b/ui/src/elements.tsx @@ -1,13 +1,22 @@ import * as React from "react"; import {GameWasm, Letter as LetterData, Tray} from "word_grid"; import {createRoot} from "react-dom/client"; -import {Children, useReducer, useState} from "react"; +import {Children, useMemo, useReducer, useState} from "react"; export enum LocationType { GRID, TRAY } +enum CellType { + Normal = "Normal", + DoubleWord = "DoubleWord", + DoubleLetter = "DoubleLetter", + TripleLetter = "TripleLetter", + TripleWord = "TripleWord", + Start = "Start", +} + export interface CoordinateData { location: LocationType; index: number; @@ -53,6 +62,10 @@ function movePlayableLetters(playerLetters: PlayableLetterData[], update: {start export function Game(props: {wasm: GameWasm}) { + const cellTypes = useMemo(() => { + return props.wasm.get_board_cell_types(); + }, []); + const [playerLetters, dispatch] = useReducer(movePlayableLetters, null, (_) => { let tray: Tray = props.wasm.get_tray(); @@ -67,7 +80,10 @@ export function Game(props: {wasm: GameWasm}) { }) + + return <> +