chess

package module
v0.0.0-...-9e0bfa7 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2015 License: MIT Imports: 9 Imported by: 0

README

chess.go

Documentation

Index

Constants

View Source
const (
	NagNull = iota
	NagGoodMove
	NagMistake
	NagBrilliantMove
	NagBlunder
	NagSpeculativeMove
	NagDubiousMove
	NagForcedMove
	NagSingularMove
	NagWorstMove
	NagDrawishPosition
	NagQuietPosition
	NagActivePosition
	NagUnclearPosition
	NagWhiteSlightAdvantage
	NagBlackSlightAdvantage

	NagWhiteModerateCounterPlay = 132
	NagBlackModerateCounterPlay
	NagWhiteDecisiveCounterPlay
	NagBlackDecisiveCounterPlay
	NagWhiteModerateTimePressure
	NagBlackModerateTimePressure
	NagWhiteSevereTimePressure
	NagBlackSevereTimePressure
)
View Source
const (
	A1 = 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
)
View Source
const (
	CastlingNone          = 0
	CastlingWhiteKingSide = 1 << iota
	CastlingBlackKingSide
	CastlingWhiteQueenSide
	CastlingBlackQueenSide

	CastlingWhite = CastlingWhiteKingSide | CastlingWhiteQueenSide
	CastlingBlack = CastlingBlackKingSide | CastlingBlackQueenSide
	Castling      = CastlingWhite | CastlingBlack
)
View Source
const (
	BBA1 uint64 = 1 << iota
	BBB1
	BBC1
	BBD1
	BBE1
	BBF1
	BBG1
	BBH1
	BBA2
	BBB2
	BBC2
	BBD2
	BBE2
	BBF2
	BBG2
	BBH2
	BBA3
	BBB3
	BBC3
	BBD3
	BBE3
	BBF3
	BBG3
	BBH3
	BBA4
	BBB4
	BBC4
	BBD4
	BBE4
	BBF4
	BBG4
	BBH4
	BBA5
	BBB5
	BBC5
	BBD5
	BBE5
	BBF5
	BBG5
	BBH5
	BBA6
	BBB6
	BBC6
	BBD6
	BBE6
	BBF6
	BBG6
	BBH6
	BBA7
	BBB7
	BBC7
	BBD7
	BBE7
	BBF7
	BBG7
	BBH7
	BBA8
	BBB8
	BBC8
	BBD8
	BBE8
	BBF8
	BBG8
	BBH8
)
View Source
const (
	BBFileA = BBA1 | BBA2 | BBA3 | BBA4 | BBA5 | BBA6 | BBA7 | BBA8
	BBFileB = BBB1 | BBB2 | BBB3 | BBB4 | BBB5 | BBB6 | BBB7 | BBB8
	BBFileC = BBC1 | BBC2 | BBC3 | BBC4 | BBC5 | BBC6 | BBC7 | BBC8
	BBFileD = BBD1 | BBD2 | BBD3 | BBD4 | BBD5 | BBD6 | BBD7 | BBD8
	BBFileE = BBE1 | BBE2 | BBE3 | BBE4 | BBE5 | BBE6 | BBE7 | BBE8
	BBFileF = BBF1 | BBF2 | BBF3 | BBF4 | BBF5 | BBF6 | BBF7 | BBF8
	BBFileG = BBG1 | BBG2 | BBG3 | BBG4 | BBG5 | BBG6 | BBG7 | BBG8
	BBFileH = BBH1 | BBH2 | BBH3 | BBH4 | BBH5 | BBH6 | BBH7 | BBH8
)
View Source
const (
	BBRank1 = BBA1 | BBB1 | BBC1 | BBD1 | BBE1 | BBF1 | BBG1 | BBH1
	BBRank2 = BBA2 | BBB2 | BBC2 | BBD2 | BBE2 | BBF2 | BBG2 | BBH2
	BBRank3 = BBA3 | BBB3 | BBC3 | BBD3 | BBE3 | BBF3 | BBG3 | BBH3
	BBRank4 = BBA4 | BBB4 | BBC4 | BBD4 | BBE4 | BBF4 | BBG4 | BBH4
	BBRank5 = BBA5 | BBB5 | BBC5 | BBD5 | BBE5 | BBF5 | BBG5 | BBH5
	BBRank6 = BBA6 | BBB6 | BBC6 | BBD6 | BBE6 | BBF6 | BBG6 | BBH6
	BBRank7 = BBA7 | BBB7 | BBC7 | BBD7 | BBE7 | BBF7 | BBG7 | BBH7
	BBRank8 = BBA8 | BBB8 | BBC8 | BBD8 | BBE8 | BBF8 | BBG8 | BBH8
)
View Source
const BBAll uint64 = 0xffffffffffffffff
View Source
const BBVoid uint64 = 0
View Source
const StartingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"

