chess

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 6 Imported by: 5

README

Chess

A lightweight, simple, and well tested library for enforcing the rules of chess.

Yes I know it's not pretty code, but it's my first go package ever. Take it easy on me.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasLegalMove

func HasLegalMove(board *Board) bool

func IsLegalMove

func IsLegalMove(board *Board, move *Move) bool

func UpdateBoardCounters

func UpdateBoardCounters(lastBoard *Board, boardBuilder *BoardBuilder, move *Move)

func UpdateBoardEnPassantSquare

func UpdateBoardEnPassantSquare(lastBoard *Board, boardBuilder *BoardBuilder, move *Move)

func UpdateBoardResult

func UpdateBoardResult(lastBoard *Board, boardBuilder *BoardBuilder, repetitions uint8)

func UpdateCastleRights

func UpdateCastleRights(lastBoard *Board, boardBuilder *BoardBuilder, move *Move)

func UpdatePiecesFromMove

func UpdatePiecesFromMove(lastBoard *Board, boardBuilder *BoardBuilder, move *Move)

func UpdateRepetitionsByFENMap

func UpdateRepetitionsByFENMap(lastBoard *Board, boardBuilder *BoardBuilder, move *Move) uint8

Types

type Board

type Board struct {
	Pieces                  [8][8]Piece      `json:"pieces"`
	OptEnPassantSquare      *Square          `json:"enPassantSquare"`
	IsWhiteTurn             bool             `json:"isWhiteTurn"`
	CanWhiteCastleQueenside bool             `json:"canWhiteCastleQueenside"`
	CanWhiteCastleKingside  bool             `json:"canWhiteCastleKingside"`
	CanBlackCastleQueenside bool             `json:"canBlackCastleQueenside"`
	CanBlackCastleKingside  bool             `json:"canBlackCastleKingside"`
	HalfMoveClockCount      uint8            `json:"halfMoveClockCount"`
	FullMoveCount           uint16           `json:"fullMoveCount"`
	RepetitionsByMiniFEN    map[string]uint8 `json:"repetitionsByMiniFEN"`
	Result                  BoardResult      `json:"result"`
	// contains filtered or unexported fields
}

func BoardFromFEN

func BoardFromFEN(fen string) (*Board, error)

func GetBoardFromMove

func GetBoardFromMove(board *Board, move *Move) *Board

func GetInitBoard

func GetInitBoard() *Board

func NewBoard

func NewBoard(pieces *[8][8]Piece,
	enPassantSquare *Square,
	isWhiteTurn bool,
	canWhiteCastleKingside bool,
	canWhiteCastleQueenside bool,
	canBlackCastleKingside bool,
	canBlackCastleQueenside bool,
	halfMoveClockCount uint8,
	fullMoveCount uint16,
	repetitionsByMiniFEN map[string]uint8,
	result BoardResult,
) *Board

func (*Board) ComputeKingPositions

func (board *Board) ComputeKingPositions() (*Square, *Square)

func (*Board) ComputeMaterialCount

func (board *Board) ComputeMaterialCount() *MaterialCount

func (*Board) GetKingSquare

func (board *Board) GetKingSquare(isWhiteKing bool) *Square

func (*Board) GetPieceOnSquare

func (board *Board) GetPieceOnSquare(square *Square) Piece

func (*Board) HasLegalNextMove

func (board *Board) HasLegalNextMove() bool

func (*Board) IsCheckmate

func (board *Board) IsCheckmate() bool

func (*Board) IsForcedDrawByMaterial

func (board *Board) IsForcedDrawByMaterial() bool

func (*Board) IsInitBoard

func (board *Board) IsInitBoard() bool

func (*Board) String

func (board *Board) String() string

func (*Board) ToFEN

func (board *Board) ToFEN() string

func (*Board) ToMiniFEN

func (board *Board) ToMiniFEN() string

type BoardBuilder

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

func NewBoardBuilder

func NewBoardBuilder() *BoardBuilder

func (*BoardBuilder) Build

func (bb *BoardBuilder) Build() *Board

func (*BoardBuilder) FromBoard

func (bb *BoardBuilder) FromBoard(board *Board) *BoardBuilder

func (*BoardBuilder) WithCanBlackCastleKingside

func (bb *BoardBuilder) WithCanBlackCastleKingside(canBlackCastleKingside bool) *BoardBuilder

func (*BoardBuilder) WithCanBlackCastleQueenside

