chess

package
v0.0.0-...-305eda8 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BBFiles = [8]Bitboard{BBFileA, BBFileB, BBFileC, BBFileD, BBFileE, BBFileF, BBFileG, BBFileH}
	BBRanks = [8]Bitboard{BBRank1, BBRank2, BBRank3, BBRank4, BBRank5, BBRank6, BBRank7, BBRank8}

	BBDiagonals = [64]Bitboard{9241421688590303745, 4620710844295151872, 2310355422147575808, 1155177711073755136, 577588855528488960, 288794425616760832, 144396663052566528, 72057594037927936, 36099303471055874, 9241421688590303745, 4620710844295151872, 2310355422147575808, 1155177711073755136, 577588855528488960, 288794425616760832, 144396663052566528, 141012904183812, 36099303471055874, 9241421688590303745, 4620710844295151872, 2310355422147575808, 1155177711073755136, 577588855528488960, 288794425616760832, 550831656968, 141012904183812, 36099303471055874, 9241421688590303745, 4620710844295151872, 2310355422147575808, 1155177711073755136, 577588855528488960, 2151686160, 550831656968, 141012904183812, 36099303471055874, 9241421688590303745, 4620710844295151872, 2310355422147575808, 1155177711073755136, 8405024, 2151686160, 550831656968, 141012904183812, 36099303471055874, 9241421688590303745, 4620710844295151872, 2310355422147575808, 32832, 8405024, 2151686160, 550831656968, 141012904183812, 36099303471055874, 9241421688590303745, 4620710844295151872, 128, 32832, 8405024, 2151686160, 550831656968, 141012904183812, 36099303471055874, 9241421688590303745}

	BBAntiDiagonals = [64]Bitboard{9223372036854775808, 4647714815446351872, 2323998145211531264, 1161999622361579520, 580999813328273408, 290499906672525312, 145249953336295424, 72624976668147840, 4647714815446351872, 2323998145211531264, 1161999622361579520, 580999813328273408, 290499906672525312, 145249953336295424, 72624976668147840, 283691315109952, 2323998145211531264, 1161999622361579520, 580999813328273408, 290499906672525312, 145249953336295424, 72624976668147840, 283691315109952, 1108169199648, 1161999622361579520, 580999813328273408, 290499906672525312, 145249953336295424, 72624976668147840, 283691315109952, 1108169199648, 4328785936, 580999813328273408, 290499906672525312, 145249953336295424, 72624976668147840, 283691315109952, 1108169199648, 4328785936, 16909320, 290499906672525312, 145249953336295424, 72624976668147840, 283691315109952, 1108169199648, 4328785936, 16909320, 66052, 145249953336295424, 72624976668147840, 283691315109952, 1108169199648, 4328785936, 16909320, 66052, 258, 72624976668147840, 283691315109952, 1108169199648, 4328785936, 16909320, 66052, 258, 1}

	BBKnightMoves = [64]Bitboard{9077567998918656, 4679521487814656, 38368557762871296, 19184278881435648, 9592139440717824, 4796069720358912, 2257297371824128, 1128098930098176, 2305878468463689728, 1152939783987658752, 9799982666336960512, 4899991333168480256, 2449995666584240128, 1224997833292120064, 576469569871282176, 288234782788157440, 4620693356194824192, 11533718717099671552, 5802888705324613632, 2901444352662306816, 1450722176331153408, 725361088165576704, 362539804446949376, 145241105196122112, 18049583422636032, 45053588738670592, 22667534005174272, 11333767002587136, 5666883501293568, 2833441750646784, 1416171111120896, 567348067172352, 70506185244672, 175990581010432, 88545054707712, 44272527353856, 22136263676928, 11068131838464, 5531918402816, 2216203387392, 275414786112, 687463207072, 345879119952, 172939559976, 86469779988, 43234889994, 21609056261, 8657044482, 1075839008, 2685403152, 1351090312, 675545156, 337772578, 168886289, 84410376, 33816580, 4202496, 10489856, 5277696, 2638848, 1319424, 659712, 329728, 132096}

	BBBishopMoves = [64]Bitboard{18049651735527937, 45053622886727936, 22667548931719168, 11334324221640704, 5667164249915392, 2833579985862656, 1416240237150208, 567382630219904, 4611756524879479810, 11529391036782871041, 5764696068147249408, 2882348036221108224, 1441174018118909952, 720587009051099136, 360293502378066048, 144117404414255168, 2323857683139004420, 1197958188344280066, 9822351133174399489, 4911175566595588352, 2455587783297826816, 1227793891648880768, 577868148797087808, 288793334762704928, 1161999073681608712, 581140276476643332, 326598935265674242, 9386671504487645697, 4693335752243822976, 2310639079102947392, 1155178802063085600, 577588851267340304, 580999811184992272, 290500455356698632, 145390965166737412, 108724279602332802, 9241705379636978241, 4620711952330133792, 2310355426409252880, 1155177711057110024, 290499906664153120, 145249955479592976, 72625527495610504, 424704217196612, 36100411639206946, 9241421692918565393, 4620710844311799048, 2310355422147510788, 145249953336262720, 72624976676520096, 283693466779728, 1659000848424, 141017232965652, 36099303487963146, 9241421688590368773, 4620710844295151618, 72624976668147712, 283691315142656, 1108177604608, 6480472064, 550848566272, 141012904249856, 36099303471056128, 9241421688590303744}

	BBRookMoves = [64]Bitboard{9187484529235886208, 13781085504453754944, 16077885992062689312, 17226286235867156496, 17800486357769390088, 18087586418720506884, 18231136449196065282, 18302911464433844481, 9259260648297103488, 4665518383679160384, 2368647251370188832, 1220211685215703056, 645993902138460168, 358885010599838724, 215330564830528002, 143553341945872641, 9259541023762186368, 4629910699613634624, 2315095537539358752, 1157687956502220816, 578984165983651848, 289632270724367364, 144956323094725122, 72618349279904001, 9259542118978846848, 4629771607097753664, 2314886351157207072, 1157443723186933776, 578722409201797128, 289361752209228804, 144681423712944642, 72341259464802561, 9259542123257036928, 4629771063767613504, 2314885534022901792, 1157442769150545936, 578721386714368008, 289360695496279044, 144680349887234562, 72340177082712321, 9259542123273748608, 4629771061645230144, 2314885530830970912, 1157442765423841296, 578721382720276488, 289360691368494084, 144680345692602882, 72340172854657281, 9259542123273813888, 4629771061636939584, 2314885530818502432, 1157442765409283856, 578721382704674568, 289360691352369924, 144680345676217602, 72340172838141441, 9259542123273814143, 4629771061636907199, 2314885530818453727, 1157442765409226991, 578721382704613623, 289360691352306939, 144680345676153597, 72340172838076926}

	BBQueenMoves = [64]Bitboard{9205534180971414145, 13826139127340482880, 16100553540994408480, 17237620560088797200, 17806153522019305480, 18090419998706369540, 18232552689433215490, 18303478847064064385, 13871017173176583298, 16194909420462031425, 8133343319517438240, 4102559721436811280, 2087167920257370120, 1079472019650937860, 575624067208594050, 287670746360127809, 11583398706901190788, 5827868887957914690, 12137446670713758241, 6068863523097809168, 3034571949281478664, 1517426162373248132, 722824471891812930, 361411684042608929, 10421541192660455560, 5210911883574396996, 2641485286422881314, 10544115227674579473, 5272058161445620104, 2600000831312176196, 1299860225776030242, 649930110732142865, 9840541934442029200, 4920271519124312136, 2460276499189639204, 1266167048752878738, 9820426766351346249, 4910072647826412836, 2455035776296487442, 1227517888139822345, 9550042029937901728, 4775021017124823120, 2387511058326581416, 1157867469641037908, 614821794359483434, 9530782384287059477, 4765391190004401930, 2382695595002168069, 9404792076610076608, 4702396038313459680, 2315169224285282160, 1157444424410132280, 578862399937640220, 325459994840333070, 9386102034266586375, 4693051017133293059, 9332167099941961855, 4630054752952049855, 2314886638996058335, 1157442771889699055, 578721933553179895, 289501704256556795, 180779649147209725, 9313761861428380670}

	BBKingMoves = [64]Bitboard{4665729213955833856, 11592265440851656704, 5796132720425828352, 2898066360212914176, 1449033180106457088, 724516590053228544, 362258295026614272, 144959613005987840, 13853283560024178688, 16186183351374184448, 8093091675687092224, 4046545837843546112, 2023272918921773056, 1011636459460886528, 505818229730443264, 216739030602088448, 54114388906344448, 63227278716305408, 31613639358152704, 15806819679076352, 7903409839538176, 3951704919769088, 1975852459884544, 846636838289408, 211384331665408, 246981557485568, 123490778742784, 61745389371392, 30872694685696, 15436347342848, 7718173671424, 3307175149568, 825720045568, 964771708928, 482385854464, 241192927232, 120596463616, 60298231808, 30149115904, 12918652928, 3225468928, 3768639488, 1884319744, 942159872, 471079936, 235539968, 117769984, 50463488, 12599488, 14721248, 7360624, 3680312, 1840156, 920078, 460039, 197123, 49216, 57504, 28752, 14376, 7188, 3594, 1797, 770}

	BBSquares = [64]Bitboard{}
)
View Source
var SquareBB [65]Bitboard

