diff --git a/front_end/src/state.rs b/front_end/src/state.rs index 5c4feee..4966d9b 100644 --- a/front_end/src/state.rs +++ b/front_end/src/state.rs @@ -25,6 +25,21 @@ impl Display for BoardState { } } +impl From for BoardState { + fn from(t: Turn) -> Self { + match t { + Turn::CyanPlayer => BoardState::Cyan, + Turn::MagentaPlayer => BoardState::Magenta, + } + } +} + +impl BoardState { + pub fn is_empty(&self) -> bool { + *self == BoardState::Empty + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Turn { CyanPlayer, @@ -40,6 +55,15 @@ impl Display for Turn { } } +impl From for Turn { + fn from(board_state: BoardState) -> Self { + match board_state { + BoardState::Empty | BoardState::Cyan => Turn::CyanPlayer, + BoardState::Magenta => Turn::MagentaPlayer, + } + } +} + impl Turn { pub fn get_color(&self) -> &str { match *self { @@ -47,6 +71,13 @@ impl Turn { Turn::MagentaPlayer => "magenta", } } + + pub fn get_opposite(&self) -> Self { + match *self { + Turn::CyanPlayer => Turn::MagentaPlayer, + Turn::MagentaPlayer => Turn::CyanPlayer, + } + } } #[derive(Clone, Debug, PartialEq)] diff --git a/front_end/src/yew_components.rs b/front_end/src/yew_components.rs index d5d264a..817f28d 100644 --- a/front_end/src/yew_components.rs +++ b/front_end/src/yew_components.rs @@ -1,5 +1,5 @@ use crate::constants::{COLS, INFO_TEXT_MAX_ITEMS, ROWS}; -use crate::state::{BoardState, SharedState, Turn}; +use crate::state::{BoardState, SharedState}; use std::cell::Cell; use std::rc::Rc; use yew::prelude::*; @@ -155,26 +155,21 @@ impl Component for Wrapper { let current_player = shared.turn.get(); // check if clicked on empty slot - if shared.board[idx as usize].get() == BoardState::Empty { + if shared.board[idx as usize].get().is_empty() { // get bottom-most empty slot while bottom_idx + COLS < ROWS * COLS - && shared.board[(bottom_idx + COLS) as usize].get() == BoardState::Empty + && shared.board[(bottom_idx + COLS) as usize].get().is_empty() { bottom_idx += COLS; } // apply current player's color to bottom-most empty slot - shared.board[bottom_idx as usize].replace(match shared.turn.get() { - Turn::CyanPlayer => BoardState::Cyan, - Turn::MagentaPlayer => BoardState::Magenta, - }); + shared.board[bottom_idx as usize].replace(shared.turn.get().into()); + let current_board_state = shared.board[bottom_idx as usize].get(); // swap turn - shared.turn.replace(match shared.turn.get() { - Turn::CyanPlayer => Turn::MagentaPlayer, - Turn::MagentaPlayer => Turn::CyanPlayer, - }); + shared.turn.replace(shared.turn.get().get_opposite()); // get handle to slot if let Some(slot) = document.get_element_by_id(&format!("slot{bottom_idx}")) {