Documentation ¶
Index ¶
- Constants
- Variables
- func BruteforceMoveList(send func(Move))
- func ColorIdx(color Color) int
- func DuelingRank(t PieceType) int
- func EncodeEpd(game Game) string
- func EncodeFen(board Board) string
- func EncodeFenPiece(p Piece) rune
- func Perft(game Game, depth int) []uint64
- func PerftBruteforce(game Game, depth int) []uint64
- func SquareDistance(a, b Square) int
- type Army
- type Board
- type Color
- type Duel
- type Game
- func (g *Game) ApplyMove(move Move) Game
- func (g *Game) FullmoveNumber() int
- func (g *Game) GameState() GameState
- func (g *Game) GenerateDuels(move Move) []Move
- func (g *Game) GenerateLegalMoves() []Move
- func (g *Game) IsInCheck(color Color) bool
- func (g *Game) ToMove() Color
- func (g *Game) ValidateDuels(move Move) error
- func (g *Game) ValidateLegalMove(move Move) error
- func (g *Game) ValidatePseudoLegalMove(move Move) error
- type GameFlags
- type GameState
- type IllegalMoveError
- type Move
- type ParseError
- type Piece
- type PieceName
- type PieceType
- type Square
Constants ¶
const ( // GameOverError is any move on a completed game. GameOverError = IllegalMoveError(iota + 1) // IllegalDropError is any drop move. IllegalDropError // IllegalPassError is a pass outside of a king-turn. IllegalPassError // NotMovablePieceError is a move attempting to move anything other than // your own piece. NotMovablePieceError // IllegalCastleError is any illegal castle. IllegalCastleError // IllegalKingTurnError is a move other than a king during a king-turn. IllegalKingTurnError // IllegalWhirlwindAttackError is a whirlwind attack from anything other // than a Warrior King on a king-turn IllegalWhirlwindAttackError // IllegalPromotionError is a promotion that doesn't involve a pawn moving // to the last rank. IllegalPromotionError // UnreachableSquareError is a move to an unreachable square. Reachability // is always checked before capture legality. UnreachableSquareError // IllegalCaptureError is a move that looks like a capture but isn't valid. IllegalCaptureError // IllegalRampageError is any Elephant move that captures and doesn't follow // the rampage rules. IllegalRampageError // TooManyDuelsError is a move with more duels than there are pieces // captures. TooManyDuelsError // NotEnoughStonesError is a move where the challenges or responses would // result in negative stone counts. NotEnoughStonesError // NotDuelableError is a move that attempts to duel with a king. NotDuelableError // MoveIntoCheckError is a move that results in a king being in check. MoveIntoCheckError )
const ( // FenEmpty is a FEN for an empty board FenEmpty = "8/8/8/8/8/8/8/8" // FenDefault is a FEN for a normal starting position FenDefault = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" )
const ( // GameInProgress means the game is not yet finished. GameInProgress = GameState(iota) // GameOverWhite means the game is over and white won. GameOverWhite // GameOverBlack means the game is over and black won. GameOverBlack // GameOverDraw means the game is over and was a draw. GameOverDraw )
const ( // GameFlagMidline enables the midline victory condition. GameFlagMidline = GameFlags(1 << iota) // GameFlagStalemate causes stalemates to be treated as a draw instead of a // loss. GameFlagStalemate )
const ( // VariantChess2 is the default flag configuration for a Chess2 game. VariantChess2 = GameFlagMidline // VariantClassic is the flag configuration for a game of classic Chess. VariantClassic = GameFlagStalemate )
const ( // TypeNone is not a real piece TypeNone = PieceType(0x00) // TypeKing means a king TypeKing = PieceType(0x01) // TypeQueen means a queen TypeQueen = PieceType(0x02) // TypeBishop means a bishop TypeBishop = PieceType(0x03) // TypeKnight means a knight TypeKnight = PieceType(0x04) // TypeRook means a rook TypeRook = PieceType(0x05) // TypePawn means a pawn TypePawn = PieceType(0x06) // ArmyNone means the army is not known from the piece alone ArmyNone = Army(0x00) // ArmyClassic means classic army ArmyClassic = Army(0x10) // ArmyNemesis means nemesis army ArmyNemesis = Army(0x20) // ArmyEmpowered means empowered army ArmyEmpowered = Army(0x30) // ArmyReaper means reaper army ArmyReaper = Army(0x40) // ArmyTwoKings means two kings army ArmyTwoKings = Army(0x50) // ArmyAnimals means animals army ArmyAnimals = Army(0x60) // ColorWhite means white ColorWhite = Color(0x00) // ColorBlack means black ColorBlack = Color(0x80) // PieceNameNone is not a real piece PieceNameNone = PieceName(0x00) // PieceNameBasicKing means a king PieceNameBasicKing = PieceName(0x01) // PieceNameBasicQueen means a queen PieceNameBasicQueen = PieceName(0x02) // PieceNameBasicBishop means a bishop PieceNameBasicBishop = PieceName(0x03) // PieceNameBasicKnight means a knight PieceNameBasicKnight = PieceName(0x04) // PieceNameBasicRook means a rook PieceNameBasicRook = PieceName(0x05) // PieceNameBasicPawn means a pawn PieceNameBasicPawn = PieceName(0x06) // PieceNameClassicKing means Classic King PieceNameClassicKing = PieceName(int(ArmyClassic) | int(TypeKing)) // PieceNameNemesisQueen means Nemesis PieceNameNemesisQueen = PieceName(int(ArmyNemesis) | int(TypeQueen)) // PieceNameNemesisPawn means Nemesis Pawn PieceNameNemesisPawn = PieceName(int(ArmyNemesis) | int(TypePawn)) // PieceNameEmpoweredQueen means Abdicated Queen PieceNameEmpoweredQueen = PieceName(int(ArmyEmpowered) | int(TypeQueen)) // PieceNameEmpoweredBishop means Empowered Bishop PieceNameEmpoweredBishop = PieceName(int(ArmyEmpowered) | int(TypeBishop)) // PieceNameEmpoweredKnight means Empowered Knight PieceNameEmpoweredKnight = PieceName(int(ArmyEmpowered) | int(TypeKnight)) // PieceNameEmpoweredRook means Empowered Rook PieceNameEmpoweredRook = PieceName(int(ArmyEmpowered) | int(TypeRook)) // PieceNameReaperQueen means Reaper PieceNameReaperQueen = PieceName(int(ArmyReaper) | int(TypeQueen)) // PieceNameReaperRook means Ghost PieceNameReaperRook = PieceName(int(ArmyReaper) | int(TypeRook)) // PieceNameTwoKingsKing means Warrior King PieceNameTwoKingsKing = PieceName(int(ArmyTwoKings) | int(TypeKing)) // PieceNameAnimalsQueen means Jungle Queen PieceNameAnimalsQueen = PieceName(int(ArmyAnimals) | int(TypeQueen)) // PieceNameAnimalsBishop means Tiger PieceNameAnimalsBishop = PieceName(int(ArmyAnimals) | int(TypeBishop)) // PieceNameAnimalsKnight means Wild Horse PieceNameAnimalsKnight = PieceName(int(ArmyAnimals) | int(TypeKnight)) // PieceNameAnimalsRook means Elephant PieceNameAnimalsRook = PieceName(int(ArmyAnimals) | int(TypeRook)) )
Variables ¶
var InvalidPiece = Piece{}
InvalidPiece is the default value for Piece. It represents "no piece".
var InvalidSquare = Square{127}
InvalidSquare is not a real Square. It will cause undefined behavior if you use it as anything other than a sentinel.
Functions ¶
func BruteforceMoveList ¶
func BruteforceMoveList(send func(Move))
BruteforceMoveList calls the given function once for each possible move. Drop moves are not emitted, but passes are.
func DuelingRank ¶
DuelingRank computes the dueling rank of the given piece type. Dueling a piece of a higher dueling rank requires paying a stone.
func EncodeFenPiece ¶
EncodeFenPiece returns the FEN code for a specific piece
func Perft ¶
Perft returns the number of valid sequences of moves of length depth from the given game. Challenges are never issued while counting moves.
func PerftBruteforce ¶
PerftBruteforce is similar to Perft, except that it generates the moves by trying every possible square combination rather than using the (much faster) move generator. It is useful for testing the move generator.
func SquareDistance ¶
SquareDistance calculates the greater of the horizontal or vertical distance between the squares.
Types ¶
type Army ¶
type Army int
Army is Classic, Nemesis, etc
func FindArmySymbol ¶
FindArmySymbol takes the EPD symbol for an army and converts it to the proper Army value.
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board represents the pieces on a chess board.
func (*Board) ClearPieceAt ¶
ClearPieceAt adjusts the receiver to have an empty square at the provided space.
func (*Board) MovePiece ¶
MovePiece adjusts the receiver to clear the source square and place the piece that was there at the destination square. It will replace any piece already in the destination square.
func (*Board) PieceAt ¶
PieceAt returns the piece at the given square in the receiver, and a boolean indicating whether the square is occupied. The returned piece will always have ArmyNone as the Army.
func (*Board) ReplacePieces ¶
ReplacePieces modifies the board so that all pieces of the given color and find type are replaced by a corresponding piece of the same color of the replace type.
func (*Board) SetPieceAt ¶
SetPieceAt adjusts the reciever to have the provided piece at the provided space.
type Color ¶
type Color int
Color is White or Black
func OtherColor ¶
OtherColor returns black for white and vice versa
type Duel ¶
type Duel struct {
// contains filtered or unexported fields
}
A Duel is a packed representation of a duel outcome. A duel is initiated by the defender (the one who's piece was captured), and the attacker responds to the duel.
func DuelWithChallenge ¶
DuelWithChallenge creates an incomplete duel with the given challenge value.
func DuelWithResponse ¶
DuelWithResponse creates a complete duel from the passed incomplete duel with the attacker's response included.
func (Duel) Gain ¶
Gain indicates that the attacker has bid 0 and wishes to gain a stone, rather than having the opponent lose a stone.
func (Duel) IsComplete ¶
IsComplete indicates that the attacker has responded to the duel. This is necessary to differentiate from a 0 Response value.
func (Duel) IsStarted ¶
IsStarted indicates that the defender has initiated a duel. This is necessary to differentiate from the default value of a Duel.
type Game ¶
type Game struct {
// contains filtered or unexported fields
}
A Game fully describes a Chess 2 game.
func GameFromArmies ¶
GameFromArmies initializes a new Game with the provided armies.
func ParseEpdClassic ¶
ParseEpdClassic parses a classic Chess EPD string and returns a classic chess Game object.
func ParseEpdFlags ¶
ParseEpdFlags parses and EPD string and returns a game object. The flags on the game can be adjusted.
func (*Game) ApplyMove ¶
ApplyMove clones the receiver, applies the given move to the clone, and returns the clone. It does not validate that the move is legal, and if an illegal move is made the resulting Game may not be in a valid state.
func (*Game) FullmoveNumber ¶
FullmoveNumber is the number of moves black has made, not counting king-turns. This starts at 0 and becomes 1 on white's next regular turn.
func (*Game) GenerateDuels ¶
GenerateDuels returns an array of Moves based on the given move, corresponding to every legal combination of duels. The existing duels on the move are ignored. The duels returns by this method always choose to gain a stone when calling a bluff, but choosing to have the opponent lose a stone is always also valid.
func (*Game) GenerateLegalMoves ¶
GenerateLegalMoves returns an array of all legal moves from the current board state.
func (*Game) IsInCheck ¶
IsInCheck determines if the given player is currently in check, regardless of if they are the player to move. If the game is over due to checkmate, this method will return true for the losing player.
func (*Game) ValidateDuels ¶
ValidateDuels returns an error describing why the duels on given move are not legal.
func (*Game) ValidateLegalMove ¶
ValidateLegalMove returns an error describing why the given move is not legal.
- A move is "into check" if it leaves the board in a state where any of the player's kings are threatened.
- A move is "legal" if it is pseudo-legal and not into check.
func (*Game) ValidatePseudoLegalMove ¶
ValidatePseudoLegalMove returns an error describing why the given move is not pseudo-legal.
A move is "pseudo-legal" if it is one of the player to move's pieces; follows the distance and direction rules for the piece moved; all intermediate squares are empty (except for jump moves) or capturable (for the Elephant's rampage); and the target square is empty or contains a capturable piece. Additionally, a pass move is pseudo-legal during a king turn.
type IllegalMoveError ¶
type IllegalMoveError int
IllegalMoveError represents the error that was encountered when attempting to make a move.
func (IllegalMoveError) Error ¶
func (code IllegalMoveError) Error() string
type Move ¶
Move represents a move, including the duels that resulted from it.
var MovePass Move = Move{From: InvalidSquare, To: InvalidSquare}
MovePass represents a pass move.
type ParseError ¶
type ParseError string
ParseError represents the error that was encountered when parsing a board or move.
func (ParseError) Error ¶
func (msg ParseError) Error() string
type Piece ¶
type Piece struct {
// contains filtered or unexported fields
}
Piece represents a single piece, including army, color, and type.
func ParseFenPiece ¶
ParseFenPiece takes the FEN for a specific piece and converts it to a Piece in the Classic army.
type Square ¶
type Square struct {
Address uint8
}
A Square represents a location on the board. Squares are numbered from the top left square (A8), moving right, then down.
func SquareFromCoords ¶
SquareFromCoords takes an x, y pair and returns a Square.
func SquareFromName ¶
SquareFromName takes a name like A1 and returns a Square. Will return InvalidSquare if the name is not valid.