Impl convenience functions, refactoring

This commit is contained in:
Stephen Seo 2022-03-03 18:01:46 +09:00
parent eefa8f5bdc
commit 079bf6229f
2 changed files with 37 additions and 11 deletions

View file

@ -25,6 +25,21 @@ impl Display for BoardState {
} }
} }
impl From<Turn> 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)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Turn { pub enum Turn {
CyanPlayer, CyanPlayer,
@ -40,6 +55,15 @@ impl Display for Turn {
} }
} }
impl From<BoardState> for Turn {
fn from(board_state: BoardState) -> Self {
match board_state {
BoardState::Empty | BoardState::Cyan => Turn::CyanPlayer,
BoardState::Magenta => Turn::MagentaPlayer,
}
}
}
impl Turn { impl Turn {
pub fn get_color(&self) -> &str { pub fn get_color(&self) -> &str {
match *self { match *self {
@ -47,6 +71,13 @@ impl Turn {
Turn::MagentaPlayer => "magenta", Turn::MagentaPlayer => "magenta",
} }
} }
pub fn get_opposite(&self) -> Self {
match *self {
Turn::CyanPlayer => Turn::MagentaPlayer,
Turn::MagentaPlayer => Turn::CyanPlayer,
}
}
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]

View file

@ -1,5 +1,5 @@
use crate::constants::{COLS, INFO_TEXT_MAX_ITEMS, ROWS}; 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::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
use yew::prelude::*; use yew::prelude::*;
@ -155,26 +155,21 @@ impl Component for Wrapper {
let current_player = shared.turn.get(); let current_player = shared.turn.get();
// check if clicked on empty slot // 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 // get bottom-most empty slot
while bottom_idx + COLS < ROWS * COLS 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; bottom_idx += COLS;
} }
// apply current player's color to bottom-most empty slot // apply current player's color to bottom-most empty slot
shared.board[bottom_idx as usize].replace(match shared.turn.get() { shared.board[bottom_idx as usize].replace(shared.turn.get().into());
Turn::CyanPlayer => BoardState::Cyan,
Turn::MagentaPlayer => BoardState::Magenta,
});
let current_board_state = shared.board[bottom_idx as usize].get(); let current_board_state = shared.board[bottom_idx as usize].get();
// swap turn // swap turn
shared.turn.replace(match shared.turn.get() { shared.turn.replace(shared.turn.get().get_opposite());
Turn::CyanPlayer => Turn::MagentaPlayer,
Turn::MagentaPlayer => Turn::CyanPlayer,
});
// get handle to slot // get handle to slot
if let Some(slot) = document.get_element_by_id(&format!("slot{bottom_idx}")) { if let Some(slot) = document.get_element_by_id(&format!("slot{bottom_idx}")) {