The FEN notation of the standard chess starting position.

Variables

View Source
var BBDarkSquares = BBVoid
View Source
var BBFileAttacks [64][64]uint64
View Source
var BBKingAttacks []uint64
View Source
var BBKnightAttacks []uint64
View Source
var BBL45Attacks [64][64]uint64
View Source
var BBLightSquares = BBVoid
View Source
var BBPawnAll [2][64]uint64
View Source
var BBPawnAttacks [2][64]uint64
View Source
var BBPawnF1 [2][64]uint64
View Source
var BBPawnF2 [2][64]uint64
View Source
var BBR45Attacks [64][64]uint64
View Source
var BBRankAttacks [64][64]uint64
View Source
var BBShiftL45 = [...]uint{
	9, 17, 25, 33, 41, 49, 57, 1,
	17, 25, 33, 41, 49, 57, 1, 10,
	25, 33, 41, 49, 57, 1, 10, 19,
	33, 41, 49, 57, 1, 10, 19, 28,
	41, 49, 57, 1, 10, 19, 28, 37,
	49, 57, 1, 10, 19, 28, 37, 46,
	57, 1, 10, 19, 28, 37, 46, 55,
	1, 10, 19, 28, 37, 46, 55, 64,
}
View Source
var BBShiftR45 = [...]uint{
	1, 58, 51, 44, 37, 30, 23, 16,
	9, 1, 58, 51, 44, 37, 30, 23,
	17, 9, 1, 58, 51, 44, 37, 30,
	25, 17, 9, 1, 58, 51, 44, 37,
	33, 25, 17, 9, 1, 58, 51, 44,
	41, 33, 25, 17, 9, 1, 58, 51,
	49, 41, 33, 25, 17, 9, 1, 58,
	57, 49, 41, 33, 25, 17, 9, 1,
}
View Source
var BBSquaresL45 [64]uint64
View Source
var BBSquaresL90 [64]uint64
View Source
var BBSquaresR45 [64]uint64
View Source
var FenCastlingRegex = regexp.MustCompile("^(KQ?k?q?|Qk?q?|kq?|q|-)$")
View Source
var FileNames = [...]string{
	"a", "b", "c", "d", "e", "f", "g", "h",
}
View Source
var MoveTextRegex = regexp.MustCompile("(?s)(%.*?[\\n\\r])|(\\{.*)|(\\$[0-9]+)|(\\()|(\\))|(\\*|1-0|0-1|1/2-1/2)|([NBKRQ]?[a-h]?[1-8]?[\\-x]?[a-h][1-8](?:=[nbrqNBRQ])?|--|O-O(?:-O)?|0-0(?:-0)?)|([\\?!]{1,2})")
View Source
var PieceSymbols = [...]string{
	None:   "",
	Pawn:   "p",
	Knight: "n",
	Bishop: "b",
	Rook:   "r",
	Queen:  "q",
	King:   "k",
}
View Source
var PolyglotRandomArray = []uint64{}/* 781 elements not displayed */
View Source
var SanRegex = regexp.MustCompile("^([NBKRQ])?([a-h])?([1-8])?x?([a-h][1-8])(=[nbrqNBRQ])?(\\+|#)?$")
View Source
var SquareNames = [...]string{
	"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",
}
View Source
var Squares = [...]int{
	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,
}
View Source
var Squares180 = [...]int{
	A8, B8, C8, D8, E8, F8, G8, H8,
	A7, B7, C7, D7, E7, F7, G7, H7,
	A6, B6, C6, D6, E6, F6, G6, H6,
	A5, B5, C5, D5, E5, F5, G5, H5,
	A4, B4, C4, D4, E4, F4, G4, H4,
	A3, B3, C3, D3, E3, F3, G3, H3,
	A2, B2, C2, D2, E2, F2, G2, H2,
	A1, B1, C1, D1, E1, F1, G1, H1,
}
View Source
var SquaresL45 = [...]int{
	A2, B3, C4, D5, E6, F7, G8, H1,
	A3, B4, C5, D6, E7, F8, G1, H2,
	A4, B5, C6, D7, E8, F1, G2, H3,
	A5, B6, C7, D8, E1, F2, G3, H4,
	A6, B7, C8, D1, E2, F3, G4, H5,
	A7, B8, C1, D2, E3, F4, G5, H6,
	A8, B1, C2, D3, E4, F5, G6, H7,
	A1, B2, C3, D4, E5, F6, G7, H8,
}
View Source
var SquaresL90 = [...]int{
	H1, H2, H3, H4, H5, H6, H7, H8,
	G1, G2, G3, G4, G5, G6, G7, G8,
	F1, F2, F3, F4, F5, F6, F7, F8,
	E1, E2, E3, E4, E5, E6, E7, E8,
	D1, D2, D3, D4, D5, D6, D7, D8,
	C1, C2, C3, C4, C5, C6, C7, C8,
	B1, B2, B3, B4, B5, B6, B7, B8,
	A1, A2, A3, A4, A5, A6, A7, A8,
}
View Source
var SquaresR45 = [...]int{
	A1, B8, C7, D6, E5, F4, G3, H2,
	A2, B1, C8, D7, E6, F5, G4, H3,
	A3, B2, C1, D8, E7, F6, G5, H4,
	A4, B3, C2, D1, E8, F7, G6, H5,
	A5, B4, C3, D2, E1, F8, G7, H6,
	A6, B5, C4, D3, E2, F1, G8, H7,
	A7, B6, C5, D4, E3, F2, G1, H8,
	A8, B7, C6, D5, E4, F3, G2, H1,
}
View Source
var TagRegex = regexp.MustCompile("\\[([A-Za-z0-9]+)\\s+\"(.*)\"\\]")

