Documentation
¶
Overview ¶
Package chess is a go library designed to accomplish the following:
- chess game / turn management
- move validation
- PGN encoding / decoding
- FEN encoding / decoding
Using Moves
game := chess.NewGame() moves := game.ValidMoves() game.Move(moves[0])
Using Algebraic Notation
game := chess.NewGame() game.MoveStr("e4")
Using PGN
pgn, _ := chess.PGN(pgnReader) game := chess.NewGame(pgn)
Using FEN
fen, _ := chess.FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") game := chess.NewGame(fen)
Random Game
package main import ( "fmt" "math/rand" "github.com/barakmich/chess" ) func main() { game := chess.NewGame() // generate moves until game is over for game.Outcome() == chess.NoOutcome { // select a random move moves := game.ValidMoves() move := moves[rand.Intn(len(moves))] game.Move(move) } // print outcome and game PGN fmt.Println(game.Position().Board().Draw()) fmt.Printf("Game completed. %s by %s.\n", game.Outcome(), game.Method()) fmt.Println(game.String()) }
Index ¶
- Constants
- type Board
- func (b *Board) Draw() string
- func (b *Board) Eq(other *Board) bool
- func (b *Board) FEN() string
- func (b *Board) Flip(fd FlipDirection) *Board
- func (b *Board) MarshalBinary() (data []byte, err error)
- func (b *Board) MarshalText() (text []byte, err error)
- func (b *Board) Piece(sq Square) Piece
- func (b *Board) Rotate() *Board
- func (b *Board) SquareMap() map[Square]Piece
- func (b *Board) String() string
- func (b *Board) Transpose() *Board
- func (b *Board) UnmarshalBinary(data []byte) error
- func (b *Board) UnmarshalText(text []byte) error
- type CastleRights
- type Color
- type File
- type FlipDirection
- type Game
- func (g *Game) AddTagPair(k, v string) bool
- func (g *Game) Clone() *Game
- func (g *Game) Draw(method Method) error
- func (g *Game) EligibleDraws() []Method
- func (g *Game) FEN() string
- func (g *Game) GetTagPair(k string) *TagPair
- func (g *Game) MarshalText() (text []byte, err error)
- func (g *Game) Method() Method
- func (g *Game) Move(m Move) error
- func (g *Game) MoveHistory() []*MoveHistory
- func (g *Game) MoveStr(s string) error
- func (g *Game) Moves() []Move
- func (g *Game) Outcome() Outcome
- func (g *Game) Position() *Position
- func (g *Game) Positions() []*Position
- func (g *Game) RemoveTagPair(k string) bool
- func (g *Game) Resign(color Color)
- func (g *Game) String() string
- func (g *Game) TagPairs() []*TagPair
- func (g *Game) UnmarshalText(text []byte) error
- func (g *Game) ValidMoves() []Move
- type Method
- type Move
- type MoveHistory
- type MoveTag
- type Notation
- type Outcome
- type ParallelScanner
- type Piece
- type PieceType
- type Position
- func (pos *Position) Board() *Board
- func (pos *Position) CastleRights() CastleRights
- func (pos *Position) DecodeLongAlgebraic(s string) (Move, error)
- func (pos *Position) DecodeMove(s string, n ...Notation) (Move, error)
- func (pos *Position) DecodeSAN(s string) (Move, error)
- func (pos *Position) DecodeUCI(s string) (Move, error)
- func (pos *Position) EncodeLongAlgebraic(m Move) string
- func (pos *Position) EncodeMove(m Move, n Notation) string
- func (pos *Position) EncodeSAN(m Move) string
- func (pos *Position) EncodeUCI(m Move) string
- func (pos *Position) Hash() [16]byte
- func (pos *Position) MarshalBinary() (data []byte, err error)
- func (pos *Position) MarshalText() (text []byte, err error)
- func (pos *Position) Status() Method
- func (pos *Position) String() string
- func (pos *Position) Turn() Color
- func (pos *Position) UnmarshalBinary(data []byte) error
- func (pos *Position) UnmarshalText(text []byte) error
- func (pos *Position) Update(m Move) *Position
- func (pos *Position) ValidMoves() []Move
- type PromoType
- type Rank
- type Scanner
- type Side
- type Square
- type TagPair
Constants ¶
const ( SANNotation = iota StrictSANNotation UCINotation LongAlgebraicNotation )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
A Board represents a chess board and its relationship between squares and pieces.
func (*Board) Flip ¶ added in v1.9.0
func (b *Board) Flip(fd FlipDirection) *Board
Flip flips the board over the vertical or hoizontal center line.
func (*Board) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface and returns the bitboard representations as a array of bytes. Bitboads are encoded in the following order: WhiteKing, WhiteQueen, WhiteRook, WhiteBishop, WhiteKnight WhitePawn, BlackKing, BlackQueen, BlackRook, BlackBishop, BlackKnight, BlackPawn
func (*Board) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface and returns a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
func (*Board) SquareMap ¶
SquareMap returns a mapping of squares to pieces. A square is only added to the map if it is occupied.
func (*Board) String ¶
String implements the fmt.Stringer interface and returns a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
func (*Board) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface and parses the bitboard representations as a array of bytes. Bitboads are decoded in the following order: WhiteKing, WhiteQueen, WhiteRook, WhiteBishop, WhiteKnight WhitePawn, BlackKing, BlackQueen, BlackRook, BlackBishop, BlackKnight, BlackPawn
func (*Board) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnarshaler interface and takes a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
type CastleRights ¶
type CastleRights string
CastleRights holds the state of both sides castling abilities.
func (CastleRights) CanCastle ¶
func (cr CastleRights) CanCastle(c Color, side Side) bool
CanCastle returns true if the given color and side combination can castle, otherwise returns false.
func (CastleRights) String ¶
func (cr CastleRights) String() string
String implements the fmt.Stringer interface and returns a FEN compatible string. Ex. KQq
type FlipDirection ¶ added in v1.9.0
type FlipDirection int
FlipDirection is the direction for the Board.Flip method
const ( // UpDown flips the board's rank values UpDown FlipDirection = iota // LeftRight flips the board's file values LeftRight )
type Game ¶
type Game struct { Notation Notation // contains filtered or unexported fields }
A Game represents a single chess game.
func NewGame ¶
func NewGame() *Game
NewGame defaults to returning a game in the standard opening position. Options can be given to configure the game's initial state.
func NewGameFromFEN ¶ added in v1.9.0
FEN takes a string and returns a function that creates the game to reflect the FEN data. Since FEN doesn't encode prior moves, the move list will be empty. An error is returned if there is a problem parsing the FEN data.
func NewGameFromPGN ¶ added in v1.9.0
NewGameFromPGN takes a reader and returns a function that creates the game to reflect the PGN data. The PGN can use any move notation supported by this package. An error is returned if there is a problem parsing the PGN data.
func (*Game) AddTagPair ¶
AddTagPair adds or updates a tag pair with the given key and value and returns true if the value is overwritten.
func (*Game) Draw ¶
Draw attempts to draw the game by the given method. If the method is valid, then the game is updated to a draw by that method. If the method isn't valid then an error is returned.
func (*Game) EligibleDraws ¶
EligibleDraws returns valid inputs for the Draw() method.
func (*Game) GetTagPair ¶
GetTagPair returns the tag pair for the given key or nil if it is not present.
func (*Game) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface and encodes the game's PGN.
func (*Game) Move ¶
Move updates the game with the given move. An error is returned if the move is invalid or the game has already been completed.
func (*Game) MoveHistory ¶ added in v1.9.0
func (g *Game) MoveHistory() []*MoveHistory
MoveHistory returns the moves in order along with the pre and post positions and any comments.
func (*Game) MoveStr ¶
MoveStr decodes the given string, trying the obvious notations as though it were a PGN, and calls the Move function. To parse with a An error is returned if the move can't be decoded or the move is invalid.
func (*Game) RemoveTagPair ¶
RemoveTagPair removes the tag pair for the given key and returns true if a tag pair was removed.
func (*Game) Resign ¶
Resign resigns the game for the given color. If the game has already been completed then the game is not updated.
func (*Game) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnarshaler interface and assumes the data is in the PGN format.
func (*Game) ValidMoves ¶
ValidMoves returns a list of valid moves in the current position.
type Method ¶
type Method uint8
A Method is the method that generated the outcome.
const ( // NoMethod indicates that an outcome hasn't occurred or that the method can't be determined. NoMethod Method = iota // Checkmate indicates that the game was won checkmate. Checkmate // Resignation indicates that the game was won by resignation. Resignation // DrawOffer indicates that the game was drawn by a draw offer. DrawOffer // Stalemate indicates that the game was drawn by stalemate. Stalemate // ThreefoldRepetition indicates that the game was drawn when the game // state was repeated three times and a player requested a draw. ThreefoldRepetition // FivefoldRepetition indicates that the game was automatically drawn // by the game state being repeated five times. FivefoldRepetition // FiftyMoveRule indicates that the game was drawn by the half // move clock being one hundred or greater when a player requested a draw. FiftyMoveRule // SeventyFiveMoveRule indicates that the game was automatically drawn // when the half move clock was one hundred and fifty or greater. SeventyFiveMoveRule // InsufficientMaterial indicates that the game was automatically drawn // because there was insufficient material for checkmate. InsufficientMaterial )
type Move ¶
type Move uint64
A Move is the movement of a piece from one square to another.
func (Move) String ¶
String returns a string useful for debugging. String doesn't return algebraic notation.
func (Move) StringWithTags ¶ added in v1.9.0
type MoveHistory ¶ added in v1.9.0
MoveHistory is a move's result from Game's MoveHistory method. It contains the move itself, any comments, and the pre and post positions.
type MoveTag ¶
type MoveTag uint16
A MoveTag represents a notable consequence of a move.
const ( // KingSideCastle indicates that the move is a king side castle. KingSideCastle MoveTag = 1 << iota // QueenSideCastle indicates that the move is a queen side castle. QueenSideCastle // Capture indicates that the move captures a piece. Capture // EnPassant indicates that the move captures via en passant. EnPassant // Check indicates that the move puts the opposing player in check. Check // IsCheckmate indicates that the move puts the opposing player in checkmate. IsCheckmate )
type Outcome ¶
type Outcome string
A Outcome is the result of a game.
const ( // NoOutcome indicates that a game is in progress or ended without a result. NoOutcome Outcome = "*" // WhiteWon indicates that white won the game. WhiteWon Outcome = "1-0" // BlackWon indicates that black won the game. BlackWon Outcome = "0-1" // Draw indicates that game was a draw. Draw Outcome = "1/2-1/2" )
type ParallelScanner ¶ added in v1.9.0
type ParallelScanner struct {
// contains filtered or unexported fields
}
func NewParallelScanner ¶ added in v1.9.0
func NewParallelScanner(r io.Reader) *ParallelScanner
NewParallelScanner returns a new scanner that decodes PGN in parallel.
func (*ParallelScanner) Begin ¶ added in v1.9.0
func (s *ParallelScanner) Begin(ctx context.Context, output chan *Game) error
func (*ParallelScanner) Err ¶ added in v1.9.0
func (s *ParallelScanner) Err() error
Err returns an error encountered during scanning. Typically this will be a PGN parsing error or an io.EOF.
type Piece ¶
type Piece uint8
Piece is a piece type with a color.
const ( // WhiteKing is a white king WhiteKing Piece = 0 // WhiteQueen is a white queen WhiteQueen Piece = 1 // WhiteRook is a white rook WhiteRook Piece = 2 // WhiteBishop is a white bishop WhiteBishop Piece = 3 // WhiteKnight is a white knight WhiteKnight Piece = 4 // WhitePawn is a white pawn WhitePawn Piece = 5 // BlackKing is a black king BlackKing Piece = 6 // BlackQueen is a black queen BlackQueen Piece = 7 // BlackRook is a black rook BlackRook Piece = 8 // BlackBishop is a black bishop BlackBishop Piece = 9 // BlackKnight is a black knight BlackKnight Piece = 10 // BlackPawn is a black pawn BlackPawn Piece = 11 // NoPiece represents no piece NoPiece Piece = 255 )
type PieceType ¶
type PieceType uint8
PieceType is the type of a piece.
const ( // King represents a king King PieceType = 0 // Queen represents a queen Queen PieceType = 1 // Rook represents a rook Rook PieceType = 2 // Bishop represents a bishop Bishop PieceType = 3 // Knight represents a knight Knight PieceType = 4 // Pawn represents a pawn Pawn PieceType = 5 // NoPieceType represents a lack of piece type NoPieceType PieceType = 15 )
type Position ¶
type Position struct {
// contains filtered or unexported fields
}
Position represents the state of the game without reguard to its outcome. Position is translatable to FEN notation.
func StartingPosition ¶ added in v1.9.0
func StartingPosition() *Position
StartingPosition returns the starting position rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
func (*Position) CastleRights ¶
func (pos *Position) CastleRights() CastleRights
CastleRights returns the castling rights of the position.
func (*Position) DecodeLongAlgebraic ¶ added in v1.9.0
Decode implements the Decoder interface.
func (*Position) DecodeMove ¶ added in v1.9.0
func (*Position) EncodeLongAlgebraic ¶ added in v1.9.0
func (*Position) EncodeMove ¶ added in v1.9.0
func (*Position) MarshalBinary ¶ added in v1.9.0
MarshalBinary implements the encoding.BinaryMarshaler interface
func (*Position) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface and encodes the position's FEN.
func (*Position) Status ¶
Status returns the position's status as one of the outcome methods. Possible returns values include Checkmate, Stalemate, and NoMethod.
func (*Position) String ¶
String implements the fmt.Stringer interface and returns a string with the FEN format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
func (*Position) UnmarshalBinary ¶ added in v1.9.0
UnmarshalBinary implements the encoding.BinaryMarshaler interface
func (*Position) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnarshaler interface and assumes the data is in the FEN format.
func (*Position) Update ¶
Update returns a new position resulting from the given move. The move itself isn't validated, if validation is needed use Game's Move method. This method is more performant for bots that rely on the ValidMoves because it skips redundant validation.
func (*Position) ValidMoves ¶
ValidMoves returns a list of valid moves for the position.
type Scanner ¶ added in v1.9.0
type Scanner struct {
// contains filtered or unexported fields
}
Scanner is modeled on the bufio.Scanner type but instead of reading lines, it reads chess games from concatenated PGN files. It is designed to replace GamesFromPGN in order to handle very large PGN database files such as https://database.lichess.org/.
func NewScanner ¶ added in v1.9.0
NewScanner returns a new scanner.
func (*Scanner) Err ¶ added in v1.9.0
Err returns an error encountered during scanning. Typically this will be a PGN parsing error or an io.EOF.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package image is a go library that creates images from board positions
|
Package image is a go library that creates images from board positions |
Package opening implements chess opening determination and exploration.
|
Package opening implements chess opening determination and exploration. |