core

package
v0.0.0-...-786fda4 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package core implements basic chess functionality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bitboard

type Bitboard uint64

A Bitboard represents each square on the board as a bit. The LSB is a1, and the MSB is h8.

func NewBitboard

func NewBitboard(s ...Square) Bitboard

NewBitboard returns a new bitboard with the given squares set.

func (*Bitboard) Clear

func (b *Bitboard) Clear(s Square)

Clear clears the given square to 0.

Example
b := NewBitboard(A1, H8)
b.Clear(H8)
fmt.Println(b.Debug())
Output:

........
........
........
........
........
........
........
X.......

func (*Bitboard) Count

func (b *Bitboard) Count() int

Count returns the number of squares set to 1.

Example
b := NewBitboard(A1, H8)
fmt.Println(b.Count())
Output:

2

func (*Bitboard) Debug

func (b *Bitboard) Debug() string

Debug returns an 8x8 representation of the bitboard.

func (*Bitboard) First

func (b *Bitboard) First() Square

First returns the first square set to 1. If the bitboard is empty, it returns an invalid square.

Example
b := NewBitboard(E5, D4, C3)
fmt.Println(b.First())
Output:

C3

func (*Bitboard) FlipV

func (b *Bitboard) FlipV()

FlipV flips the bitboard horizontally.

Example
b := NewBitboard(C2, D2, E2, F2)
b.FlipV()
fmt.Println(b.Debug())
Output:

........
..XXXX..
........
........
........
........
........
........

func (*Bitboard) Get

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

Get returns true if the given square is set to 1.

Example
b := NewBitboard(A1, H8)
fmt.Println(b.Get(A1))
fmt.Println(b.Get(A2))
Output:

true
false

func (*Bitboard) Intersects

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

Intersects returns true if the bitboards have any squares in common.

Example
a := NewBitboard(A1, H8)
b := NewBitboard(A2, H7)
c := NewBitboard(A1, H7)
fmt.Println(a.Intersects(b))
fmt.Println(a.Intersects(c))
Output:

false
true

func (*Bitboard) Set

func (b *Bitboard) Set(s Square)

Set sets the given square to 1.

Example
b := NewBitboard(A1, H8)
b.Set(A2)
fmt.Println(b.Debug())
Output:

.......X
........
........
........
........
........
X.......
X.......

func (*Bitboard) With

func (b *Bitboard) With(other Bitboard)

With sets all squares set in the other bitboard.

Example
p := NewBitboard(A1, H8)
q := NewBitboard(A2, H7)
p.With(q)
fmt.Println(p.Debug())
Output:

.......X
.......X
........
........
........
........
X.......
X.......

type Board

type Board [12]Bitboard

Board contains piece placements.

func (*Board) AllEmpty

func (b *Board) AllEmpty(bb Bitboard) bool

AllEmpty returns true if all squares set in the bitboard are empty.

func (*Board) BlackKing

func (b *Board) BlackKing() Square

BlackKing returns the location of the black king.

func (*Board) BlackPieces

func (b *Board) BlackPieces() Bitboard

BlackPieces returns the location of all black pieces.

func (*Board) Clear

func (b *Board) Clear(s Square)

Clear removes a piece from a square, if any.

func (*Board) Get

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

Get returns the piece on the given square, if any.

func (*Board) IsEmpty

func (b *Board) IsEmpty(s Square) bool

IsEmpty returns true if the square is empty.

func (*Board) IsOccupied

func (b *Board) IsOccupied(s Square) bool

IsOccupied returns true if the square is occupied.

func (*Board) Move

func (b *Board) Move(p Piece, from, to Square)

Move moves a piece to a square. If there is already a piece at the destination, that piece is removed. If the destination is known to be empty, MoveToEmpty is faster.

func (*Board) MoveToEmpty

func (b *Board) MoveToEmpty(p Piece, from, to Square)

MoveToEmpty moves a piece to an empty square.

func (*Board) Promote

func (b *Board) Promote(from, to Square, p PieceType)

Promote moves a pawn to a square, promoting it.

