[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

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)
}
}