mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-16 16:25:07 +01:00
minmax-rs
This commit is contained in:
parent
a3b836265a
commit
9900001888
15 changed files with 0 additions and 0 deletions
184
minmax-rs/src/tic_tac_toe/board.rs
Normal file
184
minmax-rs/src/tic_tac_toe/board.rs
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
use std::fmt::{Display, Write};
|
||||
|
||||
use crate::{Game, Player, Score, State};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicTacToe(u32);
|
||||
|
||||
impl TicTacToe {
|
||||
pub fn empty() -> Self {
|
||||
// A = 1010
|
||||
// 18 bits - 9 * 2 bits - 4.5 nibbles
|
||||
Self(0x0002AAAA)
|
||||
}
|
||||
|
||||
fn validate(&self) {
|
||||
if cfg!(debug_assertions) {
|
||||
let board = self.0;
|
||||
for i in 0..16 {
|
||||
let next_step = board >> (i * 2);
|
||||
let mask = 0b11;
|
||||
let pos = next_step & mask;
|
||||
if pos >= 3 {
|
||||
panic!("Invalid bits, self: {board:0X}, bits: {pos:0X}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, index: usize) -> Option<Player> {
|
||||
debug_assert!(index < 9);
|
||||
|
||||
let board = self.0;
|
||||
|
||||
let shifted = board >> (index * 2);
|
||||
let masked = shifted & 0b11;
|
||||
|
||||
// SAFETY: So uh, this is a bit unlucky.
|
||||
// You see, there are two entire bits of information at our disposal for each position.
|
||||
// This is really bad. We only have three valid states. So we need to do _something_ if it's invalid.
|
||||
// We just hope that it will never be invalid which it really shouldn't be and also have a debug assertion
|
||||
// here to make sure that it really is valid and then if it's not invalid we just mov it out and are happy.
|
||||
self.validate();
|
||||
unsafe { Player::from_u8(masked as u8).unwrap_unchecked() }
|
||||
}
|
||||
|
||||
pub fn set(&mut self, index: usize, value: Option<Player>) {
|
||||
debug_assert!(index < 9);
|
||||
self.validate();
|
||||
|
||||
let value = Player::as_u8(value) as u32;
|
||||
|
||||
let value = value << (index * 2);
|
||||
let mask = 0b11 << (index * 2);
|
||||
|
||||
let current_masked_off_new = self.0 & !mask;
|
||||
let result = value | current_masked_off_new;
|
||||
self.0 = result;
|
||||
|
||||
self.validate();
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = Option<Player>> {
|
||||
let mut i = 0;
|
||||
let this = self.clone();
|
||||
std::iter::from_fn(move || {
|
||||
let result = (i < 9).then(|| this.get(i));
|
||||
i += 1;
|
||||
result
|
||||
})
|
||||
}
|
||||
|
||||
pub fn result(&self) -> State {
|
||||
win_table::result(self)
|
||||
}
|
||||
}
|
||||
|
||||
mod win_table {
|
||||
use super::TicTacToe;
|
||||
use crate::{Player, State};
|
||||
|
||||
const WIN_TABLE_SIZE: usize = 2usize.pow(2 * 9);
|
||||
static WIN_TABLE: &[u8; WIN_TABLE_SIZE] =
|
||||
include_bytes!(concat!(env!("OUT_DIR"), "/win_table"));
|
||||
|
||||
pub fn result(board: &TicTacToe) -> State {
|
||||
match WIN_TABLE[board.0 as usize] {
|
||||
0 => State::Winner(Player::X),
|
||||
1 => State::Winner(Player::X),
|
||||
2 => State::InProgress,
|
||||
3 => State::Draw,
|
||||
n => panic!("Invalid value {n} in table"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TicTacToe {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for i in 0..3 {
|
||||
for j in 0..3 {
|
||||
let index = i * 3 + j;
|
||||
match self.get(index) {
|
||||
Some(player) => {
|
||||
write!(f, "\x1B[33m{player}\x1B[0m ")?;
|
||||
}
|
||||
None => {
|
||||
write!(f, "\x1B[35m{index}\x1B[0m ")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
f.write_char('\n')?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Game for TicTacToe {
|
||||
type Move = usize;
|
||||
|
||||
const REASONABLE_SEARCH_DEPTH: Option<usize> = None;
|
||||
|
||||
fn empty() -> Self {
|
||||
Self::empty()
|
||||
}
|
||||
|
||||
fn possible_moves(&self) -> impl Iterator<Item = Self::Move> {
|
||||
debug_assert!(
|
||||
!self.iter().all(|x| x.is_some()),
|
||||
"the board is full but state is InProgress"
|
||||
);
|
||||
|
||||
self.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, position)| position.is_none())
|
||||
.map(|(pos, _)| pos)
|
||||
}
|
||||
|
||||
fn result(&self) -> State {
|
||||
TicTacToe::result(self)
|
||||
}
|
||||
|
||||
fn rate(&self, _: Player) -> Score {
|
||||
unimplemented!("we always finish the board")
|
||||
}
|
||||
|
||||
fn make_move(&mut self, position: Self::Move, player: Player) {
|
||||
self.set(position, Some(player));
|
||||
}
|
||||
|
||||
fn undo_move(&mut self, position: Self::Move) {
|
||||
self.set(position, None);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Player, TicTacToe};
|
||||
|
||||
#[test]
|
||||
fn board_field() {
|
||||
let mut board = TicTacToe::empty();
|
||||
board.set(0, None);
|
||||
board.set(8, Some(Player::X));
|
||||
board.set(4, Some(Player::O));
|
||||
board.set(5, Some(Player::X));
|
||||
|
||||
let expected = [
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Player::O),
|
||||
Some(Player::X),
|
||||
None,
|
||||
None,
|
||||
Some(Player::X),
|
||||
];
|
||||
|
||||
board
|
||||
.iter()
|
||||
.zip(expected.into_iter())
|
||||
.enumerate()
|
||||
.for_each(|(idx, (actual, expected))| assert_eq!(actual, expected, "Position {idx}"));
|
||||
}
|
||||
}
|
||||
33
minmax-rs/src/tic_tac_toe/game.rs
Normal file
33
minmax-rs/src/tic_tac_toe/game.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use crate::{GamePlayer, Player, State};
|
||||
|
||||
use super::TicTacToe;
|
||||
|
||||
impl TicTacToe {
|
||||
pub fn play<A: GamePlayer<TicTacToe>, B: GamePlayer<TicTacToe>>(
|
||||
&mut self,
|
||||
x: &mut A,
|
||||
o: &mut B,
|
||||
) -> Option<Player> {
|
||||
let mut current_player = Player::X;
|
||||
|
||||
for _ in 0..9 {
|
||||
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();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
43
minmax-rs/src/tic_tac_toe/mod.rs
Normal file
43
minmax-rs/src/tic_tac_toe/mod.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
mod board;
|
||||
mod game;
|
||||
mod player;
|
||||
|
||||
pub use {board::TicTacToe, player::*};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{minmax::PerfectPlayer, tic_tac_toe::board::TicTacToe, GamePlayer, Player};
|
||||
|
||||
use super::player::{GreedyPlayer, RandomPlayer};
|
||||
|
||||
fn assert_win_ratio<X: GamePlayer<TicTacToe>, O: GamePlayer<TicTacToe>>(
|
||||
runs: u64,
|
||||
x_win_ratio: f64,
|
||||
x: impl Fn() -> X,
|
||||
o: impl Fn() -> O,
|
||||
) {
|
||||
let mut results = [0u64, 0, 0];
|
||||
|
||||
for _ in 0..runs {
|
||||
let result = TicTacToe::empty().play::<X, O>(&mut x(), &mut o());
|
||||
let idx = Player::as_u8(result);
|
||||
results[idx as usize] += 1;
|
||||
}
|
||||
|
||||
let total = results.iter().copied().sum::<u64>();
|
||||
|
||||
let ratio = (total as f64) / (results[0] as f64);
|
||||
println!("{ratio} >= {x_win_ratio}");
|
||||
assert!(ratio >= x_win_ratio);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perfect_always_beats_greedy() {
|
||||
assert_win_ratio(20, 1.0, || PerfectPlayer::new(false), || GreedyPlayer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perfect_beats_random() {
|
||||
assert_win_ratio(10, 0.95, || PerfectPlayer::new(false), || RandomPlayer);
|
||||
}
|
||||
}
|
||||
65
minmax-rs/src/tic_tac_toe/player.rs
Normal file
65
minmax-rs/src/tic_tac_toe/player.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
use std::io::Write;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{GamePlayer, Player};
|
||||
|
||||
use super::TicTacToe;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct GreedyPlayer;
|
||||
|
||||
impl GamePlayer<TicTacToe> for GreedyPlayer {
|
||||
fn next_move(&mut self, board: &mut TicTacToe, this_player: Player) {
|
||||
let first_free = board.iter().position(|p| p.is_none()).unwrap();
|
||||
board.set(first_free, Some(this_player));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct HumanPlayer;
|
||||
|
||||
impl GamePlayer<TicTacToe> for HumanPlayer {
|
||||
fn next_move(&mut self, board: &mut TicTacToe, this_player: Player) {
|
||||
loop {
|
||||
print!("{board}where to put the next {this_player}? (0-8): ");
|
||||
|
||||
std::io::stdout().flush().unwrap();
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).unwrap();
|
||||
|
||||
match buf.trim().parse() {
|
||||
Ok(number) if number < 9 => match board.get(number) {
|
||||
None => {
|
||||
board.set(number, Some(this_player));
|
||||
return;
|
||||
}
|
||||
Some(_) => {
|
||||
println!("Field is occupied already.")
|
||||
}
|
||||
},
|
||||
Ok(_) | Err(_) => {
|
||||
println!("Invalid input.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct RandomPlayer;
|
||||
|
||||
impl GamePlayer<TicTacToe> for RandomPlayer {
|
||||
fn next_move(&mut self, board: &mut TicTacToe, this_player: Player) {
|
||||
loop {
|
||||
let next = rand::thread_rng().gen_range(0..9);
|
||||
match board.get(next) {
|
||||
Some(_) => {}
|
||||
None => {
|
||||
board.set(next, Some(this_player));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue