summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs24
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.",