movegen

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package movegen contains functionality to create moves on a chess position. It implements several variants like generate pseudo legal moves, legal moves or on demand generation of pseudo legal moves.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenMode

type GenMode int

GenMode generation modes for on demand move generation.

const (
	GenZero     GenMode = 0b00
	GenNonQuiet GenMode = 0b01
	GenQuiet    GenMode = 0b10
	GenAll      GenMode = 0b11
)

GenMode generation modes for on demand move generation.

type Movegen

type Movegen struct {
	// contains filtered or unexported fields
}

Movegen data structure. Create new move generator via

movegen.NewMoveGen()

Creating this directly will not work.

func NewMoveGen

func NewMoveGen() *Movegen

NewMoveGen creates a new instance of a move generator This is the only time when we allocate new memory. The instance will not create any move lists during normal move generation as it will reuse pre-created internal lists which will be returned via pointer to a caller. OBS: Be careful when trying to store the list of generated moves as the underlying list will be changed when move gen is called again. A deep copy is necessary if you need a copy of the move list. For a deep copy use:

moveslice.MoveSlice.Clone()

func (*Movegen) GenerateLegalMoves

func (mg *Movegen) GenerateLegalMoves(position *position.Position, mode GenMode) *moveslice.MoveSlice

GenerateLegalMoves generates legal moves for the next player. Uses GeneratePseudoLegalMoves and filters out illegal moves. Usually only used for root moves generation as this is expensive. During the AlphaBeta search we will only use pseudo legal move generation.

func (*Movegen) GeneratePseudoLegalMoves

func (mg *Movegen) GeneratePseudoLegalMoves(p *position.Position, mode GenMode, evasion bool) *moveslice.MoveSlice

GeneratePseudoLegalMoves generates pseudo moves for the next player. Does not check if king is left in check or if it passes an attacked square when castling or has been in check before castling.

If a PV move is set with setPV(Move pv) this move will be returned first and will not be returned at its normal place.

Killer moves will be played as soon as possible after non quiet moves. As Killer moves are stored for the whole ply a Killer move might not be valid for the current position. Therefore we need to wait until they are generated. Killer moves will then be pushed to the top of the the quiet moves.

Evasion is a parameter given when the position is in check and only evasion moves should be generated. For testing purposes this is a parameter but obviously we could determine checks very quickly internally in this function. The idea of evasion is to avoid generating moves which are obviously not getting the king out of check. This may reduce the total number of generated moves but there might still be a few non legal moves. This is the case if considering and calculating all possible scenarios is more expensive than to just generate the move and dismiss is later. Because of beta cuts off we quite often will never have to check the full legality of these moves anyway.

func (*Movegen) GetMoveFromSan

func (mg *Movegen) GetMoveFromSan(posPtr *position.Position, sanMove string) Move

GetMoveFromSan Generates all legal moves and matches the given SAN move string against them. If there is a match the actual move is returned. Otherwise MoveNone is returned.

As this uses string creation and comparison this is not very efficient. Use only when performance is not critical.

func (*Movegen) GetMoveFromUci

func (mg *Movegen) GetMoveFromUci(posPtr *position.Position, uciMove string) Move

GetMoveFromUci Generates all legal moves and matches the given UCI move string against them. If there is a match the actual move is returned. Otherwise MoveNone is returned.

As this uses string creation and comparison this is not very efficient. Use only when performance is not critical.

func (*Movegen) GetNextMove

func (mg *Movegen) GetNextMove(p *position.Position, mode GenMode, evasion bool) Move

GetNextMove is the main function for phased generation of pseudo legal moves. It returns the next move for the given position and will usually be called in a loop during search. As we hope for an early beta cut this will save time as not all moves will have been generated.

To reuse this on the same position a call to ResetOnDemand() is necessary. This is not necessary when a different position is called as this func will reset it self in this case.

If a PV move is set with setPV(Move pv) this will be returned first and will not be returned at its normal place.

Killer moves will be played as soon as possible. As Killer moves are stored for the whole ply a Killer move might not be valid for the current position. Therefore we need to wait until they are generated by the phased move generation. Killers will then be pushed to the top of the list of the generation stage.

Evasion is a parameter given when the position is in check and only evasion moves should be generated. For testing purposes this is a parameter for now but obviously we could determine checks very quickly internally in this function. The idea of evasion is to avoid generating moves which are obviously not getting the king out of check. This may reduce the total number of generated moves but there might still be a few non legal moves. This is the case if considering and calculating all possible scenarios is more expensive than to just generate the move and dismiss is later. Because of beta cuts off we quite often will never have to check the full legality of these moves anyway.

func (*Movegen) HasLegalMove

func (mg *Movegen) HasLegalMove(position *position.Position) bool

HasLegalMove determines if we have at least one legal move. We only have to find one legal move. We search for any KING, PAWN, KNIGHT, BISHOP, ROOK, QUEEN move and return immediately if we found one. The order of our search is approx from the most likely to the least likely.

func (*Movegen) KillerMoves

func (mg *Movegen) KillerMoves() *[2]Move

KillerMoves returns a pointer to the killer moves array

func (*Movegen) PvMove

func (mg *Movegen) PvMove() Move

PvMove returns the current PV move

func (*Movegen) ResetOnDemand

func (mg *Movegen) ResetOnDemand()

ResetOnDemand resets the move on demand generator to start fresh. Also deletes Killer and PV moves.

func (*Movegen) SetHistoryData added in v1.0.0

func (mg *Movegen) SetHistoryData(historyData *history.History)

SetHistoryData provides a pointer to the search's history data for the move generator so it can optimize sorting.

func (*Movegen) SetPvMove

func (mg *Movegen) SetPvMove(move Move)

SetPvMove sets a PV move which should be returned first by the OnDemand MoveGenerator.

func (*Movegen) StoreKiller

func (mg *Movegen) StoreKiller(move Move)

StoreKiller provides the on demand move generator with a new killer move which should be returned as soon as possible when generating moves with the on demand generator.

func (*Movegen) String

func (mg *Movegen) String() string

String returns a string representation of a MoveGen instance

func (*Movegen) ValidateMove

func (mg *Movegen) ValidateMove(p *position.Position, move Move) bool

ValidateMove validates if a move is a valid legal move on the given position

type Perft

type Perft struct {
	Nodes            uint64
	CheckCounter     uint64
	CheckMateCounter uint64
	CaptureCounter   uint64
	EnpassantCounter uint64
	CastleCounter    uint64
	PromotionCounter uint64
	// contains filtered or unexported fields
}

Perft is class to test move generation of the chess engine.

func NewPerft

func NewPerft() *Perft

NewPerft creates a new empty Perft instance

func (*Perft) StartPerft

func (perft *Perft) StartPerft(fen string, depth int, onDemandFlag bool)

StartPerft is using normal or on demand move generation and doesn't divide the the perft depths. If this has been started in a go routine it can be stopped via Stop()

func (*Perft) StartPerftMulti

func (perft *Perft) StartPerftMulti(fen string, startDepth int, endDepth int, onDemandFlag bool)

StartPerftMulti is using normal or on demand move generation and doesn't divide the the perft depths. It iterates through the given start to end depths. If this has been started in a go routine it can be stopped via Stop()

func (*Perft) Stop

func (perft *Perft) Stop()

Stop can be used when perft has been started in a goroutine to stop the currently running perft test

Jump to

Keyboard shortcuts

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