hive/engine/grid/
piece.rs

1use std::fmt::{Debug, Display, Formatter, Result};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
6pub enum PieceType {
7  QUEENBEE,
8  BEETLE,
9  GRASSHOPPER,
10  LADYBUG,
11  MOSQUITO,
12  SOLDIERANT,
13  SPIDER,
14  NONE,
15}
16
17impl Debug for PieceType {
18  fn fmt(&self, f: &mut Formatter) -> Result {
19    match *self {
20      PieceType::QUEENBEE => write!(f, "QBEE"),
21      PieceType::BEETLE => write!(f, "BETL"),
22      PieceType::GRASSHOPPER => write!(f, "GRHP"),
23      PieceType::LADYBUG => write!(f, "LDBG"),
24      PieceType::MOSQUITO => write!(f, "MSQT"),
25      PieceType::SOLDIERANT => write!(f, "SANT"),
26      PieceType::SPIDER => write!(f, "SPDR"),
27      PieceType::NONE => write!(f, " NA "),
28    }
29  }
30}
31
32#[derive(Clone, Eq, PartialEq, Copy, Serialize, Deserialize)]
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, Serialize, Deserialize)]
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 white(&self) -> Piece {
106    Piece {
107      p_type: self.p_type,
108      p_color: PieceColor::WHITE,
109    }
110  }
111
112  pub fn black(&self) -> Piece {
113    Piece {
114      p_type: self.p_type,
115      p_color: PieceColor::BLACK,
116    }
117  }
118}
119
120impl Display for Piece {
121  fn fmt(&self, f: &mut Formatter) -> Result {
122    write!(f, "{:?} {:?}", self.p_color, self.p_type)
123  }
124}