hive/engine/rules/
hive.rs1use std::collections::HashSet;
2
3use crate::engine::grid::{coordinate::hex::Hex, Grid};
4
5pub fn one_hive_rule_grid_validation(grid: &Grid) -> bool {
6 let mut is_valid = true;
7 let mut keys_it = grid.grid.keys();
8 let mut found_pieces = HashSet::new();
9
10 if let Some(start) = keys_it.find(|h| grid.is_hex_occupied(h)) {
11 found_pieces = one_hive_rule_iterative_pieces_search(grid, found_pieces, start)
12 }
13
14 if found_pieces.len() != grid.number_of_pieces() {
15 is_valid = false;
16 }
17
18 is_valid
19}
20
21fn one_hive_rule_iterative_pieces_search(
22 grid: &Grid,
23 found_pieces: HashSet<(Hex, usize)>,
24 hex: &Hex,
25) -> HashSet<(Hex, usize)> {
26 let mut _found_pieces = found_pieces;
27 let pieces = match grid.grid.get(hex) {
28 Some(v) => v.clone(),
29 None => Vec::new(),
30 };
31
32 for i in 0..pieces.len() {
33 _found_pieces.insert((*hex, i));
34 }
35
36 for neighbor in hex.neighbors() {
37 let zero: usize = 0;
38 if !_found_pieces.contains(&(neighbor, zero)) && grid.is_hex_occupied(&neighbor) {
39 _found_pieces = one_hive_rule_iterative_pieces_search(grid, _found_pieces, &neighbor);
40 }
41 }
42 _found_pieces
43}