Functions

func ScanHeaders

func ScanHeaders(handle io.ReadSeeker) ([]int64, []map[string]string)

Scan a PGN from io.Reader for game offsets and headers.

Returns an array of offsets for the games a map for game headers.

Since actually parsing many games from a big file is relatively expensive, this is a better way to look only for specific games and seek and parse them later.

This example scans for the first game with Kasparov as the white player.

pgn, _ := os.Open("mega.pgn")
offsets, headers := chess.ScanHeaders(pgn, 0)
for index, header := range headers {
    if strings.Contains(header["White"], "Kasparov") {
        kasparovOffset = offsets[index]
        break
    }
}

Then it can later be seeked an parsed.

pgn.Seek(kasparovOffset, 0)
game = chess.ReadGame(pgn)

Be careful when seeking a game in the file while more offsets are being generated.

func ScanOffsets

func ScanOffsets(handle io.ReadSeeker) []int64

Scan a PGN for game offsets.

Returns the starting offsets of all the games, so that they can be seeked later. This is just like `ScanHeaders()` but more efficient if you do not actually need the header information.

The PGN standard requires each game to start with an Event-tag. So does this scanner.

Types

type Bitboard

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

A bitboard and additional information representing a position.

Provides move generation, validation, parsing, attack generation, game end detection, move counters and the capability to make and unmake moves.

The bitboard is initialized to the starting position, unless otherwise specified in the optional `fen` argument.

func NewBitboard

func NewBitboard(fen string) *Bitboard

func (*Bitboard) AttackerMask

func (b *Bitboard) AttackerMask(color Colors, square int) uint64

func (*Bitboard) Attackers

func (b *Bitboard) Attackers(color Colors, square int) *SquareSet

