Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

minichess source pure

Package minichess implements standard chess (8x8) for gno.land realms: position representation, FEN, full move legali...

Overview

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.

Constants 7

const CastleWK, CastleWQ, CastleBK, CastleBQ

1const (
2	CastleWK = 1 // white king side (e1g1)
3	CastleWQ = 2 // white queen side (e1c1)
4	CastleBK = 4 // black king side (e8g8)
5	CastleBQ = 8 // black queen side (e8c8)
6)
source

Castling rights bits.

const StartFEN

1const StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
source

StartFEN is the standard starting position.

Functions 8

func ParseSquare

1func ParseSquare(s string) (int, bool)
source

ParseSquare converts algebraic notation to an index.

func SquareName

1func SquareName(sq int) string
source

SquareName converts an index to algebraic notation ("a1".."h8").

func Level

1func Level(level int) LevelInfo
source

Level returns the limits for a level, clamped to [MinLevel, MaxLevel].

func BestMove

1func BestMove(p Position, level int, seed int) (Move, bool)
source

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

1func BestMoveWithHistory(p Position, level int, seed int, history []uint64) (Move, bool)
source

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

1func ParseMove(s string) (Move, error)
source

ParseMove parses coordinate notation: "e2e4", "e1g1" or "e7e8n". It does not check legality; use Position.FindLegal for that.

func NewGame

1func NewGame() Position
source

NewGame returns the starting position.

func ParseFEN

1func ParseFEN(s string) (Position, error)
source

ParseFEN parses standard FEN. The halfmove and fullmove fields are optional.

Types 6

type Color

ident
1type Color int8
source

Color is the side to move.

Methods on Color

func Other

method on Color
1func (c Color) Other() Color
source

func String

method on Color
1func (c Color) String() string
source

type LevelInfo

struct
1type LevelInfo struct {
2	Name     string
3	MaxDepth int // full-width plies searched before quiescence
4	QDepth   int // capture plies searched after MaxDepth
5	Budget   int // maximum nodes visited per move (bounds the gas cost)
6}
source

LevelInfo describes the search limits of a bot level.

type Move

struct
1type Move struct {
2	From, To int
3	Promo    Piece
4}
source

Move 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.

Methods on Move

func String

method on Move
1func (m Move) String() string
source

String returns the move in coordinate notation, e.g. "e2e4" or "e7e8q".

type Piece

ident
1type Piece int8
source

Piece is a signed piece code: positive for white, negative for black.

Methods on Piece

func Color

method on Piece
1func (p Piece) Color() Color
source

Color returns the colour of a non-empty piece.

func Type

method on Piece
1func (p Piece) Type() Piece
source

Type returns the unsigned piece type.

type Position

struct
1type 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}
source

Position is a complete game state.

Methods on Position

func Attacked

method on Position
1func (p Position) Attacked(sq int, by Color) bool
source

Attacked reports whether square sq is attacked by any piece of colour by.

func BoardKey

method on Position
1func (p Position) BoardKey() string
source

BoardKey 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 Position
1func (p Position) Evaluate() int
source

Evaluate scores the position in centipawns from the side to move's view.

func FEN

method on Position
1func (p Position) FEN() string
source

FEN returns the full FEN string of the position.

func FindLegal

method on Position
1func (p Position) FindLegal(m Move) (Move, bool)
source

FindLegal 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 Position
1func (p Position) Hash() uint64
source

Hash returns a 64-bit hash of the position (for repetition tracking).

func InCheck

method on Position
1func (p Position) InCheck() bool
source

InCheck reports whether the side to move is in check.

func IsIrreversible

method on Position
1func (p Position) IsIrreversible(m Move) bool
source

IsIrreversible reports whether m is a capture or pawn move (resets the repetition history and the 50-move counter).

func LegalMoves

method on Position
1func (p Position) LegalMoves() []Move
source

LegalMoves returns all legal moves for the side to move.

func PieceAt

method on Position
1func (p Position) PieceAt(square string) Piece
source

PieceAt returns the piece on a square given by name, e.g. "e4".

func Play

method on Position
1func (p Position) Play(m Move) Position
source

Play 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 Position
1func (p Position) Status() Status
source

Status computes the game status from the position alone. Threefold repetition needs move history and is handled by the caller.

type Status

ident
1type Status int
source

Status describes whether the game is still going and how it ended.

Methods on Status

func String

method on Status
1func (s Status) String() string
source

Dependencies

Imports 3

/p/g1um2pwwnx3gn8p3ryfdert8mhpu4u4up49sc9ru/minichess

Imported by indexer

Source Files 7