A global constant where each entry represents a square on the chess board, and each entry contains a bitboard with the bit set high at that square. An extra entry is given so that the invalid square constant NoSq can be indexed into the table without the program crashing.

Functions

func FEN

func FEN(fen string) (func(*Game), error)

FEN takes a string and returns a function that updates the game to reflect the FEN data. Since FEN doesn't encode prior moves, the move list will be empty. The returned function is designed to be used in the NewGame constructor. An error is returned if there is a problem parsing the FEN data.

func InitBitboards

func InitBitboards()

Initalize the bitboard constants.

func PGN

func PGN(r io.Reader) (func(*Game), error)

PGN takes a reader and returns a function that updates the game to reflect the PGN data. The PGN can use any move notation supported by this package. The returned function is designed to be used in the NewGame constructor. An error is returned if there is a problem parsing the PGN data.

func TagPairs

func TagPairs(tagPairs []*TagPair) func(*Game)

TagPairs returns a function that sets the tag pairs to the given value. The returned function is designed to be used in the NewGame constructor.

func UseNotation

func UseNotation(n Notation) func(*Game)

UseNotation returns a function that sets the game's notation to the given value. The notation is used to parse the string supplied to the MoveStr() method as well as the any PGN output. The returned function is designed to be used in the NewGame constructor.

