summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs90
1 files changed, 54 insertions, 36 deletions
diff --git a/src/parser.rs b/src/parser.rs
index 49ded9b..346ecb2 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -1,8 +1,3 @@
-// #[macro_use]
-// extern crate nom;
-
-// use nom::*;
-
use crate::game::*;
fn parse_move(player: Player, mv: &str) -> Result<Action, String> {
@@ -29,7 +24,7 @@ fn parse_move(player: Player, mv: &str) -> Result<Action, String> {
let x = c.unwrap();
if (x < 'a') || (x > 'h') {
return Err(String::from(
- "Moves must specify a position on the game board.",
+ "Moves must specify a valid position on the game board.",
));
}
let x: u8 = x as u8 - 'a' as u8;
@@ -53,7 +48,7 @@ fn parse_move(player: Player, mv: &str) -> Result<Action, String> {
}
let c = c.unwrap();
- let mut dir = Direction::Up;
+ let dir; // This is so cool! Rust supports some very nice patterns!
match c {
'+' => dir = Direction::Up,
'-' => dir = Direction::Down,
@@ -91,7 +86,58 @@ fn parse_move(player: Player, mv: &str) -> Result<Action, String> {
}
fn parse_place(player: Player, pl: &str) -> Result<Action, String> {
- Ok(Action::Place(player, Position { x: 0, y: 0 }, Stone::Flat))
+ let mut chars = pl.chars();
+
+ let c = chars.next();
+ if c.is_none() {
+ return Err(String::from("An action cannot be blank."));
+ }
+
+ let stone_c = c.unwrap();
+
+ let stone;
+ let x;
+ if (stone_c == 'F') || (stone_c == 'C') || (stone_c == 'S') {
+ match stone_c {
+ 'C' => stone = Stone::Capstone,
+ 'S' => stone = Stone::Standing,
+ _ => stone = Stone::Flat,
+ }
+ let c = chars.next();
+ if c.is_none() {
+ return Err(String::from(
+ "Placements must specify a position on the game board.",
+ ));
+ }
+ x = c.unwrap();
+ } else {
+ stone = Stone::Flat;
+ x = stone_c;
+ }
+
+ if (x < 'a') || (x > 'h') {
+ return Err(String::from(
+ "Placements must specify a valid position on the game board.",
+ ));
+ }
+ let x: u8 = x as u8 - 'a' as u8;
+
+ let c = chars.next();
+ if c.is_none() {
+ return Err(String::from(
+ "Placements must specify a position on the game board.",
+ ));
+ }
+
+ let y = c.unwrap();
+ if (y < '1') || (y > '8') {
+ return Err(String::from(
+ "Placements must specify a valid position on the game board.",
+ ));
+ }
+ let y: u8 = (y.to_digit(10).unwrap() - 1) as u8;
+
+ Ok(Action::Place(player, Position { x: x, y: y }, stone))
}
pub fn parse_action(player: Player, act: &str) -> Result<Action, String> {
@@ -104,31 +150,3 @@ pub fn parse_action(player: Player, act: &str) -> Result<Action, String> {
parse_place(player, act)
}
}
-
-// fn parse_move(mv: &str) -> Result<Action, Err<&str>> {
-// one_of!("abcdefgh");
-// Ok(Action::Place(
-// Player::Black,
-// Position { x: 0, y: 0 },
-// Stone::Flat,
-// ))
-// }
-
-// fn parse_place(pl: &str) -> Result<Action, Err<&str>> {
-// Ok(Action::Place(
-// Player::Black,
-// Position { x: 0, y: 0 },
-// Stone::Flat,
-// ))
-// }
-
-// pub fn parse_action(act: &str) -> Result<Action, Err<&str>> {
-// if act
-// .chars()
-// .any(|c| c == '+' || c == '-' || c == '>' || c == '<')
-// {
-// parse_move(act)
-// } else {
-// parse_place(act)
-// }
-// }