Documentation
¶
Overview ¶
Package chess provides a chess engine with a focus on algebraic notation, game state management, and move validation.
Index ¶
- Constants
- func CreateBoardValidator(g *Game) *boardValidator
- func CreateGameValidator(g *Game) *gameValidator
- func CreateOpeningsLibrary() (*openingsLibrary, error)
- func CreatePieceValidator(pt pieceType, b *Board) *pieceValidator
- type AlgebraicClientOptions
- type AlgebraicGameClient
- type Board
- type Game
- type GameStatus
- type KingThreatEvent
- type MoveEvent
- type Opening
- type Piece
- type Side
- type Square
Constants ¶
const ( NeighborAbove neighbor = 8 NeighborAboveLeft neighbor = 7 NeighborAboveRight neighbor = 9 NeighborBelow neighbor = -8 NeighborBelowLeft neighbor = -9 NeighborBelowRight neighbor = -7 NeighborLeft neighbor = -1 NeighborRight neighbor = 1 NeighborKnightAboveLeft neighbor = 15 NeighborKnightAboveRight neighbor = 17 NeighborKnightBelowLeft neighbor = -17 NeighborKnightBelowRight neighbor = -15 NeighborKnightLeftAbove neighbor = 6 NeighborKnightLeftBelow neighbor = -10 NeighborKnightRightAbove neighbor = 10 NeighborKnightRightBelow neighbor = -6 )
Variables ¶
This section is empty.
Functions ¶
func CreateBoardValidator ¶
func CreateBoardValidator(g *Game) *boardValidator
func CreateGameValidator ¶
func CreateGameValidator(g *Game) *gameValidator
func CreateOpeningsLibrary ¶ added in v0.1.2
func CreateOpeningsLibrary() (*openingsLibrary, error)
CreateOpeningsLibrary initializes a new openings library by reading from the default openings CSV file. It returns an error if the file cannot be read or parsed.
func CreatePieceValidator ¶
func CreatePieceValidator(pt pieceType, b *Board) *pieceValidator
CreatePieceValidator initializes a new validator for a specific piece type on a given board. It configures movement rules (e.g., diagonal, horizontal) and repetition counts based on the piece type.
Types ¶
type AlgebraicClientOptions ¶
type AlgebraicClientOptions struct {
PGN bool // PGN specifies whether to use PGN-style notation for castling (O-O) instead of (0-0).
}
AlgebraicClientOptions provides configuration options for an AlgebraicGameClient.
type AlgebraicGameClient ¶
type AlgebraicGameClient struct {
// contains filtered or unexported fields
}
AlgebraicGameClient provides a client for interacting with a chess game using algebraic notation.
func CreateAlgebraicGameClient ¶
func CreateAlgebraicGameClient(opts ...AlgebraicClientOptions) *AlgebraicGameClient
CreateAlgebraicGameClient creates a new game client with a standard starting board. It accepts optional AlgebraicClientOptions.
func CreateAlgebraicGameClientFromFEN ¶
func CreateAlgebraicGameClientFromFEN(fen string, opts ...AlgebraicClientOptions) (*AlgebraicGameClient, error)
CreateAlgebraicGameClientFromFEN creates a new game client from a FEN string. It returns an error if the FEN string is invalid.
func (*AlgebraicGameClient) CaptureHistory ¶
func (c *AlgebraicGameClient) CaptureHistory() []*Piece
CaptureHistory returns a slice of pieces that have been captured during the game.
func (*AlgebraicGameClient) FEN ¶
func (c *AlgebraicGameClient) FEN() string
FEN returns the Forsyth-Edwards Notation (FEN) string for the current board state.
func (*AlgebraicGameClient) Move ¶
func (c *AlgebraicGameClient) Move(ntn string) (*moveResult, error)
Move attempts to make a move using algebraic notation.
func (*AlgebraicGameClient) On ¶
func (c *AlgebraicGameClient) On(ev string, hndlr func(any))
On registers an event handler for the given event. The client supports the following events:
- "move": emitted after a piece has been moved. The handler receives a *MoveEvent.
- "capture": emitted when a piece is captured. The handler receives a *MoveEvent.
- "castle": emitted when a castling move is performed. The handler receives a *MoveEvent.
- "enPassant": emitted when an en passant capture occurs. The handler receives a *MoveEvent.
- "promote": emitted when a pawn is promoted. The handler receives the promoted *Square.
- "undo": emitted after a move has been undone. The handler receives the undone *MoveEvent.
- "check": emitted when a player is put in check. The handler receives a *KingThreatEvent.
- "checkmate": emitted when a player is checkmated. The handler receives a *KingThreatEvent.
func (*AlgebraicGameClient) Status ¶
func (c *AlgebraicGameClient) Status(frc ...bool) (*GameStatus, error)
Status returns the current status of the game. If force is true, it will re-calculate all valid moves and game-end conditions.
type Board ¶
type Board struct {
// Squares is a slice of 64 squares representing the board.
Squares []*Square
// LastMovedPiece points to the piece that was last moved.
LastMovedPiece *Piece
// contains filtered or unexported fields
}
Board represents the chess board and its state. It contains all the squares and the last moved piece. It can emit events for moves, captures, promotions, etc.
func (*Board) Move ¶
Move performs a move on the board from a source square to a destination square. If simulate is true, the move is not committed to the board's history and no events are emitted. The returned moveResult contains an `undo` function that can be called to revert the move. It returns an error if the move is invalid.
type Game ¶
type Game struct {
// Board is the current chessboard state.
Board *Board
// CaptureHistory is a list of pieces that have been captured.
CaptureHistory []*Piece
// MoveHistory is a chronological record of all moves made in the game.
MoveHistory []*MoveEvent
// contains filtered or unexported fields
}
Game represents the state of a single chess game. It manages the board, move history, captured pieces, and turn sequence.
type GameStatus ¶ added in v0.0.6
type GameStatus struct {
Game *Game // The current game state.
IsCheck bool // True if the current player is in check.
IsCheckmate bool // True if the current player is in checkmate.
IsRepetition bool // True if the current board state is a result of repetition.
IsStalemate bool // True if the game is a stalemate.
NotatedMoves map[string]notationMove // A map of all valid moves in algebraic notation.
}
GameStatus represents the state of the game at a certain point in time.
func (*GameStatus) Side ¶ added in v0.0.6
func (s *GameStatus) Side() Side
Side returns the side of the player who made the last move. If no moves have been made, it defaults to sideWhite (unless this state has been overridden for the game).
type KingThreatEvent ¶ added in v0.1.0
type Opening ¶ added in v0.1.2
type Opening struct {
// ECO is the Encyclopedia of Chess Openings code (e.g., "A00").
ECO string
// Moves is a slice of algebraic notation strings representing the move sequence.
Moves []string
// Name is the common name of the opening (e.g., "Ruy Lopez").
Name string
// ResultFEN is the Forsyth-Edwards Notation string of the final board position.
ResultFEN string
// SequenceFENs is a slice of FEN strings for each board state in the opening sequence.
SequenceFENs []string
// SequenceMoves is a string representation of each turn
SequenceMoves string
}
Opening represents a single chess opening, including its name, move sequence, and the FEN strings for each position in the sequence.
type Piece ¶
type Piece struct {
// Type is the type of the piece (e.g., Pawn, Rook, King).
Type pieceType
// Side is the color of the piece (White or Black).
Side Side
// Notation is the standard algebraic notation for the piece (e.g., "R" for Rook).
// Pawns have an empty string.
Notation string
// MoveCount tracks how many times the piece has moved. This is important for castling and pawn's first move.
MoveCount int
}
Piece represents a single chess piece on the board.
func (*Piece) AlgebraicSymbol ¶
AlgebraicSymbol returns the rune used to represent the piece in various notations. White pieces are uppercase (e.g., 'P'), and black pieces are lowercase (e.g., 'p').