chess

package
v0.0.0-...-9a4bc7e Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package chess implements basic chess-related functionality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitboard

type Bitboard uint64

Bitboard is a bitset of squares. From LSB to MSB, the bits represent A1, B1, ..., H1, A2, ..., G8, H8.

func (*Bitboard) Clear

func (b *Bitboard) Clear(s Square)

Clear clears the bit at the given square.

func (*Bitboard) Get

func (b *Bitboard) Get(s Square) bool

Get returns true if the bit at the given square is set.

func (*Bitboard) IsEmpty

func (b *Bitboard) IsEmpty() bool

IsEmpty returns true if all bits are 0.

func (*Bitboard) IsFull

func (b *Bitboard) IsFull() bool

IsFull returns true if all bits are 1.

func (*Bitboard) Reset

func (b *Bitboard) Reset()

Reset sets all bits to 0.

func (*Bitboard) Set

func (b *Bitboard) Set(s Square)

Set sets the bit at the given square.

func (*Bitboard) Square

func (b *Bitboard) Square() Square

Square returns the square corresponding to the least significant set bit. Calling Square on an empty Bitboard returns an invalid square.

Example
var b Bitboard

b.Set(G1)
fmt.Println(b.Square() == G1)

b.Set(A7)
fmt.Println(b.Square() == G1)
Output:

true
true

func (*Bitboard) Toggle

func (b *Bitboard) Toggle(s Square)

Toggle toggles the bit at the given square.

type Board

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

func NewBoard

func NewBoard() Board

NewBoard returns a new board with all pieces in their starting positions.

func (*Board) At

func (b *Board) At(s Square) (Piece, bool)

At returns the piece on the square, if any.

func (*Board) ByColor

func (b *Board) ByColor(c Color) Bitboard

ByColor returns a bitboard of pieces by color.

func (*Board) ByRole

func (b *Board) ByRole(r Role) Bitboard

func (*Board) IsValid

func (b *Board) IsValid() error

IsValid returns nil if the board is valid.

func (*Board) KingOf

func (b *Board) KingOf(c Color) Square

KingOf returns the square of the king of the given color.

func (*Board) Put

func (b *Board) Put(p Piece, s Square)

Put puts a piece on a square. Any piece already on the square is removed.

func (*Board) PutDangerous

func (b *Board) PutDangerous(p Piece, s Square)

PutDangerous puts a piece on a square without checking if a different piece already exists there. It is faster than Board.Put.

func (*Board) Remove

func (b *Board) Remove(s Square)

Remove removes a piece from the square, if any.

type CastleRights

type CastleRights uint8

CastleRights is a bitset of available castle rights. The zero value indicates no castle rights are available.

const (
	WhiteOO CastleRights = 1 << iota
	WhiteOOO
	BlackOO
	BlackOOO
)

Castle rights constants.

func NewCastleRights

func NewCastleRights() CastleRights

func (*CastleRights) Add

func (c *CastleRights) Add(other CastleRights)

func (*CastleRights) Contains

func (c *CastleRights) Contains(other CastleRights) bool

func (*CastleRights) IsValid

func (c *CastleRights) IsValid() bool

func (*CastleRights) Remove

func (c *CastleRights) Remove(other CastleRights)

type Color

type Color bool

Color is the color of a player, piece, square, or other item. A Color is either White or Black.

const (
	White Color = false
	Black Color = true
)

Color constants.

func (Color) String

func (c Color) String() string

type File

type File uint8

File is a file on a chess board.

const (
	FileA File = iota
	FileB
	FileC
	FileD
	FileE
	FileF
	FileG
	FileH
)

Board files.

func (File) IsValid

func (f File) IsValid() bool

func (File) String

func (f File) String() string

type Move

type Move struct {
	// The starting square. When castling, this is the king's starting square.
	From Square

	// The destination square. When castling, this is the king's destination square.
	To Square

	// Promotion information. For non-promotion moves, this is [NoPromotion].
	PromotionInfo PromotionInfo
}

Move is a chess move. The zero value for Move is a null move.

func NewMove

func NewMove(s string) (Move, error)

NewMove accepts UCI-compatible long algebraic notation (LAN) and returns the corresponding move.

Example
m, _ := NewMove("e2e4")
fmt.Printf("%+v\n", m)

m, _ = NewMove("e7e8q") // promotion
fmt.Printf("%+v\n", m)

