diff options
| author | tslil clingman <> | 2020-01-21 21:49:41 -0800 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-21 21:49:41 -0800 |
| commit | 12be9c779da86a4e68ac69819bf22e351172c948 (patch) | |
| tree | bd9cae2d87338289c21363d535c091d983d658e1 /src/parser.rs | |
| parent | fa04cc6ecf3ac1b4b8e8561c5969ee64f09f59fa (diff) | |
fixed tall stack nonsense, improved parser
Diffstat (limited to 'src/parser.rs')
| -rw-r--r-- | src/parser.rs | 24 |
1 files changed, 15 insertions, 9 deletions
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<Action, String> { 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<Action, String> { "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<Action, String> { } } - let sum = drops.iter().map(|&d| d as u32).sum::<u32>(); 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::<u32>(); + 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.", |
