implement iterability for theatened()
This commit is contained in:
parent
88e1c43781
commit
960f06c4f4
@ -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<bool> {
|
||||
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,21 +348,29 @@ 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;
|
||||
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;
|
||||
}
|
||||
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()});
|
||||
[(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
attacker
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user