summaryrefslogtreecommitdiff
path: root/2019-worms/src/command.rs
blob: c6d66953d3513433953c32e138c4ba9b6dee1836 (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
use crate::geometry::Direction;
use crate::geometry::Point2d;
use std::fmt;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Command {
    pub worm: Option<i32>,
    pub action: Action,
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.worm {
            Some(worm) => write!(f, "select {};{}", worm, self.action),
            None => write!(f, "{}", self.action),
        }
    }
}

impl Command {
    pub fn with_select(worm: i32, action: Action) -> Command {
        Command {
            worm: Some(worm),
            action,
        }
    }

    pub fn new(action: Action) -> Command {
        Command { worm: None, action }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Action {
    Move(Point2d),
    Dig(Point2d),
    Shoot(Direction),
    Bomb(Point2d),
    Snowball(Point2d),
    DoNothing,
}

impl fmt::Display for Action {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Action::*;
        match self {
            Move(p) => write!(f, "move {} {}", p.x, p.y),
            Dig(p) => write!(f, "dig {} {}", p.x, p.y),
            Shoot(dir) => write!(f, "shoot {}", dir),
            Bomb(p) => write!(f, "banana {} {}", p.x, p.y),
            Snowball(p) => write!(f, "snowball {} {}", p.x, p.y),
            DoNothing => write!(f, "nothing"),
        }
    }
}

impl Action {
    pub fn is_attack(&self) -> bool {
        use Action::*;
        match self {
            Shoot(_) | Bomb(_) => true,
            _ => false,
        }
    }

    pub fn is_snowball(&self) -> bool {
        use Action::*;
        match self {
            Snowball(_) => true,
            _ => false,
        }
    }
}