//! A tiny DSL for writing Star Gambit coms. //! //! Implement [`Com`] and hand it to [`run_as_com`]. The runner connects to a //! server as a registered com (authenticated with an API token), plays the //! games the server assigns it, and drives your com to the end of each game. //! Move/placement legality is provided by replaying candidate actions through //! the `stellar` engine, so a com only ever sees legal options. mod cli; mod client; pub use cli::{operate_com, DEFAULT_SERVER}; pub use client::{run_as_com, ComError, Outcome}; pub use stellar::{GameEngine, GameResult, SgnError}; /// The view a [`Com`] is given when it is asked to act. /// /// Wraps the authoritative game state ([`GameEngine`]) plus which player the /// com is (`0` or `1`). pub struct Game<'a> { engine: &'a GameEngine, me: i32, } impl<'a> Game<'a> { /// The full game state. Use this for any custom inspection beyond /// [`Game::legal_actions`]. pub fn engine(&self) -> &GameEngine { self.engine } /// Which player this com controls: `0` (moves first) or `1`. pub fn me(&self) -> i32 { self.me } /// `true` while it is this com's turn and the game is unfinished. pub fn is_my_turn(&self) -> bool { self.engine.result.is_none() && self.engine.whos_turn == self.me } /// Every legal SGN action in the current position, including `"END"` when /// the turn may be ended. Any of these may be returned from [`Com::act`] /// (one, several, or all of them); each is validated before it is sent. pub fn legal_actions(&self) -> Vec { legal_actions(self.engine) } } /// A Star Gambit com. /// /// [`Com::act`] is called when it becomes the com's turn, and again while the /// turn continues, until the com ends the turn. /// /// Return the action(s) to play, in order, each a single SGN action (one of /// [`Game::legal_actions`]). End the turn by including `"END"` as the final /// action. You may return the whole turn at once or one action per call. /// /// Each returned action is validated against the engine before it is sent; /// illegal actions are skipped and logged. A call that makes no progress /// (all-illegal or empty) is retried with the same state, but the same /// no-progress output three times in a row, or 20 consecutive no-progress /// calls, is treated as a crash and abandons the game. pub trait Com { fn act(&mut self, game: &Game) -> Vec; } /// All legal single-action SGN strings for `engine`'s current position. /// /// Each candidate is validated by replaying it through /// [`GameEngine::execute`], so this exactly matches what the engine accepts. /// `"END"` is offered (rather than `"END: