start to write Breakthrough
This commit is contained in:
1 parent
18d6a96b0d
commit
e27482082b
8 files changed
+158
-15
No files matched your search
@@ -0,0 +1,75 @@
|
|||||||
|
mod trajectory;
|
||||||
|
pub use trajectory::*;
|
||||||
|
|
||||||
|
mod movement_mask;
|
||||||
|
pub use movement_mask::mov_mask;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct Board<const N: usize> {
|
||||||
|
layers: [[u64; 2]; N],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Board<N> {
|
||||||
|
pub fn apply_patch(&mut self, patch: &MaskPatch) {
|
||||||
|
for layer in self.layers.iter_mut() {
|
||||||
|
layer[0] &= !patch.clear_mask;
|
||||||
|
layer[1] &= !patch.clear_mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (x, y, mask) in patch.iter() {
|
||||||
|
debug_assert!(x < 64 && y < 2 && y < N);
|
||||||
|
if x >= 64 || y >= 2 || y >= N {
|
||||||
|
eprintln!(
|
||||||
|
"Board::apply_patch: invalid set_mask coords (x={}, y={}) for Board<{}>",
|
||||||
|
x, y, N
|
||||||
|
);
|
||||||
|
panic!("invalid set_mask coordinates");
|
||||||
|
}
|
||||||
|
self.layers[y][x] |= mask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mask(&self, x: usize, y: usize) -> u64 {
|
||||||
|
if x >= 64 || y >= 2 || y >= N {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
self.layers[y][x]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_mask_zero(&self, x: usize, y: usize) -> bool {
|
||||||
|
self.get_mask(x, y) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn and_mask_is_zero(&self, x: usize, y: usize, mask: u64) -> bool {
|
||||||
|
// if x >= 64 || y >= 2 || y >= N {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
(self.layers[y][x] & mask) == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct MaskPatch{
|
||||||
|
pub clear_mask: u64,
|
||||||
|
pub set_masks: [Option<(usize, usize, u64)>; 4],
|
||||||
|
len: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl MaskPatch {
|
||||||
|
pub fn new(clear_mask: u64) -> Self {
|
||||||
|
Self { clear_mask, set_masks: [None; 4], len: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, x: usize, y: usize, mask: u64) {
|
||||||
|
debug_assert!((self.len as usize) < 4);
|
||||||
|
self.set_masks[self.len as usize] = Some((x, y, mask));
|
||||||
|
self.len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (usize, usize, u64)> + '_ {
|
||||||
|
self.set_masks[..self.len as usize]
|
||||||
|
.iter()
|
||||||
|
.map(|o| o.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,57 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub const WHITE_WON_ZONE: u64 = 0xff;
|
||||||
|
pub const BLACK_WON_ZONE: u64 = 0xff << 56;
|
||||||
|
|
||||||
|
struct Breakthrough{
|
||||||
|
board: Board<1>,
|
||||||
|
side: Color,
|
||||||
|
legal_moves: Option<HashMap<Move, MaskPatch>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Breakthrough {
|
||||||
|
fn generate_legal(&self) -> HashMap<Move, MaskPatch>{
|
||||||
|
todo!();
|
||||||
|
HashMap::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refresh_legal_moves(&mut self) {
|
||||||
|
self.legal_moves = Some(self.generate_legal());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChessEngine for Breakthrough {
|
||||||
|
fn legal_moves(&self) -> Vec<Move> {
|
||||||
|
self.legal_moves.clone().unwrap().keys().cloned().collect()/*.expect("cache empty; call refresh first")*/
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors> {
|
||||||
|
if let Some(ref h) = self.legal_moves {
|
||||||
|
todo!();
|
||||||
|
} else {
|
||||||
|
Err(MoveErrors::NoMove)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
GameStatus::Ongoing
|
||||||
|
}
|
||||||
|
|
||||||
|
fn side_to_move(&self) -> Color{
|
||||||
|
self.side
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
mod breakthrough;
|
||||||
@@ -1,14 +1,19 @@
|
|||||||
pub mod board;
|
pub mod board;
|
||||||
use board::*;
|
pub use board::*;
|
||||||
|
|
||||||
|
mod engines;
|
||||||
|
|
||||||
pub trait ChessEngine {
|
pub trait ChessEngine {
|
||||||
fn new() -> Self;
|
// fn new() -> Self;
|
||||||
fn legal_moves(&self) -> Vec<Move>;
|
fn legal_moves(&self) -> Vec<Move>;
|
||||||
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors>;
|
fn apply(&mut self, mv: Move) -> Result<(), MoveErrors>;
|
||||||
|
|
||||||
|
fn side_to_move(&self) -> Color;
|
||||||
|
fn status(&self) -> GameStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum MoveErrors { GameOver, InvalidMove }
|
pub enum MoveErrors { GameOver, InvalidMove, NoMove }
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Move{
|
pub struct Move{
|
||||||
@@ -49,3 +54,16 @@ impl TryFrom<u8> for Promotion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Color {
|
||||||
|
White,
|
||||||
|
Black,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum GameStatus {
|
||||||
|
Ongoing,
|
||||||
|
Won(Color),
|
||||||
|
Draw,
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
mod trajectory;
|
|
||||||
pub use trajectory::*;
|
|
||||||
|
|
||||||
mod movement_mask;
|
|
||||||
pub use movement_mask::mov_mask;
|
|
||||||
|
|
||||||
struct Board<const N: usize> {
|
|
||||||
layers: [[u64; 2]; N],
|
|
||||||
}
|
|
||||||
|
|
||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
mod chess_engines;
|
mod chess_api;
|
||||||
use chess_engines::board::*;
|
use chess_api::board::*;
|
||||||
|
|
||||||
mod render_bitmask;
|
mod render_bitmask;
|
||||||
use render_bitmask::*;
|
use render_bitmask::*;
|
||||||
|
|||||||
Reference in new issue
Block a user