1use std::{
2 collections::HashMap,
3 fmt::{Display, Formatter},
4};
5
6use crate::engine::grid::{
7 coordinate::hex::Hex,
8 piece::{Piece, PieceColor},
9};
10
11pub mod coordinate;
12pub mod piece;
13
14#[cfg(test)]
15#[path = "../tests/grid_tests.rs"]
16mod grid_tests;
17
18#[derive(Debug, Clone)]
19pub struct Grid {
20 pub grid: HashMap<Hex, Vec<Piece>>,
21}
22
23impl Grid {
24 pub fn new() -> Grid {
25 let grid: HashMap<Hex, Vec<Piece>> = HashMap::new();
26
27 Grid { grid }
28 }
29
30 pub fn place_piece_to_hex(&mut self, piece: Piece, hex: Hex) {
31 self.grid.entry(hex).or_default().push(piece)
32 }
33
34 pub fn remove_top_piece_from_hex(&mut self, hex: Hex) -> Option<Piece> {
35 let mut removed = None;
36 let mut is_empty = false;
37
38 if let Some(stack) = self.grid.get_mut(&hex) {
39 removed = stack.pop();
40 is_empty = stack.is_empty();
41 }
42
43 if is_empty {
44 self.grid.remove(&hex);
45 }
46
47 removed
48 }
49
50 pub fn move_piece_from_to(&mut self, from: Hex, to: Hex) {
51 let removed_piece = self.remove_top_piece_from_hex(from);
52 if let Some(p) = removed_piece {
53 self.place_piece_to_hex(p, to)
54 };
55 }
56
57 pub fn find_top_piece(&self, hex: &Hex) -> Option<&Piece> {
58 if self.is_hex_occupied(hex) {
59 match self.grid.get(hex) {
60 Some(v) => v.last(),
61 None => None,
62 }
63 } else {
64 None
65 }
66 }
67
68 pub fn is_hex_surrounded(&self, hex: &Hex) -> bool {
69 let neighbors = hex.neighbors();
70
71 let mut is_surrended = true;
72
73 for neighbor in &neighbors {
74 let pieces = self.grid.get(neighbor);
75 is_surrended = is_surrended
76 && match pieces {
77 Some(p) => !p.is_empty(),
78 None => false,
79 }
80 }
81
82 is_surrended
83 }
84
85 pub fn is_hex_neighbor_of(&self, hex: &Hex, of: &Hex) -> bool {
86 let mut is_neighbor = false;
87
88 for neighbor in hex.neighbors() {
89 is_neighbor = is_neighbor || neighbor == *of;
90 }
91
92 is_neighbor
93 }
94
95 pub fn is_hex_occupied(&self, hex: &Hex) -> bool {
96 let pieces = self.grid.get(hex);
97
98 match pieces {
99 Some(p) => !p.is_empty(),
100 None => false,
101 }
102 }
103
104 pub fn is_hex_alone(&self, hex: &Hex) -> bool {
105 let neighbors = hex.neighbors();
106
107 let mut is_alone = true;
108
109 for neighbor in neighbors {
110 is_alone = is_alone && !self.is_hex_occupied(&neighbor);
111 }
112
113 is_alone
114 }
115
116 pub fn is_hex_of_color(&self, hex: &Hex, piece_color: &PieceColor) -> bool {
117 let piece = self.find_top_piece(hex);
118
119 match piece {
120 Some(p) => p.p_color == *piece_color,
121 None => false,
122 }
123 }
124
125 pub fn is_hex_neighbors_only_piece_color(&self, hex: &Hex, piece_color: &PieceColor) -> bool {
126 if self.is_hex_alone(hex) {
127 return false;
128 }
129
130 let mut is_hex_surrounded_by_piece_color = true;
131
132 for neighbor in hex.neighbors() {
133 if self.is_hex_occupied(&neighbor) && !self.is_hex_of_color(&neighbor, piece_color) {
134 is_hex_surrounded_by_piece_color = false;
135 }
136 }
137
138 is_hex_surrounded_by_piece_color
139 }
140
141 pub fn number_of_pieces(&self) -> usize {
142 let mut count = 0;
143 for vec in self.grid.values() {
144 count += vec.len()
145 }
146
147 count
148 }
149
150 pub fn get_stack_size(&self, hex: &Hex) -> usize {
151 if !self.is_hex_occupied(hex) {
152 return 0;
153 }
154
155 let Some(stack) = self.grid.get(hex) else {
156 return 0;
157 };
158
159 stack.len()
160 }
161}
162
163impl Default for Grid {
164 fn default() -> Self {
165 Self::new()
166 }
167}
168
169impl Display for Grid {
170 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
171 let mut initialized = false;
172 let mut min_q = 0;
173 let mut max_q = 0;
174 let mut min_r = 0;
175 let mut max_r = 0;
176
177 for key in self.grid.keys() {
178 if !initialized {
179 min_q = key.q;
180 max_q = key.q;
181 min_r = key.r;
182 max_r = key.r;
183 initialized = true;
184 } else {
185 if min_q > key.q {
186 min_q = key.q;
187 } else if max_q < key.q {
188 max_q = key.q;
189 }
190
191 if min_r > key.r {
192 min_r = key.r;
193 } else if max_r < key.r {
194 max_r = key.r;
195 }
196 }
197 }
198
199 let mut count_r = -1;
200
201 write!(f, "GRID START")?;
202 for r in min_r..=max_r {
203 for m in 1..=2 {
204 writeln!(f)?;
205
206 if m % 2 == 1 {
207 count_r += 1;
208 }
209
210 for _i in 0..count_r {
211 write!(f, " ")?;
212 }
213
214 for q in min_q..=max_q {
215 let hex = Hex::new(q, r);
216
217 let occupied = self.is_hex_occupied(&hex);
218
219 if occupied {
220 if m % 2 == 0 {
221 write!(f, "{}", hex)?;
222 } else {
223 let piece = self.find_top_piece(&hex);
224 let stack_size = self.get_stack_size(&hex);
225 match piece {
226 Some(p) => {
227 if stack_size > 1 {
228 write!(f, "({})", p)
229 } else {
230 write!(f, " {} ", p)
231 }?
232 }
233 None => write!(f, " NA ")?,
234 }
235 }
236 } else if m % 2 == 1 {
237 write!(f, " __ ")?;
238 } else {
239 write!(f, "{}", hex)?;
240 }
241 }
242 }
243 }
244
245 write!(f, "\nGRID END")
246 }
247}