Documentation
¶
Index ¶
- Constants
- type Board
- func (b Board) CanBlackCastleKingside() bool
- func (b Board) CanBlackCastleQueenside() bool
- func (b Board) CanWhiteCastleKingside() bool
- func (b Board) CanWhiteCastleQueenside() bool
- func (b Board) ColourFlipped() Board
- func (b Board) EnPassant() uint8
- func (b *Board) Evaluate() int16
- func (b *Board) FEN() string
- func (b Board) FullMoves() int
- func (b *Board) GenerateLegalMoves(moves []Move) ([]Move, bool)
- func (b Board) HalfMoves() int
- func (b *Board) HydrateMove(m chess.FromToPromoter) (Move, error)
- func (b Board) PieceAt(i uint8) Piece
- func (b Board) String() string
- func (b Board) ToMove() Colour
- func (b Board) Validate() error
- func (b *Board) WriteFEN(w io.Writer) error
- type Colour
- type Game
- func (g *Game) BestMoveInfinite(stopch <-chan struct{}, statusch chan<- SearchStatus) (Move, int16)
- func (g *Game) BestMoveToDepth(depth uint8, stopch <-chan struct{}, statusch chan<- SearchStatus) (Move, int16)
- func (g *Game) BestMoveToTime(whiteTime, blackTime, whiteIncrement, blackIncrement time.Duration, ...) (Move, int16)
- func (g *Game) MakeMove(move Move)
- func (g *Game) SetBoard(b *Board)
- func (g *Game) UnmakeMove()
- type Move
- func NewBishopPromotion(from, to uint8, capture bool) Move
- func NewCapture(from, to uint8) Move
- func NewEnPassant(from, to uint8) Move
- func NewKnightPromotion(from, to uint8, capture bool) Move
- func NewMove(from, to uint8) Move
- func NewPawnDoublePush(from, to uint8) Move
- func NewQueenPromotion(from, to uint8, capture bool) Move
- func NewRookPromotion(from, to uint8, capture bool) Move
- func (m Move) From() uint8
- func (m Move) IsCapture() bool
- func (m Move) IsEnPassant() bool
- func (m Move) IsKingsideCastling() bool
- func (m Move) IsPawnDoublePush() bool
- func (m Move) IsPromotion() bool
- func (m Move) IsQueensideCastling() bool
- func (m Move) PromoteTo() chess.PromoteTo
- func (m Move) SAN() string
- func (m Move) To() uint8
- type Piece
- type SearchStatus
Constants ¶
const ( A1 = iota B1 C1 D1 E1 F1 G1 H1 A2 B2 C2 D2 E2 F2 G2 H2 A3 B3 C3 D3 E3 F3 G3 H3 A4 B4 C4 D4 E4 F4 G4 H4 A5 B5 C5 D5 E5 F5 G5 H5 A6 B6 C6 D6 E6 F6 G6 H6 A7 B7 C7 D7 E7 F7 G7 H7 A8 B8 C8 D8 E8 F8 G8 H8 )
A1...H8 are constants defined for hardcoding an index by its rank and file.
const ( BlackKingsideCastle = Move(uint16(E8)<<6|uint16(G8)) | moveIsKingsideCastle BlackQueensideCastle = Move(uint16(E8)<<6|uint16(C8)) | moveIsQueensideCastle WhiteKingsideCastle = Move(uint16(E1)<<6|uint16(G1)) | moveIsKingsideCastle WhiteQueensideCastle = Move(uint16(E1)<<6|uint16(C1)) | moveIsQueensideCastle )
Castling moves are represented with constants.
const InitialBoardFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
InitialBoardFEN is the FEN for a board in the initial state.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board represents an 8×8 chess board.
The 0th index represents A1 and the 63rd index represents H8.
A B C D E F G H 8 | 56 57 58 59 60 61 62 63 7 | 48 49 50 51 52 53 54 55 6 | 40 41 42 43 44 45 46 47 5 | 32 33 34 35 36 37 38 39 4 | 24 25 26 27 28 29 30 31 3 | 16 17 18 19 20 21 22 23 2 | 8 9 10 11 12 13 14 15 1 | 0 1 2 3 4 5 6 7
For an index i into the board, i/8 is the rank and i%8 is the file.
func NewBoardFromFEN ¶
NewBoardFromFEN returns a new board initialised as per the provided Forsyth–Edwards Notation. Only 8×8 boards are supported. Only basic validation of resulting board state is performed.
func (Board) CanBlackCastleKingside ¶
CanBlackCastleKingside returns true if black can castle kingside.
func (Board) CanBlackCastleQueenside ¶
CanBlackCastleQueenside returns true if black can castle queenside.
func (Board) CanWhiteCastleKingside ¶
CanWhiteCastleKingside returns true if white can castle kingside.
func (Board) CanWhiteCastleQueenside ¶
CanWhiteCastleQueenside returns true if white can castle queenside.
func (Board) ColourFlipped ¶
ColourFlipped returns a board such that all the black pieces are now white pieces and all the white pieces are now black pieces, and the players have switched sides.
func (Board) EnPassant ¶
EnPassant returns the index of the square under threat of en passant, or math.MaxUint8 if there is no such square. TODO: return bool here, this isn't used anywhere performance critical
func (*Board) Evaluate ¶
Evaluate returns a score evaluation for the board. A positive number indicates that white is winning, a negative number indicates that black is winning. Larger numbers indicate that the side is winning by a wider margin than lower numbers.
func (*Board) GenerateLegalMoves ¶
GenerateLegalMoves returns a slice of possible moves from the current board state. It also returns whether or not the side to move is in check. An empty or nil slice of moves combined with an an indication of check implies that the side to move is in checkmate.
This function will panic if run with certain invalid boards, e.g. if there are more than two pieces giving check, or if one side doesn't have a king on the board. You should wrap it in a recover, or ideally ensure that you're only calling GenerateLegalMoves() on valid boards by calling Validate() first.
func (Board) HalfMoves ¶
HalfMoves returns the number of half moves (moves by one player) since the last pawn moved or piece was captured. This is used for determining if a draw can be claimed by the fifty move rule.
func (*Board) HydrateMove ¶
func (b *Board) HydrateMove(m chess.FromToPromoter) (Move, error)
HydrateMove takes a minimal move (likely parsed from Long Algebraic Notation or similar), checks it against the board for sanity, and returns the engines internal representation of a move.
type Game ¶
type Game struct {
*Board // TODO: Board (no ref), to embed mem and avoid lots of pointer lookups?
// contains filtered or unexported fields
}
func (*Game) BestMoveInfinite ¶
func (g *Game) BestMoveInfinite(stopch <-chan struct{}, statusch chan<- SearchStatus) (Move, int16)
func (*Game) BestMoveToDepth ¶
func (g *Game) BestMoveToDepth(depth uint8, stopch <-chan struct{}, statusch chan<- SearchStatus) (Move, int16)
BestMoveToDepth returns the best move (with its score) to the given depth.
func (*Game) BestMoveToTime ¶
func (*Game) UnmakeMove ¶
func (g *Game) UnmakeMove()
UnmakeMove unapplies the most recent move on the board.
type Move ¶
type Move uint16
Move represents a chess move.
This type unambiguously represents any chess move, and also encodes some meta information such as whether the move is a capture, en passant, castling, etc. This meta information is redundant (it can be recalculated from the move plus the current board state) but can be useful for ordering moves.
func NewBishopPromotion ¶
NewBishopPromotion returns a new move which represents a pawn promoting to a bishop.
func NewCapture ¶
NewCapture returns a new move which represents a capture.
func NewEnPassant ¶
NewEnPassant returns a new move which represents a capture en passant.
func NewKnightPromotion ¶
NewKnightPromotion returns a new move which represents a pawn promoting to a knight.
func NewPawnDoublePush ¶
NewPawnDoublePush returns a new move which represents a pawn double push.
func NewQueenPromotion ¶
NewQueenPromotion returns a new move where the move represents a pawn promoting to a queen.
func NewRookPromotion ¶
NewRookPromotion returns a new move which represents a pawn promoting to a rook.
func (Move) IsEnPassant ¶
IsEnPassant returns true if the move represents a capture en passant.
func (Move) IsKingsideCastling ¶
IsKingsideCastling returns true if the move represents kingside castling.
func (Move) IsPawnDoublePush ¶
IsPawnDoublePush returns true if the move represents a pawn double push.
func (Move) IsPromotion ¶
IsPromotion returns true if the move represents a pawn promotion.
func (Move) IsQueensideCastling ¶
IsQueensideCastling returns true if the move represents queenside castling.
func (Move) SAN ¶
SAN returns the move in Somewhat Algebraic Notation, which is very similar to (but not quite the same as) Standard Algebraic Notation.
The main difference is that Standard Algebraic Notation indicates a piece moving to a square (e.g. "Ka1" to represent the king moving to the A1 square), omitting the source square when it is unambiguous to do so (i.e. when there's only one piece of the type indicated that could have made the move).
Somewhat Algebraic Notation always includes both source square and target square (similarly to Pure Coordinate Notation), and never includes the piece type. Piece type can be unambiguously determined from the source square and the current state of the board.
type Piece ¶
type Piece byte
Piece represents a chess piece.
const ( PieceNone Piece = 0b00000000 PieceWhite Piece = 0b10000000 PieceBlack Piece = 0b01000000 PiecePawn Piece = 0b00100000 PieceKnight Piece = 0b00010000 PieceBishop Piece = 0b00001000 PieceRook Piece = 0b00000100 PieceQueen Piece = 0b00000010 PieceKing Piece = 0b00000001 PieceWhitePawn Piece = PieceWhite | PiecePawn PieceWhiteKnight Piece = PieceWhite | PieceKnight PieceWhiteBishop Piece = PieceWhite | PieceBishop PieceWhiteRook Piece = PieceWhite | PieceRook PieceWhiteQueen Piece = PieceWhite | PieceQueen PieceWhiteKing Piece = PieceWhite | PieceKing PieceBlackPawn Piece = PieceBlack | PiecePawn PieceBlackKnight Piece = PieceBlack | PieceKnight PieceBlackBishop Piece = PieceBlack | PieceBishop PieceBlackRook Piece = PieceBlack | PieceRook PieceBlackQueen Piece = PieceBlack | PieceQueen PieceBlackKing Piece = PieceBlack | PieceKing )
Pieces will have a bit set for the colour and a bit set for the type.