From 12be9c779da86a4e68ac69819bf22e351172c948 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 21 Jan 2020 21:49:41 -0800 Subject: fixed tall stack nonsense, improved parser --- src/parser.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 73457f0..9d50377 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,12 +8,13 @@ fn parse_move(mv: &str, game: &Game) -> Result { return Err(String::from("Empty string cannot be parsed.")); } - let dc = c.unwrap(); - let sum_drops: u8; let x; + let fill_in_number; + let dc = c.unwrap(); + let mut sum_drops: u8 = 1; if (dc >= 'a') && (dc <= 'h') { - // Drop number omitted, must be 1 drop. - sum_drops = 1; + // Optional drop number omitted, assume everything + fill_in_number = true; x = dc; } else { if (dc < '1') || ('9' < dc) { @@ -21,6 +22,7 @@ fn parse_move(mv: &str, game: &Game) -> Result { "Moves must begin with a digit in 1-8 indicating the number of drops.", )); } + fill_in_number = false; sum_drops = dc.to_digit(10).unwrap() as u8; let c = chars.next(); @@ -85,18 +87,22 @@ fn parse_move(mv: &str, game: &Game) -> Result { } } - let sum = drops.iter().map(|&d| d as u32).sum::(); let pos = Position { x: x, y: y }; - if sum == 0 { - // Omitted all the drops, it's a total stack move - drops.push(0); + if fill_in_number { if let Some(stack) = game.query_square(&pos) { - drops.push(std::cmp::min(stack.len() as u8, game.get_size())); + 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 { return Err(format!( "The move called for {} stones, but {} were dropped.", -- cgit v1.3.1