From 38200848953b1d8d7ba083752c2621603842b54e Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Mon, 27 Jan 2020 22:16:59 -0800 Subject: fix ptn issues --- src/parser.rs | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 9d50377..68e49a8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,6 @@ use crate::game::*; -fn parse_move(mv: &str, game: &Game) -> Result { +fn parse_move(mv: &str) -> Result { let mut chars = mv.chars(); let c = chars.next(); @@ -9,21 +9,19 @@ fn parse_move(mv: &str, game: &Game) -> Result { } let x; - let fill_in_number; + let picked_up; let dc = c.unwrap(); - let mut sum_drops: u8 = 1; if (dc >= 'a') && (dc <= 'h') { // Optional drop number omitted, assume everything - fill_in_number = true; x = dc; + picked_up = 1; } else { if (dc < '1') || ('9' < dc) { return Err(String::from( - "Moves must begin with a digit in 1-8 indicating the number of drops.", + "Moves must begin with a digit in 1-8 indicating the stones picked up.", )); } - fill_in_number = false; - sum_drops = dc.to_digit(10).unwrap() as u8; + picked_up = dc.to_digit(10).unwrap() as u8; let c = chars.next(); if c.is_none() { @@ -88,32 +86,22 @@ fn parse_move(mv: &str, game: &Game) -> Result { } let pos = Position { x: x, y: y }; - if fill_in_number { - if let Some(stack) = game.query_square(&pos) { - sum_drops = std::cmp::min(stack.len() as u8, game.get_size()); - } else { - return Err(String::from( - "Moves must specify a valid position on the game board.", - )); - } - } let sum = drops.iter().map(|&d| d as u32).sum::(); if sum == 0 { // Omitted all the drops, it's a total stack move - drops.push(0); - drops.push(sum_drops); - } else if sum != sum_drops as u32 { + drops.push(picked_up); + } else if sum != picked_up as u32 { return Err(format!( "The move called for {} stones, but {} were dropped.", - sum_drops, sum + picked_up, sum )); } - Ok(Action::Move(game.query_stone_owner(), pos, dir, drops)) + Ok(Action::Move(pos, dir, picked_up, drops)) } -fn parse_place(pl: &str, game: &Game) -> Result { +fn parse_place(pl: &str, stone_owner: Player) -> Result { let mut chars = pl.chars(); let c = chars.next(); @@ -167,20 +155,16 @@ fn parse_place(pl: &str, game: &Game) -> Result { } let y: u8 = (y.to_digit(10).unwrap() - 1) as u8; - Ok(Action::Place( - game.query_stone_owner(), - Position { x: x, y: y }, - stone, - )) + Ok(Action::Place(stone_owner, Position { x: x, y: y }, stone)) } -pub fn parse_action(game: &Game, act: &str) -> Result { +pub fn parse_action(stone_owner: Player, act: &str) -> Result { if act .chars() .any(|c| c == '+' || c == '-' || c == '>' || c == '<') { - parse_move(act, game) + parse_move(act) } else { - parse_place(act, game) + parse_place(act, stone_owner) } } -- cgit v1.3.1