Types

type AlgebraicNotation

type AlgebraicNotation struct{}

AlgebraicNotation (or Standard Algebraic Notation) is the official chess notation used by FIDE. Examples: e4, e5, O-O (short castling), e8=Q (promotion)

func (AlgebraicNotation) Decode

func (AlgebraicNotation) Decode(pos *Position, s string) (*Move, error)

Decode implements the Decoder interface.

func (AlgebraicNotation) Encode

func (AlgebraicNotation) Encode(pos *Position, m *Move) string

Encode implements the Encoder interface.

func (AlgebraicNotation) String

func (AlgebraicNotation) String() string

String implements the fmt.Stringer interface and returns the notation's name.

type Bitboard

type Bitboard uint64

Bitboard is a board representation encoded in an unsigned 64-bit integer. The 64 board positions begin with A1 as the most significant bit and H8 as the least.

const (
	BBFileA Bitboard = 9259542123273814144
	BBFileB Bitboard = 4629771061636907072
	BBFileC Bitboard = 2314885530818453536
	BBFileD Bitboard = 1157442765409226768
	BBFileE Bitboard = 578721382704613384
	BBFileF Bitboard = 289360691352306692
	BBFileG Bitboard = 144680345676153346
	BBFileH Bitboard = 72340172838076673

	BBRank1 Bitboard = 18374686479671623680
	BBRank2 Bitboard = 71776119061217280
	BBRank3 Bitboard = 280375465082880
	BBRank4 Bitboard = 1095216660480
	BBRank5 Bitboard = 4278190080
	BBRank6 Bitboard = 16711680
	BBRank7 Bitboard = 65280
	BBRank8 Bitboard = 255
)
const EmptyBB Bitboard = 0x0
const FullBB Bitboard = 0xffffffffffffffff

