chessfriend/core/src/score.rs

88 lines
1.6 KiB
Rust
Raw Normal View History

// Eryn Wells <eryn@erynwells.me>
2025-06-24 15:20:31 -07:00
use std::{
fmt,
ops::{Add, AddAssign, Mul, Neg, 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)
}
}
2025-06-24 15:20:31 -07:00
impl fmt::Display for Score {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = self.0;
if *self == Self::MAX {
write!(f, "INF")
} else if *self == Self::MIN {
write!(f, "-INF")
} else {
write!(f, "{value}cp")
}
}
}