Gets a set of attackers of the given color for the given square.

Returns a set of squares.

func (*Bitboard) BishopAttacksFrom

func (b *Bitboard) BishopAttacksFrom(square int) uint64

func (*Bitboard) BoardZobristHash

func (b *Bitboard) BoardZobristHash(array []uint64) uint64

func (*Bitboard) CanClaimDraw

func (b *Bitboard) CanClaimDraw() bool

Checks if the side to move can claim a draw by the fifty-move rule or by threefold repitition.

func (*Bitboard) CanClaimFiftyMoves

func (b *Bitboard) CanClaimFiftyMoves() bool

Draw by the fifty-move rule can be claimed once the clock of halfmoves since the last capture or pawn move becomes equal or greater to 100 and the side to move still has a legal move they can make.

func (*Bitboard) CanClaimThreefoldRepitition

func (b *Bitboard) CanClaimThreefoldRepitition() bool

Draw by threefold repitition can be claimed if the position on the board occured for the third time or if such a repitition is reached with one of the possible legal moves.

func (*Bitboard) CheckSquareColor

func (b *Bitboard) CheckSquareColor(square int) Colors

func (*Bitboard) Clear

func (b *Bitboard) Clear()

Clears the board.

Resets move stacks and move counters. The side to move is white. There are no rooks or kings, so castling is not allowed.

In order to be in a valid `status()` at least kings need to be put on the board. This is required for move generation and validation to work properly.

func (*Bitboard) Epd

func (b *Bitboard) Epd(operations map[string]interface{}) string

Gets an EPD representation of the current position.

EPD operations can be given as keyword arguments. Supported operands are strings, integers, floats and moves. All other operands are converted to strings.

`hmvc` and `fmvc` are *not* included by default. You can use:

board.epd(map[string]interface{}{"hmvc":board.halfMoveClock, "fmvc":board.fullMoveNumber});

func (*Bitboard) Fen

func (b *Bitboard) Fen() string

Gets the FEN representation of the position.

func (*Bitboard) GenerateLegalMoves

func (b *Bitboard) GenerateLegalMoves(castling, pawns, knights, bishops, rooks, queens, kings bool) []*Move

func (*Bitboard) GeneratePseudoLegalMoves

func (b *Bitboard) GeneratePseudoLegalMoves(castling, pawns, knights, bishops, rooks, queens, king bool) []*Move

func (*Bitboard) GetPieces

func (b *Bitboard) GetPieces() [64]PieceTypes

func (*Bitboard) GetTurn

func (b *Bitboard) GetTurn() Colors

func (*Bitboard) IsAttackedBy

func (b *Bitboard) IsAttackedBy(color Colors, square int) bool

Checks if the given side attacks the given square. Pinned pieces still count as attackers.

func (*Bitboard) IsCheck

func (b *Bitboard) IsCheck() bool

Checks if the current side to move is in check.

func (*Bitboard) IsCheckmate

func (b *Bitboard) IsCheckmate() bool

Checks if the current position is a checkmate.

func (*Bitboard) IsFivefoldRepitition

func (b *Bitboard) IsFivefoldRepitition() bool

Since the first of July 2014 a game is automatically drawn (without a claim by one of the players) if a position occurs for the fifth time on consecutive alternating moves.

func (*Bitboard) IsGameOver

func (b *Bitboard) IsGameOver() bool

Checks if the game is over due to checkmate, stalemate, insufficient mating material, the seventyfive-move rule or fivefold repitition.

func (*Bitboard) IsInsufficientMaterial

func (b *Bitboard) IsInsufficientMaterial() bool

Checks for a draw due to insufficient mating material.

func (*Bitboard) IsIntoCheck

func (b *Bitboard) IsIntoCheck(move *Move) bool

Checks if the given move would move would leave the king in check or put it into check.

func (*Bitboard) IsLegal

func (b *Bitboard) IsLegal(move *Move) bool

func (*Bitboard) IsPseudoLegal

func (b *Bitboard) IsPseudoLegal(move *Move) bool

func (*Bitboard) IsSeventyfiveMoves

