engine

package
v0.0.0-...-7a7351a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
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.

View Source
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.

View Source
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 NewBoard

func NewBoard() *Board

NewBoard returns a board in the initial state.

func NewBoardFromFEN

func NewBoardFromFEN(fen io.Reader) (*Board, error)

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

func (b Board) CanBlackCastleKingside() bool

CanBlackCastleKingside returns true if black can castle kingside.

func (Board) CanBlackCastleQueenside

func (b Board) CanBlackCastleQueenside() bool

CanBlackCastleQueenside returns true if black can castle queenside.

func (Board) CanWhiteCastleKingside

func (b Board) CanWhiteCastleKingside() bool

CanWhiteCastleKingside returns true if white can castle kingside.

func (Board) CanWhiteCastleQueenside

func (b Board) CanWhiteCastleQueenside() bool

CanWhiteCastleQueenside returns true if white can castle queenside.

func (Board) ColourFlipped

func (b Board) ColourFlipped() Board

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

func (b Board) EnPassant() uint8

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

func (b *Board) Evaluate() int16

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) FEN

func (b *Board) FEN() string

FEN returns the Forsyth–Edwards Notation for the board as a string.

func (Board) FullMoves

func (b Board) FullMoves() int

FullMoves returns the number of full moves (moves by both players).

func (*Board) GenerateLegalMoves

func (b *Board) GenerateLegalMoves(moves []Move) ([]Move, bool)

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

func (b Board) HalfMoves() int

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.

func (Board) PieceAt

func (b Board) PieceAt(i uint8) Piece

PieceAt returns the piece at index i.

func (Board) String

func (b Board) String() string

String renders the board from whites perspective.

func (Board) ToMove

func (b Board) ToMove() Colour

ToMove returns the colour whose move it is.

func (Board) Validate

func (b Board) Validate() error

Validate returns an error on an inconsistent or invalid board.

func (*Board) WriteFEN

func (b *Board) WriteFEN(w io.Writer) error

WriteFEN writes the Forsyth–Edwards Notation for the board to w.

type Colour

type Colour byte

Colour is used to represent each colour.

const (
	White Colour = 'w'
	Black Colour = 'b'
)

White and Black are constants defined for the colours.

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 NewGame

func NewGame(b *Board) *Game

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 (g *Game) BestMoveToTime(whiteTime, blackTime, whiteIncrement, blackIncrement time.Duration, stopch <-chan struct{}, statusch chan<- SearchStatus) (Move, int16)

func (*Game) MakeMove

func (g *Game) MakeMove(move Move)

MakeMove applies move to the board, updating its state.

func (*Game) SetBoard

func (g *Game) SetBoard(b *Board)

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

func NewBishopPromotion(from, to uint8, capture bool) Move

NewBishopPromotion returns a new move which represents a pawn promoting to a bishop.

func NewCapture

func NewCapture(from, to uint8) Move

NewCapture returns a new move which represents a capture.

func NewEnPassant

func NewEnPassant(from, to uint8) Move

NewEnPassant returns a new move which represents a capture en passant.

func NewKnightPromotion

func NewKnightPromotion(from, to uint8, capture bool) Move

NewKnightPromotion returns a new move which represents a pawn promoting to a knight.

func NewMove

func NewMove(from, to uint8) Move

NewMove returns a new move which is not a capture, promotion or castling.

func NewPawnDoublePush

func NewPawnDoublePush(from, to uint8) Move

NewPawnDoublePush returns a new move which represents a pawn double push.

func NewQueenPromotion

func NewQueenPromotion(from, to uint8, capture bool) Move

NewQueenPromotion returns a new move where the move represents a pawn promoting to a queen.

func NewRookPromotion

func NewRookPromotion(from, to uint8, capture bool) Move

NewRookPromotion returns a new move which represents a pawn promoting to a rook.

func (Move) From

func (m Move) From() uint8

From returns the from index for the move.

func (Move) IsCapture

func (m Move) IsCapture() bool

IsCapture returns true if the move represents a capture.

func (Move) IsEnPassant

func (m Move) IsEnPassant() bool

IsEnPassant returns true if the move represents a capture en passant.

func (Move) IsKingsideCastling

func (m Move) IsKingsideCastling() bool

IsKingsideCastling returns true if the move represents kingside castling.

func (Move) IsPawnDoublePush

func (m Move) IsPawnDoublePush() bool

IsPawnDoublePush returns true if the move represents a pawn double push.

func (Move) IsPromotion

func (m Move) IsPromotion() bool

IsPromotion returns true if the move represents a pawn promotion.

func (Move) IsQueensideCastling

func (m Move) IsQueensideCastling() bool

IsQueensideCastling returns true if the move represents queenside castling.

func (Move) PromoteTo

func (m Move) PromoteTo() chess.PromoteTo

PromoteTo returns which piece the move should promote to, or PieceNone.

func (Move) SAN

func (m Move) SAN() string

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.

func (Move) To

func (m Move) To() uint8

To returns the to index for the move.

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.

func (Piece) Rune

func (p Piece) Rune() rune

Rune returns a rune that uniquely represents the piece colour and type.

type SearchStatus

type SearchStatus struct {
	Depth              uint8
	Time               time.Duration
	PrincipalVariation []Move
	NodesPerSecond     uint64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL