1use crate::engine::game::action::Action;
2use crate::engine::grid::piece::{PieceColor, PieceType};
3use crate::engine::grid::{
4 Grid,
5 coordinate::{cube::Cube, hex::Hex},
6};
7
8pub mod hive;
9
10#[cfg(test)]
11#[path = "../tests/rules_tests.rs"]
12mod rules_tests;
13
14pub fn one_hive_rule(grid: &Grid, from: &Hex, to: &Hex) -> bool {
15 if !grid.is_hex_occupied(from) {
16 return false;
17 }
18
19 let mut after_move_grid = Grid {
20 grid: grid.grid.clone(),
21 };
22 after_move_grid.move_piece_from_to(*from, *to);
23 let mut without_piece_grid = Grid {
24 grid: grid.grid.clone(),
25 };
26 without_piece_grid.remove_top_piece_from_hex(*from);
27
28 hive::one_hive_rule_grid_validation(grid)
29 && hive::one_hive_rule_grid_validation(&after_move_grid)
30 && hive::one_hive_rule_grid_validation(&without_piece_grid)
31}
32
33pub fn freedom_to_move_rule(grid: &Grid, from: &Hex, to: &Hex) -> bool {
34 if !grid.is_hex_neighbor_of(to, from) {
35 return false;
36 }
37
38 let to_stack_size = grid.get_stack_size(to);
39
40 let cube = Cube::from(*to);
41 let cube_from = Cube::from(*from);
42 let h1;
43 let h2;
44
45 if cube.x == cube_from.x {
46 let xz_offset = cube.z - cube_from.z;
47 let xy_offset = cube.y - cube_from.y;
48
49 let c1 = Cube {
50 x: cube_from.x - xz_offset,
51 y: cube_from.y,
52 z: cube.z,
53 };
54 let c2 = Cube {
55 x: cube_from.x - xy_offset,
56 y: cube.y,
57 z: cube_from.z,
58 };
59
60 h1 = c1.into();
61 h2 = c2.into();
62 } else if cube.z == cube_from.z {
63 let zx_offset = cube.x - cube_from.x;
64 let zy_offset = cube.y - cube_from.y;
65
66 let c1 = Cube {
67 x: cube.x,
68 y: cube_from.y,
69 z: cube_from.z - zx_offset,
70 };
71 let c2 = Cube {
72 x: cube_from.x,
73 y: cube.y,
74 z: cube_from.z - zy_offset,
75 };
76
77 h1 = c1.into();
78 h2 = c2.into();
79 } else {
80 let yx_offset = cube.x - cube_from.x;
81 let yz_offset = cube.z - cube_from.z;
82
83 let c1 = Cube {
84 x: cube.x,
85 y: cube_from.y - yx_offset,
86 z: cube_from.z,
87 };
88 let c2 = Cube {
89 x: cube_from.x,
90 y: cube_from.y - yz_offset,
91 z: cube.z,
92 };
93
94 h1 = c1.into();
95 h2 = c2.into();
96 }
97
98 !(grid.get_stack_size(&h1) > to_stack_size && grid.get_stack_size(&h2) > to_stack_size)
99}
100
101pub fn queen_surrounded_rule(grid: &Grid, color: PieceColor) -> bool {
102 let mut is_queen_surrounded = false;
103
104 for hex_pieces in &grid.grid {
105 for piece in hex_pieces.1 {
106 if piece.p_type != PieceType::QUEENBEE {
107 continue;
108 }
109
110 if piece.p_color != color {
111 continue;
112 }
113
114 if !grid.is_hex_surrounded(hex_pieces.0) {
115 continue;
116 }
117
118 is_queen_surrounded = true
119 }
120 }
121
122 is_queen_surrounded
123}
124
125pub fn pillbug_special_move_rule(grid: &Grid, from: &Hex, actions_history: &[Action]) -> bool {
126 let Some(piece) = grid.find_top_piece(from) else {
127 return false;
128 };
129
130 let Some(last) = actions_history.last() else {
131 return false;
132 };
133
134 if piece.p_color == last.piece.p_color && *from == last.to && last.is_pillbug_special_move {
135 return true;
136 }
137
138 if actions_history.len() < 2 {
139 return false;
140 }
141
142 let Some(second_from_last) = actions_history.get(actions_history.len() - 2) else {
143 return false;
144 };
145
146 if piece.p_color == second_from_last.piece.p_color
147 && *from == second_from_last.to
148 && second_from_last.is_pillbug_special_move
149 {
150 return true;
151 }
152
153 false
154}