func (b *Bitboard) IsSeventyfiveMoves() bool

Since the first of July 2014 a game is automatically drawn (without a claim by one of the players) if the half move clock since a capture or pawn move is equal to or grather than 150. Other means to end a game take precedence.

func (*Bitboard) IsStalemate

func (b *Bitboard) IsStalemate() bool

Checks if the current position is a stalemate.

func (*Bitboard) KingAttacksFrom

func (b *Bitboard) KingAttacksFrom(square int) uint64

func (*Bitboard) KnightAttacksFrom

func (b *Bitboard) KnightAttacksFrom(square int) uint64

func (*Bitboard) ParseSan

func (b *Bitboard) ParseSan(san string) (*Move, error)

Uses the current position as the context to parse a move in standard algebraic notation and return the corresponding move object.

The returned move is guaranteed to be either legal or a null move.

Returns an error if the SAN is invalid or ambiguous.

func (*Bitboard) PawnMovesFrom

func (b *Bitboard) PawnMovesFrom(square int) uint64

func (*Bitboard) Peek

func (b *Bitboard) Peek() *Move

Gets the last move from the move stack.

func (*Bitboard) PieceAt

func (b *Bitboard) PieceAt(square int) *Piece

Gets the piece at the given square.

func (*Bitboard) PieceTypeAt

func (b *Bitboard) PieceTypeAt(square int) PieceTypes

Gets the piece type at the given square.

func (*Bitboard) Pop

func (b *Bitboard) Pop() *Move

Restores the previous position and returns the last move from the stack.

func (*Bitboard) PseudoLegalMoveCount

func (b *Bitboard) PseudoLegalMoveCount() int

In a way duplicates GeneratePseudoLegalMoves() in order to use population counts instead of counting actually yielded moves.

func (*Bitboard) Push

func (b *Bitboard) Push(move *Move)

Updates the position with the given move and puts it onto a stack.

Null moves just increment the move counters, switch turns and forfeit en passant capturing.

No validation is performed. For performance moves are assumed to be at least pseudo legal. Otherwise there is no guarantee that the previous board state can be restored.

func (*Bitboard) PushSan

func (b *Bitboard) PushSan(san string) (*Move, error)

Parses a move in standard algebraic notation, makes the move and puts it on the the move stack.

Returns an error if neither legal nor a null move.

Returns the move.

func (*Bitboard) QueenAttacksFrom

func (b *Bitboard) QueenAttacksFrom(square int) uint64

func (*Bitboard) RemovePieceAt

func (b *Bitboard) RemovePieceAt(square int)

Removes a piece from the given square if present.

func (*Bitboard) Reset

func (b *Bitboard) Reset()

Restores the starting position.

func (*Bitboard) RookAttacksFrom

func (b *Bitboard) RookAttacksFrom(square int) uint64

func (*Bitboard) San

func (b *Bitboard) San(move *Move) string

Gets the standard algebraic notation of the given move in the context of the current position.

There is no validation. It is only guaranteed to work if the move is legal or a null move.

func (*Bitboard) SetEpd

func (b *Bitboard) SetEpd(epd string) (map[string]interface{}, error)

Parses the given EPD string and uses it to set the position.

If present the `hmvc` and the `fmvn` are used to set the half move clock and the fullmove number. Otherwise `0` and `1` are used.

Returns a dictionary of parsed operations. Values can be strings, integers, floats or move objects.

Returns nil if the EPD string is invalid.

func (*Bitboard) SetFen

func (b *Bitboard) SetFen(fen string) error

Parses a FEN and sets the position from it.

Returns an error if the FEN string is invalid.

func (*Bitboard) SetPieceAt

func (b *Bitboard) SetPieceAt(square int, piece *Piece)

Sets a piece at the given square. An existing piece is replaced.

func (*Bitboard) Status

func (b *Bitboard) Status() Status

Gets a bitmask of possible problems with the position. Move making, generation and validation are only guaranteed to work on a completely valid board.

func (*Bitboard) String

func (b *Bitboard) String() string

func (*Bitboard) WasIntoCheck