A constant representing a bitboard with every square set and every square empty.

func BBForPossibleMoves

func BBForPossibleMoves(pos *Position, pt PieceType, sq Square) Bitboard

func BBForSquare

func BBForSquare(sq Square) Bitboard

TODO make method on Square

func DiaAttack

func DiaAttack(occupied Bitboard, sq Square) Bitboard

func HvAttack

func HvAttack(occupied Bitboard, sq Square) Bitboard

func PawnAttacks

func PawnAttacks(pos *Position, sq Square) Bitboard

func (Bitboard) BitSet

func (bb Bitboard) BitSet(sq uint8) bool

Test whether the bit of the given bitbord at the given position is set.

func (*Bitboard) ClearBit

func (bitboard *Bitboard) ClearBit(sq uint8)

Clear the bit at given square.

func (Bitboard) CountBits

func (bitboard Bitboard) CountBits() int

Count the bits in a given bitboard using the SWAR-popcount algorithm for 64-bit integers.

func (Bitboard) Draw

func (b Bitboard) Draw() string

Draw returns visual representation of the Bitboard useful for debugging.

func (Bitboard) Mapping

func (b Bitboard) Mapping() map[Square]bool

func (Bitboard) Msb

func (bitboard Bitboard) Msb() uint8

Get the position of the MSB of the given bitboard.

func (Bitboard) Occupied

func (b Bitboard) Occupied(sq Square) bool

Occupied returns true if the square's Bitboard position is 1.

func (*Bitboard) PopBit

func (bitboard *Bitboard) PopBit() uint8

Get the position of the MSB of the given bitboard, and clear the MSB.

func (Bitboard) Reverse

func (b Bitboard) Reverse() Bitboard

Reverse returns a Bitboard where the bit order is reversed.

func (*Bitboard) SetBit

func (bitboard *Bitboard) SetBit(sq uint8)

Set the bit at given square.

func (Bitboard) String

func (bitboard Bitboard) String() (bitboardAsString string)

Return a string representation of the given bitboard

type Board

type Board struct {
	BBWhiteKing   Bitboard
	BBWhiteQueen  Bitboard
	BBWhiteRook   Bitboard
	BBWhiteBishop Bitboard
	BBWhiteKnight Bitboard
	BBWhitePawn   Bitboard
	BBBlackKing   Bitboard
	BBBlackQueen  Bitboard
	BBBlackRook   Bitboard
	BBBlackBishop Bitboard
	BBBlackKnight Bitboard
	BBBlackPawn   Bitboard
	WhiteSqs      Bitboard
	BlackSqs      Bitboard
	EmptySqs      Bitboard
	WhiteKingSq   Square
	BlackKingSq   Square
}

A Board represents a chess board and its relationship between squares and pieces.

func NewBoard

func NewBoard(m map[Square]Piece) *Board

NewBoard returns a board from a square to piece mapping.

func (*Board) BBForPiece

func (b *Board) BBForPiece(p Piece) Bitboard

func (*Board) Draw

func (b *Board) Draw() string

Draw returns visual representation of the board useful for debugging.

func (*Board) Flip

func (b *Board) Flip(fd FlipDirection) *Board

Flip flips the board over the vertical or hoizontal center line.

func (*Board) MarshalBinary

func (b *Board) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface and returns the Bitboard representations as a array of bytes. Bitboads are encoded in the following order: WhiteKing, WhiteQueen, WhiteRook, WhiteBishop, WhiteKnight WhitePawn, BlackKing, BlackQueen, BlackRook, BlackBishop, BlackKnight, BlackPawn

func (*Board) MarshalText

