inplement .index() in Color

This commit is contained in:
vova-rs committed 2026-10-10 12:33:21 +03:00
1 parent 45e1843fba
commit a61395ca6a
3 files changed
+57 -24

No files matched your search

+9 -7
View File
@@ -4,6 +4,8 @@ pub use trajectory::*;
mod movement_mask;
pub use movement_mask::mov_mask;
use super::Color;
#[derive(Clone, Copy)]
pub struct Board<const N: usize> {
layers: [[u64; 2]; N],
@@ -29,22 +31,22 @@ impl<const N: usize> Board<N> {
}
}
pub fn get_mask(&self, x: usize, y: usize) -> u64 {
if y >= N || x >= 2 {
pub fn get_mask(&self, x: Color, y: usize) -> u64 {
if y >= N || x.index() >= 2 {
return 0;
}
self.layers[y][x]
self.layers[y][x.index()]
}
pub fn is_mask_zero(&self, x: usize, y: usize) -> bool {
pub fn is_mask_zero(&self, x: Color, y: usize) -> bool {
self.get_mask(x, y) == 0
}
pub fn and_mask_is_zero(&self, x: usize, y: usize, mask: u64) -> bool {
if y >= N || x >= 2 {
pub fn and_mask_is_zero(&self, x: Color, y: usize, mask: u64) -> bool {
if y >= N || x.index() >= 2 {
return true;
}
(self.layers[y][x] & mask) == 0
(self.layers[y][x.index()] & mask) == 0
}
}
+32 -16
View File
@@ -27,26 +27,42 @@ impl ChessEngine for Breakthrough {
}
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors> {
if let Some(ref h) = self.legal_moves {
todo!();
} else {
Err(MoveErrors::NoMove)
if self.status() != GameStatus::Ongoing {
return Err(MoveErrors::GameOver);
}
// 1. Достаём патч. Если хода нет в кэше — он нелегален.
let patch = match self.legal_moves.as_ref().and_then(|m| m.get(&mv)) {
Some(p) => p.clone(),
None => return Err(MoveErrors::InvalidMove),
};
// 2. Применяем патч к доске (детали зависят от Board API).
self.board.apply_patch(&patch);
// 3. Меняем сторону.
self.side = self.side.opposite();
// 4. Пересчитываем легальные ходы для новой стороны.
self.refresh_legal_moves();
Ok(())
}
fn status(&self) -> GameStatus {
if self.board.is_mask_zero(0, 0) {
return GameStatus::Won(Color::White);
}
if self.board.is_mask_zero(0, 1) {
return GameStatus::Won(Color::Black);
}
if !self.board.and_mask_is_zero(0, 1, WHITE_WON_ZONE){
return GameStatus::Won(Color::White);
}
if !self.board.and_mask_is_zero(0, 0, BLACK_WON_ZONE){
return GameStatus::Won(Color::Black);
}
todo!();
// if self.board.is_mask_zero(0, 0) {
// return GameStatus::Won(Color::White);
// }
// if self.board.is_mask_zero(0, 1) {
// return GameStatus::Won(Color::Black);
// }
// if !self.board.and_mask_is_zero(0, 1, WHITE_WON_ZONE){
// return GameStatus::Won(Color::White);
// }
// if !self.board.and_mask_is_zero(0, 0, BLACK_WON_ZONE){
// return GameStatus::Won(Color::Black);
// }
GameStatus::Ongoing
}
+16 -1
View File
@@ -13,7 +13,7 @@ pub trait ChessEngine {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MoveErrors { GameOver, InvalidMove, NoMove }
pub enum MoveErrors { GameOver, InvalidMove, NoMoveStack }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Move{
@@ -61,6 +61,21 @@ pub enum Color {
Black,
}
impl Color {
pub fn opposite(self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
pub fn index(self) -> usize {
match self {
Color::White => 0,
Color::Black => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GameStatus {
Ongoing,