func (b *Bitboard) WasIntoCheck() bool

Checks if the king of the other side is attacked. Such a position is not valid and could only be reached by an illegal move.

func (*Bitboard) ZobristHash

func (b *Bitboard) ZobristHash(array []uint64) uint64

Returns a Zobrist hash of the current position.

A zobrist hash is an exclusive or of pseudo random values picked from an array. Which values are picked is decided by features of the position, such as piece positions, castling rights and en-passant squares. For this implementation an array of 781 values is required.

The default behaviour is to use values from `PolyglotRandomArray`, which makes for hashes compatible with polyglot opening books.

type Colors

type Colors int
const (
	White Colors = iota
	Black
)

type Element

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

type Exporter

type Exporter interface {
	PutFullMoveNumber(turn Colors, fullMoveNumber int, afterVariation bool)
	PutMove(board *Bitboard, move *Move)
	PutNags(nags []int)
	PutComment(comment string)
	StartVariation()
	PutStartingComment(startingComment string)
	EndVariation()
	StartGame()
	StartHeaders()
	PutHeader(tagname, tagvalue string)
	EndHeaders()
	PutResult(result string)
	EndGame()
}

type FileExporter

type FileExporter struct {
	*StringExporter
	// contains filtered or unexported fields
}

Like a StringExporter, but games are written directly to a text file.

There will always be a blank line after each game. Handling encodings is up to the caller.

newPgn, _ := os.Create("new.pgn")
exporter := NewFileExporter(newPgn)
game.Export(exporter, true, true, nil, false)

func NewFileExporter

func NewFileExporter(handle *os.File, columns int) *FileExporter

func (*FileExporter) EndGame

func (s *FileExporter) EndGame()

func (*FileExporter) EndHeaders

func (s *FileExporter) EndHeaders()

func (*FileExporter) EndVariation

func (s *FileExporter) EndVariation()

func (*FileExporter) File

func (s *FileExporter) File() string

func (*FileExporter) FlushCurrentLine

func (f *FileExporter) FlushCurrentLine()

func (*FileExporter) PutComment

func (s *FileExporter) PutComment(comment string)

func (*FileExporter) PutFullMoveNumber

func (s *FileExporter) PutFullMoveNumber(turn Colors, fullMoveNumber int, variationStart bool)

func (*FileExporter) PutHeader

func (s *FileExporter) PutHeader(tagname, tagvalue string)

func (*FileExporter) PutMove

func (s *FileExporter) PutMove(board *Bitboard, move *Move)

func (*FileExporter) PutNag

func (s *FileExporter) PutNag(nag int)

func (*FileExporter) PutNags

func (s *FileExporter) PutNags(nags []int)

func (*FileExporter) PutResult

func (s *FileExporter) PutResult(result string)

func (*FileExporter) PutStartingComment

func (s *FileExporter) PutStartingComment(comment string)

func (*FileExporter) StartGame

func (s *FileExporter) StartGame()

func (*FileExporter) StartHeaders

func (s *FileExporter) StartHeaders()

func (*FileExporter) StartVariation

func (s *FileExporter) StartVariation()

func (*FileExporter) WriteLine

func (f *FileExporter) WriteLine(line string)

func (*FileExporter) WriteToken

func (s *FileExporter) WriteToken(token string)

type GameNode

type GameNode struct {
	Headers map[string]string
	// contains filtered or unexported fields
}

func NewGame

func NewGame() *GameNode

func (*GameNode) AddMainVariation

func (g *GameNode) AddMainVariation(move *Move, comment string) *GameNode

Creates a child node with the given attributes and promotes it to the main variation.

func (*GameNode) AddVariation

func (g *GameNode) AddVariation(move *Move, comment, startingComment string, nags []int) *GameNode

Creates a child nodew with the given attributes.

func (*GameNode) Board

func (g *GameNode) Board() *Bitboard

Gets a bitboard with the position of the node. If it's a parent, it will get the starting position of the game as a bitboard.

Unless the `SetUp` and `FEN` header tags are set this is the default starting position.

It's a copy, so modifying the board will not alter the game.

