]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Impl convenience functions, refactoring
authorStephen Seo <seo.disparate@gmail.com>
Thu, 3 Mar 2022 09:01:46 +0000 (18:01 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 3 Mar 2022 09:01:46 +0000 (18:01 +0900)
front_end/src/state.rs
front_end/src/yew_components.rs

index 5c4feee81b63911729419fca019b9d2bc95473a9..4966d9bd5fd697cc0622bb6743d8749f4b0419bf 100644 (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)]
 pub enum Turn {
     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 {
     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)]
index d5d264a0f0d285c1bfd5e1f2aa0ca159b512e0d1..817f28d35fccb090fd367c863964109d209aebf9 100644 (file)
@@ -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}")) {