func (bb *BoardBuilder) WithCanBlackCastleQueenside(canBlackCastleQueenside bool) *BoardBuilder

func (*BoardBuilder) WithCanWhiteCastleKingside

func (bb *BoardBuilder) WithCanWhiteCastleKingside(canWhiteCastleKingside bool) *BoardBuilder

func (*BoardBuilder) WithCanWhiteCastleQueenside

func (bb *BoardBuilder) WithCanWhiteCastleQueenside(canWhiteCastleQueenside bool) *BoardBuilder

func (*BoardBuilder) WithEnPassantSquare

func (bb *BoardBuilder) WithEnPassantSquare(enPassantSquare *Square) *BoardBuilder

func (*BoardBuilder) WithFullMoveCount

func (bb *BoardBuilder) WithFullMoveCount(fullMoveCount uint16) *BoardBuilder

func (*BoardBuilder) WithHalfMoveClockCount

func (bb *BoardBuilder) WithHalfMoveClockCount(halfMoveClockCount uint8) *BoardBuilder

func (*BoardBuilder) WithIsWhiteTurn

func (bb *BoardBuilder) WithIsWhiteTurn(isWhiteTurn bool) *BoardBuilder

func (*BoardBuilder) WithMiniFENCount

func (bb *BoardBuilder) WithMiniFENCount(miniFEN string, count uint8) *BoardBuilder

func (*BoardBuilder) WithPiece

func (bb *BoardBuilder) WithPiece(piece Piece, square *Square) *BoardBuilder

func (*BoardBuilder) WithPieces

func (bb *BoardBuilder) WithPieces(pieces [8][8]Piece) *BoardBuilder

func (*BoardBuilder) WithRepetitionsByMiniFEN

func (bb *BoardBuilder) WithRepetitionsByMiniFEN(repetitionsByMiniFEN map[string]uint8) *BoardBuilder

func (*BoardBuilder) WithResult

func (bb *BoardBuilder) WithResult(result BoardResult) *BoardBuilder

type BoardResult

type BoardResult string
const (
	BOARD_RESULT_IN_PROGRESS                   BoardResult = "in_progress"
	BOARD_RESULT_WHITE_WINS_BY_CHECKMATE       BoardResult = "white_wins_by_checkmate"
	BOARD_RESULT_BLACK_WINS_BY_CHECKMATE       BoardResult = "black_wins_by_checkmate"
	BOARD_RESULT_DRAW_BY_STALEMATE             BoardResult = "draw_by_stalemate"
	BOARD_RESULT_DRAW_BY_INSUFFICIENT_MATERIAL BoardResult = "draw_by_insufficient_material"
	BOARD_RESULT_DRAW_BY_THREEFOLD_REPETITION  BoardResult = "draw_by_threefold_repetition"
	BOARD_RESULT_DRAW_BY_FIFTY_MOVE_RULE       BoardResult = "draw_by_fifty_move_rule"
)

type MaterialCount

type MaterialCount struct {
	WhitePawnCount        uint8
	WhiteKnightCount      uint8
	WhiteLightBishopCount uint8
	WhiteDarkBishopCount  uint8
	WhiteRookCount        uint8
	WhiteQueenCount       uint8
	BlackPawnCount        uint8
	BlackKnightCount      uint8
	BlackLightBishopCount uint8
	BlackDarkBishopCount  uint8
	BlackRookCount        uint8
	BlackQueenCount       uint8
}

type MaterialCountBuilder

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

func NewMaterialCountBuilder

func NewMaterialCountBuilder() *MaterialCountBuilder

func (*MaterialCountBuilder) Build

func (mcb *MaterialCountBuilder) Build() *MaterialCount

func (*MaterialCountBuilder) WithMaterialCount

func (mcb *MaterialCountBuilder) WithMaterialCount(otherMaterialCount *MaterialCount) *MaterialCountBuilder

func (*MaterialCountBuilder) WithPiece

func (mcb *MaterialCountBuilder) WithPiece(piece Piece, square *Square) *MaterialCountBuilder

func (*MaterialCountBuilder) WithoutPiece

func (mcb *MaterialCountBuilder) WithoutPiece(piece Piece, square *Square) *MaterialCountBuilder

type Move

