hive/engine/
rules.rs

1use crate::engine::grid::piece::{PieceColor, PieceType};
2use crate::engine::grid::{
3  coordinate::{cube::Cube, hex::Hex},
4  Grid,
5};
6
7pub mod hive;
8
9#[cfg(test)]
10#[path = "../tests/rules_tests.rs"]
11mod rules_tests;
12
13pub fn one_hive_rule(grid: &Grid, from: &Hex, to: &Hex) -> bool {
14  let mut is_valid = false;
15  let mut temp_grid = Grid {
16    grid: grid.grid.clone(),
17  };
18  temp_grid.move_piece_from_to(*from, *to);
19
20  if grid.is_hex_occupied(from) {
21    is_valid =
22      hive::one_hive_rule_grid_validation(grid) && hive::one_hive_rule_grid_validation(&temp_grid);
23  }
24
25  is_valid
26}
27
28pub fn freedom_to_move_rule(grid: &Grid, from: &Hex, to: &Hex) -> bool {
29  let is_accessible;
30
31  if grid.is_hex_neighbor_of(to, from) {
32    let cube = Cube::from(*to);
33    let cube_from = Cube::from(*from);
34
35    if cube.x == cube_from.x {
36      let xz_offset = cube.z - cube_from.z;
37      let xy_offset = cube.y - cube_from.y;
38
39      let c1 = Cube {
40        x: cube_from.x - xz_offset,
41        y: cube_from.y,
42        z: cube.z,
43      };
44      let c2 = Cube {
45        x: cube_from.x - xy_offset,
46        y: cube.y,
47        z: cube_from.z,
48      };
49
50      let h1 = c1.into();
51      let h2 = c2.into();
52
53      is_accessible = !(grid.is_hex_occupied(&h1) && grid.is_hex_occupied(&h2));
54    } else if cube.z == cube_from.z {
55      let zx_offset = cube.x - cube_from.x;
56      let zy_offset = cube.y - cube_from.y;
57
58      let c1 = Cube {
59        x: cube.x,
60        y: cube_from.y,
61        z: cube_from.z - zx_offset,
62      };
63      let c2 = Cube {
64        x: cube_from.x,
65        y: cube.y,
66        z: cube_from.z - zy_offset,
67      };
68
69      let h1 = c1.into();
70      let h2 = c2.into();
71
72      is_accessible = !(grid.is_hex_occupied(&h1) && grid.is_hex_occupied(&h2));
73    } else {
74      let yx_offset = cube.x - cube_from.x;
75      let yz_offset = cube.z - cube_from.z;
76
77      let c1 = Cube {
78        x: cube.x,
79        y: cube_from.y - yx_offset,
80        z: cube_from.z,
81      };
82      let c2 = Cube {
83        x: cube_from.x,
84        y: cube_from.y - yz_offset,
85        z: cube.z,
86      };
87
88      let h1 = c1.into();
89      let h2 = c2.into();
90
91      is_accessible = !(grid.is_hex_occupied(&h1) && grid.is_hex_occupied(&h2));
92    }
93  } else {
94    is_accessible = false;
95  }
96  is_accessible
97}
98
99pub fn queen_surrounded_rule(grid: &Grid, color: PieceColor) -> bool {
100  let mut is_queen_surrounded = false;
101
102  for hex_pieces in &grid.grid {
103    for piece in hex_pieces.1 {
104      if piece.p_type == PieceType::QUEENBEE
105        && piece.p_color == color
106        && grid.is_hex_surrounded(hex_pieces.0)
107      {
108        is_queen_surrounded = true
109      }
110    }
111  }
112
113  is_queen_surrounded
114}