func (b *Board) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface and returns a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

func (*Board) Piece

func (b *Board) Piece(sq Square) Piece

Piece returns the piece for the given square.

func (*Board) Rotate

func (b *Board) Rotate() *Board

Rotate rotates the board 90 degrees clockwise.

func (*Board) SquareMap

func (b *Board) SquareMap() map[Square]Piece

SquareMap returns a mapping of squares to pieces. A square is only added to the map if it is occupied.

func (*Board) String

func (b *Board) String() string

String implements the fmt.Stringer interface and returns a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

func (*Board) Transpose

func (b *Board) Transpose() *Board

Transpose flips the board over the A8 to H1 diagonal.

func (*Board) UnmarshalBinary

func (b *Board) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface and parses the Bitboard representations as a array of bytes. Bitboads are decoded in the following order: WhiteKing, WhiteQueen, WhiteRook, WhiteBishop, WhiteKnight WhitePawn, BlackKing, BlackQueen, BlackRook, BlackBishop, BlackKnight, BlackPawn

func (*Board) UnmarshalText

func (b *Board) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnarshaler interface and takes a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

type CastleRights

type CastleRights string

CastleRights holds the state of both sides castling abilities.

func (CastleRights) CanCastle

func (cr CastleRights) CanCastle(c Color, side Side) bool

CanCastle returns true if the given color and side combination can castle, otherwise returns false.

func (CastleRights) String

func (cr CastleRights) String() string

String implements the fmt.Stringer interface and returns a FEN compatible string. Ex. KQq

type Color

type Color int8

Color represents the color of a chess piece.

const (
	// White represents the color white
	White Color = iota
	// Black represents the color black
	Black
	// NoColor represents no color
	NoColor
)

func (Color) Name

func (c Color) Name() string

Name returns a display friendly name.

func (Color) Other

func (c Color) Other() Color

Other returns the opposite color of the receiver.

func (Color) String

func (c Color) String() string

String implements the fmt.Stringer interface and returns the color's FEN compatible notation.

type Decoder

type Decoder interface {
	Decode(pos *Position, s string) (*Move, error)
}

Decoder is the interface implemented by objects that can decode a string into a move given the position. It is not the decoders responsibility to validate the move. An error is returned if the string could not be decoded.

type Encoder

type Encoder interface {
	Encode(pos *Position, m *Move) string
}

Encoder is the interface implemented by objects that can encode a move into a string given the position. It is not the encoders responsibility to validate the move.

type File

type File int8

A File is the file of a square.

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

func (File) String

func (f File) String() string

type FlipDirection

type FlipDirection int

FlipDirection is the direction for the Board.Flip method

const (
	// UpDown flips the board's rank values
	UpDown FlipDirection = iota
	// LeftRight flips the board's file values
	LeftRight
)

type Game

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

A Game represents a single chess game.

func GamesFromPGN

func GamesFromPGN(r io.Reader) ([]*Game, error)

GamesFromPGN returns all PGN decoding games from the reader. It is designed to be used decoding multiple PGNs in the same file. An error is returned if there is an issue parsing the PGNs. Deprecated: Use Scanner instead.

func NewGame

func NewGame(options ...func(*Game)) *Game

NewGame defaults to returning a game in the standard opening position. Options can be given to configure the game's initial state.

func (*Game) AddTagPair

func (g *Game) AddTagPair(k, v string) bool

AddTagPair adds or updates a tag pair with the given key and value and returns true if the value is overwritten.

func (*Game) Clone

func (g *Game) Clone() *Game

func (*Game) Comments

func (g *Game) Comments() [][]string

Comments returns the comments for the game indexed by moves.

func (*Game) Draw

func (g *Game) Draw(method Method) error

Draw attempts to draw the game by the given method. If the method is valid, then the game is updated to a draw by that method. If the method isn't valid then an error is returned.

func (*Game) EligibleDraws

func (g *Game) EligibleDraws() []Method

EligibleDraws returns valid inputs for the Draw() method.

func (*Game) FEN

func (g *Game) FEN() string

FEN returns the FEN notation of the current position.

