Compare commits

..

No commits in common. "b05ecb035a89ea626e9df4f39b6ee8cc57db5d96" and "7ea62870d0124cfd3492d3fe1c35a2163a5af806" have entirely different histories.

1 changed files with 18 additions and 45 deletions

View File

@ -255,14 +255,7 @@ impl GameState {
return Err(MoveErr::Other("Game is over")); return Err(MoveErr::Other("Game is over"));
} }
self.do_move(game_move.clone()).map_err(|e| e)?; self.do_move(game_move).map_err(|e| e)?;
match game_move.policy {
//if validate return ok and do not iterate
Policy::Validate => return Ok(()),
//else continue the function
_ => (),
}
if self.is_empty() { if self.is_empty() {
self.score_unchecked().map_err(|e| MoveErr::Other(e))?; self.score_unchecked().map_err(|e| MoveErr::Other(e))?;
@ -292,18 +285,30 @@ impl GameState {
match game_move.policy { match game_move.policy {
Policy::Strict => self.do_move_strict(game_move), Policy::Strict => self.do_move_strict(game_move),
Policy::Loose => self.do_move_loose(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
/// Validates a move, errors out if the move is weird in any way fn do_move_strict(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
fn do_move_validate(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
if self.current_player != game_move.player { if self.current_player != game_move.player {
return Err(MoveErr::Player("Not this player's turn")); return Err(MoveErr::Player("Not this player's turn"));
} }
// let player = &mut self.players[current_player];
let color = game_move.color; let color = game_move.color;
let src = game_move.source; let src = game_move.source;
let dst = game_move.destination; let dst = game_move.destination;
@ -342,32 +347,6 @@ impl GameState {
"That pattern line and color is already filled on the wall", "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)) => { (c, s, Destination::PatternLine(p)) => {
let amount = self.take_tiles(s, c); let amount = self.take_tiles(s, c);
@ -399,7 +378,6 @@ impl GameState {
.add_color(c, amount as isize) .add_color(c, amount as isize)
.map_err(|e| MoveErr::Other(e))?; .map_err(|e| MoveErr::Other(e))?;
} }
_ => (),
}; };
self.current_player = self self.current_player = self
@ -892,14 +870,9 @@ enum Policy {
/// Anything weird will return an error /// Anything weird will return an error
#[serde(rename = "strict")] #[serde(rename = "strict")]
Strict, Strict,
/// If you place tiles wrongly, they will fall on the floor, anything else will return an error
#[serde(rename = "loose")] #[serde(rename = "loose")]
/// If you place tiles wrongly, they will fall on the floor, anything else will return an error
Loose, Loose,
/// Will validate the move, but not execute it
#[serde(rename = "validate")]
Validate,
// Anything weird will instead play a random move // Anything weird will instead play a random move
// #[serde(rename = "random")] // #[serde(rename = "random")]
// Random, // Random,