m, _ = NewMove("e1c1") // white short castling
fmt.Printf("%+v\n", m)
Output:

{From:E2 To:E4 PromotionInfo:NoPromotion}
{From:E7 To:E8 PromotionInfo:QueenPromotion}
{From:E1 To:C1 PromotionInfo:NoPromotion}

type Piece

type Piece struct {
	Color Color
	Role  Role
}

Piece is a chess piece.

func (Piece) IsValid

func (p Piece) IsValid() bool

IsValid returns true if the piece is valid.

type Position

type Position struct {
	Board        Board
	SideToMove   Color
	CastleRights CastleRights

	EnPassantFlag   bool   // Whether the previous move was a double pawn push.
	EnPassantSquare Square // The en passant square. Only valid if enPassantFlag is true.

	FullMoveNumber uint16 // Number of full moves. Starts at 1 and increments after Black moves.
	HalfMoveClock  uint8  // Number of plies since last capture or pawn move.
}

Position is a chess position.

func NewPosition

func NewPosition() Position

NewPosition returns a new starting position.

func (*Position) IsLegalMove

func (p *Position) IsLegalMove(m Move) bool

IsLegalMove returns true if the move is legal in the position. It does not account for insufficient material or three-fold repetition.

func (*Position) IsValid

func (p *Position) IsValid() error

IsValid returns nil if the position is valid.

func (*Position) LegalMoves

func (p *Position) LegalMoves() []Move

LegalMoves returns a list of legal moves.

func (*Position) Move

func (p *Position) Move(m Move) *Undo

Move updates the position by making a move. It returns information that can be used to undo the move.

The move must be legal by the definition of Position.IsLegalMove. If not, behavior is undefined.

func (*Position) Undo

func (p *Position) Undo(u *Undo)

Undo undoes a Position.Move call.

type PromotionInfo

type PromotionInfo uint8

PromotionInfo represents promotion information for a move.

const (
	NoPromotion PromotionInfo = iota
	KnightPromotion
	BishopPromotion
	RookPromotion
	QueenPromotion
)

PromotionInfo constants.

func (PromotionInfo) IsValid

func (p PromotionInfo) IsValid() bool

IsValid returns true if p is a package-defined PromotionInfo constant.

func (PromotionInfo) Role

func (p PromotionInfo) Role() (r Role, ok bool)

Role returns the role that corresponds to the promotion information. For NoPromotion, ok is false.

func (PromotionInfo) String

func (p PromotionInfo) String() string

type Rank

type Rank uint8

Rank is a rank on a chess board. Note that Rank1 = 0.

const (
	Rank1 Rank = iota
	Rank2
	Rank3
	Rank4
	Rank5
	Rank6
	Rank7
	Rank8
)

Board ranks.

func (Rank) IsValid

func (r Rank) IsValid() bool

func (Rank) String

func (r Rank) String() string

type Role

type Role uint8

Role is the role of a piece.

const (
	Pawn Role = iota
	Knight
	Bishop
	Rook
	Queen
	King
)

func (Role) IsValid

func (r Role) IsValid() bool

func (Role) String

func (r Role) String() string

type Square

type Square uint8

Square is a square on a chess board.

const (
	A1 Square = 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
)

Board squares.

func SquareAt

func SquareAt(f File, r Rank) Square

func (Square) Bitboard

func (s Square) Bitboard() Bitboard

Bitboard returns a bitboard with only this square set.

func (Square) File

func (s Square) File() File

File returns the file of the square.

func (Square) IsAdjacentTo

func (s Square) IsAdjacentTo(other Square) bool

IsAdjacentTo returns true if the square is adjacent to the other square.

func (Square) IsValid

func (s Square) IsValid() bool

IsValid returns true if the square is between A1 and H8 inclusive.

func (Square) Rank

func (s Square) Rank() Rank

Rank returns the rank of the square.

func (Square) String

func (s Square) String() string

type Undo

type Undo struct {
	Move Move // The move to undo.

	WasCapture   bool // Was the move a capture?
	CapturedRole Role // Role of the captured piece, if any.

	EnPassantFlag   bool   // Was the move to undo preceded by a double pawn push?
	EnPassantSquare Square // En passant square, if any.

	CastleRights CastleRights // Previous castle rights.
}

Undo contains information about a move that can be used to undo it.

Jump to

Keyboard shortcuts

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