2026-10-26 01:05:49 +03:00
|
|
|
//! A game against Maia on the phone (docs/design/android-play.md §31): the small model
|
|
|
|
|
//! (5M) chooses her moves at a strength on the Lichess blitz scale, by the same tables
|
|
|
|
|
//! and rule as on the desktop (`maia::strength`). No clocks on the phone.
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
Android: Maia on her own scale (600–2600); games written into the player's database
As on the desktop (decisions «Партии игрока записываются сами», «Сила
соперника — по шкале самого движка»):
- mobile-api: Maia plays the rating on the slider, temperature 1; the sheet
says it is her own rating, not a person's Elo.
- PlayerGames: «<name> — games against the computer» (no name: «Games
against the computer»), an own database made at the first game; a game
ends → written once (the mark kept with commit, guarded by the game's
serial against a rematch); a game put aside and let go (×, «Start new»,
Maia failing) → written unfinished, once both have moved.
- Play sheet: «Your name» with the phone's players (filtered by what is
typed), the line where games go; result card «✓ В «…», № n ›» (a new
database said there), opens it; no third button; a failed write said
with its reason. ♞ on the row, «пишутся сами»; a setting.
- «Play from here» and «Continue it» inside a play game show the new game.
Checked on the emulator by the tester (kill right after the end, rematch,
rename/delete of the database, setting off) and the designer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 16:30:16 +03:00
|
|
|
use maia::strength::choose;
|
2026-10-26 01:05:49 +03:00
|
|
|
use shakmaty::{CastlingMode, Chess};
|
|
|
|
|
|
2026-10-26 01:11:44 +03:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
2026-10-26 01:05:49 +03:00
|
|
|
use crate::ApiError;
|
|
|
|
|
use crate::session::GameSession;
|
|
|
|
|
|
2026-10-26 01:11:44 +03:00
|
|
|
/// How a game stands at a position: over or not.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
|
|
|
|
|
pub enum GameEnding {
|
|
|
|
|
/// The side named won by mate.
|
|
|
|
|
Checkmate {
|
|
|
|
|
white_won: bool,
|
|
|
|
|
},
|
|
|
|
|
Stalemate,
|
|
|
|
|
InsufficientMaterial,
|
|
|
|
|
Threefold,
|
|
|
|
|
FiftyMoves,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
impl GameSession {
|
|
|
|
|
/// How the game stands at the move at place `at` (its line from the start counts
|
|
|
|
|
/// for repetitions); none while it goes on.
|
|
|
|
|
pub fn ending(&self, at: u32) -> Option<GameEnding> {
|
|
|
|
|
let line = self.read(|g| {
|
|
|
|
|
let id = crate::order(g).get(at as usize).copied()?;
|
|
|
|
|
Some(
|
|
|
|
|
g.path(id)
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|n| g.position(*n).cloned())
|
|
|
|
|
.collect::<Vec<Chess>>(),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
Some(match ::play::ending(&line)? {
|
|
|
|
|
::play::Ending::Checkmate { winner } => GameEnding::Checkmate {
|
|
|
|
|
white_won: winner == shakmaty::Color::White,
|
|
|
|
|
},
|
|
|
|
|
::play::Ending::Stalemate => GameEnding::Stalemate,
|
|
|
|
|
::play::Ending::InsufficientMaterial => GameEnding::InsufficientMaterial,
|
|
|
|
|
::play::Ending::Threefold => GameEnding::Threefold,
|
|
|
|
|
::play::Ending::FiftyMoves => GameEnding::FiftyMoves,
|
|
|
|
|
// No clocks and no resignations here: the app knows those itself.
|
2026-10-26 02:44:05 +03:00
|
|
|
::play::Ending::Time { .. }
|
|
|
|
|
| ::play::Ending::Resigned { .. }
|
|
|
|
|
| ::play::Ending::DrawAgreed => return None,
|
2026-10-26 01:11:44 +03:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// How long Maia "thinks" before her move `ply` of an untimed game (milliseconds):
|
|
|
|
|
/// as a person would, quicker with one legal move.
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
pub fn maia_pause_ms(ply: u32, only_move: bool) -> u64 {
|
|
|
|
|
::play::pause(None, ply as usize, only_move).as_millis() as u64
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-26 01:14:40 +03:00
|
|
|
/// The tags of a game against Maia on the phone (no clocks): `event` in the app's
|
|
|
|
|
/// language; the user plays White when `user_white`.
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
pub fn maia_game_tags(
|
|
|
|
|
event: String,
|
|
|
|
|
user_white: bool,
|
|
|
|
|
user_name: String,
|
|
|
|
|
strength: u32,
|
|
|
|
|
) -> Vec<crate::Tag> {
|
|
|
|
|
let user = if user_white {
|
|
|
|
|
shakmaty::Color::White
|
|
|
|
|
} else {
|
|
|
|
|
shakmaty::Color::Black
|
|
|
|
|
};
|
Play: Rodent V as an opponent (desktop): downloaded on request, strength and style
The dialog (docs/design/rodent-opponent.md): «Maia · Rodent V» tiles, one
strength slider for both (Maia's zone marked; past it Rodent is chosen at
once), Rodent's style (Balanced, Attacking, Chaotic, Restricting, Classical,
Tal, Tal (network)) always in its place, greyed for Maia. Rodent V 1.2 (the
authors' release, pinned SHA-256) is downloaded and unpacked with its
personalities, nets and books; it plays through UCI at UCI_Elo from the
measured table (provisional until the measurement ends), with its book,
after Maia's human pause. Its own texts where Russian makes Maia «she» and
Rodent «he». The download helper is shared with Stockfish's.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 03:02:55 +03:00
|
|
|
::play::game_tags(
|
|
|
|
|
&event,
|
|
|
|
|
user,
|
|
|
|
|
&user_name,
|
|
|
|
|
None,
|
|
|
|
|
&format!("Maia {strength}"),
|
|
|
|
|
"-",
|
|
|
|
|
)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(name, value)| crate::Tag { name, value })
|
|
|
|
|
.collect()
|
2026-10-26 01:14:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-26 01:11:44 +03:00
|
|
|
/// Maia's turns in a row with her position hopeless before she offers to resign.
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
pub fn hopeless_turns() -> u32 {
|
|
|
|
|
::play::HOPELESS_TURNS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stockfish judging the positions where Maia is to move (unseen by the user), for her
|
|
|
|
|
/// resignation; one for the whole game.
|
|
|
|
|
#[derive(uniffi::Object)]
|
|
|
|
|
pub struct ResignJudge {
|
|
|
|
|
judge: Mutex<::play::Judge>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
impl ResignJudge {
|
|
|
|
|
/// The engine at `path` with its network `eval_file`.
|
|
|
|
|
#[uniffi::constructor]
|
|
|
|
|
pub fn start(path: String, eval_file: String) -> Arc<ResignJudge> {
|
|
|
|
|
let config = uci::EngineConfig {
|
|
|
|
|
nice: 10,
|
|
|
|
|
..uci::EngineConfig::new(path)
|
|
|
|
|
};
|
|
|
|
|
let options = if eval_file.is_empty() {
|
|
|
|
|
Vec::new()
|
|
|
|
|
} else {
|
|
|
|
|
vec![("EvalFile".to_owned(), eval_file)]
|
|
|
|
|
};
|
|
|
|
|
Arc::new(ResignJudge {
|
|
|
|
|
judge: Mutex::new(::play::Judge::start(config, options, Box::new(|| {}))),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Judges the position at place `at` of the game (Maia to move there).
|
|
|
|
|
pub fn ask(&self, game: Arc<GameSession>, at: u32) {
|
|
|
|
|
let pos = game.read(|g| {
|
|
|
|
|
let id = crate::order(g).get(at as usize).copied()?;
|
|
|
|
|
g.engine_history(id)
|
|
|
|
|
});
|
|
|
|
|
if let Some(h) = pos {
|
|
|
|
|
let p = match h.start_fen {
|
|
|
|
|
None => uci::EnginePosition::startpos(h.moves),
|
|
|
|
|
Some(fen) => uci::EnginePosition::fen(fen, h.moves),
|
|
|
|
|
};
|
|
|
|
|
self.judge.lock().unwrap_or_else(|e| e.into_inner()).ask(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Whether the position asked last is hopeless for Maia, once the answer came.
|
|
|
|
|
pub fn hopeless(&self) -> Option<bool> {
|
|
|
|
|
self.judge
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.answer()
|
|
|
|
|
.map(::play::hopeless)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Drops the request in flight (its position is gone).
|
|
|
|
|
pub fn forget(&self) {
|
|
|
|
|
self.judge
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.forget();
|
|
|
|
|
}
|
2026-10-26 02:44:05 +03:00
|
|
|
|
|
|
|
|
/// Moves were taken back: the scores of Maia's turns gone are forgotten.
|
|
|
|
|
pub fn forget_scores(&self) {
|
|
|
|
|
self.judge
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.forget_scores();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A draw may be offered now (not right after a refusal).
|
|
|
|
|
pub fn may_offer_draw(&self) -> bool {
|
|
|
|
|
self.judge
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.may_offer_draw()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The user offers a draw: whether Maia accepts (the shared rule, `play::accepts_draw`).
|
|
|
|
|
pub fn offer_draw(&self) -> bool {
|
|
|
|
|
self.judge
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
.offer_draw()
|
|
|
|
|
}
|
2026-10-26 01:11:44 +03:00
|
|
|
}
|
|
|
|
|
|
Android: Maia on her own scale (600–2600); games written into the player's database
As on the desktop (decisions «Партии игрока записываются сами», «Сила
соперника — по шкале самого движка»):
- mobile-api: Maia plays the rating on the slider, temperature 1; the sheet
says it is her own rating, not a person's Elo.
- PlayerGames: «<name> — games against the computer» (no name: «Games
against the computer»), an own database made at the first game; a game
ends → written once (the mark kept with commit, guarded by the game's
serial against a rematch); a game put aside and let go (×, «Start new»,
Maia failing) → written unfinished, once both have moved.
- Play sheet: «Your name» with the phone's players (filtered by what is
typed), the line where games go; result card «✓ В «…», № n ›» (a new
database said there), opens it; no third button; a failed write said
with its reason. ♞ on the row, «пишутся сами»; a setting.
- «Play from here» and «Continue it» inside a play game show the new game.
Checked on the emulator by the tester (kill right after the end, rematch,
rename/delete of the database, setting off) and the designer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 16:30:16 +03:00
|
|
|
/// Maia's own scale, as on the desktop (decisions «Сила соперника — по шкале самого
|
|
|
|
|
/// движка»): the ratings her network was trained on, not a person's Elo.
|
|
|
|
|
pub const PHONE_MIN: u32 = 600;
|
|
|
|
|
pub const PHONE_MAX: u32 = 2600;
|
2026-10-26 01:05:49 +03:00
|
|
|
|
|
|
|
|
/// The strengths the phone offers: (weakest, strongest).
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
pub fn maia_range() -> Vec<u32> {
|
|
|
|
|
vec![PHONE_MIN, PHONE_MAX]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Maia, loaded once for a game.
|
|
|
|
|
#[derive(uniffi::Object)]
|
|
|
|
|
pub struct MaiaPlayer {
|
|
|
|
|
maia: maia::Maia,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[uniffi::export]
|
|
|
|
|
impl MaiaPlayer {
|
|
|
|
|
/// Reads the weights (`maia3-5m.pt`).
|
|
|
|
|
#[uniffi::constructor]
|
|
|
|
|
pub fn load(path: String) -> Result<Arc<MaiaPlayer>, ApiError> {
|
|
|
|
|
let maia =
|
|
|
|
|
maia::Maia::load(std::path::Path::new(&path)).map_err(|e| ApiError::Corrupt {
|
|
|
|
|
detail: e.to_string(),
|
|
|
|
|
})?;
|
|
|
|
|
Ok(Arc::new(MaiaPlayer { maia }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Maia's move (UCI) in the position at place `at` of the game, playing at
|
|
|
|
|
/// `strength` against a player rated `opponent` (her own strength when unknown).
|
|
|
|
|
/// `random` is in [0, 1). None when the game is over there.
|
|
|
|
|
pub fn choose(
|
|
|
|
|
&self,
|
|
|
|
|
game: Arc<GameSession>,
|
|
|
|
|
at: u32,
|
|
|
|
|
strength: u32,
|
|
|
|
|
opponent: Option<u32>,
|
|
|
|
|
random: f32,
|
|
|
|
|
) -> Option<String> {
|
|
|
|
|
let history: Vec<Chess> = game.read(|g| {
|
|
|
|
|
let id = crate::order(g).get(at as usize).copied()?;
|
|
|
|
|
Some(
|
|
|
|
|
g.path(id)
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|n| g.position(*n).cloned())
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
let strength = strength.clamp(PHONE_MIN, PHONE_MAX);
|
2026-10-26 16:33:38 +03:00
|
|
|
let p = self
|
|
|
|
|
.maia
|
|
|
|
|
.predict(&history, strength, opponent.unwrap_or(strength));
|
|
|
|
|
choose(&p, 1.0, random).map(|m| m.to_uci(CastlingMode::Standard).to_string())
|
2026-10-26 01:05:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
/// Needs the weights: `MAIA_WEIGHTS=/path/maia3-5m.pt cargo test -p mobile-api -- --ignored`.
|
|
|
|
|
#[test]
|
|
|
|
|
#[ignore = "needs the weights file (MAIA_WEIGHTS)"]
|
|
|
|
|
fn maia_answers_with_a_legal_move() {
|
|
|
|
|
let path = std::env::var("MAIA_WEIGHTS").expect("MAIA_WEIGHTS");
|
|
|
|
|
let m = MaiaPlayer::load(path).unwrap();
|
|
|
|
|
let s = GameSession::from_pgn("1. e4 *".into()).unwrap();
|
|
|
|
|
let e4 = s.data().nodes.iter().find(|n| n.san == "e4").unwrap().id;
|
|
|
|
|
let uci = m.choose(s.clone(), e4, 1500, None, 0.3).unwrap();
|
|
|
|
|
assert!(s.legal_moves(e4).contains(&uci), "{uci}");
|
|
|
|
|
let mated = GameSession::from_pgn("1. f3 e5 2. g4 Qh4# *".into()).unwrap();
|
2026-10-26 01:11:44 +03:00
|
|
|
let end_id = mated
|
|
|
|
|
.data()
|
|
|
|
|
.nodes
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|n| n.san == "Qh4#")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.id;
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mated.ending(end_id),
|
|
|
|
|
Some(GameEnding::Checkmate { white_won: false })
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(mated.ending(0), None);
|
2026-10-26 01:05:49 +03:00
|
|
|
let end = mated
|
|
|
|
|
.data()
|
|
|
|
|
.nodes
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|n| n.san == "Qh4#")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.id;
|
|
|
|
|
assert!(m.choose(mated, end, 1500, None, 0.3).is_none());
|
|
|
|
|
}
|
|
|
|
|
}
|