//! The only impure part: read lines from stdin, write lines to stdout. use rechner::game::Decision; use rechner::wire::encode::encode; use rechner::wire::respond::respond; use std::io::{self, BufRead, Write}; use std::panic::{catch_unwind, set_hook, AssertUnwindSafe}; /// A panic anywhere in the core still yields a valid ABSTAIN line. fn guarded(line: &str) -> String { catch_unwind(AssertUnwindSafe(|| respond(line))) .unwrap_or_else(|_| encode(&Decision::abstain("internal error"))) } fn serve(input: impl BufRead, mut output: impl Write) -> io::Result<()> { for line in input.lines() { let line = line?; if line.trim().is_empty() { continue; } writeln!(output, "{}", guarded(&line))?; output.flush()?; } Ok(()) } fn main() { set_hook(Box::new(|_| {})); // TURN_SPIEL=grund (plan E3, Owner 14.11.): the turn+river game with blueprint ranges plays the // turn where the blueprint recognises the spot (GRUNDSTRATEGIE = folder with tables and // checkpoints). Without the files every turn falls back to the heuristic (`turn_game_decision` // returns None), so a missing folder never breaks a decision. if std::env::var("TURN_SPIEL").as_deref() == Ok("grund") { rechner::engine::turn_game::set_turn_player(rechner::engine::turn_game::TurnPlayer::Grund); } let stdin = io::stdin(); let stdout = io::stdout(); if let Err(error) = serve(stdin.lock(), stdout.lock()) { eprintln!("io error: {error}"); std::process::exit(1); } }