func (*Game) GetTagPair

func (g *Game) GetTagPair(k string) *TagPair

GetTagPair returns the tag pair for the given key or nil if it is not present.

func (*Game) MarshalText

func (g *Game) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface and encodes the game's PGN.

func (*Game) Method

func (g *Game) Method() Method

Method returns the method in which the outcome occurred.

func (*Game) Move

func (g *Game) Move(m *Move) error

Move updates the game with the given move. An error is returned if the move is invalid or the game has already been completed.

func (*Game) MoveHistory

func (g *Game) MoveHistory() []*MoveHistory

MoveHistory returns the moves in order along with the pre and post positions and any comments.

func (*Game) MoveStr

func (g *Game) MoveStr(s string) error

MoveStr decodes the given string in game's notation and calls the Move function. An error is returned if the move can't be decoded or the move is invalid.

func (*Game) Moves

func (g *Game) Moves() []*Move

Moves returns the move history of the game.

func (*Game) Outcome

func (g *Game) Outcome() Outcome

Outcome returns the game outcome.

func (*Game) Position

func (g *Game) Position() *Position

Position returns the game's current position.

func (*Game) Positions

func (g *Game) Positions() []*Position

Positions returns the position history of the game.

func (*Game) RemoveTagPair

func (g *Game) RemoveTagPair(k string) bool

RemoveTagPair removes the tag pair for the given key and returns true if a tag pair was removed.

func (*Game) Resign

func (g *Game) Resign(color Color)

Resign resigns the game for the given color. If the game has already been completed then the game is not updated.

func (*Game) String

func (g *Game) String() string

String implements the fmt.Stringer interface and returns the game's PGN.

func (*Game) TagPairs

func (g *Game) TagPairs() []*TagPair

TagPairs returns the game's tag pairs.

func (*Game) UnmarshalText

func (g *Game) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnarshaler interface and assumes the data is in the PGN format.

func (*Game) ValidMoves

func (g *Game) ValidMoves() []*Move

ValidMoves returns a list of valid moves in the current position.

type LongAlgebraicNotation

type LongAlgebraicNotation struct{}

LongAlgebraicNotation is a fully expanded version of algebraic notation in which the starting and ending squares are specified. Examples: e2e4, Rd3xd7, O-O (short castling), e7e8=Q (promotion)

func (LongAlgebraicNotation) Decode

func (LongAlgebraicNotation) Decode(pos *Position, s string) (*Move, error)

Decode implements the Decoder interface.

func (LongAlgebraicNotation) Encode

func (LongAlgebraicNotation) Encode(pos *Position, m *Move) string

Encode implements the Encoder interface.

func (LongAlgebraicNotation) String

func (LongAlgebraicNotation) String() string

String implements the fmt.Stringer interface and returns the notation's name.

type Method

type Method uint8

A Method is the method that generated the outcome.

const (
	// NoMethod indicates that an outcome hasn't occurred or that the method can't be determined.
	NoMethod Method = iota
	// Checkmate indicates that the game was won checkmate.
	Checkmate
	// Resignation indicates that the game was won by resignation.
	Resignation
	// DrawOffer indicates that the game was drawn by a draw offer.
	DrawOffer
	// Stalemate indicates that the game was drawn by stalemate.
	Stalemate
	// ThreefoldRepetition indicates that the game was drawn when the game
	// state was repeated three times and a player requested a draw.
	ThreefoldRepetition
	// FivefoldRepetition indicates that the game was automatically drawn
	// by the game state being repeated five times.
	FivefoldRepetition
	// FiftyMoveRule indicates that the game was drawn by the half
	// move clock being one hundred or greater when a player requested a draw.
	FiftyMoveRule
	// SeventyFiveMoveRule indicates that the game was automatically drawn
	// when the half move clock was one hundred and fifty or greater.
	SeventyFiveMoveRule
	// InsufficientMaterial indicates that the game was automatically drawn
	// because there was insufficient material for checkmate.
	InsufficientMaterial
)

func (Method) String

func (i Method) String() string

type Move

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

