Skip to content

Commit

Permalink
variious updates to eval and search
Browse files Browse the repository at this point in the history
  • Loading branch information
TierynnB committed Sep 13, 2024
1 parent f6cd046 commit c0de9ca
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 241 deletions.
97 changes: 45 additions & 52 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::moves::Move;
use crate::{constants::*, conversion::*, evaluate};
use std::collections;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
#[derive(Clone)]
Expand Down Expand Up @@ -49,8 +50,8 @@ impl Board {
];

let colour_array = [
[2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2],
[-1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
Expand Down Expand Up @@ -94,10 +95,6 @@ impl Board {
}

pub fn get_fen(&self) -> String {
// TODO

// let mut fen = String::new();

return "".to_string();
}
pub fn reset_board(&mut self) {
Expand All @@ -112,8 +109,8 @@ impl Board {
[4, 2, 3, 5, 6, 3, 2, 4],
];
self.colour_array = [
[2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2],
[-1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
Expand Down Expand Up @@ -278,15 +275,15 @@ impl Board {
self.set_piece_and_colour(castle_from_to_square.0, EMPTY, EMPTY);
}

self.add_hash_of_current_position();

// set side to move to opposite
self.side_to_move = if self.side_to_move == WHITE {
BLACK
} else {
WHITE
};

self.add_hash_of_current_position();

self.ply += 1;
}

Expand All @@ -306,11 +303,6 @@ impl Board {
return Err("cannot move empty colour".to_string());
}

println!(
"from: {:?},from piece: {:?}, to: {:?}, to piece: {}",
move_to_do.from, move_to_do.to, move_to_do.from_piece, move_to_do.to_piece
);

self.make_move(&move_to_do);
return Ok(move_to_do);
}
Expand Down Expand Up @@ -385,8 +377,23 @@ impl Board {
}
// add pawn back from en passant
}

pub fn convert_notation_to_move(&self, mut chess_move: String) -> Result<Move, String> {
pub fn is_piece_type_on_board_for_side(&self, piece: i8, colour: i8) -> bool {
// go through each piece on the board, by colour to only get moves for side to move.
for (row_index, row) in self.board_array.iter().enumerate() {
for (column_index, piece_type) in row.iter().enumerate() {
if piece_type != &piece {
continue;
}
let piece_colour = self.get_piece_colour((row_index, column_index));
if piece_colour != colour {
continue;
}
return true;
}
}
return false;
}
pub fn convert_notation_to_move(&self, chess_move: String) -> Result<Move, String> {
let mut converted_move = Move::default();

// convert "e1g1" "e1c1" "e8g8" "e8c8" into O-O or O-O-O
Expand All @@ -395,35 +402,19 @@ impl Board {
|| ((chess_move == "e8g8" && self.can_castle_h8)
|| (chess_move == "e8c8" && self.can_castle_h1))
{
chess_move = match chess_move.as_str() {
"e1g1" => "O-O".to_string(),
"e1c1" => "O-O-O".to_string(),
"e8g8" => "O-O".to_string(),
"e8c8" => "O-O-O".to_string(),
_ => return Err("Invalid castling move".to_string()),
};
}

// castling is a special case
if ((self.side_to_move == WHITE && self.can_castle_a1 && self.can_castle_h1)
|| (self.side_to_move == BLACK && self.can_castle_a8 && self.can_castle_h8))
&& (chess_move == "O-O" || chess_move == "O-O-O")
{
// chess_move = match chess_move.as_str() {
// "e1g1" => "O-O".to_string(),
// "e1c1" => "O-O-O".to_string(),
// "e8g8" => "O-O".to_string(),
// "e8c8" => "O-O-O".to_string(),
// _ => return Err("Invalid castling move".to_string()),
// };
// castling is a special case
let from_to_squares = match chess_move.as_str() {
"O-O" => {
if self.side_to_move == WHITE {
((7, 4), (7, 6), (7, 7), (7, 5))
} else {
((0, 4), (0, 6), (0, 7), (0, 5))
}
}
"O-O-O" => {
if self.side_to_move == WHITE {
((7, 4), (7, 2), (7, 0), (7, 3))
} else {
((0, 4), (0, 2), (0, 9), (0, 3))
}
}
"e1g1" => ((7, 4), (7, 6), (7, 7), (7, 5)),
"e8g8" => ((0, 4), (0, 6), (0, 7), (0, 5)),
"e1c1" => ((7, 4), (7, 2), (7, 0), (7, 3)),
"e8c8" => ((0, 4), (0, 2), (0, 9), (0, 3)),
_ => return Err("Invalid castling move".to_string()),
};
// depends on side to move where piece is
Expand All @@ -434,8 +425,6 @@ impl Board {
to: from_to_squares.1,
to_piece: EMPTY,
to_colour: EMPTY,

// notation_move: chess_move.clone(),
promotion_to: None,
en_passant: false,
castle_from_to_square: Some((from_to_squares.2, from_to_squares.3)),
Expand Down Expand Up @@ -585,22 +574,26 @@ impl Board {

return if count >= 3 { true } else { false };
}
pub fn get_king_location(&self, side: i8) -> (usize, usize) {
pub fn get_king_location(&self, side: i8) -> Option<(usize, usize)> {
// find king for side
// go through each piece on the board, by colour to only get moves for side to move.
for (row_index, row) in self.board_array.iter().enumerate() {
for (column_index, piece) in row.iter().enumerate() {
if *piece != KING {
continue;
}

let location = (row_index, column_index);
let square_colour = &self.get_piece_colour((row_index, column_index));

if square_colour != &side || *piece != KING {
if square_colour != &side {
continue;
}
return location;
return Some(location);
}
}

panic!("King not found!");
return None;
}
}
pub fn print_board(board: &Board) {
Expand Down Expand Up @@ -646,7 +639,7 @@ pub fn print_board(board: &Board) {
}
let colour = match square {
1 => "W",
2 => "B",
-1 => "B",
0 => " ",
_ => " ",
};
Expand Down
170 changes: 60 additions & 110 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const QUEEN: i8 = 5;
pub const KING: i8 = 6;

pub const WHITE: i8 = 1;
pub const BLACK: i8 = 2;
pub const BLACK: i8 = -1;
pub const EMPTY: i8 = 0;

pub const BOARD_COORDINATES: [[&str; 8]; 8] = [
Expand All @@ -24,137 +24,87 @@ pub const BOARD_COORDINATES: [[&str; 8]; 8] = [
// MVV_VLA[victim][attacker]
// Most Valued Victim, Least Valued Attacker
pub const MVV_LVA: [[u8; 7]; 7] = [
[0, 0, 0, 0, 0, 0, 0], // victim K, attacker K, Q, R, B, N, P, None
[60, 61, 62, 63, 64, 65, 0], // victim K, attacker K, Q, R, B, N, P, None
[10, 11, 12, 13, 14, 15, 0], // victim P, attacker K, Q, R, B, N, P, None
[20, 21, 22, 23, 24, 25, 0], // victim N, attacker K, Q, R, B, N, P, None
[30, 31, 32, 33, 34, 35, 0], // victim B, attacker K, Q, R, B, N, P, None
[40, 41, 42, 43, 44, 45, 0], // victim R, attacker K, Q, R, B, N, P, None
[50, 51, 52, 53, 54, 55, 0], // victim Q, attacker K, Q, R, B, N, P, None
[0, 0, 0, 0, 0, 0, 0], // victim None, attacker K, Q, R, B, N, P, None
];
// pub const MG_PAWN_TABLE: [[i32; 8]; 8] = [
// [0, 0, 0, 0, 0, 0, 0, 0],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 2, 2, 1, 1, 1],
// [1, 1, 2, 1, 1, 2, 1, 1],
// [1, 1, 1, -1, -1, 1, 1, 1],
// [0, 0, 0, 0, 0, 0, 0, 0],
// ];
// pub const MG_KNIGHT_TABLE: [[i32; 8]; 8] = [
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// ];
// pub const MG_BISHOP_TABLE: [[i32; 8]; 8] = [
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// ];

// pub const MG_ROOK_TABLE: [[i32; 8]; 8] = [
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// ];
// pub const MG_QUEEN_TABLE: [[i32; 8]; 8] = [
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// ];
// pub const MG_KING_TABLE: [[i32; 8]; 8] = [
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// [1, 1, 1, 1, 1, 1, 1, 1],
// ];

pub const MG_PAWN_TABLE: [[i32; 8]; 8] = [
[0, 0, 0, 0, 0, 0, 0, 0],
[98, 134, 61, 95, 68, 126, 34, -11],
[-6, 7, 26, 31, 65, 56, 25, -20],
[-14, 13, 6, 21, 23, 12, 17, -23],
[-27, -2, -5, 12, 17, 6, 10, -25],
[-26, -4, -4, -10, 3, 3, 33, -12],
[-35, -1, -20, -23, -15, 24, 38, -22],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[5, 5, 10, 25, 25, 10, 5, 5],
[0, 0, 0, 20, 20, 0, 0, 0],
[5, -5, -10, 0, 0, -10, -5, 5],
[5, 10, 10, -20, -20, 10, 10, 5],
[0, 0, 0, 0, 0, 0, 0, 0],
];
pub const MG_KNIGHT_TABLE: [[i32; 8]; 8] = [
[-167, -89, -34, -49, 61, -97, -15, -107],
[-73, -41, 72, 36, 23, 62, 7, -17],
[-47, 60, 37, 65, 84, 129, 73, 44],
[-9, 17, 19, 53, 37, 69, 18, 22],
[-13, 4, 16, 13, 28, 19, 21, -8],
[-23, -9, 12, 10, 19, 17, 25, -16],
[-29, -53, -12, -3, -1, 18, -14, -19],
[-105, -21, -58, -33, -17, -28, -19, -23],
[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50],
];
pub const MG_BISHOP_TABLE: [[i32; 8]; 8] = [
[-29, 4, -82, -37, -25, -42, 7, -8],
[-26, 16, -18, -13, 30, 59, 18, -47],
[-16, 37, 43, 40, 35, 50, 37, -2],
[-4, 5, 19, 50, 37, 37, 7, -2],
[-6, 13, 13, 26, 34, 12, 10, 4],
[0, 15, 15, 15, 14, 27, 18, 10],
[4, 15, 16, 0, 7, 21, 33, 1],
[-33, -3, -14, -21, -13, -12, -39, -21],
[-20, -10, -10, -10, -10, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 10, 10, 5, 0, -10],
[-10, 5, 5, 10, 10, 5, 5, -10],
[-10, 0, 10, 10, 10, 10, 0, -10],
[-10, 10, 10, 10, 10, 10, 10, -10],
[-10, 5, 0, 0, 0, 0, 5, -10],
[-20, -10, -10, -10, -10, -10, -10, -20],
];

pub const MG_ROOK_TABLE: [[i32; 8]; 8] = [
[32, 42, 32, 51, 63, 9, 31, 43],
[27, 32, 58, 62, 80, 67, 26, 44],
[-5, 19, 26, 36, 17, 45, 61, 16],
[-24, -11, 7, 26, 24, 35, -8, -20],
[-36, -26, -12, -1, 9, -7, 6, -23],
[-45, -25, -16, -17, 3, 0, -5, -33],
[-44, -16, -20, -9, -1, 11, -6, -71],
[-19, -13, 1, 17, 16, 7, -37, -26],
[0, 0, 0, 0, 0, 0, 0, 0],
[5, 10, 10, 10, 10, 10, 10, 5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[0, 0, 0, 5, 5, 0, 0, 0],
];
pub const MG_QUEEN_TABLE: [[i32; 8]; 8] = [
[-28, 0, 29, 12, 59, 44, 43, 45],
[-24, -39, -5, 1, -16, 57, 28, 54],
[-13, -17, 7, 8, 29, 56, 47, 57],
[-27, -27, -16, -16, -1, 17, -2, 1],
[-9, -26, -9, -10, -2, -4, 3, -3],
[-14, 2, -11, -2, -5, 2, 14, 5],
[-35, -8, 11, 2, 8, 15, -3, 1],
[-1, -18, -9, 10, -15, -25, -31, -50],
[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 5, 5, 5, 0, -10],
[-5, 0, 5, 5, 5, 5, 0, -5],
[0, 0, 5, 5, 5, 5, 0, -5],
[-10, 5, 5, 5, 5, 5, 0, -10],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20],
];
pub const MG_KING_TABLE: [[i32; 8]; 8] = [
[-65, 23, 16, -15, -56, -34, 2, 13],
[29, -1, -20, -7, -8, -4, -38, -29],
[-9, 24, 2, -16, -20, 6, 22, -22],
[-17, -20, -12, -27, -30, -25, -14, -36],
[-49, -1, -27, -39, -46, -44, -33, -51],
[-14, -14, -22, -46, -44, -30, -15, -27],
[1, 7, -8, -64, -43, -16, 9, 8],
[-15, 36, 12, -54, 8, -28, 24, 14],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[20, 20, 0, 0, 0, 0, 20, 20],
[20, 30, 10, 0, 0, 10, 30, 20],
];

pub const EG_KING_TABLE: [[i32; 8]; 8] = [
[-50, -40, -30, -20, -20, -30, -40, -50],
[-30, -20, -10, 0, 0, -10, -20, -30],
[-30, -10, 20, 30, 30, 20, -10, -30],
[-30, -10, 30, 40, 40, 30, -10, -30],
[-30, -10, 30, 40, 40, 30, -10, -30],
[-30, -10, 20, 30, 30, 20, -10, -30],
[-30, -30, 0, 0, 0, 0, -30, -30],
[-50, -30, -30, -30, -30, -30, -30, -50],
];

pub const BENCH_FENS: [&str; 50] = [
"r3k2r/2pb1ppp/2pp1q2/p7/1nP1B3/1P2P3/P2N1PPP/R2QK2R w KQkq a6 0 14",
"4rrk1/2p1b1p1/p1p3q1/4p3/2P2n1p/1P1NR2P/PB3PP1/3R1QK1 b - - 2 24",
Expand Down
Loading

0 comments on commit c0de9ca

Please sign in to comment.