diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:27:56 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:27:56 +0200 |
commit | 3f5492b2bb67326be43cd7c5ba02ccf0ba1ae0e3 (patch) | |
tree | 96963ba885a9393106b4a88ffc4266203e87582e /2019-worms/src/command.rs | |
parent | 4ceec65b088f05d4ad03f9ac70b1d63452fd8197 (diff) |
Refile for merging repos
Diffstat (limited to '2019-worms/src/command.rs')
-rw-r--r-- | 2019-worms/src/command.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/2019-worms/src/command.rs b/2019-worms/src/command.rs new file mode 100644 index 0000000..c6d6695 --- /dev/null +++ b/2019-worms/src/command.rs @@ -0,0 +1,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, + } + } +} |