Add chess

The full game: castling with every condition, en passant including the
case where taking would expose your own king, promotion to any of the
four, pins, checkmate, stalemate, insufficient material, the fifty-move
rule and threefold repetition.

Move generation is checked against the published perft counts rather
than against hand-written cases — the opening array to depth four
(197,281 positions) plus four standard test positions chosen precisely
because they break generators. Those counts are exact, so matching them
leaves very little room for a rule to be subtly wrong.

The opponent is a negamax search with alpha-beta, iterative deepening
and a capture-only quiescence tail, at three strengths. The quiescence
tail is the part that stops it hanging pieces: a fixed-depth search that
stops mid-exchange scores the position as though the recapture never
comes.

Two supporting changes:

The AI now thinks off the caller's thread. A card AI answers instantly,
but a chess search runs for a second or more, and the match controller's
scope belongs to the screen — searching there would freeze the board it
is drawn on.

Offline setup can seat the local player somewhere other than seat 0,
which is what lets you play Black. Seat 0 is White by definition, so
choosing a colour has to choose a seat.

FEN is read and written, so a table can start from a set position.

203 engine tests pass locally.
This commit is contained in:
Claude
2026-08-06 13:34:31 +00:00
parent fb70396d09
commit d6e237ce4a
14 changed files with 2243 additions and 17 deletions
@@ -1,7 +1,9 @@
package org.prolibertate.games.net
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -231,17 +233,24 @@ class MatchController<S : Any, M : Any>(
if (seatKinds[seat] != PlayerKind.AI) return // a person's turn
delay(holdBeforeNextMove(current) + aiThinkingMillis())
val chosen = lock.withLock {
val legal = rules.legalMoves(current, seat)
if (legal.isEmpty()) return
// Off the caller's thread. A card AI answers instantly, but a chess
// search runs for a second or more, and this scope belongs to the
// screen — thinking here would freeze the board it is drawn on.
val move = withContext(Dispatchers.Default) { ai.chooseMove(current, seat, legal) }
val applied = lock.withLock {
val live = authoritative ?: return
if (rules.currentSeat(live) != seat) return@withLock null
val legal = rules.legalMoves(live, seat)
if (legal.isEmpty()) return@withLock null
val move = ai.chooseMove(live, seat, legal)
// Nothing else can move while an AI seat is on the clock, but
// the state is re-read rather than assumed.
if (rules.currentSeat(live) != seat) return@withLock false
authoritative = rules.applyMove(live, seat, move)
publish()
move
true
}
if (chosen == null) return
if (!applied) return
}
}