summaryrefslogtreecommitdiff
path: root/src/parser.rs
blob: a1d9a328314f27882616c8e701e1b0ad5c3aacd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::game::*;

fn parse_move(player: Player, mv: &str) -> Result<Action, String> {
    let mut chars = mv.chars();

    let c = chars.next();
    if c.is_none() {
        return Err(String::from("Empty string cannot be parsed."));
    }

    let dc = c.unwrap();
    if (dc < '1') || ('9' < dc) {
        return Err(String::from(
            "Moves must begin with a digit in 1-8 indicating the number of drops.",
        ));
    }
    let sum_drops = dc.to_digit(10).unwrap();

    let c = chars.next();
    if c.is_none() {
        return Err(String::from("Moves must specify a stack position."));
    }

    let x = c.unwrap();
    if (x < 'a') || (x > 'h') {
        return Err(String::from(
            "Moves 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("Moves must specify a stack position."));
    }

    let y = c.unwrap();
    if (y < '1') || (y > '8') {
        return Err(String::from(
            "Moves must specify a position on the game board.",
        ));
    }
    let y: u8 = (y.to_digit(10).unwrap() - 1) as u8;

    let c = chars.next();
    if c.is_none() {
        return Err(String::from("Moves must specify a move direction."));
    }

    let c = c.unwrap();
    let dir; // This is so cool! Rust supports some very nice patterns!
    match c {
        '+' => dir = Direction::Up,
        '-' => dir = Direction::Down,
        '<' => dir = Direction::Left,
        '>' => dir = Direction::Right,
        _ => return Err(String::from("The valid directions are +,-,<, and >.")),
    }

    let mut drops: Vec<u8> = Vec::new();
    for d in chars {
        if let Some(u) = d.to_digit(10) {
            if u > 9 {
                return Err(String::from(
                    "No more than 8 pieces may be dropped on a given square.",
                ));
            } else {
                drops.push(u as u8)
            }
        } else {
            return Err(String::from(
                "A move must specify a number of pieces dropped for each square.",
            ));
        }
    }

    let sum = drops.iter().map(|&d| d as u32).sum::<u32>();
    if sum != sum_drops {
        return Err(format!(
            "The move called for {} stones, but {} were dropped.",
            sum_drops, sum
        ));
    }

    Ok(Action::Move(player, Position { x: x, y: y }, dir, drops))
}

fn parse_place(player: Player, pl: &str) -> Result<Action, String> {
    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(if (x >= 'A') && (x <= 'Z') {
            format!("Unrecognised stone type '{}' in placement.", x)
        } else {
            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> {
    if act
        .chars()
        .any(|c| c == '+' || c == '-' || c == '>' || c == '<')
    {
        parse_move(player, act)
    } else {
        parse_place(player, act)
    }
}