Skip to content

Commit

Permalink
fix: fixed king moving very early
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphereye committed Oct 9, 2024
1 parent 199f8b4 commit b92a09e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shard"
version = "2.1.0"
version = "2.2.0"
edition = "2021"
authors = ["Saphereye <[email protected]>"]

Expand Down
114 changes: 68 additions & 46 deletions src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,88 @@ const QUEEN_VALUE: i32 = 900;

#[rustfmt::skip]
const PAWN_TABLE: [i32; 64] = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
];

#[rustfmt::skip]
const KNIGHT_TABLE: [i32; 64] = [
-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,
-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
];

#[rustfmt::skip]
const BISHOP_TABLE: [i32; 64] = [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
];

#[rustfmt::skip]
const ROOK_TABLE: [i32; 64] = [
0, 0, 0, 5, 5, 0, 0, 0,
-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,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 5, 5, 0, 0, 0,
-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,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
];

#[rustfmt::skip]
const QUEEN_TABLE: [i32; 64] = [
-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,
-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
];

#[rustfmt::skip]
const KING_TABLE: [i32; 64] = [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, -10, -10, -10, -10, 20, 20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, -30, -30, -40, -40, -30, -30, -20,
20, -30, -30, -40, -40, -30, -30, -20,
-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
];

#[rustfmt::skip]
const KING_ENDGAME_TABLE: [i32; 64] = [
-50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -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, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50,
];

pub fn evaluate_board(board: &Board) -> i64 {
let is_white_turn = board.side_to_move() == Color::White;
// Checkmate condition
Expand Down Expand Up @@ -130,9 +142,9 @@ pub fn evaluate_board(board: &Board) -> i64 {

// Positional value based on the side (flip for Black)
let positional_score = if board.color_on(sq) == Some(Color::White) {
positional_value(piece, sq)
positional_value(piece, sq, board.combined().popcnt() < 16, Color::White)
} else {
-positional_value(piece, sq)
-positional_value(piece, sq, board.combined().popcnt() < 16, Color::Black)
};

let total_value = if board.color_on(sq) == Some(Color::White) {
Expand Down Expand Up @@ -216,15 +228,15 @@ fn king_safety_score(king_sq: Square, board: &Board) -> i32 {
let king_file = king_sq.get_file();

// Simple heuristic: kings are safer near the center and with pawns in front of them
let king_center_distance =
(king_rank.to_index() as i32 - 3).abs() + (king_file.to_index() as i32 - 3).abs();
score -= king_center_distance * 10; // Penalize distance from the center
// let king_center_distance =
// (king_rank.to_index() as i32 - 3).abs() + (king_file.to_index() as i32 - 3).abs();
// score -= king_center_distance * 10; // Penalize distance from the center

// Check if the king has a pawn shield (pawns in front of the king)
for file_offset in -1..=1 {
let new_file = (king_file.to_index() as i32 + file_offset).clamp(0, 7) as u8;
let shield_sq = Square::make_square(
Rank::from_index(king_rank.to_index()),
Rank::from_index(king_rank.to_index() + 1),
File::from_index(new_file as usize),
);
if let Some(piece) = board.piece_on(shield_sq) {
Expand Down Expand Up @@ -312,15 +324,25 @@ fn rook_on_open_file(rook_sq: Square, board: &Board) -> bool {
true // Open file if no pawns found
}

fn positional_value(piece: Piece, square: Square) -> i32 {
let index = square.to_index();
fn positional_value(piece: Piece, square: Square, is_endgame: bool, side_to_move: Color) -> i32 {
let mut index = square.to_index();

if side_to_move == Color::Black {
index = 63 - index;
}

match piece {
Piece::Pawn => PAWN_TABLE[index],
Piece::Knight => KNIGHT_TABLE[index],
Piece::Bishop => BISHOP_TABLE[index],
Piece::Rook => ROOK_TABLE[index],
Piece::Queen => QUEEN_TABLE[index],
Piece::King => KING_TABLE[index],
Piece::King => {
if is_endgame {
KING_ENDGAME_TABLE[index]
} else {
KING_TABLE[index]
}
}
}
}
28 changes: 16 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use chess::*;
use rand::Rng;
use std::time::{Duration, Instant};
use std::{
fmt::{self, Debug, Formatter},
str::FromStr,
Expand Down Expand Up @@ -140,7 +138,7 @@ fn mcts(root: &mut Node) {
}

fn rollout(current: &Node) -> i64 {
return evaluate_board(&current.board);
evaluate_board(&current.board)
}

fn ucb1(child: &Node, parent: &Node) -> f64 {
Expand Down Expand Up @@ -197,13 +195,7 @@ fn uci_to_move(uci: &str) -> Result<ChessMove, String> {
fn main() {
let mut root = Node::default();
let mut current_root: *mut Node = &mut root;

// Precomputing the tree
unsafe {
for _ in 0..1_00_000 {
mcts(&mut *current_root);
}
}
let mut is_moving_first_time: bool = true;

loop {
let mut input = String::new();
Expand All @@ -226,6 +218,12 @@ fn main() {

current_root = &mut root;

if moves.len() == 0 {
is_moving_first_time = true;
} else {
is_moving_first_time = false;
}

for chess_move in moves {
unsafe {
// Check current_child nodes
Expand Down Expand Up @@ -267,8 +265,14 @@ fn main() {
nodes,
},
)) => unsafe {
for _ in 0..50_000 {
mcts(&mut *current_root);
if is_moving_first_time {
for _ in 0..500_000 {
mcts(&mut *current_root);
}
} else {
for _ in 0..50_000 {
mcts(&mut *current_root);
}
}

let best_move = find_best_move(&*current_root);
Expand Down

0 comments on commit b92a09e

Please sign in to comment.