This commit is contained in:
2025-04-24 11:10:11 -04:00
parent 23e7ae2822
commit e8d05e0f9d
4 changed files with 87 additions and 84 deletions

70
src/logic/mvs.rs Normal file
View File

@@ -0,0 +1,70 @@
use allocative::Allocative;
use std::cmp::Ordering;
#[derive(Clone, Copy, PartialEq, Eq, Allocative, Debug, PartialOrd, Ord)]
pub enum MVSGameState {
Win = 1,
Tie = 0,
Loss = -1,
}
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default)]
pub struct MoveValueStats {
state: Option<MVSGameState>,
wins: u16,
losses: u16,
pub value: i32,
}
impl MoveValueStats {
fn chance_win(&self) -> Option<f32> {
let sum = self.losses + self.wins;
if sum == 0 {
return None;
}
Some(self.wins as f32 / sum as f32)
}
pub const fn set_state(&mut self, state: Option<MVSGameState>) {
self.state = state;
}
pub fn populate_self_from_children(&mut self, others: &[Self]) {
self.wins = others.iter().map(|x| x.wins).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Win))
.count() as u16;
self.losses = others.iter().map(|x| x.losses).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Loss))
.count() as u16;
}
}
impl PartialOrd for MoveValueStats {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MoveValueStats {
fn cmp(&self, other: &Self) -> Ordering {
if self.state.is_some() || other.state.is_some() {
return self.state.cmp(&other.state);
}
if let Some(s_cw) = self.chance_win() {
if let Some(o_cw) = other.chance_win() {
if s_cw > o_cw {
return Ordering::Greater;
} else if o_cw > s_cw {
return Ordering::Less;
}
}
}
self.value.cmp(&other.value)
}
}