stuff
This commit is contained in:
parent
952b4b5ce3
commit
88e1c43781
@ -73,7 +73,7 @@ pub struct Board {
|
|||||||
// | 0;7 | 1;7 | 2;7 | 3;7 | 4;7 | 5;7 | 6;7 | 7;7 |
|
// | 0;7 | 1;7 | 2;7 | 3;7 | 4;7 | 5;7 | 6;7 | 7;7 |
|
||||||
// |-----------------------------------------------|
|
// |-----------------------------------------------|
|
||||||
// Chess board repesented in code
|
// Chess board repesented in code
|
||||||
//
|
|
||||||
// NOTE: capture left/right is implemented relative to color
|
// NOTE: capture left/right is implemented relative to color
|
||||||
// TODO: make this a doc comment
|
// TODO: make this a doc comment
|
||||||
|
|
||||||
@ -241,39 +241,135 @@ impl Board {
|
|||||||
|
|
||||||
/// Use position
|
/// Use position
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn get_moves(&mut self) -> Vec<Move> {
|
pub fn position_init(&mut self) -> &mut Self {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
1 => Some(false),
|
||||||
|
_ => Some(true),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn castling_possibilities_init(&mut self) -> [[bool; 2]; 2] {
|
||||||
|
let fields = if *self.mut_ref_whites_turn() {
|
||||||
|
if self.mut_ref_rows()[7][4] != Some(Piece::new(true, PieceVariant::King)) {
|
||||||
|
return [[false; 2]; 2]
|
||||||
|
}
|
||||||
|
[[5, 6], [2, 3]]
|
||||||
|
} else {
|
||||||
|
if self.mut_ref_rows()[0][4] != Some(Piece::new(false, PieceVariant::King)) {
|
||||||
|
return [[false; 2]; 2]
|
||||||
|
}
|
||||||
|
[[1, 2], [5, 6]]
|
||||||
|
};
|
||||||
|
let mut castle_possibilities = [[false; 2]; 2];
|
||||||
|
|
||||||
|
let y = match self.mut_ref_whites_turn() {
|
||||||
|
true => 7,
|
||||||
|
false => 0,
|
||||||
|
};
|
||||||
|
for (i, side) in fields.iter().enumerate() {
|
||||||
|
for (j, field) in side.iter().enumerate() {
|
||||||
|
if self.used(Square::new(*field as u8, y)) || self.threatened(Square::new(*field as u8, y)) {
|
||||||
|
castle_possibilities[i][j] = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
castle_possibilities
|
||||||
|
}
|
||||||
|
|
||||||
|
fn valid_moves_init(&mut self) -> Vec<Move> {
|
||||||
|
let mut moves: Vec<Move> = vec![];
|
||||||
if self.ref_position().ref_check().is_some() {
|
if self.ref_position().ref_check().is_some() {
|
||||||
if self.ref_position().ref_check().unwrap() {
|
if self.ref_position().ref_check().unwrap() {
|
||||||
let king_pos = self.king_pos(self.whites_turn);
|
let king_pos = self.king_pos(self.whites_turn);
|
||||||
self.mut_ref_position().ref_set_moves(MoveVariant::king_move(king_pos));
|
moves.append(&mut MoveVariant::king_move(king_pos));
|
||||||
|
for i in 0..moves.len() {
|
||||||
|
if self.used(moves[i].to()) || self.threatened(moves[i].to()) {
|
||||||
|
moves.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
todo!("take, block or king move");
|
todo!("take, block or king move");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
todo!("finish");
|
|
||||||
|
if moves.is_empty() {
|
||||||
|
todo!("Any other move")
|
||||||
|
}
|
||||||
|
moves
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// INFO: solution 1 => reverse Change
|
|
||||||
// solution 2 => loops
|
impl Board {
|
||||||
pub fn rc_threatened(&self, square: Square) -> bool {
|
pub fn used(&self, square: Square) -> bool {
|
||||||
|
self.ref_rows()[square.x() as usize][square.y() as usize].is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn threatened(&self, square: Square) -> bool {
|
||||||
let mut changes;
|
let mut changes;
|
||||||
let mut to_move;
|
let to_move = self.whites_turn;
|
||||||
for variant in MoveVariant::iter() {
|
for variant in MoveVariant::iter() {
|
||||||
to_move = self.whites_turn;
|
|
||||||
changes = MoveVariant::variant_move(variant.borrow());
|
changes = MoveVariant::variant_move(variant.borrow());
|
||||||
for change in changes {
|
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()
|
if self.ref_rows()
|
||||||
[(square.clone().x() as i8 + change.ref_x()) as usize]
|
[(square.x() as i8 + change.ref_x()) as usize]
|
||||||
[(square.clone().y() as i8 + change.ref_y()) as usize] ==
|
[(square.y() as i8 + change.ref_y()) as usize] ==
|
||||||
Some(Piece::new(to_move, variant.as_piece_variant())) {
|
Some(Piece::new(to_move, variant.as_piece_variant()))
|
||||||
return true;
|
{
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn threatened_by(&self, _square: Square) -> Square {
|
pub fn threatened_by(&self, square: Square) -> Vec<Square> {
|
||||||
todo!();
|
let mut changes;
|
||||||
|
let to_move = self.whites_turn;
|
||||||
|
let mut attacker = vec![];
|
||||||
|
for variant in MoveVariant::iter() {
|
||||||
|
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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
attacker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ use super::{piece::PieceVariant, square::Square};
|
|||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Move {
|
pub struct Move {
|
||||||
from: Square,
|
from: Square,
|
||||||
to: Square,
|
to: Square,
|
||||||
@ -70,17 +70,23 @@ impl Change {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Change {
|
impl Change {
|
||||||
pub fn reverse(&mut self) -> &Self {
|
pub fn ref_reverse(&mut self) -> &Self {
|
||||||
let x = *self.ref_x();
|
let x = *self.ref_x();
|
||||||
self.set_x(-x);
|
self.set_x(-x);
|
||||||
let y = self.ref_y();
|
let y = self.ref_y();
|
||||||
self.set_y(-y)
|
self.set_y(-y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reverse(mut self) -> Self {
|
||||||
|
self.set_x(-self.x);
|
||||||
|
self.set_y(-self.y);
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(strum_macros::EnumIter)]
|
#[derive(strum_macros::EnumIter, Debug)]
|
||||||
pub enum MoveVariant {
|
pub enum MoveVariant {
|
||||||
King,
|
King,
|
||||||
KingCastle,
|
KingCastle,
|
||||||
@ -122,7 +128,7 @@ impl MoveVariant {
|
|||||||
pub fn variant_move(&self) -> Vec<Change> {
|
pub fn variant_move(&self) -> Vec<Change> {
|
||||||
match self {
|
match self {
|
||||||
Self::King => Self::vec_king(),
|
Self::King => Self::vec_king(),
|
||||||
Self::KingCastle => todo!(),
|
Self::KingCastle => vec![],
|
||||||
Self::Queen => Self::vec_queen(),
|
Self::Queen => Self::vec_queen(),
|
||||||
Self::Rook => Self::vec_rook(),
|
Self::Rook => Self::vec_rook(),
|
||||||
Self::Bishop => Self::vec_bishop(),
|
Self::Bishop => Self::vec_bishop(),
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||||
pub enum PieceVariant {
|
pub enum PieceVariant {
|
||||||
King,
|
King,
|
||||||
Queen,
|
Queen,
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use log::{debug, trace};
|
|||||||
|
|
||||||
use super::moves::Change;
|
use super::moves::Change;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Square {
|
pub struct Square {
|
||||||
x: u8,
|
x: u8,
|
||||||
y: u8,
|
y: u8,
|
||||||
@ -25,6 +25,16 @@ impl Square {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_change(&self, change: &Change) -> Option<Self> {
|
||||||
|
let x = self.x as i8 + change.ref_x();
|
||||||
|
let y = self.y as i8 + change.ref_y();
|
||||||
|
if !(0..7).contains(&x) || !(0..7).contains(&y) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(Square::new(x as u8, y as u8))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Square {
|
impl Square {
|
||||||
@ -41,15 +51,3 @@ impl Square {
|
|||||||
&self.y
|
&self.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Square {
|
|
||||||
pub fn add_change(&self, change: &Change) -> Option<Self> {
|
|
||||||
let x = self.x as i8 + change.ref_x();
|
|
||||||
let y = self.y as i8 + change.ref_y();
|
|
||||||
if !(0..7).contains(&x) || !(0..7).contains(&y) {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Square::new(x as u8, y as u8))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
11
src/main.rs
11
src/main.rs
@ -18,6 +18,13 @@ fn main() {
|
|||||||
let config = get_config().unwrap_or_else(||panic!(""));
|
let config = get_config().unwrap_or_else(||panic!(""));
|
||||||
trace!("{:?}", config);
|
trace!("{:?}", config);
|
||||||
|
|
||||||
let board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string());
|
let mut board = Board::from_fen(
|
||||||
trace!("{:?}", board);
|
// "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_string() // start
|
||||||
|
"2r2q1k/5pp1/4p1N1/8/1bp5/5P1R/6P1/2R4K b - - 0 1".to_string() // double-check
|
||||||
|
);
|
||||||
|
|
||||||
|
let moves = board
|
||||||
|
.position_init()
|
||||||
|
.ref_position()
|
||||||
|
.ref_moves();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user