From 960f06c4f46b6886b906e4566555cc3617b85572 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Tue, 23 Apr 2024 20:44:38 +0200 Subject: [PATCH] implement iterability for theatened() --- src/chess/board.rs | 43 +++++++++++++++++++++++-------------------- src/chess/moves.rs | 21 +++++++++++++++++++++ src/main.rs | 3 ++- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/chess/board.rs b/src/chess/board.rs index 398bd67..8a4835b 100644 --- a/src/chess/board.rs +++ b/src/chess/board.rs @@ -109,11 +109,9 @@ impl Board { } pub fn from_fen(fen: String) -> Self { - trace!("{fen}"); let mut pos = Board::default(); let mut parts = fen.split_whitespace(); - trace!("{:?}", parts); // parse board parts.next().unwrap().split('/').enumerate().for_each(|(y, row)| { let mut x = 0; @@ -245,16 +243,14 @@ impl Board { let check = self.check_init(); self.mut_ref_position().ref_set_check(check); let castl_possibilities = self.castling_possibilities_init(); - trace!("{self:?}"); self.mut_ref_position().ref_set_castle_possibilities(castl_possibilities); let moves = self.valid_moves_init(); - // self.mut_ref_position().ref_set_moves(moves); + self.mut_ref_position().ref_set_moves(moves); self } fn check_init(&mut self) -> Option { let whites_turn = self.ref_whites_turn(); - trace!("{whites_turn}"); let attacker = self.threatened_by(self.king_pos(*whites_turn)); match attacker.len() { 0 => None, @@ -332,7 +328,6 @@ impl Board { square.y() as i8 + change.ref_y() < 0 || square.y() as i8 + change.ref_y() > 7 { continue; } - trace!("{:?}:{:?} => {}:{:?}", {square.x() as i8 + change.ref_x()}, {square.y() as i8 + change.ref_y()}, {if to_move {"w".to_string()} else {"b".to_string()}}, variant); if self.ref_rows() [(square.x() as i8 + change.ref_x()) as usize] [(square.y() as i8 + change.ref_y()) as usize] == @@ -353,20 +348,28 @@ impl Board { changes = MoveVariant::variant_move(variant.borrow()); for mut change in changes { change = change.reverse(); - if square.x() as i8 + change.ref_x() < 0 || square.x() as i8 + change.ref_x() > 7 || - square.y() as i8 + change.ref_y() < 0 || square.y() as i8 + change.ref_y() > 7 { - continue; - } - trace!("{:?}:{:?} => {}:{:?}", {square.x() as i8 + change.ref_x()}, {square.y() as i8 + change.ref_y()}, {if to_move {"w".to_string()} else {"b".to_string()}}, variant); - if self.ref_rows() - [(square.x() as i8 + change.ref_x()) as usize] - [(square.y() as i8 + change.ref_y()) as usize] == - Some(Piece::new(to_move, variant.as_piece_variant())) { - trace!("matched: {:?}:{:?}", {square.x() as i8 + change.ref_x()}, {square.y() as i8 + change.ref_y()}); - attacker.push(Square::new( - (square.x() as i8 + change.ref_x()) as u8, - (square.y() as i8 + change.ref_y()) as u8, - )); + let mut multiplier = 1; + + loop { + multiplier += 1; + if square.x() as i8 + change.ref_x() * multiplier < 0 || square.x() as i8 + change.ref_x() * multiplier > 7 || + square.y() as i8 + change.ref_y() * multiplier < 0 || square.y() as i8 + change.ref_y() * multiplier > 7 || + self.used(Square::new((square.x() as i8 + change.ref_x() * multiplier) as u8, (square.y() as i8 + change.ref_y() * multiplier) as u8)) { + break; + } + + if self.ref_rows() + [(square.y() as i8 + change.ref_y()) as usize] + [(square.x() as i8 + change.ref_x()) as usize] == + Some(Piece::new(!to_move, variant.as_piece_variant())) { + attacker.push(Square::new( + (square.x() as i8 + change.ref_x()) as u8, + (square.y() as i8 + change.ref_y()) as u8, + )); + } + if !variant.iterable() { + break; + } } } } diff --git a/src/chess/moves.rs b/src/chess/moves.rs index 2e668be..d1cec27 100644 --- a/src/chess/moves.rs +++ b/src/chess/moves.rs @@ -146,6 +146,27 @@ impl MoveVariant { } } + pub fn iterable(&self) -> bool { + match self { + Self::King => false, + Self::KingCastle => false, + Self::Queen => true, + Self::Rook => true, + Self::Bishop => true, + Self::Knight => false, + Self::PawnWhite => false, + Self::PawnWhiteDouble => false, + Self::PawnWhiteEnPassent => false, + Self::PawnWhiteCaptureLeft => false, + Self::PawnWhiteCaptureRight => false, + Self::PawnBlack => false, + Self::PawnBlackDouble => false, + Self::PawnBlackEnPassent => false, + Self::PawnBlackCaptureLeft => false, + Self::PawnBlackCaptureRight => false, + } + } + pub fn as_piece_variant(&self) -> PieceVariant { match self { Self::King => PieceVariant::King, diff --git a/src/main.rs b/src/main.rs index b49cd46..3e9cda4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use crate::{chess::board::Board, config::get_config}; -use log::trace; +use log::{debug, info, trace}; mod chess; mod config; @@ -27,4 +27,5 @@ fn main() { .position_init() .ref_position() .ref_moves(); + info!("{:?}", moves); }