This commit is contained in:
nora 2022-12-05 11:05:23 +01:00
parent 6172250ff5
commit 5b0c3106c0
No known key found for this signature in database
11 changed files with 560 additions and 185 deletions

View file

@ -12,15 +12,28 @@ pub mod tic_tac_toe;
mod player;
use std::fmt::Display;
use std::{fmt::Display, ops::Neg};
use minmax::Score;
pub use self::minmax::PerfectPlayer;
pub use player::{Player, State};
pub trait GamePlayer<G: ?Sized + Game>: Default {
pub trait GamePlayer<G: ?Sized + Game> {
fn next_move(&mut self, board: &mut G, this_player: Player);
}
impl<G: Game, P: GamePlayer<G> + ?Sized> GamePlayer<G> for &mut P {
fn next_move(&mut self, board: &mut G, this_player: Player) {
P::next_move(self, board, this_player)
}
}
impl<G: Game, P: GamePlayer<G> + ?Sized> GamePlayer<G> for Box<P> {
fn next_move(&mut self, board: &mut G, this_player: Player) {
P::next_move(self, board, this_player)
}
}
pub trait Game: Display {
type Move: Copy;
@ -65,3 +78,31 @@ pub trait Game: Display {
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Score(i32);
impl Score {
const MIN: Self = Self(i32::MIN);
const LOST: Self = Self(-100);
const TIE: Self = Self(0);
const WON: Self = Self(100);
pub fn new(int: i32) -> Self {
Self(int)
}
fn randomize(self) -> Self {
let score = self.0 as f32;
let rand = rand::thread_rng();
self
}
}
impl Neg for Score {
type Output = Self;
fn neg(self) -> Self::Output {
Self(-self.0)
}
}