A Move is the movement of a piece from one square to another.

func (*Move) HasTag

func (m *Move) HasTag(tag MoveTag) bool

HasTag returns true if the move contains the MoveTag given.

func (*Move) Promo

func (m *Move) Promo() PieceType

Promo returns promotion piece type of the move.

func (*Move) S1

func (m *Move) S1() Square

S1 returns the origin square of the move.

func (*Move) S2

func (m *Move) S2() Square

S2 returns the destination square of the move.

func (*Move) String

func (m *Move) String() string

String returns a string useful for debugging. String doesn't return algebraic notation.

type MoveHistory

type MoveHistory struct {
	PrePosition  *Position
	PostPosition *Position
	Move         *Move
	Comments     []string
}

MoveHistory is a move's result from Game's MoveHistory method. It contains the move itself, any comments, and the pre and post positions.

type MoveTag

type MoveTag uint16

A MoveTag represents a notable consequence of a move.

const (
	// KingSideCastle indicates that the move is a king side castle.
	KingSideCastle MoveTag = 1 << iota
	// QueenSideCastle indicates that the move is a queen side castle.
	QueenSideCastle
	// Capture indicates that the move captures a piece.
	Capture
	// EnPassant indicates that the move captures via en passant.
	EnPassant
	// Check indicates that the move puts the opposing player in check.
	Check
)

type Notation

type Notation interface {
	Encoder
	Decoder
}

Notation is the interface implemented by objects that can encode and decode moves.

type Outcome

type Outcome string

A Outcome is the result of a game.

const (
	// NoOutcome indicates that a game is in progress or ended without a result.
	NoOutcome Outcome = "*"
	// WhiteWon indicates that white won the game.
	WhiteWon Outcome = "1-0"
	// BlackWon indicates that black won the game.
	BlackWon Outcome = "0-1"
	// Draw indicates that game was a draw.
	Draw Outcome = "1/2-1/2"
)

func (Outcome) String

func (o Outcome) String() string

String implements the fmt.Stringer interface

type Piece

type Piece int8

Piece is a piece type with a color.

const (
	// NoPiece represents no piece
	NoPiece Piece = iota
	// WhiteKing is a white king
	WhiteKing
	// WhiteQueen is a white queen
	WhiteQueen
	// WhiteRook is a white rook
	WhiteRook
	// WhiteBishop is a white bishop
	WhiteBishop
	// WhiteKnight is a white knight
	WhiteKnight
	// WhitePawn is a white pawn
	WhitePawn
	// BlackKing is a black king
	BlackKing
	// BlackQueen is a black queen
	BlackQueen
	// BlackRook is a black rook
	BlackRook
	// BlackBishop is a black bishop
	BlackBishop
	// BlackKnight is a black knight
	BlackKnight
	// BlackPawn is a black pawn
	BlackPawn
)

func NewPiece

func NewPiece(t PieceType, c Color) Piece

NewPiece returns the piece matching the PieceType and Color. NoPiece is returned if the PieceType or Color isn't valid.

func (Piece) Color

func (p Piece) Color() Color

Color returns the color of the piece.

func (Piece) String

func (p Piece) String() string

String implements the fmt.Stringer interface

func (Piece) Type

func (p Piece) Type() PieceType

Type returns the type of the piece.

type PieceType

type PieceType int8

PieceType is the type of a piece.

const (
	// NoPieceType represents a lack of piece type
	NoPieceType PieceType = iota
	// King represents a king
	King
	// Queen represents a queen
	Queen
	// Rook represents a rook
	Rook
	// Bishop represents a bishop
	Bishop
	// Knight represents a knight
	Knight
	// Pawn represents a pawn
	Pawn
)

func PieceTypes

func PieceTypes() [6]PieceType

PieceTypes returns a slice of all piece types.

func (PieceType) String

func (p PieceType) String() string

type Position

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

Position represents the state of the game without reguard to its outcome. Position is translatable to FEN notation.

func StartingPosition

func StartingPosition() *Position

StartingPosition returns the starting position rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

func (*Position) Board