type Move struct {
	Piece               Piece     `json:"piece"`
	StartSquare         *Square   `json:"startSquare"`
	EndSquare           *Square   `json:"endSquare"`
	CapturedPiece       Piece     `json:"capturedPiece"`
	KingCheckingSquares []*Square `json:"kingCheckingSquares"`
	PawnUpgradedTo      Piece     `json:"pawnUpgradedTo"`
}

func GetLegalMoves

func GetLegalMoves(board *Board) ([]*Move, error)

func GetLegalMovesForBishop

func GetLegalMovesForBishop(board *Board, square *Square) ([]*Move, error)

func GetLegalMovesForKing

func GetLegalMovesForKing(board *Board) []*Move

func GetLegalMovesForKnight

func GetLegalMovesForKnight(board *Board, square *Square) ([]*Move, error)

func GetLegalMovesForPawn

func GetLegalMovesForPawn(board *Board, square *Square) ([]*Move, error)

func GetLegalMovesForQueen

func GetLegalMovesForQueen(board *Board, square *Square) ([]*Move, error)

func GetLegalMovesForRook

func GetLegalMovesForRook(board *Board, square *Square) ([]*Move, error)

func GetLegalMovesFromOrigin

func GetLegalMovesFromOrigin(board *Board, square *Square) ([]*Move, error)

func MoveFromAlgebraic added in v0.0.1

func MoveFromAlgebraic(algMove string, priorBoard *Board) (*Move, error)

func MoveFromLongAlgebraic added in v0.0.1

func MoveFromLongAlgebraic(algMove string, priorBoard *Board) (*Move, error)

func (*Move) DoesAllowEnPassant

func (move *Move) DoesAllowEnPassant() bool

func (*Move) Equal added in v0.0.1

func (move *Move) Equal(otherMove *Move) bool

func (*Move) EqualTo deprecated

func (move *Move) EqualTo(otherMove *Move) bool

Deprecated: Replaced with "Equal" instead - due to support from google/go-cmp

func (*Move) IsCastles

func (move *Move) IsCastles() bool

func (*Move) ToAlgebraic

func (move *Move) ToAlgebraic(board *Board) string

func (*Move) ToLongAlgebraic added in v0.0.1

func (move *Move) ToLongAlgebraic() string

ToLongAlgebraic adheres to the UCI "long algebraic notation", which differs from the standard "long algebraic notation". The UCI version is restricted to the start/end squares and a piece delimiter for upgrading pawns.

type Piece

type Piece uint8
const (
	EMPTY Piece = iota
	WHITE_PAWN
	WHITE_KNIGHT
	WHITE_BISHOP
	WHITE_ROOK
	WHITE_QUEEN
	WHITE_KING
	BLACK_PAWN
	BLACK_KNIGHT
	BLACK_BISHOP
	BLACK_ROOK
	BLACK_QUEEN
	BLACK_KING
)

func (Piece) IsBishop

func (p Piece) IsBishop() bool

func (Piece) IsKing

func (p Piece) IsKing() bool

func (Piece) IsKnight

func (p Piece) IsKnight() bool

func (Piece) IsPawn

func (p Piece) IsPawn() bool

func (Piece) IsQueen

func (p Piece) IsQueen() bool

func (Piece) IsRook

func (p Piece) IsRook() bool

func (Piece) IsWhite

func (p Piece) IsWhite() bool

func (Piece) String

func (p Piece) String() string

func (Piece) ToAlgebraic

func (p Piece) ToAlgebraic() string

type Square

type Square struct {
	Rank uint8 `json:"rank"`
	File uint8 `json:"file"`
}

func GetCheckingSquares

func GetCheckingSquares(board *Board, isWhiteKing bool) []*Square

func SquareFromAlgebraicCoords

func SquareFromAlgebraicCoords(algCoords string) (*Square, error)

func (*Square) Copy

func (s *Square) Copy() *Square

func (*Square) Equal added in v0.0.1

func (s *Square) Equal(other *Square) bool

func (*Square) EqualTo deprecated

func (s *Square) EqualTo(other *Square) bool

Deprecated: Replaced by Equal in favor of support for google/go-cmp

func (*Square) IsDarkSquare

func (s *Square) IsDarkSquare() bool

func (*Square) IsLightSquare

func (s *Square) IsLightSquare() bool

func (*Square) IsValidBoardSquare

func (s *Square) IsValidBoardSquare() bool

func (*Square) String

func (s *Square) String() string

func (*Square) ToAlgebraicCoords

func (s *Square) ToAlgebraicCoords() string

Jump to

Keyboard shortcuts

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