mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-17 08:45:04 +01:00
AAAAAAAAAAAAAAAAAAAAAAAAa
This commit is contained in:
parent
7f3a0e5ad2
commit
d3d7011c43
13 changed files with 280 additions and 88 deletions
60
src/lib.rs
60
src/lib.rs
|
|
@ -1,4 +1,9 @@
|
|||
#![feature(never_type, try_trait_v2, return_position_impl_trait_in_trait)]
|
||||
#![feature(
|
||||
never_type,
|
||||
try_trait_v2,
|
||||
return_position_impl_trait_in_trait,
|
||||
let_chains
|
||||
)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
pub mod connect4;
|
||||
|
|
@ -7,13 +12,56 @@ pub mod tic_tac_toe;
|
|||
|
||||
mod player;
|
||||
|
||||
use self::minmax::GameBoard;
|
||||
use std::fmt::Display;
|
||||
|
||||
use minmax::Score;
|
||||
pub use player::{Player, State};
|
||||
|
||||
pub trait Game {
|
||||
type Board: GameBoard;
|
||||
pub trait GamePlayer<G: ?Sized + Game>: Default {
|
||||
fn next_move(&mut self, board: &mut G, this_player: Player);
|
||||
}
|
||||
|
||||
pub trait GamePlayer<G: Game>: Default {
|
||||
fn next_move(&mut self, board: &mut G::Board, this_player: Player);
|
||||
pub trait Game: Display {
|
||||
type Move: Copy;
|
||||
|
||||
const REASONABLE_SEARCH_DEPTH: Option<usize>;
|
||||
|
||||
fn empty() -> Self;
|
||||
|
||||
fn possible_moves(&self) -> impl Iterator<Item = Self::Move>;
|
||||
|
||||
fn result(&self) -> State;
|
||||
|
||||
/// Only called if [`GameBoard::REASONABLE_SEARCH_DEPTH`] is `Some`.
|
||||
fn rate(&self, player: Player) -> Score;
|
||||
|
||||
fn make_move(&mut self, position: Self::Move, player: Player);
|
||||
|
||||
fn undo_move(&mut self, position: Self::Move);
|
||||
|
||||
fn play<A: GamePlayer<Self>, B: GamePlayer<Self>>(
|
||||
&mut self,
|
||||
x: &mut A,
|
||||
o: &mut B,
|
||||
) -> Option<Player> {
|
||||
let mut current_player = Player::X;
|
||||
|
||||
loop {
|
||||
if current_player == Player::X {
|
||||
x.next_move(self, current_player);
|
||||
} else {
|
||||
o.next_move(self, current_player);
|
||||
}
|
||||
|
||||
match self.result() {
|
||||
State::Winner(player) => return Some(player),
|
||||
State::Draw => {
|
||||
return None;
|
||||
}
|
||||
State::InProgress => {}
|
||||
}
|
||||
|
||||
current_player = current_player.opponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue