probabilistic progress

This commit is contained in:
2025-04-28 01:12:15 -04:00
parent 2586b43294
commit c3c07fcb28
4 changed files with 58 additions and 66 deletions

View File

@@ -8,48 +8,44 @@ pub enum MVSGameState {
Loss = -1,
}
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default)]
#[derive(Clone, Copy, Debug, Allocative, PartialEq, Eq, Default, PartialOrd)]
pub struct MoveValueStats {
state: Option<MVSGameState>,
wins: u16,
losses: u16,
ties: u16,
pub value: i32,
}
impl MoveValueStats {
#[cfg(test)]
pub const fn new_from_wins_losses(wins: u16, losses: u16) -> Self {
pub fn new_from_wins_losses(wins: u16, losses: u16) -> Self {
Self {
state: None,
wins,
losses,
value: 0,
..Default::default()
}
}
#[cfg(test)]
pub const fn new_from_value(value: i32) -> Self {
pub fn new_from_value(value: i32) -> Self {
Self {
state: None,
wins: 0,
losses: 0,
value,
..Default::default()
}
}
#[cfg(test)]
pub const fn new_from_state(state: Option<MVSGameState>) -> Self {
pub fn new_from_state(state: Option<MVSGameState>) -> Self {
Self {
state,
wins: 0,
losses: 0,
value: 0,
..Default::default()
}
}
fn chance_win(&self) -> Option<f32> {
let sum = self.losses + self.wins;
if sum == 0 {
let sum = self.losses + self.wins + self.ties;
if 20 > sum {
return None;
}
Some(self.wins as f32 / sum as f32)
@@ -70,12 +66,11 @@ impl MoveValueStats {
.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))
self.ties = others.iter().map(|x| x.ties).sum::<u16>()
+ others
.iter()
.filter(|x| x.state == Some(MVSGameState::Tie))
.count() as u16;
}
}
@@ -122,4 +117,11 @@ mod tests {
let two = MoveValueStats::new_from_state(Some(MVSGameState::Win));
assert!(one < two);
}
#[test]
fn two_prob_zero() {
let one = MoveValueStats::new_from_wins_losses(10, 0);
let two = MoveValueStats::new_from_wins_losses(0, 6);
assert!(one > two);
}
}