Skip to main content

hive/engine/grid/
piece.rs

1use std::fmt::{Debug, Display, Formatter, Result};
2
3#[derive(Clone, Copy, Eq, PartialEq)]
4pub enum PieceType {
5  QUEENBEE,
6  BEETLE,
7  GRASSHOPPER,
8  LADYBUG,
9  MOSQUITO,
10  SOLDIERANT,
11  SPIDER,
12  PILLBUG,
13  NONE,
14}
15
16impl Debug for PieceType {
17  fn fmt(&self, f: &mut Formatter) -> Result {
18    match *self {
19      PieceType::QUEENBEE => write!(f, "QBEE"),
20      PieceType::BEETLE => write!(f, "BETL"),
21      PieceType::GRASSHOPPER => write!(f, "GRHP"),
22      PieceType::LADYBUG => write!(f, "LDBG"),
23      PieceType::MOSQUITO => write!(f, "MSQT"),
24      PieceType::SOLDIERANT => write!(f, "SANT"),
25      PieceType::SPIDER => write!(f, "SPDR"),
26      PieceType::PILLBUG => write!(f, "PLBG"),
27      PieceType::NONE => write!(f, " NA "),
28    }
29  }
30}
31
32#[derive(Clone, Eq, PartialEq, Copy)]
33pub enum PieceColor {
34  BLACK,
35  WHITE,
36  NONE,
37}
38
39impl Debug for PieceColor {
40  fn fmt(&self, f: &mut Formatter) -> Result {
41    match *self {
42      PieceColor::WHITE => write!(f, "W"),
43      PieceColor::BLACK => write!(f, "B"),
44      PieceColor::NONE => write!(f, " "),
45    }
46  }
47}
48
49#[derive(Debug, PartialEq, Clone, Copy)]
50pub struct Piece {
51  pub p_type: PieceType,
52  pub p_color: PieceColor,
53}
54
55impl Piece {
56  pub fn queen_bee() -> Piece {
57    Piece {
58      p_type: PieceType::QUEENBEE,
59      p_color: PieceColor::NONE,
60    }
61  }
62
63  pub fn beetle() -> Piece {
64    Piece {
65      p_type: PieceType::BEETLE,
66      p_color: PieceColor::NONE,
67    }
68  }
69
70  pub fn grasshopper() -> Piece {
71    Piece {
72      p_type: PieceType::GRASSHOPPER,
73      p_color: PieceColor::NONE,
74    }
75  }
76
77  pub fn ladybug() -> Piece {
78    Piece {
79      p_type: PieceType::LADYBUG,
80      p_color: PieceColor::NONE,
81    }
82  }
83
84  pub fn mosquito() -> Piece {
85    Piece {
86      p_type: PieceType::MOSQUITO,
87      p_color: PieceColor::NONE,
88    }
89  }
90
91  pub fn soldier_ant() -> Piece {
92    Piece {
93      p_type: PieceType::SOLDIERANT,
94      p_color: PieceColor::NONE,
95    }
96  }
97
98  pub fn spider() -> Piece {
99    Piece {
100      p_type: PieceType::SPIDER,
101      p_color: PieceColor::NONE,
102    }
103  }
104
105  pub fn pillbug() -> Piece {
106    Piece {
107      p_type: PieceType::PILLBUG,
108      p_color: PieceColor::NONE,
109    }
110  }
111
112  pub fn white(&self) -> Piece {
113    Piece {
114      p_type: self.p_type,
115      p_color: PieceColor::WHITE,
116    }
117  }
118
119  pub fn black(&self) -> Piece {
120    Piece {
121      p_type: self.p_type,
122      p_color: PieceColor::BLACK,
123    }
124  }
125}
126
127impl Display for Piece {
128  fn fmt(&self, f: &mut Formatter) -> Result {
129    write!(f, "{:?} {:?}", self.p_color, self.p_type)
130  }
131}