Documentation ¶
Overview ¶
Package position represents data structures and functions for a chess board and its position. It uses a 8x8 piece board and bitboards, a stack for undo moves, zobrist keys for transposition tables, piece lists, material and positional value counter.
Create a new instance with NewPosition(...) with no parameters to get the chess start position.
Index ¶
- Constants
- func NewRandom(seed uint64) random
- type Key
- type Position
- func (p *Position) CastlingRights() CastlingRights
- func (p *Position) CheckRepetitions(reps int) bool
- func (p *Position) DoMove(m Move)
- func (p *Position) DoNullMove()
- func (p *Position) GamePhase() int
- func (p *Position) GamePhaseFactor() float64
- func (p *Position) GetEnPassantSquare() Square
- func (p *Position) GetPiece(sq Square) Piece
- func (p *Position) GivesCheck(move Move) bool
- func (p *Position) HalfMoveClock() int
- func (p *Position) HasCheck() bool
- func (p *Position) HasInsufficientMaterial() bool
- func (p *Position) IsAttacked(sq Square, by Color) bool
- func (p *Position) IsCapturingMove(move Move) bool
- func (p *Position) IsLegalMove(move Move) bool
- func (p *Position) KingSquare(c Color) Square
- func (p *Position) LastCapturedPiece() Piece
- func (p *Position) LastMove() Move
- func (p *Position) Material(c Color) Value
- func (p *Position) MaterialNonPawn(c Color) Value
- func (p *Position) NextPlayer() Color
- func (p *Position) OccupiedAll() Bitboard
- func (p *Position) OccupiedBb(c Color) Bitboard
- func (p *Position) PiecesBb(c Color, pt PieceType) Bitboard
- func (p *Position) PsqEndValue(c Color) Value
- func (p *Position) PsqMidValue(c Color) Value
- func (p *Position) String() string
- func (p *Position) StringBoard() string
- func (p *Position) StringFen() string
- func (p *Position) UndoMove()
- func (p *Position) UndoNullMove()
- func (p *Position) WasCapturingMove() bool
- func (p *Position) WasLegalMove() bool
- func (p *Position) ZobristKey() Key
Constants ¶
const ( // StartFen is a string with the fen position for a standard chess game StartFen string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Key ¶
type Key uint64
Key is used for zobrist keys in chess positions. Zobrist keys need all 64 bits for distribution
type Position ¶
type Position struct {
// contains filtered or unexported fields
}
Position This struct represents the chess board and its position. It uses a 8x8 piece board and bitboards, a stack for undo moves, zobrist keys for transposition tables, piece lists, material and positional value counter.
Needs to be created with NewPosition() or NewPosition(fen string)
func NewPosition ¶
NewPosition creates a new position. When called without an argument the position will have the start position When a fen string is given it will create a position with based on this fen. Additional fens/strings are ignored
func NewPositionFen ¶
NewPositionFen creates a new position with the given fen string as board position It returns nil and an error if the fen was invalid.
func (*Position) CastlingRights ¶
func (p *Position) CastlingRights() CastlingRights
CastlingRights returns the castling rights instance of the position
func (*Position) CheckRepetitions ¶
CheckRepetitions Repetition of a position:. To detect a 3-fold repetition the given position must occur at least 2 times before:<br/> <code>position.checkRepetitions(2)</code> checks for 3 fold-repetition <p> 3-fold repetition: This most commonly occurs when neither side is able to avoid repeating moves without incurring a disadvantage. The three occurrences of the position need not occur on consecutive moves for a claim to be valid. FIDE rules make no mention of perpetual check; this is merely a specific type of draw by threefold repetition.
Return true if this position has been played reps times before
func (*Position) DoMove ¶
func (p *Position) DoMove(m Move)
DoMove commits a move to the board. Due to performance there is no check if this move is legal on the current position. Legal check needs to be done beforehand or after in case of pseudo legal moves. Usually the move will be generated by a MoveGenerator and therefore the move will be assumed legal anyway.
func (*Position) DoNullMove ¶
func (p *Position) DoNullMove()
DoNullMove is used in Null Move Pruning. The position is basically unchanged but the next player changes. The state before the null move will be stored to history. The history entry will be changed. So in effect after an UndoNullMove() the external view on the position is unchanged (e.g. fenBeforeNull == fenAfterNull and zobristBeforeNull == zobristAfterNull but positionBeforeNull != positionAfterNull.
func (*Position) GamePhase ¶
GamePhase returns the current game phase value of the position. GamePhase is 24 at the start of the game (24 is also the max). End games when no officers are left have a GamePhase value of 0.
func (*Position) GamePhaseFactor ¶
GamePhaseFactor returns a factor between 0 and 1 which reflects the ratio between the actual game phase and the max game phase
func (*Position) GetEnPassantSquare ¶
func (p *Position) GetEnPassantSquare() Square
GetEnPassantSquare returns the en passant square or SqNone if not set
func (*Position) GetPiece ¶
func (p *Position) GetPiece(sq Square) Piece
GetPiece returns the piece on the given square. Empty squares are initialized with PieceNone and return the same.
func (*Position) GivesCheck ¶
GivesCheck determines if the given move will give check to the opponent of p.NextPlayer() and returns true if so.
func (*Position) HalfMoveClock ¶
HalfMoveClock returns the positions half move clock
func (*Position) HasCheck ¶
HasCheck returns true if the next player is threatened by a check (king is attacked). This is cached for the current position. Multiple calls to this on the same position are therefore very efficient.
func (*Position) HasInsufficientMaterial ¶
HasInsufficientMaterial returns true if no side has enough material to force a mate (does not exclude combination where a helpmate would be possible, e.g. the opponent needs to support a mate by mistake)
func (*Position) IsAttacked ¶
IsAttacked checks if the given square is attacked by a piece of the given color.
func (*Position) IsCapturingMove ¶
IsCapturingMove determines if a move on this position is a capturing move incl. en passant
func (*Position) IsLegalMove ¶
IsLegalMove tests a move if it is legal on the current position. Basically tests if the king would be left in check after the move or if the king crosses an attacked square during castling.
func (*Position) KingSquare ¶
func (p *Position) KingSquare(c Color) Square
KingSquare returns the current square of the king of color c
func (*Position) LastCapturedPiece ¶
func (p *Position) LastCapturedPiece() Piece
LastCapturedPiece returns the captured piece of the the last move made on the position or MoveNone if the move was non-capturing or the position has no history of earlier moves.
func (*Position) LastMove ¶
func (p *Position) LastMove() Move
LastMove returns the last move made on the position or MoveNone if the position has no history of earlier moves.
func (*Position) Material ¶
func (p *Position) Material(c Color) Value
Material returns the material value for the given color on this position
func (*Position) MaterialNonPawn ¶
func (p *Position) MaterialNonPawn(c Color) Value
MaterialNonPawn returns the non pawn material value for given color
func (*Position) NextPlayer ¶
func (p *Position) NextPlayer() Color
NextPlayer returns the next player as Color for the position
func (*Position) OccupiedAll ¶
func (p *Position) OccupiedAll() Bitboard
OccupiedAll returns a Bitboard of all pieces currently on the board
func (*Position) OccupiedBb ¶
func (p *Position) OccupiedBb(c Color) Bitboard
OccupiedBb returns a Bitboard f all pieces of Color c
func (*Position) PiecesBb ¶
func (p *Position) PiecesBb(c Color, pt PieceType) Bitboard
PiecesBb returns the Bitboard for the given piece type of the given color
func (*Position) PsqEndValue ¶
func (p *Position) PsqEndValue(c Color) Value
PsqEndValue returns the positional value for the given color for later game phases. Best used together with a game phase factor
func (*Position) PsqMidValue ¶
func (p *Position) PsqMidValue(c Color) Value
PsqMidValue returns the positional value for the given color for early game phases. Best used together with a game phase factor
func (*Position) String ¶
String returns a string representing the board instance. This includes the fen, a board matrix, game phase, material and pos values.
func (*Position) StringBoard ¶
StringBoard returns a visual matrix of the board and pieces
func (*Position) UndoMove ¶
func (p *Position) UndoMove()
UndoMove resets the position to a state before the last move has been made
func (*Position) UndoNullMove ¶
func (p *Position) UndoNullMove()
UndoNullMove restores the state of the position to before the DoNullMove() call. The history entry will be changed but the history counter reset. So in effect the external view on the position is unchanged (e.g. fenBeforeNull == fenAfterNull and zobristBeforeNull == zobristAfterNull but positionBeforeNull != positionAfterNull If positionBeforeNull != positionAfterNull would be required this function would have to be changed to reset the history entry as well. Currently this is not necessary and therefore we spare the time to do this.
func (*Position) WasCapturingMove ¶
WasCapturingMove returns true if the last move was a capturing move.
func (*Position) WasLegalMove ¶
WasLegalMove tests if the last move was legal. Basically tests if the king is now in check or if the king crossed an attacked square during castling or of there was a castling although in check. If the position does not have a last move (history empty) this will only check if the king of the opponent is attacked e.g. could now be captured by the next player.
func (*Position) ZobristKey ¶
ZobristKey returns the current zobrist key for this position