Documentation
¶
Index ¶
- Constants
- Variables
- func ChebyshevDistance(s1 Square, s2 Square) uint8
- func GenerateFen(p *Position) string
- func IsCheck(p *Position) bool
- func IsCheckMate(p *Position) bool
- func IsStaleMate(p *Position) bool
- func IsValidPosition(p *Position) bool
- func ManhattanDistance(s1 Square, s2 Square) uint8
- func WritePgn(g *Game, w io.Writer) error
- type Color
- type File
- type Game
- func (g *Game) BlackKingSideCastle() bool
- func (g *Game) BlackQueenSideCastle() bool
- func (g *Game) CanClaimDraw() bool
- func (g *Game) Copy() *Game
- func (g *Game) EnPassant() Square
- func (g *Game) FullMove() uint16
- func (g *Game) GetAllTags() map[string]string
- func (g *Game) GetResult() Result
- func (g *Game) GetTag(t string) (string, error)
- func (g *Game) HalfMove() uint16
- func (g *Game) HasThreeFoldRepetition() bool
- func (g *Game) IsCheckMate() bool
- func (g *Game) IsStaleMate() bool
- func (g *Game) LegalMoves() []Move
- func (g *Game) Move(m Move) error
- func (g *Game) MoveSan(s string) error
- func (g *Game) Position() *Position
- func (g *Game) PrintPosition()
- func (g *Game) RemoveTag(tag string)
- func (g *Game) SetPosition(p *Position) error
- func (g *Game) SetResult(r Result)
- func (g *Game) SetTag(tag string, value string)
- func (g *Game) String() string
- func (g *Game) Turn() Color
- func (g *Game) WhiteKingSideCastle() bool
- func (g *Game) WhiteQueenSideCastle() bool
- type Move
- type Piece
- type PieceType
- type Position
- type Rank
- type Result
- type Square
Constants ¶
const DefaultFen string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Variables ¶
var ( NoSquare = Square{} A8 = Square{FileA, Rank8} B8 = Square{FileB, Rank8} C8 = Square{FileC, Rank8} D8 = Square{FileD, Rank8} E8 = Square{FileE, Rank8} F8 = Square{FileF, Rank8} G8 = Square{FileG, Rank8} H8 = Square{FileH, Rank8} A7 = Square{FileA, Rank7} B7 = Square{FileB, Rank7} C7 = Square{FileC, Rank7} D7 = Square{FileD, Rank7} E7 = Square{FileE, Rank7} F7 = Square{FileF, Rank7} G7 = Square{FileG, Rank7} H7 = Square{FileH, Rank7} A6 = Square{FileA, Rank6} B6 = Square{FileB, Rank6} C6 = Square{FileC, Rank6} D6 = Square{FileD, Rank6} E6 = Square{FileE, Rank6} F6 = Square{FileF, Rank6} G6 = Square{FileG, Rank6} H6 = Square{FileH, Rank6} A5 = Square{FileA, Rank5} B5 = Square{FileB, Rank5} C5 = Square{FileC, Rank5} D5 = Square{FileD, Rank5} E5 = Square{FileE, Rank5} F5 = Square{FileF, Rank5} G5 = Square{FileG, Rank5} H5 = Square{FileH, Rank5} A4 = Square{FileA, Rank4} B4 = Square{FileB, Rank4} C4 = Square{FileC, Rank4} D4 = Square{FileD, Rank4} E4 = Square{FileE, Rank4} F4 = Square{FileF, Rank4} G4 = Square{FileG, Rank4} H4 = Square{FileH, Rank4} A3 = Square{FileA, Rank3} B3 = Square{FileB, Rank3} C3 = Square{FileC, Rank3} D3 = Square{FileD, Rank3} E3 = Square{FileE, Rank3} F3 = Square{FileF, Rank3} G3 = Square{FileG, Rank3} H3 = Square{FileH, Rank3} A2 = Square{FileA, Rank2} B2 = Square{FileB, Rank2} C2 = Square{FileC, Rank2} D2 = Square{FileD, Rank2} E2 = Square{FileE, Rank2} F2 = Square{FileF, Rank2} G2 = Square{FileG, Rank2} H2 = Square{FileH, Rank2} A1 = Square{FileA, Rank1} B1 = Square{FileB, Rank1} C1 = Square{FileC, Rank1} D1 = Square{FileD, Rank1} E1 = Square{FileE, Rank1} F1 = Square{FileF, Rank1} G1 = Square{FileG, Rank1} H1 = Square{FileH, Rank1} AllSquares = [64]Square{ 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, } )
Functions ¶
func ChebyshevDistance ¶
ChebyshevDistance returns the number of king moves between two squares. Returns math.MaxUint8 if either square is invalid.
func GenerateFen ¶
func IsCheckMate ¶
IsCheckMate returns true is the side to move is in check and has no legal moves.
func IsStaleMate ¶
IsStaleMate does not check the fifty move rule. It only checks if a player is not able to move, and is not in check.
func IsValidPosition ¶
IsValidPosition determines if a given position is a legal chess position. It checks the following:
- There is one king of each color on the board
- There are no pawns on their last rank
- Castling rights are logical
- The enPassant Square is logical
- Turn is set
- All pieces are valid chess pieces
func ManhattanDistance ¶
ManhattanDistance give the number of non-diagonal king moves to get from s1 to s2. Returns math.MaxUint8 if either square is not a valid chess square.
Types ¶
type Game ¶
type Game struct {
// contains filtered or unexported fields
}
Game is guaranteed to always represent a valid game of chess. Invalid positions are not allowed, but the move history may not represent an entire game (undoing all moves may not lead to the starting chess position). Game can be used to parse and generate PGNs. Additionally Game provides many wrapper methods for functions that normally take position as an argument, increasing convenience and decreasing the need to copy Game's position excessively.
The zero value for Game is not valid and should not be used. Game should always be duplicated using the Game.Copy function.
func NewGame ¶
func NewGame() *Game
NewGame returns a *Game representing the starting position for a game of chess.
func ReadPgn ¶
ReadPgn attempts to create a Game from r. Parsing should be improved in the future, but for now only well formatted pgns containing a single game are accepted. Refer to http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm for specific details of how a pgn should be formatted.
func (*Game) BlackKingSideCastle ¶
func (*Game) BlackQueenSideCastle ¶
func (*Game) CanClaimDraw ¶
CanClaimDraw returns true if one of the following conditions is true and the game is not in checkmate
- The half move counter is >= 100 (indicating that no piece has been taken, nor pawn moved forward for 50 moves)
- The game contains a three fold repetition (the exact same position has occurred three times in the game)
- The game is in stalemate
func (*Game) GetAllTags ¶
func (*Game) GetResult ¶
GetResult gets the current result tag for the game. This result should be valid, but there are no calculations being performed in this function.
func (*Game) HasThreeFoldRepetition ¶
HasThreeFoldRepetition returns true if the game has been in the exact same position (including castling rights) At least three times at any point during the entire game.
func (*Game) IsCheckMate ¶
IsCheckMate returns true is the side to move is in check and has no legal moves.
func (*Game) IsStaleMate ¶
IsStaleMate does not check the fifty move rule. It only checks if a player is not able to move, and is not in check.
func (*Game) LegalMoves ¶
func (*Game) Move ¶
Move performs the given move. If move m is not legal g remains unchanged and an error is returned. If the move is legal the result tag is set to * (NoResult). If the position ends in checkmate or stalemate the result tag is updated accordingly.
func (*Game) MoveSan ¶
MoveSan is a helper function that automatically performs an SAN formatted move. SAN format is specified here: http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm#c8.2.3
func (*Game) PrintPosition ¶ added in v1.0.2
func (g *Game) PrintPosition()
PrintPosition prints the current position from the point of view for the current player to move.
func (*Game) RemoveTag ¶
Remove tag will remove any pgn tag except the 7 required tags specified here, and the SetUp and FEN tags specified here. If you wish to remove the SetUp and FEN tags it is best to simply make a new game.
func (*Game) SetPosition ¶
SetPosition sets the games position to the given position only if the position is a valid chess position. The result tag is updated to match if the game is in mate, or could still be going. Move history is cleared, and the pgn tags "SetUp" and "FEN" are set accordingly.
func (*Game) SetTag ¶
SetTag sets any tag for the game so that it will show up in the pgn file. The Result, SetUp, and FEN tags cannot be set with this function. Please use the Game.SetResult function to set the result, and the Game.SetPosition function to set the other two tags.
func (*Game) WhiteKingSideCastle ¶
func (*Game) WhiteQueenSideCastle ¶
type Move ¶
func GenerateLegalMoves ¶
GenerateLegalMoves expects a valid position. Behavior is undefined for invalid positions. This is to improve performance since move generation is a vital part to engine development.
func GeneratePseudoLegalMoves ¶
GeneratePseudoLegalMoves expects a valid position. Behavior is undefined for invalid positions. This is to improve performance since move generation is a vital part to engine development.
func ParseSANMove ¶
ParseSANMove returns a move given a position and an SAN formatted move. SAN format defined here: http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm#c8.2.3
func ParseUCIMove ¶
ParseUCIMove expects a UCI compatible move string. Format should be Square1Square2Promotion, where promotion is optional.
type Piece ¶
var ( NoPiece Piece = Piece{NoColor, NoPieceType} WhitePawn Piece = Piece{White, Pawn} WhiteRook Piece = Piece{White, Rook} WhiteKnight Piece = Piece{White, Knight} WhiteBishop Piece = Piece{White, Bishop} WhiteQueen Piece = Piece{White, Queen} WhiteKing Piece = Piece{White, King} BlackPawn Piece = Piece{Black, Pawn} BlackRook Piece = Piece{Black, Rook} BlackKnight Piece = Piece{Black, Knight} BlackBishop Piece = Piece{Black, Bishop} BlackQueen Piece = Piece{Black, Queen} BlackKing Piece = Piece{Black, King} )
func ParsePiece ¶
ParsePiece attempts to parse a piece from a given rune. Currently only supports ascii characters (no piece symbols). Uppercase is white, lowercase is black.
type Position ¶
type Position struct { Board [64]Piece Turn Color WhiteKingSideCastle bool WhiteQueenSideCastle bool BlackKingSideCastle bool BlackQueenSideCastle bool EnPassant Square HalfMove uint16 FullMove uint16 }
Position represents a chess position as described by Forsyth-Edwards Notation (FEN). Board is the actual representation of the pieces on the squares. It starts at A8 and moves left to right, top to bottom all the way to H1.
func ParseFen ¶
ParseFen take only fully formed valid FEN strings. All parts of the FEN must be present, though the position need not necessarily be valid.
func (*Position) FormatString ¶ added in v1.0.1
FormatString returns a representation of the board. No other information from the position is printed. blacksPerspective should be true if you which to print the board as if you were on black's side.
func (*Position) Move ¶
Position.Move does no checking of move legality. For checked moves use Game.Move, or check that your move is in the list provided by GenerateLegalMoves. All parts of the position are updated including en passant and castling rights based how the the move interacts with the board.
func (*Position) SetPieceAt ¶
func (*Position) String ¶
String returns a representation of the board from white's perspective. No other information from the position is printed. It is recommended to use Position.FormatString to print from black's perspective.
type Square ¶
func ParseSquare ¶
ParseSquare parses a two letter string for a square. "-" returns NoSquare without an error.