From 47e7932cb5ffa20d406a6312c4f6a4f20afaa048 Mon Sep 17 00:00:00 2001 From: Adrian Gunnar Lauterer Date: Mon, 25 Mar 2024 08:53:44 +0100 Subject: [PATCH] moved validation logic to seperate call --- src/azul.rs | 54 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/azul.rs b/src/azul.rs index e7fa9ac..13c340e 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -285,30 +285,18 @@ impl GameState { match game_move.policy { Policy::Strict => self.do_move_strict(game_move), Policy::Loose => self.do_move_loose(game_move), + Policy::Validate => self.do_move_validate(game_move), } } - /// Does a move, if the move is placed where it can't fit, places it on the floor - fn do_move_loose(&mut self, game_move: GameMove) -> Result<(), MoveErr> { - match self.do_move_strict(game_move.clone()) { - Ok(()) => Ok(()), - Err(MoveErr::Dst(_)) => { - let mut new_game_move = game_move; - new_game_move.destination = Destination::Floor; - self.do_move_strict(new_game_move) - } - e => e, - } - } - /// Does a move, errors out if the move is weird in any way - fn do_move_strict(&mut self, game_move: GameMove) -> Result<(), MoveErr> { + + /// Validates a move, errors out if the move is weird in any way + fn do_move_validate(&mut self, game_move: GameMove) -> Result<(), MoveErr> { if self.current_player != game_move.player { return Err(MoveErr::Player("Not this player's turn")); } - // let player = &mut self.players[current_player]; - let color = game_move.color; let src = game_move.source; let dst = game_move.destination; @@ -347,6 +335,32 @@ impl GameState { "That pattern line and color is already filled on the wall", )) } + _ => Ok(()), + } + } + + /// Does a move, if the move is placed where it can't fit, places it on the floor + fn do_move_loose(&mut self, game_move: GameMove) -> Result<(), MoveErr> { + match self.do_move_strict(game_move.clone()) { + Ok(()) => Ok(()), + Err(MoveErr::Dst(_)) => { + let mut new_game_move = game_move; + new_game_move.destination = Destination::Floor; + self.do_move_strict(new_game_move) + } + e => e, + } + } + + /// Does a move, errors out if the move is weird in any way + fn do_move_strict(&mut self, game_move: GameMove) -> Result<(), MoveErr> { + self.do_move_validate(game_move.clone())?; + + let color = game_move.color; + let src = game_move.source; + let dst = game_move.destination; + + match (color, src, dst) { (c, s, Destination::PatternLine(p)) => { let amount = self.take_tiles(s, c); @@ -378,6 +392,7 @@ impl GameState { .add_color(c, amount as isize) .map_err(|e| MoveErr::Other(e))?; } + _ => (), }; self.current_player = self @@ -870,9 +885,14 @@ enum Policy { /// Anything weird will return an error #[serde(rename = "strict")] Strict, - #[serde(rename = "loose")] /// If you place tiles wrongly, they will fall on the floor, anything else will return an error + #[serde(rename = "loose")] Loose, + + /// Will validate the move, but not execute it + #[serde(rename = "validate")] + Validate, + // Anything weird will instead play a random move // #[serde(rename = "random")] // Random,