chess

package
v0.0.0-...-27709c7 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WhitePawn   = Piece(uint8(White)<<3 | uint8(Pawn))
	WhiteKnight = Piece(uint8(White)<<3 | uint8(Knight))
	WhiteBishop = Piece(uint8(White)<<3 | uint8(Bishop))
	WhiteRook   = Piece(uint8(White)<<3 | uint8(Rook))
	WhiteQueen  = Piece(uint8(White)<<3 | uint8(Queen))
	WhiteKing   = Piece(uint8(White)<<3 | uint8(King))
	BlackPawn   = Piece(uint8(Black)<<3 | uint8(Pawn))
	BlackKnight = Piece(uint8(Black)<<3 | uint8(Knight))
	BlackBishop = Piece(uint8(Black)<<3 | uint8(Bishop))
	BlackRook   = Piece(uint8(Black)<<3 | uint8(Rook))
	BlackQueen  = Piece(uint8(Black)<<3 | uint8(Queen))
	BlackKing   = Piece(uint8(Black)<<3 | uint8(King))
)
View Source
const NumSquares = 64
View Source
const StartingBoardFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
View Source
const StartingPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

StartingPositionFEN is the FEN for the starting position.

Variables

View Source
var (
	PawnPushMasks     [64]uint64
	PawnAttackMasks   [64]uint64
	KnightAttackMasks [64]uint64
	BishopAttackMasks [64]uint64
	RookAttackMasks   [64]uint64
	QueenAttackMasks  [64]uint64
	KingAttackMasks   [64]uint64
)

All masks are from White's viewpoint.

Functions

This section is empty.

Types

type Bitboard

type Bitboard uint64

Bitboard is a 64-bit bitboard. From LSB to MSB, the bitboard goes A1, A2, ..., H8.

func (*Bitboard) Debug

func (b *Bitboard) Debug() string

Debug returns a multi-line debug string representing the bitboard. True is "1", and false is ".".

func (*Bitboard) Flip

func (b *Bitboard) Flip()

Flip vertically mirrors the bitboard. For example, A1 and A8 swap places.

func (*Bitboard) Get

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

Get returns the bit at s.

func (*Bitboard) Set

func (b *Bitboard) Set(s Square, x bool)

Set sets the bit at s to x.

func (*Bitboard) Set0

func (b *Bitboard) Set0(s Square)

Set0 sets the bit at s to 0.

func (*Bitboard) Set1

func (b *Bitboard) Set1(s Square)

Set1 sets the bit at s to 1.

func (*Bitboard) Toggle

func (b *Bitboard) Toggle(s Square)

Toggle toggles the bit at s.

type Board

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

func NewBoard

func NewBoard(boardFEN string) (Board, error)

NewBoard returns a new board with the given board-FEN representation.

func (*Board) FEN

func (b *Board) FEN() string

FEN returns the FEN representation of the board.

func (*Board) Flip

func (b *Board) Flip() *Board

Flip flips the board vertically and also returns the board.

func (Board) Generate

func (Board) Generate(r *rand.Rand, size int) reflect.Value

Generate lets Board satisfy testing/quick.Generator.

type CastlingFlag

type CastlingFlag uint8

CastlingFlag represents a single castling right.

const (
	// FriendOO represents the friendly king-side castling right.
	FriendOO CastlingFlag = 1 << iota
	// FriendOOO represents the friendly queen-side castling right.
	FriendOOO
	// EnemyOO represents the enemy king-side castling right.
	EnemyOO
	// EnemyOOO represents the enemy queen-side castling right.
	EnemyOOO
)

func (CastlingFlag) Generate

func (CastlingFlag) Generate(r *rand.Rand, size int) reflect.Value

Generate lets CastlingFlag satisfy testing/quick.Generator.

type CastlingRights

type CastlingRights uint8

CastlingRights represents all available castling rights.

func NewCastlingRights

func NewCastlingRights(flags ...CastlingFlag) CastlingRights

NewCastlingRights returns a new CastlingRights with the provided rights set to true.

func ParseCastlingRightsFEN

func ParseCastlingRightsFEN(s string) (CastlingRights, error)

func (*CastlingRights) Disable

func (c *CastlingRights) Disable(f CastlingFlag)

Disable disables a castling right.

func (*CastlingRights) Enable

func (c *CastlingRights) Enable(f CastlingFlag)

Enable enables a castling right.

func (*CastlingRights) FEN

func (c *CastlingRights) FEN() string

FEN returns the FEN representation of the castling rights.

func (*CastlingRights) Flip

func (c *CastlingRights) Flip()

Flip flips castling rights by color.

func (CastlingRights) Generate

func (CastlingRights) Generate(rand *rand.Rand, size int) reflect.Value

