[board, core, position] A simple static evaluation method for scoring positions

Implement a new Evaluator struct that evaluates a Board and returns a score. This
evaluation mechanism uses only a material balance function. It doesn't account
for anything else.

Supporting this, add a Counts struct to the internal piece set structure of a
Board. This struct is responsible for keeping counts of how many pieces of each
shape are on the board for each color. Export a count_piece() method on Board
that returns a count of the number of pieces of a particular color and shape.

Implement a newtype wrapper around i32 called Score that represents the score of
a position in centipawns, i.e. hundredths of a pawn. Add piece values to the
Shape enum.
This commit is contained in:
Eryn Wells 2025-06-20 14:23:57 -07:00
parent 481ae70698
commit 7f25548335
10 changed files with 249 additions and 10 deletions

View file

@ -1,6 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use crate::Direction;
use crate::{Direction, score::ScoreInner};
use thiserror::Error;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
@ -56,6 +56,14 @@ impl Color {
Color::Black => "black",
}
}
#[must_use]
pub const fn score_factor(self) -> ScoreInner {
match self {
Color::White => 1,
Color::Black => -1,
}
}
}
impl std::fmt::Display for Color {

View file

@ -4,6 +4,7 @@ pub mod colors;
pub mod coordinates;
pub mod pieces;
pub mod random;
pub mod score;
pub mod shapes;
mod macros;

71
core/src/score.rs Normal file
View file

@ -0,0 +1,71 @@
// Eryn Wells <eryn@erynwells.me>
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
pub(crate) type ScoreInner = i32;
/// A score for a position in centipawns.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Score(ScoreInner);
impl Score {
#[must_use]
pub const fn zero() -> Self {
Self(0)
}
#[must_use]
pub const fn new(value: ScoreInner) -> Self {
Self(value)
}
}
impl Add for Score {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Score(self.0 + rhs.0)
}
}
impl AddAssign for Score {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl Sub for Score {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Score(self.0 - rhs.0)
}
}
impl SubAssign for Score {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl Mul<ScoreInner> for Score {
type Output = Score;
fn mul(self, rhs: ScoreInner) -> Self::Output {
Score(self.0 * rhs)
}
}
impl Mul<Score> for ScoreInner {
type Output = Score;
fn mul(self, rhs: Score) -> Self::Output {
Score(self * rhs.0)
}
}
impl From<ScoreInner> for Score {
fn from(value: ScoreInner) -> Self {
Score(value)
}
}

View file

@ -3,6 +3,8 @@
use std::{array, fmt, slice, str::FromStr};
use thiserror::Error;
use crate::score::Score;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Shape {
Pawn = 0,
@ -71,6 +73,17 @@ impl Shape {
pub fn is_promotable(&self) -> bool {
Self::PROMOTABLE_SHAPES.contains(self)
}
#[must_use]
pub fn score(self) -> Score {
match self {
Shape::Pawn => Score::new(100),
Shape::Knight | Shape::Bishop => Score::new(300),
Shape::Rook => Score::new(500),
Shape::Queen => Score::new(900),
Shape::King => Score::new(20000),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]