board

package
v0.0.0-...-4c31ed0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Pieces movements in a chess board

Index

Constants

View Source
const (
	Q    = castling(0b0100)
	Qq   = castling(0b0101)
	Qk   = castling(0b0110)
	Qkq  = castling(0b0111)
	K    = castling(0b1000)
	Kq   = castling(0b1001)
	Kk   = castling(0b1010)
	Kkq  = castling(0b1011)
	KQ   = castling(0b1100)
	KQq  = castling(0b1101)
	KQk  = castling(0b1110)
	KQkq = castling(0b1111)
)
View Source
const (
	Normal = iota
	Castle
	PawnDoublePush
	EnPassant
	Promotion
	Capture
)

Type of move

View Source
const (
	NORTH     uint64 = 0
	NORTHEAST uint64 = 1
	EAST      uint64 = 2
	SOUTHEAST uint64 = 3
	SOUTH     uint64 = 4
	SOUTHWEST uint64 = 5
	WEST      uint64 = 6
	NORTHWEST uint64 = 7
	INVALID   uint64 = 8
)

Constants for orthogonal directions in the board

Variables

This section is empty.

Functions

func Bsf

func Bsf(bitboard Bitboard) int

bsf (bit scan forward) returns the bit-index of the least significant 1 bit (LS1B) in an integer Bitboard(uint64)

func Bsr

func Bsr(bitboard Bitboard) int

bsr (bit scan reverse) returns the bit-index of the most significant 1 bit (MS1B) in an integer Bitboard(uint64)

Types

type Bitboard

type Bitboard uint64

Bitboard represents a bitboard as a 64bit integer

const AllSquares Bitboard = 0xFFFFFFFFFFFFFFFF

func Attacks

func Attacks(piece Piece, from Bitboard, pos *Position) (attacks Bitboard)

func (*Bitboard) NextBit

func (b *Bitboard) NextBit() (bb Bitboard)

NextBit removes the next bit of the bitboard and returns it

func (Bitboard) Print

func (b Bitboard) Print()

Print draws a Bitboard in the terminal in a 'prettier' way

type Color

type Color int

Color is an int referencing the white or black player in chess

const (
	White Color = iota
	Black
)

func (Color) Opponent

func (c Color) Opponent() Color

Opponent returns the Opponent color to the actual color

type Move

type Move uint64

func (*Move) CapturedPiece

func (m *Move) CapturedPiece() Piece

CapturedPiece

func (*Move) MoveType

func (m *Move) MoveType() int

MoveType returns the type of move

func (*Move) Piece

func (m *Move) Piece() int

Piece returns the Piece which is being moved

func (*Move) Score

func (m *Move) Score() int

Score returns the Score evaluation after the move has been made

func (*Move) SetScore

func (m *Move) SetScore(score int) *Move

SetScore sets the score passed in centipawns evaluation

func (*Move) To

func (m *Move) To() int

To returns the number of the destination square of the move

func (*Move) ToUci

func (m *Move) ToUci() (uciString string)

ToUci returns the move in UCI format (starting square string -> destinatnion square string)

type Piece

type Piece int

Piece is an int that references piece role/color for bitboards in position struct

const (
	WhiteKing Piece = iota
	WhiteQueen
	WhiteRook
	WhiteBishop
	WhiteKnight
	WhitePawn
	BlackKing
	BlackQueen
	BlackRook
	BlackBishop
	BlackKnight
	BlackPawn
	// Constants for reference to piece type
	King
	Queen
	Rook
	Bishop
	Knight
	Pawn
)

type Position

type Position struct {
	// Bitboards piece order -> King, Queen, Rook, Bishop, Knight, Pawn (first white, second black)
	Bitboards [12]Bitboard
	Turn      Color
	Hash      uint64
	// contains filtered or unexported fields
}

Position contains all information about a chess position

func EmptyPosition

func EmptyPosition() (pos *Position)

EmptyPosition returns an empty Position struct

func From

func From(fen string) (pos *Position)

From creates a new Position struct from a fen string

func InitialPosition

func InitialPosition() (pos *Position)

InitialPosition is a factory that returns an initial postion board

func (*Position) AddPiece

func (pos *Position) AddPiece(piece Piece, square string)

addPiece adds to the position the Piece passed in the square passed

func (*Position) AttackedSquares

func (pos *Position) AttackedSquares(side Color) (attackedSquares Bitboard)

attackedSquares returns a bitboard with all squares attacked by the passed side

func (*Position) Check

func (pos *Position) Check(side Color) (inCheck bool)

Check returns if the side passed is in check

func (*Position) CheckingPieces

func (pos *Position) CheckingPieces(side Color) (kingAttackers Bitboard)

checkingPieces returns an Bitboard of squares that contain pieces attacking the king of the side passed

func (*Position) Checkmate

func (pos *Position) Checkmate(side Color) (checkmate bool)

Checkmate returns if the passed side is in checkmate on the current position

func (*Position) Divide

func (pos *Position) Divide(depth int) (divide string)

Divide, a variation of Perft, returns the perft of all moves in the current position

func (*Position) EmptySquares

func (pos *Position) EmptySquares() (emptySquares Bitboard)

emptySquares returns a Bitboard with the empty sqaures of the position

func (*Position) InsuficientMaterial

func (pos *Position) InsuficientMaterial() bool

func (*Position) KingPosition

func (pos *Position) KingPosition(side Color) (king Bitboard)

KingPosition returns the bitboard of the passed side king

func (*Position) LegalMoves

func (pos *Position) LegalMoves(side Color) (legalMoves []Move)

LegalMoves returns an slice of Move of all legal moves for the side passed

func (*Position) MakeMove

func (pos *Position) MakeMove(move *Move)

MakeMove updates the position by making the move passed as parameter

func (*Position) Perft

func (pos *Position) Perft(depth int) (nodes uint64)

Perft returns all the legal moves up to the passed depth

func (*Position) PieceAt

func (pos *Position) PieceAt(square string) (piece Piece, e error)

pieceAt returns a Piece at the given square coordinate in the Position or error

func (*Position) Pieces

func (pos *Position) Pieces(side Color) (pieces Bitboard)

Pieces returns a Bitboard with the pieces of the color pased

func (*Position) RemovePiece

func (pos *Position) RemovePiece(piece Bitboard)

Remove Piece returns a new position without the piece passed

func (*Position) Stealmate

func (pos *Position) Stealmate(side Color) (stealmate bool)

Stealmate returns if the passed side is in stealmate on the current position

func (*Position) String

func (pos *Position) String() string

Print prints the Position to the terminal from white's view perspective

func (*Position) ToFen

func (pos *Position) ToFen() (fen string)

ToFen serializes the position as a fen string

func (*Position) UnmakeMove

func (pos *Position) UnmakeMove(move Move)

UnmakeMove undoes a the passed move in the postion

Jump to

Keyboard shortcuts

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