inplement .index() in Color
This commit is contained in:
1 parent
45e1843fba
commit
a61395ca6a
3 files changed
+57
-24
No files matched your search
@@ -4,6 +4,8 @@ pub use trajectory::*;
|
|||||||
mod movement_mask;
|
mod movement_mask;
|
||||||
pub use movement_mask::mov_mask;
|
pub use movement_mask::mov_mask;
|
||||||
|
|
||||||
|
use super::Color;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Board<const N: usize> {
|
pub struct Board<const N: usize> {
|
||||||
layers: [[u64; 2]; N],
|
layers: [[u64; 2]; N],
|
||||||
@@ -29,22 +31,22 @@ impl<const N: usize> Board<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mask(&self, x: usize, y: usize) -> u64 {
|
pub fn get_mask(&self, x: Color, y: usize) -> u64 {
|
||||||
if y >= N || x >= 2 {
|
if y >= N || x.index() >= 2 {
|
||||||
return 0;
|
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
|
self.get_mask(x, y) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn and_mask_is_zero(&self, x: usize, y: usize, mask: u64) -> bool {
|
pub fn and_mask_is_zero(&self, x: Color, y: usize, mask: u64) -> bool {
|
||||||
if y >= N || x >= 2 {
|
if y >= N || x.index() >= 2 {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
(self.layers[y][x] & mask) == 0
|
(self.layers[y][x.index()] & mask) == 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,26 +27,42 @@ impl ChessEngine for Breakthrough {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors> {
|
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors> {
|
||||||
if let Some(ref h) = self.legal_moves {
|
if self.status() != GameStatus::Ongoing {
|
||||||
todo!();
|
return Err(MoveErrors::GameOver);
|
||||||
} else {
|
|
||||||
Err(MoveErrors::NoMove)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
fn status(&self) -> GameStatus {
|
||||||
if self.board.is_mask_zero(0, 0) {
|
todo!();
|
||||||
return GameStatus::Won(Color::White);
|
// 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.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, 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);
|
// if !self.board.and_mask_is_zero(0, 0, BLACK_WON_ZONE){
|
||||||
}
|
// return GameStatus::Won(Color::Black);
|
||||||
|
// }
|
||||||
|
|
||||||
GameStatus::Ongoing
|
GameStatus::Ongoing
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-1
@@ -13,7 +13,7 @@ pub trait ChessEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Move{
|
pub struct Move{
|
||||||
@@ -61,6 +61,21 @@ pub enum Color {
|
|||||||
Black,
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum GameStatus {
|
pub enum GameStatus {
|
||||||
Ongoing,
|
Ongoing,
|
||||||
|
|||||||
Reference in new issue
Block a user