minichess source pure
Package minichess implements standard chess (8x8) for gno.land realms: position representation, FEN, full move legali...
Package minichess implements standard chess (8x8) for gno.land realms: position representation, FEN, full move legality (castling, en passant, promotion), game-end detection and a gas-bounded bot.
The package is pure and stateless: Position is a value type, and every method returns a new Position instead of mutating the receiver.
7
const CastleWK, CastleWQ, CastleBK, CastleBQ
Castling rights bits.
const MinLevel, MaxLevel, mateScore, infScore, maxPly
const StartFEN
StartFEN is the standard starting position.
const White, Black
const Empty, Pawn, Knight, Bishop, Rook, Queen, King
const Ongoing, Checkmate, Stalemate, FiftyMoveDraw, InsufficientMaterial
8
func ParseSquare
ParseSquare converts algebraic notation to an index.
func SquareName
SquareName converts an index to algebraic notation ("a1".."h8").
func Level
Level returns the limits for a level, clamped to [MinLevel, MaxLevel].
func BestMove
BestMove returns the bot's choice for the side to move. seed breaks ties between equally scored moves so games don't all look identical; pass something like gameID*1000+ply. ok is false if there are no legal moves.
func BestMoveWithHistory
BestMoveWithHistory is BestMove for a game in progress: history holds the Hash of earlier positions (since the last capture or pawn move). Moves that repeat one of them are scored as a draw, so the bot avoids repetition when it is ahead and steers toward it when it is behind.
func ParseMove
ParseMove parses coordinate notation: "e2e4", "e1g1" or "e7e8n". It does not check legality; use Position.FindLegal for that.
func NewGame
NewGame returns the starting position.
func ParseFEN
ParseFEN parses standard FEN. The halfmove and fullmove fields are optional.
6
type Color
identColor is the side to move.
type LevelInfo
structLevelInfo describes the search limits of a bot level.
type Move
structMove is a move from one square to another, with an optional promotion piece type (Knight, Bishop, Rook or Queen). Castling is the king's move (e1g1); en passant is the capturing pawn's move to the target square.
type Piece
identPiece is a signed piece code: positive for white, negative for black.
type Position
struct1type Position struct {
2 Board [NumSquares]Piece // index = rank*8 + file; a1 = 0, h8 = 63
3 Turn Color
4 Castling int // CastleWK|CastleWQ|CastleBK|CastleBQ bits
5 EP int // en passant target square, or -1
6 Halfmove int // plies since the last capture or pawn move
7 Fullmove int // starts at 1, incremented after black moves
8}Position is a complete game state.
Methods on Position
func Attacked
method on PositionAttacked reports whether square sq is attacked by any piece of colour by.
func BoardKey
method on PositionBoardKey identifies the position for threefold repetition: placement, side to move, castling rights and an en passant square only when an en passant capture is actually available.
func Evaluate
method on PositionEvaluate scores the position in centipawns from the side to move's view.
func FEN
method on PositionFEN returns the full FEN string of the position.
func FindLegal
method on PositionFindLegal matches m against the legal moves. A promotion move given without a piece defaults to a queen. Returns false if m is illegal.
func Hash
method on PositionHash returns a 64-bit hash of the position (for repetition tracking).
func InCheck
method on PositionInCheck reports whether the side to move is in check.
func IsIrreversible
method on PositionIsIrreversible reports whether m is a capture or pawn move (resets the repetition history and the 50-move counter).
func LegalMoves
method on PositionLegalMoves returns all legal moves for the side to move.
func PieceAt
method on PositionPieceAt returns the piece on a square given by name, e.g. "e4".
func Play
method on PositionPlay applies a move without validating it and returns the new position. Callers must only pass moves obtained from LegalMoves or FindLegal.
func Status
method on PositionStatus computes the game status from the position alone. Threefold repetition needs move history and is handled by the caller.
type Status
identStatus describes whether the game is still going and how it ended.