func (*GameNode) Demote

func (g *GameNode) Demote(move *Move)

Moves the given variation one down in the list of variations.

func (*GameNode) End

func (g *GameNode) End() *GameNode

Follows the main variation to the end and returns the last node.

func (*GameNode) Export

func (g *GameNode) Export(exporter Exporter, comments, variations bool, board *Bitboard, afterVariation, headers bool)

func (*GameNode) GetParent

func (g *GameNode) GetParent() *GameNode

func (*GameNode) HasVariation

func (g *GameNode) HasVariation(move *Move) bool

Checks if the given move appears as a variation.

func (*GameNode) IsMainLine

func (g *GameNode) IsMainLine() bool

Checks if the node is in the main line of the game.

func (*GameNode) IsMainVariation

func (g *GameNode) IsMainVariation() bool

Checks if this node is the first variation from the point of view of its parent. The root node also is in the main variation.

func (*GameNode) Promote

func (g *GameNode) Promote(move *Move)

Moves the given variation one up in the list of variations.

func (*GameNode) PromoteToMain

func (g *GameNode) PromoteToMain(move *Move)

Promotes the given move to the main variation.

func (*GameNode) RemoveVariation

func (g *GameNode) RemoveVariation(move *Move)

Removes a variation by move.

func (*GameNode) Root

func (g *GameNode) Root() *GameNode

Gets the root node, i.e. the game.

func (*GameNode) San

func (g *GameNode) San() string

Gets the standard algebraic notation of the move leading to this node.

Do not call this on the root node.

func (*GameNode) Setup

func (g *GameNode) Setup(board *Bitboard)

Setup a specific starting position. This sets (or resets) the `SetUp` and `FEN` header tags.

func (*GameNode) StartsVariation

func (g *GameNode) StartsVariation() bool

Checks if this node starts a variation (and can thus have a starting comment). The root node does not start a variation and can have no starting comment.

func (*GameNode) String

func (g *GameNode) String() string

func (*GameNode) VariationByIndex

func (g *GameNode) VariationByIndex(index int) (*GameNode, error)

Gets a child node by index.

func (*GameNode) VariationByMove

func (g *GameNode) VariationByMove(move *Move) (*GameNode, int, error)

Gets a child node by move.

type Move

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

Represents a move from a square to a square and possibly the promotion piece type.

Castling moves are identified only by the movement of the king.

Null moves are supported.

func MoveFromUci

func MoveFromUci(uci string) *Move

Parses an UCI string.

Returns nil if the UCI string is invalid.

func NewMove

func NewMove(fromSquare, toSquare int, promotion PieceTypes) *Move

func NullMove

func NullMove() *Move

Gets a null move.

A null move just passes the turn to the other side (and possibly forfeits en-passant capturing).

func (*Move) Equals

func (m *Move) Equals(move *Move) bool

func (*Move) String

func (m *Move) String() string

func (*Move) Uci

func (m *Move) Uci() string

Gets an UCI string for the move.

For example a move from A7 to A8 would be `a7a8` or `a7a8q` if it is a promotion to a queen. The UCI representatin of null moves is `0000`.

type PGNReader

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

func NewPGNReader

func NewPGNReader(handle io.ReadSeeker) *PGNReader

func (*PGNReader) Next

func (r *PGNReader) Next() bool

Reads a game from an io.Reader interface.

pgn, _ := os.Open("data/games/kasparov-deep-blue-1997.pgn")
firstGame, _ := chess.ReadGame(pgn)
secondGame, _ := chess.ReadGame(pgn)

fmt.Println(firstGame.Headers["Event"]) // IBM Man-Machine, New York USA

Use `strings.Reader` to parse games from a string.

pgnString := "1. e4 e5 2. Nf3 *"
pgn := strings.NewReader(pgnString)
game, _ := chess.ReadGame(pgn)

The end of a game is determined by a completely blank line or the end of the file. (Of course blank lines in comments are possible.)

According to the standard at least the usual 7 header tags are required for a valid game. This parser also handles games without any headers just fine.

