package module
Version: v0.0.0-...-e79413b Latest Latest

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

Go to latest
Published: Sep 17, 2022 License: GPL-3.0 Imports: 7 Imported by: 11



dragontoothmg is a fast chess legal move generator library based on magic bitboards.



View Source
const (
	Nothing = iota
	Pawn    = iota
	Knight  = iota // list before bishop for promotion loops
	Bishop  = iota
	Rook    = iota
	Queen   = iota
	King    = iota
View Source
const Startpos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

The starting position FEN


This section is empty.


func AlgebraicToIndex

func AlgebraicToIndex(alg string) (uint8, error)

Accepts an algebraic notation chess square, and converts it to a square ID as used by Dragontooth (in both the board and move types).

func CalculateBishopMoveBitboard

func CalculateBishopMoveBitboard(currBishop uint8, allPieces uint64) uint64

Calculates the attack bitboard for a bishop. This might include targeted squares that are actually friendly pieces, so the proper usage is: bishopTargets := CalculateBishopMoveBitboard(myBishopLoc, allPieces) & (^myPieces) Externally useful for evaluation functions.

func CalculateRookMoveBitboard

func CalculateRookMoveBitboard(currRook uint8, allPieces uint64) uint64

Calculates the attack bitboard for a rook. This might include targeted squares that are actually friendly pieces, so the proper usage is: rookTargets := CalculateRookMoveBitboard(myRookLoc, allPieces) & (^myPieces) Externally useful for evaluation functions.

func Divide

func Divide(b *Board, n int)

Performs the Perft move count division operation. Useful for debugging.

func GetPieceType

func GetPieceType(square uint8, b *Board) (int, bool)

func IndexToAlgebraic

func IndexToAlgebraic(id Square) string

Accepts a Dragontooth Square ID, and converts it to an algebraic square.

func IsCapture

func IsCapture(m Move, b *Board) bool

func Perft

func Perft(b *Board, n int) int64

Run perft to count the number of moves. Useful for testing and benchmarking.


type Bitboards

type Bitboards struct {
	Pawns   uint64
	Bishops uint64
	Knights uint64
	Rooks   uint64
	Queens  uint64
	Kings   uint64
	All     uint64

Contains bitboard representations of all the pieces for a side.

type Board

type Board struct {
	Wtomove bool

	Halfmoveclock uint8
	Fullmoveno    uint16
	White         Bitboards
	Black         Bitboards
	// contains filtered or unexported fields

The board type, which uses little-endian rank-file mapping.

func ParseFen

func ParseFen(fen string) Board

Parse a board from a FEN string.

func (*Board) Apply

func (b *Board) Apply(m Move) func()

Applies a move to the board, and returns a function that can be used to unapply it. This function assumes that the given move is valid (i.e., is in the set of moves found by GenerateLegalMoves()). If the move is not valid, this function has undefined behavior.

func (*Board) GenerateLegalMoves

func (b *Board) GenerateLegalMoves() []Move

The main API entrypoint. Generates all legal moves for a given board.

func (*Board) Hash

func (b *Board) Hash() uint64

Return the Zobrist hash value for the board. The hash value does NOT change with the turn number, nor the draw move counter. All other elements of the Board type affect the hash. This function is cheap to call, since the hash is incrementally updated.

func (*Board) OurKingInCheck

func (b *Board) OurKingInCheck() bool

func (*Board) ToFen

func (b *Board) ToFen() string

Serializes a board position to a Fen string.

func (*Board) UnderDirectAttack

func (b *Board) UnderDirectAttack(byBlack bool, origin uint8) bool

Determine if a square is under attack. Potentially expensive.

type Move

type Move uint16

Move bitwise structure; internal implementation is private.

func ParseMove

func ParseMove(movestr string) (Move, error)

Some example valid move strings: e1e2 b4d6 e7e8q a2a1n TODO(dylhunn): Make the parser more forgiving. Eg: 0-0, O-O-O, a2-a3, D3D4

func (*Move) From

func (m *Move) From() uint8

func (*Move) Promote

func (m *Move) Promote() Piece

Whether the move involves promoting a pawn.

func (*Move) Setfrom

func (m *Move) Setfrom(s Square) *Move

func (*Move) Setpromote

func (m *Move) Setpromote(p Piece) *Move

func (*Move) Setto

func (m *Move) Setto(s Square) *Move

func (*Move) String

func (m *Move) String() string

func (*Move) To

func (m *Move) To() uint8

type Piece

type Piece uint8

Piece types; valid in range 0-6, as indicated by the constants for each piece.

type Square

type Square uint8

Square index values from 0-63.



  • This FEN parsing implementation doesn't handle malformed inputs.


Path Synopsis

Jump to

Keyboard shortcuts

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