Documentation
¶
Index ¶
- func AlgebraicToCoordinate(s string) (gochess.Coordinate, error)
- func CoordinateToAlgebraic(c gochess.Coordinate) string
- func UCI(origin, target gochess.Coordinate, piece ...int8) string
- type Board
- type Chess
- func (c *Chess) AvailableMoves() []string
- func (c *Chess) FEN() string
- func (c *Chess) IsCheck() bool
- func (c *Chess) IsCheckmate() bool
- func (c *Chess) IsStalemate() bool
- func (c *Chess) LoadPosition(FEN string) error
- func (c *Chess) MakeMove(move string) error
- func (c *Chess) Square(square string) (string, error)
- func (c *Chess) UnmakeMove()
- type Cloner
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlgebraicToCoordinate ¶
func AlgebraicToCoordinate(s string) (gochess.Coordinate, error)
AlgebraicToCoordinate returns a new Coordinate from text notation. For example, "a1" would return (0, 0). If the text notation is invalid, an empty Coordinate is returned.
func CoordinateToAlgebraic ¶
func CoordinateToAlgebraic(c gochess.Coordinate) string
CoordinateToAlgebraic returns a new algebraic notation from a Coordinate. For example, (0, 0) would return "a1". If the Coordinate is out of bounds, an empty string is returned.
func UCI ¶
func UCI(origin, target gochess.Coordinate, piece ...int8) string
UCI returns the UCI notation of a move.
It receives the origin and target coordinates of the move. For example, if the origin is (0, 0) and the target is (0, 1), it would return "a1a2".
If the move is a promotion, it receives the piece to promote to. If it receives more than one piece, it returns the first one.
Types ¶
type Board ¶
type Board interface { // SetSquare sets a piece in a square. SetSquare(c gochess.Coordinate, p int8) error // Square returns the piece in a square. Square(c gochess.Coordinate) (int8, error) // Width returns the width of the board. Width() int }
Board represents a chess board.
type Chess ¶
type Chess struct {
// contains filtered or unexported fields
}
Chess represents a Chess game.
func New ¶
New creates a new chess game.
The chess.AvailableMoves method will use a pool of workers to maximize performance on the moves calculation. By default, the number of workers is twice the number of available CPUs. If you are running on a container environment or you want to use the sequential version, you should set this value manually using the WithParallelism option. The pool of workers will be used only if the board implements the Cloner interface. If you are using a custom Board with the WithBoard option, you should implement the Cloner interface to take advantage of the parallelism.
func (*Chess) AvailableMoves ¶
AvailableMoves returns the available legal moves for the current turn.
It always returns a non nil slice. It could be empty if the position is checkmate or stalemate.
func (*Chess) FEN ¶
FEN returns the FEN string of the current position.
If any of the kings is not in the board, the function returns an empty string.
func (*Chess) IsCheckmate ¶ added in v1.2.0
IsCheckmate returns if the current turn is in checkmate.
func (*Chess) IsStalemate ¶ added in v1.2.0
IsStalemate returns if the current turn is in stalemate.
func (*Chess) LoadPosition ¶
LoadPosition loads a board from a FEN string.
The function will read the entire FEN string and will return an error if the FEN string is invalid.
The board and properties will not be modified if the FEN string is invalid.
func (*Chess) MakeMove ¶
MakeMove checks if the move is legal and makes it. It returns an error if the move is not legal.
func (*Chess) Square ¶
Square returns the piece in a square. The square is represented by an algebraic notation.
If the square is not valid, the function returns an error.
func (*Chess) UnmakeMove ¶
func (c *Chess) UnmakeMove()
UnmakeMove unmake the last move.
It searches for the last move in the history and unmake it. If there are no moves in the history, the function does nothing.
type Cloner ¶ added in v1.2.0
type Cloner interface { // Clone returns a clone of the object. Clone() Board }
Cloner is a generic interface for objects that can be cloned.
type Option ¶
Option is a function that configures a chess.
func WithBoard ¶
WithBoard sets the board of the chess. If the board is nil, it returns an error. If you want to use this option, it must be the first one.
func WithFEN ¶
WithFEN sets the FEN of the chess. If the FEN is invalid, it returns an error. If you try to set the FEN before the board, it will set the default board.
func WithParallelism ¶ added in v1.2.0
WithParallelism sets the number of workers to use for the moves calculation. If the number of workers is less or equal to 1, the Chess will use the sequential version without throwing goroutines.