The parser is relatively forgiving when it comes to errors. It skips over tokens it can not parse. However it is difficult to handle illegal or ambiguous moves. If such a move is encountered the default behaviour is to stop right in the middle of the game and return an error.

Returns the parsed game or nil if the EOF is reached.

func (*PGNReader) Scan

func (r *PGNReader) Scan() (*GameNode, error)

type Piece

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

A piece with type and color.

func NewPiece

func NewPiece(pieceType PieceTypes, color Colors) *Piece

func PieceFromSymbol

func PieceFromSymbol(symbol string) *Piece

Creates a piece instance from a piece symbol. Returns nil if the symbol is invalid.

func (*Piece) String

func (p *Piece) String() string

Gets the symbol `P`, `N`, `B`, `R`, `Q` or `K` for white pieces or the lower-case variants for the black pieces.

type PieceTypes

type PieceTypes int
const (
	None PieceTypes = iota
	Pawn
	Knight
	Bishop
	Rook
	Queen
	King
)

type SquareSet

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

func NewSquareSet

func NewSquareSet(mask uint64) *SquareSet

func (*SquareSet) Iter

func (s *SquareSet) Iter() <-chan int

type Stack

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

func (*Stack) Len

func (s *Stack) Len() int

Return the stack's length

func (*Stack) Pop

func (s *Stack) Pop() (value interface{})

Remove the top element from the stack and return it's value If the stack is empty, return nil

func (*Stack) Push

func (s *Stack) Push(value interface{})

Push a new element onto the stack

type Status

type Status int
const (
	StatusValid Status = 1 << iota
	StatusNoWhiteKing
	StatusNoBlackKing
	StatusTooManyKings
	StatusTooManyWhitePawns
	StatusTooManyBlackPawns
	StatusPawnsOnBackrank
	StatusTooManyWhitePieces
	StatusTooManyBlackPieces
	StatusBadCastlingRights
	StatusInvalidEpSquare
	StatusOppositeCheck
)

type StringExporter

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

Allows exporting a game as a string.

The export method of `Game` also provides options to include or exclude headers, variations or comments. By default everything is included.

exporter := NewStringExporter(0)
game.Export(exporter, true, true, true)
pgnString = exporter.String()

Only `columns` characters are written per line. If `columns` is `None` then the entire movetext will be on a single line. This does not affect header tags and comments.

There will be no newlines at the end of the string.

func NewStringExporter

func NewStringExporter(columns int) *StringExporter

func (*StringExporter) EndGame

func (s *StringExporter) EndGame()

func (*StringExporter) EndHeaders

func (s *StringExporter) EndHeaders()

func (*StringExporter) EndVariation

func (s *StringExporter) EndVariation()

func (*StringExporter) FlushCurrentLine

func (s *StringExporter) FlushCurrentLine()

func (*StringExporter) PutComment

func (s *StringExporter) PutComment(comment string)

func (*StringExporter) PutFullMoveNumber

func (s *StringExporter) PutFullMoveNumber(turn Colors, fullMoveNumber int, variationStart bool)

func (*StringExporter) PutHeader

func (s *StringExporter) PutHeader(tagname, tagvalue string)

func (*StringExporter) PutMove

func (s *StringExporter) PutMove(board *Bitboard, move *Move)

func (*StringExporter) PutNag

func (s *StringExporter) PutNag(nag int)

func (*StringExporter) PutNags

func (s *StringExporter) PutNags(nags []int)

func (*StringExporter) PutResult

func (s *StringExporter) PutResult(result string)

func (*StringExporter) PutStartingComment

func (s *StringExporter) PutStartingComment(comment string)

func (*StringExporter) StartGame

func (s *StringExporter) StartGame()

func (*StringExporter) StartHeaders

func (s *StringExporter) StartHeaders()

func (*StringExporter) StartVariation

func (s *StringExporter) StartVariation()

func (*StringExporter) String

func (s *StringExporter) String() string

func (*StringExporter) WriteLine

func (s *StringExporter) WriteLine(line string)

func (*StringExporter) WriteToken

func (s *StringExporter) WriteToken(token string)

Jump to

Keyboard shortcuts

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