hive/engine/
moves.rs

1use crate::engine::{
2  grid::{
3    coordinate::hex::Hex,
4    piece::{PieceColor, PieceType},
5    Grid,
6  },
7  moves::{
8    beetle::beetle_moves, grasshoper::grasshopper_moves, ladybug::ladybug_moves,
9    mosquito::mosquito_moves, queen_bee::queen_bee_moves, soldier_ant::soldier_ant_moves,
10    spider::spider_moves,
11  },
12};
13use std::collections::HashSet;
14
15pub mod beetle;
16pub mod grasshoper;
17pub mod ladybug;
18pub mod mosquito;
19pub mod queen_bee;
20pub mod soldier_ant;
21pub mod spider;
22
23#[cfg(test)]
24#[path = "../tests/moves_tests.rs"]
25mod moves_tests;
26
27pub fn available_moves(grid: &Grid, hex: &Hex) -> Vec<Hex> {
28  let piece = grid.find_top_piece(hex);
29
30  match piece {
31    Some(p) => match p.p_type {
32      PieceType::BEETLE => beetle_moves(grid, hex),
33      PieceType::GRASSHOPPER => grasshopper_moves(grid, hex),
34      PieceType::LADYBUG => ladybug_moves(grid, hex),
35      PieceType::MOSQUITO => mosquito_moves(grid, hex),
36      PieceType::NONE => Vec::new(),
37      PieceType::QUEENBEE => queen_bee_moves(grid, hex),
38      PieceType::SOLDIERANT => soldier_ant_moves(grid, hex),
39      PieceType::SPIDER => spider_moves(grid, hex),
40    },
41    None => Vec::new(),
42  }
43}
44
45pub fn available_actions_for_piece_color(grid: &Grid, piece_color: &PieceColor) -> Vec<Hex> {
46  let mut moves: HashSet<Hex> = HashSet::new();
47
48  if grid.number_of_pieces() == 0 {
49    moves.insert(Hex::new(0, 0));
50  } else if grid.number_of_pieces() == 1 {
51    moves.insert(Hex::new(-1, 0));
52  } else {
53    for hex in grid.grid.keys() {
54      if grid.is_hex_of_color(hex, piece_color) {
55        for neighbor in hex.neighbors() {
56          if !grid.is_hex_occupied(&neighbor)
57            && grid.is_hex_neighbors_only_piece_color(&neighbor, piece_color)
58          {
59            moves.insert(neighbor);
60          }
61        }
62      }
63    }
64  }
65
66  moves.into_iter().collect()
67}
68
69fn extract_moves_from_paths(paths: Vec<Vec<Hex>>, path_expected_length: usize) -> Vec<Hex> {
70  let mut moves: Vec<Hex> = Vec::new();
71
72  for path in paths {
73    if path.len() == path_expected_length {
74      if let Some(h) = path.last() {
75        if !moves.contains(h) {
76          moves.push(*h);
77        }
78      }
79    }
80  }
81
82  moves
83}