Documentation
¶
Index ¶
- Constants
- Variables
- func ScanHeaders(handle io.ReadSeeker) ([]int64, []map[string]string)
- func ScanOffsets(handle io.ReadSeeker) []int64
- type Bitboard
- func (b *Bitboard) AttackerMask(color Colors, square int) uint64
- func (b *Bitboard) Attackers(color Colors, square int) *SquareSet
- func (b *Bitboard) BishopAttacksFrom(square int) uint64
- func (b *Bitboard) BoardZobristHash(array []uint64) uint64
- func (b *Bitboard) CanClaimDraw() bool
- func (b *Bitboard) CanClaimFiftyMoves() bool
- func (b *Bitboard) CanClaimThreefoldRepitition() bool
- func (b *Bitboard) CheckSquareColor(square int) Colors
- func (b *Bitboard) Clear()
- func (b *Bitboard) Epd(operations map[string]interface{}) string
- func (b *Bitboard) Fen() string
- func (b *Bitboard) GenerateLegalMoves(castling, pawns, knights, bishops, rooks, queens, kings bool) []*Move
- func (b *Bitboard) GeneratePseudoLegalMoves(castling, pawns, knights, bishops, rooks, queens, king bool) []*Move
- func (b *Bitboard) GetPieces() [64]PieceTypes
- func (b *Bitboard) GetTurn() Colors
- func (b *Bitboard) IsAttackedBy(color Colors, square int) bool
- func (b *Bitboard) IsCheck() bool
- func (b *Bitboard) IsCheckmate() bool
- func (b *Bitboard) IsFivefoldRepitition() bool
- func (b *Bitboard) IsGameOver() bool
- func (b *Bitboard) IsInsufficientMaterial() bool
- func (b *Bitboard) IsIntoCheck(move *Move) bool
- func (b *Bitboard) IsLegal(move *Move) bool
- func (b *Bitboard) IsPseudoLegal(move *Move) bool
- func (b *Bitboard) IsSeventyfiveMoves() bool
- func (b *Bitboard) IsStalemate() bool
- func (b *Bitboard) KingAttacksFrom(square int) uint64
- func (b *Bitboard) KnightAttacksFrom(square int) uint64
- func (b *Bitboard) ParseSan(san string) (*Move, error)
- func (b *Bitboard) PawnMovesFrom(square int) uint64
- func (b *Bitboard) Peek() *Move
- func (b *Bitboard) PieceAt(square int) *Piece
- func (b *Bitboard) PieceTypeAt(square int) PieceTypes
- func (b *Bitboard) Pop() *Move
- func (b *Bitboard) PseudoLegalMoveCount() int
- func (b *Bitboard) Push(move *Move)
- func (b *Bitboard) PushSan(san string) (*Move, error)
- func (b *Bitboard) QueenAttacksFrom(square int) uint64
- func (b *Bitboard) RemovePieceAt(square int)
- func (b *Bitboard) Reset()
- func (b *Bitboard) RookAttacksFrom(square int) uint64
- func (b *Bitboard) San(move *Move) string
- func (b *Bitboard) SetEpd(epd string) (map[string]interface{}, error)
- func (b *Bitboard) SetFen(fen string) error
- func (b *Bitboard) SetPieceAt(square int, piece *Piece)
- func (b *Bitboard) Status() Status
- func (b *Bitboard) String() string
- func (b *Bitboard) WasIntoCheck() bool
- func (b *Bitboard) ZobristHash(array []uint64) uint64
- type Colors
- type Element
- type Exporter
- type FileExporter
- func (s *FileExporter) EndGame()
- func (s *FileExporter) EndHeaders()
- func (s *FileExporter) EndVariation()
- func (s *FileExporter) File() string
- func (f *FileExporter) FlushCurrentLine()
- func (s *FileExporter) PutComment(comment string)
- func (s *FileExporter) PutFullMoveNumber(turn Colors, fullMoveNumber int, variationStart bool)
- func (s *FileExporter) PutHeader(tagname, tagvalue string)
- func (s *FileExporter) PutMove(board *Bitboard, move *Move)
- func (s *FileExporter) PutNag(nag int)
- func (s *FileExporter) PutNags(nags []int)
- func (s *FileExporter) PutResult(result string)
- func (s *FileExporter) PutStartingComment(comment string)
- func (s *FileExporter) StartGame()
- func (s *FileExporter) StartHeaders()
- func (s *FileExporter) StartVariation()
- func (f *FileExporter) WriteLine(line string)
- func (s *FileExporter) WriteToken(token string)
- type GameNode
- func (g *GameNode) AddMainVariation(move *Move, comment string) *GameNode
- func (g *GameNode) AddVariation(move *Move, comment, startingComment string, nags []int) *GameNode
- func (g *GameNode) Board() *Bitboard
- func (g *GameNode) Demote(move *Move)
- func (g *GameNode) End() *GameNode
- func (g *GameNode) Export(exporter Exporter, comments, variations bool, board *Bitboard, ...)
- func (g *GameNode) GetParent() *GameNode
- func (g *GameNode) HasVariation(move *Move) bool
- func (g *GameNode) IsMainLine() bool
- func (g *GameNode) IsMainVariation() bool
- func (g *GameNode) Promote(move *Move)
- func (g *GameNode) PromoteToMain(move *Move)
- func (g *GameNode) RemoveVariation(move *Move)
- func (g *GameNode) Root() *GameNode
- func (g *GameNode) San() string
- func (g *GameNode) Setup(board *Bitboard)
- func (g *GameNode) StartsVariation() bool
- func (g *GameNode) String() string
- func (g *GameNode) VariationByIndex(index int) (*GameNode, error)
- func (g *GameNode) VariationByMove(move *Move) (*GameNode, int, error)
- type Move
- type PGNReader
- type Piece
- type PieceTypes
- type SquareSet
- type Stack
- type Status
- type StringExporter
- func (s *StringExporter) EndGame()
- func (s *StringExporter) EndHeaders()
- func (s *StringExporter) EndVariation()
- func (s *StringExporter) FlushCurrentLine()
- func (s *StringExporter) PutComment(comment string)
- func (s *StringExporter) PutFullMoveNumber(turn Colors, fullMoveNumber int, variationStart bool)
- func (s *StringExporter) PutHeader(tagname, tagvalue string)
- func (s *StringExporter) PutMove(board *Bitboard, move *Move)
- func (s *StringExporter) PutNag(nag int)
- func (s *StringExporter) PutNags(nags []int)
- func (s *StringExporter) PutResult(result string)
- func (s *StringExporter) PutStartingComment(comment string)
- func (s *StringExporter) StartGame()
- func (s *StringExporter) StartHeaders()
- func (s *StringExporter) StartVariation()
- func (s *StringExporter) String() string
- func (s *StringExporter) WriteLine(line string)
- func (s *StringExporter) WriteToken(token string)
Constants ¶
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 )
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 )
const ( CastlingNone = 0 CastlingWhiteKingSide = 1 << iota CastlingBlackKingSide CastlingWhiteQueenSide CastlingBlackQueenSide CastlingWhite = CastlingWhiteKingSide | CastlingWhiteQueenSide CastlingBlack = CastlingBlackKingSide | CastlingBlackQueenSide Castling = CastlingWhite | CastlingBlack )
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 )
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 )
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 )
const BBAll uint64 = 0xffffffffffffffff
const BBVoid uint64 = 0
const StartingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
The FEN notation of the standard chess starting position.
Variables ¶
var BBDarkSquares = BBVoid
var BBFileAttacks [64][64]uint64
var BBFiles = [...]uint64{ BBFileA, BBFileB, BBFileC, BBFileD, BBFileE, BBFileF, BBFileG, BBFileH, }
var BBKingAttacks []uint64
var BBKnightAttacks []uint64
var BBL45Attacks [64][64]uint64
var BBLightSquares = BBVoid
var BBPawnAll [2][64]uint64
var BBPawnAttacks [2][64]uint64
var BBPawnF1 [2][64]uint64
var BBPawnF2 [2][64]uint64
var BBR45Attacks [64][64]uint64
var BBRankAttacks [64][64]uint64
var BBRanks = [...]uint64{ BBRank1, BBRank2, BBRank3, BBRank4, BBRank5, BBRank6, BBRank7, BBRank8, }
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,
}
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,
}
var BBSquares = [...]uint64{ BBA1, 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, }
var BBSquaresL45 [64]uint64
var BBSquaresL90 [64]uint64
var BBSquaresR45 [64]uint64
var FenCastlingRegex = regexp.MustCompile("^(KQ?k?q?|Qk?q?|kq?|q|-)$")
var FileNames = [...]string{
"a", "b", "c", "d", "e", "f", "g", "h",
}
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})")
var PieceSymbols = [...]string{ None: "", Pawn: "p", Knight: "n", Bishop: "b", Rook: "r", Queen: "q", King: "k", }
var PolyglotRandomArray = []uint64{}/* 781 elements not displayed */
var SanRegex = regexp.MustCompile("^([NBKRQ])?([a-h])?([1-8])?x?([a-h][1-8])(=[nbrqNBRQ])?(\\+|#)?$")
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",
}
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, }
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, }
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, }
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, }
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, }
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 (*Bitboard) Attackers ¶
Gets a set of attackers of the given color for the given square.
Returns a set of squares.
func (*Bitboard) BishopAttacksFrom ¶
func (*Bitboard) BoardZobristHash ¶
func (*Bitboard) CanClaimDraw ¶
Checks if the side to move can claim a draw by the fifty-move rule or by threefold repitition.
func (*Bitboard) CanClaimFiftyMoves ¶
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 ¶
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 (*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 ¶
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) GenerateLegalMoves ¶
func (*Bitboard) GeneratePseudoLegalMoves ¶
func (*Bitboard) GetPieces ¶
func (b *Bitboard) GetPieces() [64]PieceTypes
func (*Bitboard) IsAttackedBy ¶
Checks if the given side attacks the given square. Pinned pieces still count as attackers.
func (*Bitboard) IsCheckmate ¶
Checks if the current position is a checkmate.
func (*Bitboard) IsFivefoldRepitition ¶
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 ¶
Checks if the game is over due to checkmate, stalemate, insufficient mating material, the seventyfive-move rule or fivefold repitition.
func (*Bitboard) IsInsufficientMaterial ¶
Checks for a draw due to insufficient mating material.
func (*Bitboard) IsIntoCheck ¶
Checks if the given move would move would leave the king in check or put it into check.
func (*Bitboard) IsPseudoLegal ¶
func (*Bitboard) IsSeventyfiveMoves ¶
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 ¶
Checks if the current position is a stalemate.
func (*Bitboard) KingAttacksFrom ¶
func (*Bitboard) KnightAttacksFrom ¶
func (*Bitboard) ParseSan ¶
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 (*Bitboard) PieceTypeAt ¶
func (b *Bitboard) PieceTypeAt(square int) PieceTypes
Gets the piece type at the given square.
func (*Bitboard) PseudoLegalMoveCount ¶
In a way duplicates GeneratePseudoLegalMoves() in order to use population counts instead of counting actually yielded moves.
func (*Bitboard) Push ¶
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 ¶
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 (*Bitboard) RemovePieceAt ¶
Removes a piece from the given square if present.
func (*Bitboard) RookAttacksFrom ¶
func (*Bitboard) San ¶
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 ¶
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 ¶
Parses a FEN and sets the position from it.
Returns an error if the FEN string is invalid.
func (*Bitboard) SetPieceAt ¶
Sets a piece at the given square. An existing piece is replaced.
func (*Bitboard) 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) WasIntoCheck ¶
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 ¶
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 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 ¶
func (*GameNode) AddMainVariation ¶
Creates a child node with the given attributes and promotes it to the main variation.
func (*GameNode) AddVariation ¶
Creates a child nodew with the given attributes.
func (*GameNode) Board ¶
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) HasVariation ¶
Checks if the given move appears as a variation.
func (*GameNode) IsMainLine ¶
Checks if the node is in the main line of the game.
func (*GameNode) IsMainVariation ¶
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) PromoteToMain ¶
Promotes the given move to the main variation.
func (*GameNode) RemoveVariation ¶
Removes a variation by move.
func (*GameNode) San ¶
Gets the standard algebraic notation of the move leading to this node.
Do not call this on the root node.
func (*GameNode) Setup ¶
Setup a specific starting position. This sets (or resets) the `SetUp` and `FEN` header tags.
func (*GameNode) StartsVariation ¶
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) VariationByIndex ¶
Gets a child node by index.
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 NewMove ¶
func NewMove(fromSquare, toSquare int, promotion PieceTypes) *Move
type PGNReader ¶
type PGNReader struct {
// contains filtered or unexported fields
}
func NewPGNReader ¶
func NewPGNReader(handle io.ReadSeeker) *PGNReader
func (*PGNReader) Next ¶
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.
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 ¶
Creates a piece instance from a piece symbol. Returns nil if the symbol is invalid.
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 ¶
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
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)