aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-25 07:15:28 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-25 07:15:28 +0300
commit8dc121b0e08cd25fab3eacde9318d7adaac1f2a4 (patch)
treea4e1af09bb368704d47e5844efc3726e71791f8d /src
parenta1ad1424bcbf0fd8fff2ce9c4048e88a58934f20 (diff)
downloadchessnost-8dc121b0e08cd25fab3eacde9318d7adaac1f2a4.tar.gz
refactor: return recoverable errors from make_move
Diffstat (limited to 'src')
-rw-r--r--src/board/engine.rs9
-rw-r--r--src/board/mod.rs20
2 files changed, 21 insertions, 8 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index 2c6e47a..4fe8652 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -32,7 +32,7 @@ impl Board {
let ep_target_before = self.ep_target.clone();
let castling_rights_before = self.castling_rights.clone();
let hash_before = self.hash.clone();
- let captured_piece = self.make_move(mov);
+ let captured_piece = self.make_move(mov).unwrap();
// King can not be in check after our own move
if !self.is_king_in_check(color) {
if depth == 1 {
@@ -135,7 +135,10 @@ impl Board {
let ep_target_before = self.ep_target.clone();
let castling_rights_before = self.castling_rights.clone();
let hash_before = self.hash.clone();
- let captured_piece = self.make_move(mov);
+ let captured_piece = match self.make_move(mov) {
+ Ok(piece) => piece,
+ Err(err) => panic!("Handle me [{:?}]", err),
+ };
if !self.is_king_in_check(color) {
let (mut score, mut subtree_pv) = self.negamax_search(-beta, -alpha, depth_left - 1);
@@ -184,7 +187,7 @@ impl Board {
let ep_target_before = self.ep_target.clone();
let castling_rights_before = self.castling_rights.clone();
let hash_before = self.hash.clone();
- let captured_piece = self.make_move(*mov);
+ let captured_piece = self.make_move(*mov).unwrap();
if !self.is_king_in_check(color) {
let evaluation = -self.quiscence(-beta, -alpha);
diff --git a/src/board/mod.rs b/src/board/mod.rs
index 19c25ac..2b580b5 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -77,6 +77,11 @@ const PIECE_CHARS: [&str; 12] = [
"♙", "♘", "♗", "♖", "♕", "♔",
];
+#[derive(Debug)]
+pub enum MakeMoveError {
+ PieceNotFound(String),
+}
+
#[allow(unused)]
impl Board {
@@ -485,10 +490,9 @@ impl Board {
mobility
}
-
/// *Blindlessly* apply a move without any validation
/// Move should be validated beforehand
- pub fn make_move(&mut self, mov: Move) -> Option<PieceType> {
+ pub fn make_move(&mut self, mov: Move) -> Result<Option<PieceType>,MakeMoveError> {
let move_source_bb = mov.source.to_bitboard();
let move_target_bb = mov.target.to_bitboard();
@@ -521,7 +525,11 @@ impl Board {
self.hash ^= self.zobrist_seed[pawn_type * 12 + captured_square as usize];
Some(PieceType::from(pawn_type))
}
- None => panic!("Pawn captured by En Passant was not found"),
+ None => {
+ return Err(MakeMoveError::PieceNotFound(
+ String::from("Pawn captured by En Passant was not found")
+ ))
+ }
}
}
@@ -543,7 +551,9 @@ impl Board {
},
None => {
self.print();
- panic!("{:?} is malformed: source piece not found", mov);
+ return Err(MakeMoveError::PieceNotFound(
+ String::from("Source piece not found")
+ ))
}
};
@@ -617,7 +627,7 @@ impl Board {
self.ply += 1;
self.hash ^= self.zobrist_seed[780];
- captured_piece
+ Ok(captured_piece)
}
/// Completely reverse make_move as if it never happened