summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
authortslil clingman <>2020-01-27 22:16:59 -0800
committertslil clingman <>2020-01-27 22:16:59 -0800
commit38200848953b1d8d7ba083752c2621603842b54e (patch)
tree9e13f26df343d472c040ac6678a9836c5ba11eb7 /src/parser.rs
parent87c2d3319effd4d6be83875bdb455be9077c0c1c (diff)
fix ptn issues
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs44
1 files changed, 14 insertions, 30 deletions
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<Action, String> {
+fn parse_move(mv: &str) -> Result<Action, String> {
let mut chars = mv.chars();
let c = chars.next();
@@ -9,21 +9,19 @@ fn parse_move(mv: &str, game: &Game) -> Result<Action, String> {
}
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<Action, String> {
}
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::<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 {
+ 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<Action, String> {
+fn parse_place(pl: &str, stone_owner: Player) -> Result<Action, String> {
let mut chars = pl.chars();
let c = chars.next();
@@ -167,20 +155,16 @@ fn parse_place(pl: &str, game: &Game) -> Result<Action, String> {
}
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<Action, String> {
+pub fn parse_action(stone_owner: Player, act: &str) -> Result<Action, String> {
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)
}
}