ai implemented, needs some perf tuning
This commit is contained in:
parent
8c4b0fc1cd
commit
05536f0dc3
7 changed files with 310 additions and 23 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -244,6 +244,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"criterion",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ name = "othello"
|
|||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "othello-gui"
|
||||
name = "othello-cli"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.100"
|
||||
regex = "1.12.2"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.7.0"
|
||||
|
|
|
|||
81
src/ai.rs
Normal file
81
src/ai.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
use crate::{
|
||||
board::{Board, explode_board},
|
||||
game::{Game, Team},
|
||||
};
|
||||
|
||||
/// Using alpha-beta pruning and the minimax algorithm, determine the best move
|
||||
/// for a game with a recursion depth of `depth`.
|
||||
pub fn alphabeta(game: Game, depth: u8, mut alpha: i8, mut beta: i8) -> (Board, i8) {
|
||||
if depth == 0 {
|
||||
return (0, game.score().diff());
|
||||
}
|
||||
let moves = game.available();
|
||||
if moves == 0 {
|
||||
return (0, game.score().diff());
|
||||
}
|
||||
|
||||
let mut best_move: Board = 0;
|
||||
// I just establish a convention of maximizing for black and minimizing for white.
|
||||
// I'm not sure if that's conventional or not, but it's what I chose.
|
||||
match game.current_team {
|
||||
Team::Black => {
|
||||
for mv in explode_board(moves) {
|
||||
let mut g = game.clone();
|
||||
g.play(mv);
|
||||
// maximize for the evaluation of subsequent moves
|
||||
let evaluation = alphabeta(g, depth - 1, alpha, beta).1;
|
||||
if evaluation > alpha {
|
||||
alpha = evaluation;
|
||||
best_move = mv;
|
||||
};
|
||||
if beta <= alpha {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(best_move, alpha)
|
||||
}
|
||||
Team::White => {
|
||||
for mv in explode_board(moves) {
|
||||
let mut g = game.clone();
|
||||
g.play(mv);
|
||||
// maximize for the evaluation of subsequent moves
|
||||
let evaluation = alphabeta(g, depth - 1, alpha, beta).1;
|
||||
if evaluation < beta {
|
||||
beta = evaluation;
|
||||
best_move = mv;
|
||||
};
|
||||
if beta <= alpha {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(best_move, beta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::board::view::View;
|
||||
use crate::board::{BitBoard, squares::*};
|
||||
use crate::game::Game;
|
||||
|
||||
#[test]
|
||||
// just a sanity check to ensure that my AI performs up to snuff with another popular engine
|
||||
fn opening() {
|
||||
let mut game = Game::default();
|
||||
|
||||
println!("{}", game.board().render(View::RankAsc, vec![]));
|
||||
game.play(D3);
|
||||
let (best_move, _) = alphabeta(game.clone(), 12, i8::MIN + 1, i8::MAX - 1);
|
||||
println!(
|
||||
"dis:\n{}",
|
||||
BitBoard {
|
||||
boards: [0, best_move]
|
||||
}
|
||||
);
|
||||
game.play(best_move);
|
||||
println!("{}", game.board().render(View::RankAsc, vec![]));
|
||||
assert_eq!(best_move, C3);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,64 @@ pub mod view;
|
|||
|
||||
pub type Board = u64;
|
||||
|
||||
/// Represents the score at a given moment in the game. It is computed as
|
||||
/// the number of black discs vs white discs. Structured as (black, white)
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Score(pub u8, pub u8);
|
||||
impl Score {
|
||||
/// Get black's current score
|
||||
pub fn black(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
/// Get white's current score
|
||||
pub fn white(&self) -> u8 {
|
||||
self.1
|
||||
}
|
||||
|
||||
/// Compute diff of evaluation. Useful for easily comparing a bunch of scores.
|
||||
pub fn diff(&self) -> i8 {
|
||||
self.0 as i8 - self.1 as i8
|
||||
}
|
||||
}
|
||||
|
||||
/// Explode a single board into an iterator over all of its excited one-hot
|
||||
/// boards.
|
||||
///
|
||||
/// For example:
|
||||
/// `0b1010` will explode into an iterator over `[0b0010, 0b1000]`.
|
||||
///
|
||||
/// The boards will be sorted by rank-ascending.
|
||||
#[inline]
|
||||
pub fn explode_board(mut b: Board) -> impl Iterator<Item = Board> {
|
||||
// a naive solution would be to simply start at the rightmost bit and
|
||||
// shift until we reach an excited bit, AND it for result, and store
|
||||
// the current shift + 1 for the next iteration until `b` is zero. This
|
||||
// is a bit wasteful, as we can abuse a handful of modular relationships
|
||||
// with negation.
|
||||
//
|
||||
// we do this using the `u64::wrapping_neg` method.
|
||||
// In two's-complement, negation can be used to isolate the least
|
||||
// significant excited bit in a signed integer. For example,
|
||||
// if `Board` were defined as `i64` instead, we could define LSB as
|
||||
// `lsb = b & -b`. Since we're using `u64`, we can mimic this using
|
||||
// `u64::wrapping_neg`.
|
||||
std::iter::from_fn(move || {
|
||||
// if we have no more excited bits, return None
|
||||
if b == 0 {
|
||||
None
|
||||
} else {
|
||||
// if we have more, continue isolating the LSB
|
||||
let lsb = b & b.wrapping_neg();
|
||||
// If you're skeptical of the claim above about these being
|
||||
// equivalent, comment out the line above this and uncomment
|
||||
// the line below this. They both pass the test.
|
||||
// let lsb = (b as i64 & -(b as i64)) as u64;
|
||||
b &= b - 1;
|
||||
Some(lsb)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Directions:
|
||||
// 7 8 9
|
||||
// -1 0 1
|
||||
|
|
@ -380,11 +438,14 @@ impl BitBoard {
|
|||
}
|
||||
|
||||
/// Compute the score (B, W) by counting the excited bits in each board.
|
||||
pub fn score(&self) -> (u32, u32) {
|
||||
pub fn score(&self) -> Score {
|
||||
// `u64::count_ones()` is implemented `unsafe` and makes use of primitives in the
|
||||
// low-level implementation of the language itself for maximum performance. Better
|
||||
// to use this than implement myself.
|
||||
(self.boards[0].count_ones(), self.boards[1].count_ones())
|
||||
Score(
|
||||
self.boards[0].count_ones() as u8,
|
||||
self.boards[1].count_ones() as u8,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -610,7 +671,6 @@ mod tests {
|
|||
fn test_available_no_moves_surrounded() {
|
||||
// Black piece at b7 completely surrounded by white
|
||||
let bb = BitBoard::from_jon("www/wbw/www////").expect("Valid board");
|
||||
println!("dis:\n{}", bb);
|
||||
// Black should have three moves in this position: d7, b5, and d5
|
||||
assert_eq!(bb.available(Team::Black), D7 | B5 | D5);
|
||||
}
|
||||
|
|
@ -619,7 +679,6 @@ mod tests {
|
|||
fn test_available_long_capture() {
|
||||
// Black at a4, white at b4-f4, empty g4, black at h4
|
||||
let bb = BitBoard::from_jon("////bwwwww1b///").expect("Valid board");
|
||||
println!("dis:\n{}", bb);
|
||||
// Black can play at g4 to capture entire row of white pieces
|
||||
assert_eq!(bb.available(Team::Black), G4);
|
||||
}
|
||||
|
|
@ -692,18 +751,27 @@ mod tests {
|
|||
#[test]
|
||||
fn play_works() {
|
||||
let mut bb = BitBoard::default();
|
||||
assert_eq!(bb.score(), (2, 2));
|
||||
assert_eq!(bb.score(), Score(2, 2));
|
||||
bb.play(Team::Black, E6);
|
||||
assert_eq!(bb, BitBoard::new(D5 | E6 | E5 | E4, D4));
|
||||
assert_eq!(bb.score(), (4, 1));
|
||||
assert_eq!(bb.score(), Score(4, 1));
|
||||
bb.play(Team::White, F4);
|
||||
assert_eq!(bb, BitBoard::new(D5 | E6 | E5, D4 | E4 | F4));
|
||||
assert_eq!(bb.score(), (3, 3));
|
||||
assert_eq!(bb.score(), Score(3, 3));
|
||||
bb.play(Team::Black, G3);
|
||||
assert_eq!(bb, BitBoard::new(D5 | E6 | E5 | G3 | F4, D4 | E4));
|
||||
assert_eq!(bb.score(), (5, 2));
|
||||
assert_eq!(bb.score(), Score(5, 2));
|
||||
bb.play(Team::White, E7);
|
||||
assert_eq!(bb, BitBoard::new(D5 | G3 | F4, D4 | E4 | E5 | E6 | E7));
|
||||
assert_eq!(bb.score(), (3, 5));
|
||||
assert_eq!(bb.score(), Score(3, 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explode_works() {
|
||||
let bb = BitBoard::default();
|
||||
assert_eq!(
|
||||
explode_board(bb.available(Team::Black)).collect::<Vec<Board>>(),
|
||||
vec![D3, C4, F5, E6] // they are sorted in rank-ascending
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
124
src/cli/mod.rs
124
src/cli/mod.rs
|
|
@ -1,18 +1,97 @@
|
|||
use othello::{board::view::Overlay, game::Game};
|
||||
use std::io;
|
||||
|
||||
use othello::{
|
||||
ai::alphabeta,
|
||||
board::{
|
||||
Board, Score,
|
||||
view::{Overlay, View},
|
||||
},
|
||||
game::Game,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use regex::Regex;
|
||||
|
||||
enum Action {
|
||||
Play(Board),
|
||||
Skip,
|
||||
Ai,
|
||||
}
|
||||
|
||||
const PLAY_RE: &str = r"^(play - )?([abcdefghABCDEFGH])(\d)$";
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
let mut game = Game::default();
|
||||
let mut board_changed = true;
|
||||
|
||||
let play_re = Regex::new(PLAY_RE).unwrap();
|
||||
|
||||
while !game.is_complete() {
|
||||
let legal_moves = game.available();
|
||||
if board_changed {
|
||||
let Score(b, w) = game.score();
|
||||
println!("Score: (Black: {} , White: {}", b, w);
|
||||
println!(
|
||||
"{}",
|
||||
game.board().render(
|
||||
game.current_team,
|
||||
vec![Overlay(
|
||||
game.board().available(game.current_team),
|
||||
"\x1b[95m*\x1b[37m"
|
||||
)]
|
||||
View::RankAsc,
|
||||
vec![Overlay(legal_moves, "\x1b[95m*\x1b[37m")]
|
||||
)
|
||||
);
|
||||
board_changed = false;
|
||||
}
|
||||
|
||||
let choice = loop {
|
||||
println!("Please choose your action: [play - (move), skip, ai]");
|
||||
let mut raw_input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut raw_input)
|
||||
.context("Failed to read input")?;
|
||||
if let Some(captures) = play_re.captures(&raw_input.trim()) {
|
||||
let file = captures.get(2).context("Failed to get file capture")?;
|
||||
let rank = captures
|
||||
.get(3)
|
||||
.context("Failed to get rank capture")?
|
||||
.as_str()
|
||||
.parse::<u8>()
|
||||
.context("Failed to parse rank")?;
|
||||
break Action::Play(create_move(file.as_str(), rank));
|
||||
}
|
||||
|
||||
match raw_input.as_str().trim() {
|
||||
"skip" => break Action::Skip,
|
||||
"ai" => break Action::Ai,
|
||||
_ => println!("Invalid input"),
|
||||
}
|
||||
};
|
||||
|
||||
match choice {
|
||||
Action::Play(mv) => {
|
||||
if mv & legal_moves == 0 {
|
||||
println!(
|
||||
"Attempted illegal moves. Legal moves are indicated by asterisks (*)."
|
||||
);
|
||||
} else {
|
||||
game.play(mv);
|
||||
board_changed = true;
|
||||
}
|
||||
}
|
||||
Action::Skip => {
|
||||
if legal_moves != 0 {
|
||||
println!("Cannot skip with legal moves available. Must choose `play` or `ai`.");
|
||||
} else {
|
||||
game.skip()
|
||||
}
|
||||
}
|
||||
Action::Ai => {
|
||||
let (mv, eval) = alphabeta(game.clone(), 12, i8::MIN + 1, i8::MAX - 1);
|
||||
println!("beep. boop. eval = {eval}");
|
||||
game.play(mv);
|
||||
board_changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
game.play(othello::board::squares::E6);
|
||||
println!();
|
||||
println!(
|
||||
|
|
@ -28,3 +107,36 @@ pub fn run() -> anyhow::Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Convert a file and rank into an actual board
|
||||
fn create_move(file: &str, rank: u8) -> Board {
|
||||
1 << (match file {
|
||||
"a" => 0,
|
||||
"b" => 1,
|
||||
"c" => 2,
|
||||
"d" => 3,
|
||||
"e" => 4,
|
||||
"f" => 5,
|
||||
"g" => 6,
|
||||
"h" => 7,
|
||||
_ => unreachable!("impossible file"),
|
||||
} + (rank - 1) * 8) as Board
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use othello::board::squares::*;
|
||||
#[test]
|
||||
fn create_move_works() {
|
||||
assert_eq!(create_move("a", 1), A1);
|
||||
assert_eq!(create_move("d", 3), D3);
|
||||
assert_eq!(create_move("h", 8), H8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn re_works() {
|
||||
let play_re = Regex::new(PLAY_RE).unwrap();
|
||||
assert!(play_re.is_match("play - d3"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
src/game.rs
27
src/game.rs
|
|
@ -1,4 +1,4 @@
|
|||
use crate::board::{BitBoard, Board};
|
||||
use crate::board::{BitBoard, Board, Score};
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Default)]
|
||||
|
|
@ -19,7 +19,8 @@ impl Team {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
// TODO: Look into potentially better memory alignment for this
|
||||
pub struct Game {
|
||||
pub current_team: Team,
|
||||
board: BitBoard,
|
||||
|
|
@ -42,9 +43,31 @@ impl Game {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Skip the current player
|
||||
pub fn skip(&mut self) {
|
||||
self.current_team = self.current_team.next();
|
||||
}
|
||||
|
||||
/// Copy the board
|
||||
pub fn board(&self) -> BitBoard {
|
||||
self.board
|
||||
}
|
||||
|
||||
/// Compute the available moves
|
||||
pub fn available(&self) -> Board {
|
||||
self.board.available(self.current_team)
|
||||
}
|
||||
|
||||
/// Compute the score of the game
|
||||
pub fn score(&self) -> Score {
|
||||
self.board.score()
|
||||
}
|
||||
|
||||
/// Determine whether or not the game has concluded
|
||||
pub fn is_complete(&self) -> bool {
|
||||
let score = self.board.score();
|
||||
score.0 + score.1 == 64
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod ai;
|
||||
pub mod board;
|
||||
pub mod game;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue