Add initial material-based board eval

probably other stuff
master
Drake 1 year ago
parent a0b507f56b
commit 2d115f5a1f

16
Cargo.lock generated

@ -196,6 +196,12 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "failure"
version = "0.1.8"
@ -274,10 +280,20 @@ name = "inkwell"
version = "0.1.0"
dependencies = [
"chess",
"itertools",
"rand 0.8.5",
"vampirc-uci",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "js-sys"
version = "0.3.60"

@ -7,5 +7,6 @@ edition = "2021"
[dependencies]
chess = "3.2.0"
itertools = "0.10.5"
rand = "0.8.5"
vampirc-uci = { version = "0.11.1", features = ["chess"] }

@ -18,3 +18,6 @@ pub const INFO: Info = Info {
version: "0.0.1-rc",
author: "Bendy (real)",
};
// FIXME: this needs to be configurable via UCI
pub const depth: u8 = 0;

@ -0,0 +1,20 @@
use chess::{Board, ChessMove, Color, MoveGen, Piece};
pub fn eval_board(board: Board, playing_as: Color, curr_depth: u8) -> i128 {
let mut current_eval: i128 = 0;
let moves_iter = MoveGen::new_legal(&board).into_iter();
moves_iter.for_each(|mov| {
let tmp_board = board.make_move_new(mov);
current_eval += tmp_board.color_combined(playing_as).count() as i128;
current_eval -= tmp_board.color_combined(!playing_as).count() as i128;
eprintln!("{}", current_eval);
if curr_depth != 0 {
eprintln!("{}", curr_depth);
current_eval += eval_board(tmp_board, playing_as, curr_depth - 1)
}
});
return current_eval;
}

@ -1,7 +1,10 @@
mod config;
mod eval;
use chess::{Board, MoveGen, EMPTY};
use chess::{Board, MoveGen};
use config::INFO;
use eval::eval_board;
use itertools::Itertools;
use rand::seq::IteratorRandom;
use std::io::{self, BufRead};
use std::process;
@ -11,6 +14,8 @@ use vampirc_uci::{
MessageList, Serializable, UciMessage, UciOptionConfig, UciSearchControl, UciTimeControl,
};
use crate::config::depth;
fn main() {
let mut rng = rand::thread_rng();
let mut board: Option<Board> = None;
@ -41,10 +46,7 @@ fn main() {
moves,
} => {
if startpos {
board = Some(
Board::from_str("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
.unwrap(),
)
board = Some(Board::default())
}
if let Some(fen) = fen {
board = Some(Board::from_str(fen.as_str()).expect("Invalid FEN board"))
@ -88,16 +90,30 @@ fn main() {
}
if let Some(board) = board {
let iter = MoveGen::new_legal(&board).into_iter();
let color = board.side_to_move();
let moves = MoveGen::new_legal(&board).into_iter();
let iter = moves.map(|mov| (mov, board.make_move_new(mov)));
println!("bestmove {}", iter.choose(&mut rng).unwrap());
println!(
"bestmove {}",
iter.sorted_by(|a, b| Ord::cmp(
&eval_board(b.1, color, depth),
&eval_board(a.1, color, depth)
))
.next()
.unwrap()
.0
);
}
}
UciMessage::Stop => {
// FIXME: impl
}
UciMessage::Quit => {
process::exit(0);
}
_ => {
todo!()
todo!("{}", msg.to_string())
}
}
}

Loading…
Cancel
Save