Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
da9358f
SCRUM-106 material-based eval
MHani0 Nov 5, 2024
b6f09b9
SCRUM-25 minimax search algorithm
MHani0 Nov 5, 2024
98cc97e
SCRUM-26 Alpha-Beta pruning
MHani0 Nov 7, 2024
4613a5d
add middle game evaluation, continue from mobility
y0sif Nov 15, 2024
035cf41
SCRUM-107 resolve conflicts
y0sif Nov 15, 2024
ea8cac5
SCRUM-107 finish middle game code translation, logic needs to be adde…
y0sif Nov 16, 2024
3b3c108
SCRUM-114 Zobrist Hashing
MHani0 Nov 16, 2024
d9c1550
Merge remote-tracking branch 'origin/alpha_beta' into alpha_beta
MHani0 Nov 16, 2024
bac4a0a
SCRUM-28 Transposition Table (I think)
MHani0 Nov 18, 2024
5f36804
SCRUM-29 Iterative Deepening
MHani0 Nov 19, 2024
5343622
SCRUM-107 complete middle game piece bonus and psqt
y0sif Nov 19, 2024
4e25a72
SCRUM-30 add aspiration windows
YusefZahran Nov 20, 2024
f468868
SCRUM-107 complete imbalance
y0sif Nov 20, 2024
6192ddc
SCRUM-108 add pawn middle game
y0sif Dec 2, 2024
1f6bf07
fix bug in psqt_bonus
farouk100 Dec 5, 2024
f3d826b
fix bug in imbalance function
farouk100 Dec 5, 2024
4a0b609
add uci
YusefZahran Jan 28, 2025
717b8be
fix bug in isolated and double isolated function
farouk100 Feb 18, 2025
7e19908
add backward function and enhance isolated and double isolated function
farouk100 Feb 19, 2025
9dffeab
add doubled function
farouk100 Feb 19, 2025
01a7d6f
fix connected and supported and phalanx function
farouk100 Feb 19, 2025
8af78c5
change piece_value_bonus and piece_value_mg & add non_pawn_material
YusefZahran Feb 19, 2025
9b1e62e
add uci.rs
YusefZahran Feb 20, 2025
f00c0a0
turn off iterative deepening
YusefZahran Feb 20, 2025
b1885fb
checkpoint: change cui depth to 5, comment evaluation code to make it…
YusefZahran Feb 22, 2025
24042bc
color_flip function done
farouk100 Feb 27, 2025
cc4bc97
use deep seek suggestion for vertical flip
y0sif Feb 28, 2025
81d4e5f
uci ready
y0sif Mar 1, 2025
1b2b2d1
add phase, remove Turn::Black match blocks and cleanup
YusefZahran Mar 9, 2025
4386e38
fix blocked function
farouk100 Mar 10, 2025
7906d8a
fix pawns mg and test it
farouk100 Mar 10, 2025
d408d03
setup pieces_mg function and its children
YusefZahran Mar 10, 2025
4fae011
add piecea_mg to middle gamme eval, implement minor_behind_pawn, rook…
YusefZahran Mar 12, 2025
e570316
implement bishop xray pawns, long diagonal bishop and king ring for p…
YusefZahran Mar 22, 2025
6288589
fix illegal moves war crimes
y0sif Mar 23, 2025
6ff842a
fix more war crimes regarding promotion in uci
y0sif Mar 23, 2025
3ed1025
SCRUM-4 fixed a bug in Alphabeta
MHani0 Mar 31, 2025
4437c15
Merge branch 'alpha_beta' of https://github.com/y0sif/rough_hook into…
MHani0 Mar 31, 2025
9202ec4
SCRUM-4 Fixed a bug in Alphabeta
MHani0 Mar 31, 2025
35e7a80
implement space function
YusefZahran Apr 4, 2025
8817d2c
add mobility mg -but there is a problem in mobility area from the poi…
farouk100 Apr 5, 2025
0ca4bae
resolve merge conflicts
YusefZahran Apr 5, 2025
367160c
fix bug in mobility area
farouk100 Apr 6, 2025
fb88a05
add pawnless flank and modify king ring function
farouk100 Apr 14, 2025
c8ed2be
add king_attackers_count function
farouk100 Apr 14, 2025
0b45e59
modify king_attakers_count to do king_attackers_weights function
farouk100 Apr 15, 2025
000092c
add king attacks and solve bug in rook xray attack
farouk100 Apr 15, 2025
8d73277
add pieces_mg incomplete, along with fixes and additions from farouk
YusefZahran Apr 15, 2025
452f4a0
fix merge conflicts
YusefZahran Apr 15, 2025
95350e8
Merge branch 'main_evaluation' of https://github.com/y0sif/rough_hook…
farouk100 Apr 15, 2025
c086ba4
add weak_bonus function
farouk100 Apr 15, 2025
031b78e
add flank attack function
farouk100 Apr 17, 2025
dc9ff91
add shelter_strength, shelter_storm functions and modify colorflip …
farouk100 Apr 17, 2025
d62e947
fix bishop_xray_pawns bug
YusefZahran Apr 17, 2025
e2e66fb
minor edit
YusefZahran Apr 17, 2025
f6b699b
Merge branch 'main_evaluation' of https://github.com/y0sif/rough_hook…
YusefZahran Apr 17, 2025
13d31ef
Merge branch 'main_evaluation' of https://github.com/y0sif/rough_hook…
farouk100 Apr 17, 2025
69254d4
add endgame_evaluation, edited main_evaluation
YusefZahran Apr 18, 2025
160163c
fix merge conflicts
YusefZahran Apr 18, 2025
0b7a847
some modificatioins in king mg
farouk100 Apr 18, 2025
9e36dda
merge with alpha_beta
y0sif Apr 18, 2025
208c53b
remove print statments
y0sif Apr 18, 2025
7ca2130
Merge branch 'main_evaluation' of https://github.com/y0sif/rough_hook…
farouk100 Apr 18, 2025
6caaaac
comment out pieces_mg and eg, start scale_factor but incomplete
YusefZahran Apr 18, 2025
baed5e9
resolve merge conflicts
YusefZahran Apr 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
/target
/hook_lens/data
/hook_lens/squares
/.vscode
/kanue
/hook_lens/data_in_sql_lite
/rough_guard/data_in_sql_lite
/nnue
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion rusty_brain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rough_guard = { path = "../rough_guard" }
rand = "0.8.5"
rough_guard = { path = "../rough_guard" }
362 changes: 362 additions & 0 deletions rusty_brain/src/alphabeta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
use std::i32;
use crate::board::{Board, Turn};
use crate::movement::Move;
use crate::transposition::{TranspositionTable, Node};


impl Board {

pub fn find_best_move(&mut self, transposition_table: &mut TranspositionTable, depth: i32) -> (Move, i32) {

/*
//using vanilla minimax

let eval = match self.turn {
Turn::White => self.maxi(true, depth),
Turn::Black => self.mini(true, depth)
};
(self.best_move.unwrap_or_else( || Move::encode(0, 0, 0)), eval)
*/

/*
//using normal alphabeta

let eval = match self.turn {
Turn::White => self.alpha_beta_max(true, i32::MIN, i32::MAX, depth),
Turn::Black => self.alpha_beta_min(true, i32::MIN, i32::MAX, depth),
};
(self.best_move.unwrap_or_else( || Move::encode(0, 0, 0)), eval)
*/


//using alphabeta with transposition table

let eval = match self.turn {
Turn::White => self.alpha_beta_max_tt(transposition_table, true, i32::MIN, i32::MAX, depth),
Turn::Black => self.alpha_beta_min_tt(transposition_table,true, i32::MIN, i32::MAX, depth),
};
(self.best_move.unwrap_or_else( || Move::encode(0, 0, 0)), eval)


/*
//Iterative deepening, needs move ordering to show its strength
let eval = match self.turn {
Turn::White => self.iterative_deepening(transposition_table, true, depth),
Turn::Black => self.iterative_deepening(transposition_table, false, depth),
};
(self.best_move.unwrap_or_else( || Move::encode(0, 0, 0)), eval)
*/


}

fn maxi(&mut self, maximazing: bool, depth: i32) -> i32 {
if depth == 0 {
return self.evaluate();
}
let mut max = i32::MIN;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {

self.make_move(current_move);
let score = self.mini(false, depth - 1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score > max {
max = score;
if maximazing {
self.best_move = Some(current_move);
}
}
}
return max;
}

fn mini(&mut self, minimizing: bool, depth: i32) -> i32 {
if depth == 0 {
return self.evaluate();
}
let mut min = i32::MAX;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {

self.make_move(current_move);
let score = self.maxi(false, depth - 1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score < min {
min = score;
if minimizing {
self.best_move = Some(current_move);
}
}
}
return min;
}

fn alpha_beta_max(&mut self, maximizing: bool, mut alpha: i32, beta: i32, depth_left: i32) -> i32 {

if depth_left == 0 {
return self.evaluate();
}

let mut best_value = i32::MIN;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {
self.make_move(current_move);
let score: i32 = self.alpha_beta_min(false, alpha, beta, depth_left - 1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score > best_value {
best_value = score;
if maximizing {
self.best_move = Some(current_move);
}
if score > alpha {
alpha = score;
}
}
if score >= beta {
if maximizing {
self.best_move = Some(current_move);
}
return score;
}
}

return best_value;

}

fn alpha_beta_min(&mut self, minimizing: bool, alpha: i32, mut beta: i32, depth_left: i32) -> i32 {

if depth_left == 0 {
return self.evaluate();
}

let mut best_value = i32::MAX;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {
self.make_move(current_move);
let score = self.alpha_beta_max(false, alpha, beta, depth_left-1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score < best_value {
best_value = score;
if minimizing {
self.best_move = Some(current_move);
}
if score < beta {
beta = score;
}
}
if score <= alpha {
if minimizing {
self.best_move = Some(current_move);
}
return score;
}
}
return best_value;
}


fn alpha_beta_max_tt(&mut self, transposition_table: &mut TranspositionTable, maximizing: bool, mut alpha: i32, mut beta: i32, depth_left: i32) -> i32 {

if let Some(entry) = transposition_table.retrieve_from_table(self) {

if entry.depth >= depth_left {
match entry.node_type {
Node::Exact => {
if maximizing {
self.best_move = entry.best_move;
}
return entry.score;
},
Node::LowerBound => {
if entry.score > alpha {
alpha = entry.score;
}
}
Node::UpperBound => {
if entry.score < beta {
beta = entry.score;
}
}
}
if alpha >= beta {
if maximizing {
self.best_move = entry.best_move;
}
return entry.score;
}

}

}

if depth_left == 0 {
return self.evaluate();
}

let mut best_value = i32::MIN;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {

self.make_move(current_move);
let score: i32 = self.alpha_beta_min_tt(transposition_table, false, alpha, beta, depth_left - 1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score > best_value {
best_value = score;
if maximizing {
self.best_move = Some(current_move);
}
if score > alpha {
alpha = score;
}
}
if score >= beta {
if maximizing {
self.best_move = Some(current_move);
transposition_table.store_in_table(self, self.best_move, depth_left, best_value, alpha, beta);
}
return score;
}
}

transposition_table.store_in_table(self, self.best_move, depth_left, best_value, alpha, beta);

return best_value;

}


fn alpha_beta_min_tt(&mut self, transposition_table: &mut TranspositionTable, minimizing: bool, mut alpha: i32, mut beta: i32, depth_left: i32) -> i32 {

if let Some(entry) = transposition_table.retrieve_from_table(self) {

if entry.depth >= depth_left {
match entry.node_type {
Node::Exact => {
if minimizing {
self.best_move = entry.best_move;
}
return entry.score;
},
Node::LowerBound => {
if entry.score > alpha {
alpha = entry.score;
}
}
Node::UpperBound => {
if entry.score < beta {
beta = entry.score;
}
}
}
if alpha >= beta {
if minimizing {
self.best_move = entry.best_move;
}
return entry.score;
}
}
}

if depth_left == 0 {
return self.evaluate();
}

let mut best_value = i32::MAX;
let moves: Vec<Move> = self.generate_legal_moves();

for current_move in moves {

self.make_move(current_move);
let score = self.alpha_beta_max_tt(transposition_table, false, alpha, beta, depth_left-1);
if self.checkmate || self.draw || self.stalemate {
self.undo_move();
return self.evaluate();
}
self.undo_move();

if score < best_value {
best_value = score;
if minimizing {
self.best_move = Some(current_move);
}
if score < beta {
beta = score;
}
}
if score <= alpha {
if minimizing {
self.best_move = Some(current_move);
transposition_table.store_in_table(self, self.best_move, depth_left, best_value, alpha, beta);
}
return score;
}
}

transposition_table.store_in_table(self, self.best_move, depth_left, best_value, alpha, beta);

return best_value;
}

fn iterative_deepening(&mut self, transposition_table: &mut TranspositionTable, maximizing: bool, max_depth: i32) -> i32 {

let mut best_score = 0;
let mut guess = 0;
let mut delta = 100;

for depth in 1..=max_depth{
let mut alpha = guess - delta;
let mut beta = guess + delta;

loop{
best_score = if maximizing{
self.alpha_beta_max_tt(transposition_table, true, alpha, beta, depth)
}else{
self.alpha_beta_min_tt(transposition_table, true, alpha, beta, depth)
};
if best_score <= alpha{
alpha = alpha -delta; //fail low, widen window low side
} else if best_score >= beta{
beta = beta + delta; // fail high, widen window high side
}else{ // success!
guess = best_score; //want to figure out if it should be zeroed out every new depth or not
break;
}
delta = delta *2;}
}

best_score
}

}
2 changes: 2 additions & 0 deletions rusty_brain/src/bitboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use std::u64;
use std::collections::HashMap;
use crate::board::Turn;

#[derive(Clone, Copy, PartialEq)]
pub struct Bitboards{
pub white_pawns: u64,
pub white_knights: u64,
Expand Down
Loading