chess

package
v0.0.0-...-0d626b8 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package chess provides basic chess constants and functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidCoords

func IsValidCoords(f File, r Rank) bool

IsValidCoords returns true if the given coordinates reference a valid square.

Types

type Bitboard

type Bitboard uint64

Bitboard is an integer where each bit represents one square. From LSB to MSB, the bits represent a1, b1, ..., h1, a2, ..., h8.

func BishopAttacks

func BishopAttacks(s Square, occupied Bitboard) Bitboard

BishopAttacks returns the squares a bishop can attack from a given square, accounting for occupied squares.

func QueenAttacks

func QueenAttacks(s Square, occupied Bitboard) Bitboard

QueenAttacks returns the squares a queen can attack from a given square, accounting for occupied squares.

func RookAttacks

func RookAttacks(s Square, occupied Bitboard) Bitboard

RookAttacks returns the squares a rook can attack from a given square, accounting for occupied squares.

func Targets

func Targets(p Piece, s Square) Bitboard

Targets returns the squares a piece can target from a given square. For example, Targets(WhitePawn, A2) returns a bitboard with A3, B3, and A4 set.

func (*Bitboard) Assign

func (b *Bitboard) Assign(s Square, cond bool)

Assign sets the bit at s to 1 if cond is true or 0 if cond is false.

func (*Bitboard) Clear

func (b *Bitboard) Clear(s Square)

Clear clears the bit at s to 0.

func (*Bitboard) ClearIf

func (b *Bitboard) ClearIf(s Square, cond bool)

ClearIf clears the bit at s to 0 if cond is true. If cond is false, the bit is left unchanged.

func (*Bitboard) DebugString

func (b *Bitboard) DebugString() string

DebugString returns a multi-line string representation of the bitboard.

func (*Bitboard) Get

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

Get returns true if the bit at s is 1.

func (*Bitboard) Intersects

func (b *Bitboard) Intersects(other Bitboard) bool

Intersects returns true if two bitboards have any bits in common.

func (*Bitboard) IsEmpty

func (b *Bitboard) IsEmpty() bool

IsEmpty returns true if the bitboard has no bits set.

func (*Bitboard) Mirror

func (b *Bitboard) Mirror()

Mirror mirrors the represented board vertically. For example, the bit at A1 is now at A8.

func (*Bitboard) Set

func (b *Bitboard) Set(s Square)

Set sets the bit at s to 1.

func (*Bitboard) SetIf

func (b *Bitboard) SetIf(s Square, cond bool)

SetIf sets the bit at s to 1 if cond is true. If cond is false, the bit is left unchanged.

func (*Bitboard) Toggle

func (b *Bitboard) Toggle(s Square)

Toggle toggles the bit at s.

type CastleRight

type CastleRight uint8

CastleRight represents a single castle right.

const (
	WhiteShortCastleRight CastleRight = 1 << iota
	WhiteLongCastleRight
	BlackShortCastleRight
	BlackLongCastleRight
)

func (CastleRight) String

func (c CastleRight) String() string

type CastleRights

type CastleRights uint8

CastleRights represents the available castle rights of both players.

const (
	// NoCastleRights represents the state where no castle rights are available.
	NoCastleRights CastleRights = 0
	// AllCastleRights represents the state where all castle rights are available to both players.
	AllCastleRights CastleRights = 0xF
)

func (*CastleRights) Disable

func (c *CastleRights) Disable(r CastleRight)

Disable disables a castle right.

func (*CastleRights) Enable

func (c *CastleRights) Enable(r CastleRight)

Enable enables a castle right.

func (*CastleRights) Get

func (c *CastleRights) Get(r CastleRight) bool

Get returns true if a castle right is available.

type Color

type Color uint8

Color represents either white or black.

const (
	White Color = iota
	Black
)

func (Color) Opposite

func (c Color) Opposite() Color

Opposite returns the opposite color.

func (Color) String

func (c Color) String() string

func (Color) Valid

func (c Color) Valid() bool

Valid returns true if the color is valid.

type EnPassantRight

type EnPassantRight uint8

EnPassantRight represents an en passant right.

A Square can be cast directly to this type, like EnPassantRight(D6).

To represent the lack of an en passant right, use NoEnPasssantRight.

const NoEnPassantRight EnPassantRight = 0xFF

NoEnPassantRight represents the lack of an en passant right.

type File

type File uint8

File represents a board file.

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

func (File) Left

func (f File) Left() File

Left returns the file to the left. For FileA, the result is invalid.

