Add Rust: minimax, runner, and codelens highlighter

This commit is contained in:
2025-11-01 17:17:13 -07:00
committed by Mark
parent 3494003683
commit 19f523d0ed
24 changed files with 3420 additions and 0 deletions

28
rust/runner/Cargo.toml Normal file
View File

@@ -0,0 +1,28 @@
[package]
name = "runner"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
minimax = { workspace = true }
anyhow = { workspace = true }
rand = { workspace = true }
itertools = { workspace = true }
wasm-bindgen = { workspace = true }
js-sys = { workspace = true }
web-sys = { workspace = true }
console_error_panic_hook = { workspace = true }
wee_alloc = { workspace = true }
serde = { workspace = true }
serde-wasm-bindgen = { workspace = true }
rhai = { workspace = true }
getrandom = { workspace = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
rhai = { workspace = true, features = ["wasm-bindgen"] }
getrandom = { workspace = true, features = ["js"] }

30
rust/runner/package.json Normal file
View File

@@ -0,0 +1,30 @@
{
"name": "script-runner-wasm",
"version": "0.1.0",
"description": "Rust WASM script runner",
"main": "pkg/script_runner.js",
"types": "pkg/script_runner.d.ts",
"files": [
"pkg"
],
"scripts": {
"build": "wasm-pack build --target web --out-dir pkg",
"build:nodejs": "wasm-pack build --target nodejs --out-dir pkg-node",
"build:bundler": "wasm-pack build --target bundler --out-dir pkg-bundler",
"dev": "wasm-pack build --dev --target web --out-dir pkg"
},
"repository": {
"type": "git",
"url": "."
},
"keywords": [
"wasm",
"rust",
"script-runner"
],
"author": "",
"license": "MIT",
"devDependencies": {
"wasm-pack": "^0.12.1"
}
}

12
rust/runner/src/ansi.rs Normal file
View File

@@ -0,0 +1,12 @@
#![expect(clippy::allow_attributes)]
#![allow(dead_code)]
pub const RESET: &str = "\x1b[0m";
pub const RED: &str = "\x1b[31m";
pub const BLUE: &str = "\x1b[34m";
pub const MAGENTA: &str = "\x1b[35m";
pub const UP: &str = "\x1b[A";
pub const DOWN: &str = "\x1b[B";
pub const LEFT: &str = "\x1b[D";
pub const RIGHT: &str = "\x1b[C";

View File

@@ -0,0 +1,421 @@
use std::cmp::Ordering;
use minimax::{
agents::{Agent, RhaiAgent},
game::Board,
};
use rand::{rngs::StdRng, SeedableRng};
use rhai::ParseError;
use wasm_bindgen::prelude::*;
use crate::ansi;
#[derive(Debug, Clone, Copy)]
enum GameState {
/// Round 1, red is maximizing
Red,
/// Round 2, red is minimizing (and goes second)
Blue { red_score: f32 },
/// Game over, red won
RedWins { blue_score: f32 },
/// Game over, blue won
BlueWins { blue_score: f32 },
/// Game over, draw
DrawScore { score: f32 },
/// Invalid board, draw
DrawInvalid,
/// Error, end early
Error,
}
#[wasm_bindgen]
pub struct MinMaxGame {
red_agent: RhaiAgent<StdRng>,
blue_agent: RhaiAgent<StdRng>,
board: Board,
is_red_turn: bool,
is_first_print: bool,
state: GameState,
game_state_callback: Box<dyn Fn(&str) + 'static>,
}
#[wasm_bindgen]
impl MinMaxGame {
#[wasm_bindgen(constructor)]
pub fn new(
red_script: &str,
red_print_callback: js_sys::Function,
red_debug_callback: js_sys::Function,
blue_script: &str,
blue_print_callback: js_sys::Function,
blue_debug_callback: js_sys::Function,
game_state_callback: js_sys::Function,
) -> Result<MinMaxGame, String> {
Self::new_native(
red_script,
move |s| {
let _ = red_print_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
move |s| {
let _ = red_debug_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
blue_script,
move |s| {
let _ = blue_print_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
move |s| {
let _ = blue_debug_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
move |s| {
let _ = game_state_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
)
.map_err(|x| format!("Error at {}: {}", x.1, x.0))
}
fn new_native(
red_script: &str,
red_print_callback: impl Fn(&str) + 'static,
red_debug_callback: impl Fn(&str) + 'static,
blue_script: &str,
blue_print_callback: impl Fn(&str) + 'static,
blue_debug_callback: impl Fn(&str) + 'static,
game_state_callback: impl Fn(&str) + 'static,
) -> Result<MinMaxGame, ParseError> {
console_error_panic_hook::set_once();
let mut seed1 = [0u8; 32];
let mut seed2 = [0u8; 32];
getrandom::getrandom(&mut seed1).unwrap();
getrandom::getrandom(&mut seed2).unwrap();
Ok(MinMaxGame {
board: Board::new(),
is_first_print: true,
is_red_turn: true,
state: GameState::Red,
red_agent: RhaiAgent::new(
red_script,
StdRng::from_seed(seed1),
red_print_callback,
red_debug_callback,
)?,
blue_agent: RhaiAgent::new(
blue_script,
StdRng::from_seed(seed2),
blue_print_callback,
blue_debug_callback,
)?,
game_state_callback: Box::new(game_state_callback),
})
}
/// Is this game over for any reason?
#[wasm_bindgen]
pub fn is_done(&self) -> bool {
match self.state {
GameState::DrawScore { .. }
| GameState::DrawInvalid
| GameState::Error
| GameState::BlueWins { .. }
| GameState::RedWins { .. } => true,
_ => false,
}
}
#[wasm_bindgen]
pub fn red_won(&self) -> Option<bool> {
match self.state {
GameState::DrawScore { .. } => Some(false),
GameState::DrawInvalid { .. } => Some(false),
GameState::BlueWins { .. } => Some(false),
GameState::RedWins { .. } => Some(true),
_ => None,
}
}
#[wasm_bindgen]
pub fn blue_won(&self) -> Option<bool> {
match self.state {
GameState::DrawScore { .. } => Some(false),
GameState::DrawInvalid { .. } => Some(false),
GameState::BlueWins { .. } => Some(true),
GameState::RedWins { .. } => Some(false),
_ => None,
}
}
#[wasm_bindgen]
pub fn is_draw_score(&self) -> Option<bool> {
match self.state {
GameState::DrawScore { .. } => Some(true),
GameState::DrawInvalid { .. } => Some(false),
GameState::BlueWins { .. } => Some(false),
GameState::RedWins { .. } => Some(false),
_ => None,
}
}
#[wasm_bindgen]
pub fn is_draw_invalid(&self) -> Option<bool> {
match self.state {
GameState::DrawScore { .. } => Some(false),
GameState::DrawInvalid { .. } => Some(true),
GameState::BlueWins { .. } => Some(false),
GameState::RedWins { .. } => Some(false),
_ => None,
}
}
#[wasm_bindgen]
pub fn is_error(&self) -> bool {
match self.state {
GameState::Error => true,
_ => false,
}
}
// Play one turn
#[wasm_bindgen]
pub fn step(&mut self) -> Result<(), String> {
if self.is_first_print {
self.print_board("", "");
(self.game_state_callback)("\r\n");
}
let action = match (self.state, self.is_red_turn) {
(GameState::Blue { .. }, false) => self.blue_agent.step_max(&self.board),
(GameState::Red, false) => self.blue_agent.step_min(&self.board),
(GameState::Blue { .. }, true) => self.red_agent.step_min(&self.board),
(GameState::Red, true) => self.red_agent.step_max(&self.board),
// Game is done, do nothing
(GameState::Error, _)
| (GameState::BlueWins { .. }, _)
| (GameState::RedWins { .. }, _)
| (GameState::DrawInvalid, _)
| (GameState::DrawScore { .. }, _) => return Ok(()),
}
.map_err(|err| format!("{err}"))?;
let player = self.is_red_turn.then_some("Red").unwrap_or("Blue");
let player_name = self
.is_red_turn
.then_some(self.red_agent.name())
.unwrap_or(self.blue_agent.name());
if !self.board.play(action, player) {
self.state = GameState::Error;
return Err(format!(
"{player} ({player_name}) made an invalid move {action}",
));
}
self.print_board(
self.is_red_turn.then_some(ansi::RED).unwrap_or(ansi::BLUE),
self.is_red_turn.then_some("Red").unwrap_or("Blue"),
);
(self.game_state_callback)("\n\r");
if !self.board.is_full() && !self.board.is_stuck() {
// This was not the last move
self.is_red_turn = !self.is_red_turn;
} else {
// This was the last move
// Close board
(self.game_state_callback)(&format!(
"{}{}\n\r",
" ".repeat(6),
" ".repeat(self.board.size())
));
// Evaluate board and update state
match (self.state, self.board.evaluate()) {
// Start next round
(GameState::Red, Some(red_score)) => {
self.board = Board::new();
self.is_first_print = true;
self.is_red_turn = false;
self.state = GameState::Blue { red_score }
}
// Game over
(GameState::Blue { red_score }, Some(blue_score)) => {
self.state = match red_score.total_cmp(&blue_score) {
Ordering::Equal => GameState::DrawScore { score: red_score },
Ordering::Greater => GameState::RedWins { blue_score },
Ordering::Less => GameState::BlueWins { blue_score },
}
}
// Could not evaluate board, tie by default
(GameState::Red, None) | (GameState::Blue { .. }, None) => {
self.state = GameState::DrawInvalid
}
// Other code should make sure this never happens
(GameState::BlueWins { .. }, _)
| (GameState::RedWins { .. }, _)
| (GameState::DrawInvalid, _)
| (GameState::DrawScore { .. }, _)
| (GameState::Error, _) => unreachable!(),
}
if self.board.is_stuck() {
self.state = GameState::DrawInvalid;
}
// Print depending on new state
match self.state {
GameState::DrawScore { score } => {
(self.game_state_callback)(&format!("Tie! Score: {score:.2}\n\r"));
}
GameState::DrawInvalid => {
(self.game_state_callback)(&format!("Tie, invalid board!\n\r"));
}
GameState::RedWins { blue_score, .. } => {
(self.game_state_callback)(&format!(
"{}Blue score:{} {blue_score:.2}\n\r",
ansi::BLUE,
ansi::RESET,
));
(self.game_state_callback)(&format!(
"{}Red wins!{}\n\r",
ansi::RED,
ansi::RESET,
));
}
GameState::BlueWins { blue_score, .. } => {
(self.game_state_callback)(&format!(
"{}Blue score:{} {blue_score:.2}\n\r",
ansi::BLUE,
ansi::RESET,
));
(self.game_state_callback)(&format!(
"{}Blue wins!{}\n\r",
ansi::BLUE,
ansi::RESET,
));
}
GameState::Blue { red_score } => {
(self.game_state_callback)(&format!(
"{}Red score:{} {red_score:.2}\n\r",
ansi::RED,
ansi::RESET,
));
}
// Other code should make sure this never happens
GameState::Error | GameState::Red => unreachable!(),
}
(self.game_state_callback)("\r\n");
}
return Ok(());
}
fn print_board(&mut self, color: &str, player: &str) {
let board_label = format!("{}{:<6}{}", color, player, ansi::RESET);
(self.game_state_callback)(&format!(
"\r{}{}{}{}",
board_label,
if self.is_first_print { '╓' } else { '║' },
self.board.prettyprint(),
if self.is_first_print { '╖' } else { '║' },
));
self.is_first_print = false;
}
}
//
// MARK: tests
//
// TODO:
// - infinite loop
// - random is different
// - incorrect return type
// - globals
#[test]
fn full_random() {
const SCRIPT: &str = r#"
fn random_action(board) {
let symb = rand_symb();
let pos = rand_int(0, 10);
let action = Action(symb, pos);
while !board.can_play(action) {
let symb = rand_symb();
let pos = rand_int(0, 10);
action = Action(symb, pos);
}
return action
}
fn step_min(board) {
random_action(board)
}
fn step_max(board) {
random_action(board)
}
"#;
let mut game =
MinMaxGame::new_native(&SCRIPT, |_| {}, |_| {}, &SCRIPT, |_| {}, |_| {}, |_| {}).unwrap();
let mut n = 0;
while !game.is_done() {
println!("{:?}", game.step());
println!("{:?}", game.board);
n += 1;
assert!(n < 10);
}
}
#[test]
fn infinite_loop() {
const SCRIPT: &str = r#"
fn step_min(board) {
loop {}
}
fn step_max(board) {
loop {}
}
"#;
let mut game =
MinMaxGame::new_native(&SCRIPT, |_| {}, |_| {}, &SCRIPT, |_| {}, |_| {}, |_| {}).unwrap();
while !game.is_done() {
println!("{:?}", game.step());
println!("{:?}", game.board);
}
}

View File

@@ -0,0 +1,288 @@
use minimax::{
agents::{Agent, RhaiAgent},
game::Board,
};
use rand::{rngs::StdRng, SeedableRng};
use rhai::ParseError;
use wasm_bindgen::prelude::*;
use crate::{ansi, terminput::TermInput};
#[wasm_bindgen]
pub struct GameStateHuman {
/// Red player
human: TermInput,
// Blue player
agent: RhaiAgent<StdRng>,
board: Board,
is_red_turn: bool,
is_first_turn: bool,
is_error: bool,
red_score: Option<f32>,
game_state_callback: Box<dyn Fn(&str) + 'static>,
}
#[wasm_bindgen]
impl GameStateHuman {
#[wasm_bindgen(constructor)]
pub fn new(
max_script: &str,
max_print_callback: js_sys::Function,
max_debug_callback: js_sys::Function,
game_state_callback: js_sys::Function,
) -> Result<GameStateHuman, String> {
Self::new_native(
max_script,
move |s| {
let _ = max_print_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
move |s| {
let _ = max_debug_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
move |s| {
let _ = game_state_callback.call1(&JsValue::null(), &JsValue::from_str(s));
},
)
.map_err(|x| format!("Error at {}: {}", x.1, x.0))
}
fn new_native(
max_script: &str,
max_print_callback: impl Fn(&str) + 'static,
max_debug_callback: impl Fn(&str) + 'static,
game_state_callback: impl Fn(&str) + 'static,
) -> Result<Self, ParseError> {
console_error_panic_hook::set_once();
let mut seed1 = [0u8; 32];
let mut seed2 = [0u8; 32];
getrandom::getrandom(&mut seed1).unwrap();
getrandom::getrandom(&mut seed2).unwrap();
Ok(Self {
board: Board::new(),
is_red_turn: true,
is_first_turn: true,
is_error: false,
red_score: None,
human: TermInput::new(ansi::RED.to_string()),
agent: RhaiAgent::new(
max_script,
StdRng::from_seed(seed1),
max_print_callback,
max_debug_callback,
)?,
game_state_callback: Box::new(game_state_callback),
})
}
#[wasm_bindgen]
pub fn is_done(&self) -> bool {
(self.board.is_full() && self.red_score.is_some()) || self.is_error
}
#[wasm_bindgen]
pub fn is_error(&self) -> bool {
self.is_error
}
#[wasm_bindgen]
pub fn print_start(&mut self) -> Result<(), String> {
self.print_board("", "");
(self.game_state_callback)("\r\n");
if !self.is_red_turn {
let action = {
if self.red_score.is_none() {
self.agent.step_min(&self.board)
} else {
self.agent.step_max(&self.board)
}
}
.map_err(|err| format!("{err}"))?;
if !self.board.play(action, "Blue") {
self.is_error = true;
return Err(format!(
"{} ({}) made an invalid move {}",
self.is_red_turn.then_some("Red").unwrap_or("Blue"),
self.is_red_turn
.then_some("Human")
.unwrap_or(self.agent.name()),
action
));
}
self.print_board(
self.is_red_turn.then_some(ansi::RED).unwrap_or(ansi::BLUE),
self.is_red_turn.then_some("Red").unwrap_or("Blue"),
);
(self.game_state_callback)("\r\n");
self.is_red_turn = true;
}
self.print_ui();
return Ok(());
}
#[wasm_bindgen]
pub fn print_board(&mut self, color: &str, player: &str) {
let board_label = format!("{}{:<6}{}", color, player, ansi::RESET);
// Print board
(self.game_state_callback)(&format!(
"\r{}{}{}{}",
board_label,
if self.is_first_turn { '╓' } else { '║' },
self.board.prettyprint(),
if self.is_first_turn { '╖' } else { '║' },
));
self.is_first_turn = false;
}
#[wasm_bindgen]
pub fn print_ui(&mut self) {
(self.game_state_callback)(
&self
.human
.print_state(&self.board, self.red_score.is_some()),
);
}
#[wasm_bindgen]
pub fn take_input(&mut self, data: String) -> Result<(), String> {
self.human.process_input(&self.board, data);
self.print_ui();
if let Some(action) = self.human.pop_action() {
if !self.board.play(action, "Red") {
self.is_error = true;
return Err(format!(
"{} ({}) made an invalid move {}",
self.is_red_turn.then_some("Red").unwrap_or("Blue"),
self.is_red_turn
.then_some("Human")
.unwrap_or(self.agent.name()),
action
));
}
self.is_red_turn = false;
if self.board.is_full() {
self.print_end()?;
return Ok(());
}
self.print_board(ansi::RED, "Red");
(self.game_state_callback)("\r\n");
let action = {
if self.red_score.is_none() {
self.agent.step_min(&self.board)
} else {
self.agent.step_max(&self.board)
}
}
.map_err(|err| format!("{err}"))?;
if !self.board.play(action, "Blue") {
self.is_error = true;
return Err(format!(
"{} ({}) made an invalid move {}",
self.is_red_turn.then_some("Red").unwrap_or("Blue"),
self.is_red_turn
.then_some("Human")
.unwrap_or(self.agent.name()),
action
));
}
self.is_red_turn = true;
if self.board.is_full() {
self.print_end()?;
return Ok(());
}
self.print_board(ansi::BLUE, "Blue");
(self.game_state_callback)("\r\n");
self.print_ui();
}
return Ok(());
}
fn print_end(&mut self) -> Result<(), String> {
let board_label = format!(
"{}{:<6}{}",
self.is_red_turn.then_some(ansi::BLUE).unwrap_or(ansi::RED),
self.is_red_turn.then_some("Blue").unwrap_or("Red"),
ansi::RESET
);
(self.game_state_callback)(&format!("\r{}{}", board_label, self.board.prettyprint()));
(self.game_state_callback)("\r\n");
(self.game_state_callback)(&format!(
"\r{}{}",
" ",
" ".repeat(self.board.size())
));
(self.game_state_callback)("\r\n");
let score = self.board.evaluate().unwrap();
(self.game_state_callback)(&format!(
"\r\n{}{} score:{} {:.2}\r\n",
self.red_score
.is_none()
.then_some(ansi::RED)
.unwrap_or(ansi::BLUE),
self.red_score.is_none().then_some("Red").unwrap_or("Blue"),
ansi::RESET,
score
));
(self.game_state_callback)("\r\n");
match self.red_score {
// Start second round
None => {
let mut seed1 = [0u8; 32];
let mut seed2 = [0u8; 32];
getrandom::getrandom(&mut seed1).unwrap();
getrandom::getrandom(&mut seed2).unwrap();
self.board = Board::new();
self.is_red_turn = false;
self.is_first_turn = true;
self.is_error = false;
self.human = TermInput::new(ansi::RED.to_string());
self.red_score = Some(score);
self.print_start()?;
}
// End game
Some(red_score) => {
if red_score == score {
(self.game_state_callback)(&format!("Tie! Score: {:.2}\r\n", score));
} else {
let red_wins = red_score > score;
(self.game_state_callback)(&format!(
"{}{} wins!{}",
red_wins.then_some(ansi::RED).unwrap_or(ansi::BLUE),
red_wins.then_some("Red").unwrap_or("Blue"),
ansi::RESET,
));
}
}
}
return Ok(());
}
}

14
rust/runner/src/lib.rs Normal file
View File

@@ -0,0 +1,14 @@
use wasm_bindgen::prelude::*;
mod ansi;
mod gamestate;
mod gamestatehuman;
mod terminput;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn init_panic_hook() {
console_error_panic_hook::set_once();
}

View File

@@ -0,0 +1,181 @@
use itertools::Itertools;
use minimax::game::{Board, PlayerAction, Symb};
use crate::ansi::{DOWN, LEFT, RESET, RIGHT, UP};
struct SymbolSelector {
symbols: Vec<char>,
cursor: usize,
}
impl SymbolSelector {
fn new(symbols: Vec<char>) -> Self {
Self { symbols, cursor: 0 }
}
fn current(&self) -> char {
self.symbols[self.cursor]
}
fn check(&mut self, board: &Board) {
while board.contains(Symb::from_char(self.current()).unwrap()) {
if self.cursor == 0 {
self.cursor = self.symbols.len() - 1;
} else {
self.cursor -= 1;
}
}
}
fn down(&mut self, board: &Board) {
if self.cursor == 0 {
self.cursor = self.symbols.len() - 1;
} else {
self.cursor -= 1;
}
while board.contains(Symb::from_char(self.current()).unwrap()) {
if self.cursor == 0 {
self.cursor = self.symbols.len() - 1;
} else {
self.cursor -= 1;
}
}
}
fn up(&mut self, board: &Board) {
if self.cursor == self.symbols.len() - 1 {
self.cursor = 0;
} else {
self.cursor += 1;
}
while board.contains(Symb::from_char(self.current()).unwrap()) {
if self.cursor == self.symbols.len() - 1 {
self.cursor = 0;
} else {
self.cursor += 1;
}
}
}
}
pub struct TermInput {
player_color: String,
cursor: usize,
symbol_selector: SymbolSelector,
/// Set to Some() when the player selects an action.
/// Should be cleared and applied immediately.
queued_action: Option<PlayerAction>,
}
impl TermInput {
pub fn new(player_color: String) -> Self {
Self {
cursor: 0,
queued_action: None,
player_color,
symbol_selector: SymbolSelector::new(vec![
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '×', '÷',
]),
}
}
pub fn pop_action(&mut self) -> Option<PlayerAction> {
self.queued_action.take()
}
pub fn print_state(&mut self, board: &Board, minimize: bool) -> String {
let cursor_max = board.size() - 1;
self.symbol_selector.check(board);
let board_label = format!(
"{}{:<6}{RESET}",
self.player_color,
if minimize { "Min" } else { "Max" },
);
return format!(
"\r{}{}{}{}{RESET}{}{}",
board_label,
// Cursor
" ".repeat(self.cursor),
self.player_color,
if board.is_full() {
' '
} else {
self.symbol_selector.current()
},
// RESET
" ".repeat(cursor_max - self.cursor),
self.symbol_selector
.symbols
.iter()
.map(|x| {
if board.contains(Symb::from_char(*x).unwrap()) {
" ".to_string()
} else if *x == self.symbol_selector.current() {
format!("{}{x}{RESET}", self.player_color,)
} else {
format!("{x}",)
}
})
.join("")
);
}
pub fn process_input(&mut self, board: &Board, data: String) {
let cursor_max = board.size() - 1;
self.symbol_selector.check(board);
match &data[..] {
RIGHT => {
self.cursor = cursor_max.min(self.cursor + 1);
}
LEFT => {
if self.cursor != 0 {
self.cursor -= 1;
}
}
UP => {
self.symbol_selector.up(board);
}
DOWN => {
self.symbol_selector.down(board);
}
" " | "\n" | "\r" => {
let symb = Symb::from_char(self.symbol_selector.current());
if let Some(symb) = symb {
let action = PlayerAction {
symb,
pos: self.cursor,
};
if board.can_play(&action) {
self.queued_action = Some(action);
}
}
}
c => {
let symb = Symb::from_str(c);
if let Some(symb) = symb {
let action = PlayerAction {
symb,
pos: self.cursor,
};
if board.can_play(&action) {
self.queued_action = Some(action);
}
}
}
};
}
}