Generate lets CastlingRights satisify testing/quick.Generator.

func (*CastlingRights) Get

func (c *CastlingRights) Get(f CastlingFlag) bool

Get returns whether a castling right is available.

type Color

type Color uint8

Color represents a color - either white or black.

const (
	White Color = iota
	Black
)

func ParseColorFEN

func ParseColorFEN(s string) (Color, error)

func (Color) FEN

func (c Color) FEN() string

FEN returns the FEN representation of a color. It returns an empty string if the color is invalid.

func (Color) Flipped

func (c Color) Flipped() Color

Flipped returns the opposite color.

func (Color) Generate

func (Color) Generate(rand *rand.Rand, size int) reflect.Value

Generate lets Color satisfy testing/quick.Generator.

type EnPassantRight

type EnPassantRight uint8

EnPassantRight represents the right to en passant.

func NewEnPassantRight

func NewEnPassantRight() EnPassantRight

NewEnPassantRight returns a new EnPassantRight.

func ParseEnPassantRightFEN

func ParseEnPassantRightFEN(s string) (EnPassantRight, error)

func (EnPassantRight) Allowed

func (e EnPassantRight) Allowed() bool

Allowed returns whether en passant is allowed.

func (EnPassantRight) FEN

func (e EnPassantRight) FEN() string

FEN returns the FEN representation of the EnPassantRight.

func (EnPassantRight) Flipped

func (e EnPassantRight) Flipped() EnPassantRight

Flipped returns a new EnPassantRight with the target square vertically flipped (if any).

func (EnPassantRight) Generate

func (EnPassantRight) Generate(r *rand.Rand, size int) reflect.Value

Generate lets EnPassantRight satisfy testing/quick.Generator.

func (EnPassantRight) Get

func (e EnPassantRight) Get() (Square, bool)

Get returns the target square (if any) and whether en passant is allowed.

type File

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

type Move

type Move uint32

6: from 6: to 3: piece captured 2: promotion

func (Move) Capture

func (m Move) Capture() (Role, bool)

func (Move) From

func (m Move) From() Square

func (Move) IsCapture

func (m Move) IsCapture() bool

func (Move) IsPromotion

func (m Move) IsPromotion() bool

func (Move) Promotion

func (m Move) Promotion() (Role, bool)

func (Move) To

func (m Move) To() Square

type Piece

type Piece uint8

func NewPiece

func NewPiece(c Color, r Role) Piece

func ParsePieceFEN

func ParsePieceFEN(s string) (Piece, error)

func (Piece) Color

func (p Piece) Color() Color

func (Piece) FEN

func (p Piece) FEN() string

func (Piece) Generate

func (Piece) Generate(rand *rand.Rand, size int) reflect.Value

func (Piece) Invalid

func (p Piece) Invalid() bool

func (Piece) Role

func (p Piece) Role() Role

func (Piece) Valid

func (p Piece) Valid() bool

func (Piece) Value

func (p Piece) Value() uint8

type Position

type Position struct {
	Board         Board
	EnPassant     EnPassantRight
	Castling      CastlingRights
	SideToMove    Color
	HalfMoveClock uint8  // Max half move clock is 75.
	FullMoveCount uint16 // Max full move count is ~4000 (?).
}

Position represents a chess position.

func NewPosition

func NewPosition(fen string) (*Position, error)

NewPosition returns a new position set to the given FEN.

func (*Position) FEN

func (p *Position) FEN() (string, error)

type Rank

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

Note that Rank1 is 0.

func (Rank) Flipped

func (r Rank) Flipped() Rank

Flipped returns a rank vertically flipped from the original.

type Role

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

func (Role) Valid

func (r Role) Valid() bool

Valid returns whether the role is valid.

type Square

type Square uint8
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 from coordinates.

func ParseSquareFEN

func ParseSquareFEN(s string) (Square, error)

func (Square) Bitboard

func (s Square) Bitboard() Bitboard

Bitboard returns a bitboard with just the square set.

func (Square) EnPassantRight

func (s Square) EnPassantRight() EnPassantRight

EnPassantRight returns an en passant right that targets the square.

func (Square) FEN

func (s Square) FEN() string

func (Square) File

func (s Square) File() File

File returns the file the square is on.

func (Square) Flipped

func (s Square) Flipped() Square

Flipped returns the vertically opposite square.

func (Square) Generate

func (Square) Generate(rand *rand.Rand, size int) reflect.Value

Generate lets Square satisfy testing/quick.Generator.

func (Square) Rank

func (s Square) Rank() Rank

Rank returns the rank the square is on.

func (Square) Valid

func (s Square) Valid() bool

Valid returns whether the square is valid.

Jump to

Keyboard shortcuts

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