func (File) Right

func (f File) Right() File

Right returns the file to the right. For FileH, the result is invalid.

func (File) String

func (f File) String() string

func (File) Valid

func (f File) Valid() bool

type Move

type Move uint16

Move represents an engine move, or equivalently, a transition between two positions. In chess terminology, this would be a ply.

func NewMove

func NewMove(from, to Square) Move

NewMove returns a new Move.

To represent promotion moves, use NewPromotionMove instead.

To represent castling moves, use the king's original and destination squares as the from and to squares respectively.

func NewPromotionMove

func NewPromotionMove(from, to Square, p Piece) Move

NewPromotionMove returns a new Move that records the given promotion.

func (Move) From

func (m Move) From() Square

From returns the square at which the move starts.

If the move is castling, the from square is the king's original square.

func (Move) Promotion

func (m Move) Promotion() (p Piece, ok bool)

Promotion returns promotion information for the move. If the move is not a promotion move, ok is false.

func (Move) To

func (m Move) To() Square

To returns the square at which the move ends.

If the move is castling, the to square is the king's destination square.

type Piece

type Piece uint8

Piece represents a piece.

const (
	WhitePawn Piece = iota
	WhiteKnight
	WhiteBishop
	WhiteRook
	WhiteQueen
	WhiteKing
	BlackPawn
	BlackKnight
	BlackBishop
	BlackRook
	BlackQueen
	BlackKing
)

func NewPiece

func NewPiece(c Color, r Role) Piece

func (Piece) Color

func (p Piece) Color() Color

Color returns the color of the piece.

func (Piece) Role

func (p Piece) Role() Role

Role returns the role of the piece.

func (Piece) String

func (p Piece) String() string

func (Piece) Valid

func (p Piece) Valid() bool

Valid returns true if the piece is valid.

type Position

type Position struct {
	Board          []Bitboard // Board describes which pieces are on the board and where.
	CastleRights   CastleRights
	EnPassantRight EnPassantRight
	SideToMove     Color
	HalfMoves      uint8  // HalfMoves is the number of half moves since the last capture or pawn move.
	FullMoves      uint16 // FullMoves starts at 1 and is incremented after Black moves.
}

Position represents a game position. Three-fold repetition is not tracked here.

func NewPosition

func NewPosition() Position

NewPosition returns a new position, pre-populated with all starting pieces.

func (*Position) AllPieces

func (p *Position) AllPieces() Bitboard

AllPieces returns a bitboard of all piece locations.

func (*Position) BlackPieces

func (p *Position) BlackPieces() Bitboard

BlackPieces returns a bitboard of all black piece locations.

func (*Position) Get

func (p *Position) Get(s Square) (pc Piece, ok bool)

Get returns the piece on the given square. If there's no piece there, ok is false.

func (*Position) Put

func (p *Position) Put(pc Piece, s Square)

Put puts a piece on the board. No other fields are updated.

func (*Position) Reset

func (p *Position) Reset()

Reset resets the position to the starting position.

func (*Position) WhitePieces

func (p *Position) WhitePieces() Bitboard

WhitePieces returns a bitboard of all white piece locations.

type Rank

type Rank uint8

Rank represents a board rank. Note that Rank1 is represented by 0.

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

func (Rank) Above

func (r Rank) Above() Rank

Above returns the rank above. For Rank8, the result is invalid.

func (Rank) Below

func (r Rank) Below() Rank

Below returns the rank below. For Rank1, the result is invalid.

func (Rank) String

func (r Rank) String() string

func (Rank) Valid

func (r Rank) Valid() bool

type Role

type Role uint8

Role represents a piece's role.

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

func (Role) String

func (r Role) String() string

func (Role) Valid

func (r Role) Valid() bool

Valid returns true if the role is valid.

type Square

type Square uint8

Square represents a board square. A1 is represented by 0, B1 by 1, and H8 by 63.

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
)

func NewSquare

func NewSquare(f File, r Rank) Square

NewSquare returns a new Square at the given file and rank.

func (Square) Bitboard

func (s Square) Bitboard() Bitboard

Bitboard returns the bitboard representation of the square.

func (Square) File

func (s Square) File() File

File returns the file of the square.

func (Square) Rank

func (s Square) Rank() Rank

Rank returns the rank of the square.

func (Square) String

func (s Square) String() string

func (Square) Valid

func (s Square) Valid() bool

Valid returns true if the square is valid.

Jump to

Keyboard shortcuts

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