Documentation ¶
Overview ¶
Package chess implements basic chess-related functionality.
Index ¶
- type Bitboard
- type Board
- func (b *Board) At(s Square) (Piece, bool)
- func (b *Board) ByColor(c Color) Bitboard
- func (b *Board) ByRole(r Role) Bitboard
- func (b *Board) IsValid() error
- func (b *Board) KingOf(c Color) Square
- func (b *Board) Put(p Piece, s Square)
- func (b *Board) PutDangerous(p Piece, s Square)
- func (b *Board) Remove(s Square)
- type CastleRights
- type Color
- type File
- type Move
- type Piece
- type Position
- type PromotionInfo
- type Rank
- type Role
- type Square
- type Undo
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.
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) PutDangerous ¶
PutDangerous puts a piece on a square without checking if a different piece already exists there. It is faster than Board.Put.
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.
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 ¶
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 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 (*Position) IsLegalMove ¶
IsLegalMove returns true if the move is legal in the position. It does not account for insufficient material or three-fold repetition.
func (*Position) LegalMoves ¶
LegalMoves returns a list of legal moves.
func (*Position) Move ¶
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.
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 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 (Square) IsAdjacentTo ¶
IsAdjacentTo returns true if the square is adjacent to the other square.
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.