func (*Board) Set

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

Set places a piece on a square. If there is already a piece on the square, that piece is removed. If the square is known to be empty, SetOnEmpty is faster.

func (*Board) SetOnEmpty

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

SetOnEmpty places a piece on an empty square.

func (*Board) WhiteKing

func (b *Board) WhiteKing() Square

WhiteKing returns the location of the white king.

func (*Board) WhitePieces

func (b *Board) WhitePieces() Bitboard

WhitePieces returns the location of all white pieces.

type Color

type Color bool

A Color is white or black.

const (
	White Color = false
	Black Color = true
)

Color constants.

func (Color) Other

func (c Color) Other() Color

Other returns the other color.

func (Color) String

func (c Color) String() string

func (Color) Uint64

func (c Color) Uint64() uint64

Uint64 returns 0 for white and 1 for black.

type File

type File uint64

A File is a column on the chess board.

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

File constants.

func (File) Left

func (f File) Left() File

Left returns the file to the left of f, wrapping around if necessary.

func (File) Right

func (f File) Right() File

Right returns the file to the right of f, wrapping around if necessary.

func (File) String

func (f File) String() string

func (File) Valid

func (f File) Valid() bool

type Move

type Move struct {
	From, To  Square
	Promotion PieceType // The zero value indicates no promotion.
}

A Move represents a chess move. For castling moves, From and To are the king's squares.

type Piece

type Piece uint64

A Piece represents a chess piece.

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

White piece constants.

func NewPiece

func NewPiece(c Color, pt PieceType) Piece

NewPiece returns a new piece.

func (Piece) Color

func (p Piece) Color() Color

Color returns a piece's color.

func (Piece) String

func (p Piece) String() string

func (Piece) Type

func (p Piece) Type() PieceType

Type returns a piece's type.

func (Piece) Valid

func (p Piece) Valid() bool

type PieceType

type PieceType uint64

A PieceType is a type of piece.

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

Piece type constants.

func (PieceType) String

func (p PieceType) String() string

func (PieceType) Valid

func (p PieceType) Valid() bool

type Position

type Position struct {
	Board      Board
	SideToMove Color
	EnPassant  Square // The zero value indicates no en passant square.

	WhiteOO, WhiteOOO bool
	BlackOO, BlackOOO bool

	HalfMoveClock  int
	FullMoveNumber int // Starts at 1.
}

Position represents a chess position.

func NewPosition

func NewPosition() Position

NewPosition returns the starting position.

func (*Position) EnemyKing

func (p *Position) EnemyKing() Square

EnemyKing returns the location of the opponent's king.

func (*Position) FriendlyKing

func (p *Position) FriendlyKing() Square

FriendlyKing returns the location of the side to move's king.

func (*Position) Make

func (p *Position) Make(m Move)

Make makes a move. It does not check for invalid moves.

type Rank

type Rank uint64

A Rank is a row on the chess board.

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

Rank constants.

func (Rank) Above

func (r Rank) Above() Rank

Above returns the rank above r, wrapping around if necessary.

func (Rank) Below

func (r Rank) Below() Rank

Below returns the rank below r, wrapping around if necessary.

func (Rank) String

func (r Rank) String() string

func (Rank) Valid

func (r Rank) Valid() bool

type Square

type Square uint64

A Square is a location on the 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
)

Square constants.

func NewSquare

func NewSquare(f File, r Rank) Square

NewSquare returns a new square.

func (Square) Above

func (s Square) Above() Square

Above returns the square above s, wrapping around if necessary.

func (Square) Below

func (s Square) Below() Square

Below returns the square below s, wrapping around if necessary.

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 a square's file.

func (Square) Left

func (s Square) Left() Square

Left returns the square to the left of s, wrapping around if necessary.

func (Square) Rank

func (s Square) Rank() Rank

Rank returns a square's rank.

func (Square) Right

func (s Square) Right() Square

Right returns the square to the right of s, wrapping around if necessary.

func (Square) String

func (s Square) String() string

Jump to

Keyboard shortcuts

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