func (pos *Position) Board() *Board

Board returns the position's board.

func (*Position) CastleRights

func (pos *Position) CastleRights() CastleRights

CastleRights returns the castling rights of the position.

func (*Position) EnPassantSquare

func (pos *Position) EnPassantSquare() Square

EnPassantSquare returns the en-passant square.

func (*Position) HalfMoveClock

func (pos *Position) HalfMoveClock() int

HalfMoveClock returns the half-move clock (50-rule).

func (*Position) Hash

func (pos *Position) Hash() [16]byte

Hash returns a unique hash of the position

func (*Position) InCheck

func (pos *Position) InCheck() bool

Returns true if position in check, otherwise false

func (*Position) MarshalBinary

func (pos *Position) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface

func (*Position) MarshalText

func (pos *Position) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface and encodes the position's FEN.

func (*Position) MoveCount

func (pos *Position) MoveCount() int

Returns move count for the position

func (*Position) NullMove

func (pos *Position) NullMove() *Position

Make a null move.

func (*Position) Status

func (pos *Position) Status() Method

Status returns the position's status as one of the outcome methods. Possible returns values include Checkmate, Stalemate, and NoMethod.

func (*Position) String

func (pos *Position) String() string

String implements the fmt.Stringer interface and returns a string with the FEN format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

func (*Position) Turn

func (pos *Position) Turn() Color

Turn returns the color to move next.

func (*Position) UnmarshalBinary

func (pos *Position) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryMarshaler interface

func (*Position) UnmarshalText

func (pos *Position) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnarshaler interface and assumes the data is in the FEN format.

func (*Position) Update

func (pos *Position) Update(m *Move) *Position

Update returns a new position resulting from the given move. The move itself isn't validated, if validation is needed use Game's Move method. This method is more performant for bots that rely on the ValidMoves because it skips redundant validation.

func (*Position) ValidMoves

func (pos *Position) ValidMoves() []*Move

ValidMoves returns a list of valid moves for the position.

type Rank

type Rank int8

A Rank is the rank of a square.

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

func (Rank) String

func (r Rank) String() string

type Scanner

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

Scanner is modeled on the bufio.Scanner type but instead of reading lines, it reads chess games from concatenated PGN files. It is designed to replace GamesFromPGN in order to handle very large PGN database files such as https://database.lichess.org/.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new scanner.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns an error encountered during scanning. Typically this will be a PGN parsing error or an io.EOF.

func (*Scanner) Next

func (s *Scanner) Next() *Game

Next returns the game from the most recent Scan.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan returns false if there was an error parsing a game or EOF was reached. Running scan populates data for Next() and Err().

type Side

type Side int

Side represents a side of the board.

const (
	// KingSide is the right side of the board from white's perspective.
	KingSide Side = iota + 1
	// QueenSide is the left side of the board from white's perspective.
	QueenSide
)

type Square

type Square int8

A Square is one of the 64 rank and file combinations that make up a chess board.

const (
	NoSquare Square = iota - 1
	A1
	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 creates a new Square from a File and a Rank

func (Square) File

func (sq Square) File() File

File returns the square's file.

func (Square) Rank

func (sq Square) Rank() Rank

Rank returns the square's rank.

func (Square) String

func (sq Square) String() string

type TagPair

type TagPair struct {
	Key   string
	Value string
}

TagPair represents metadata in a key value pairing used in the PGN format.

type UCINotation

type UCINotation struct{}

UCINotation is a more computer friendly alternative to algebraic notation. This notation uses the same format as the UCI (Universal Chess Interface). Examples: e2e4, e7e5, e1g1 (white short castling), e7e8q (for promotion)

func (UCINotation) Decode

func (UCINotation) Decode(pos *Position, s string) (*Move, error)

Decode implements the Decoder interface.

func (UCINotation) Encode

func (UCINotation) Encode(pos *Position, m *Move) string

Encode implements the Encoder interface.

func (UCINotation) String

func (UCINotation) String() string

String implements the fmt.Stringer interface and returns the notation's name.

Jump to

Keyboard shortcuts

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