chess2

package
v0.0.0-...-4682dd1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
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
)
View Source
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"
)
View Source
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
)
View Source
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
)
View Source
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
)
View Source
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

View Source
var InvalidPiece = Piece{}

InvalidPiece is the default value for Piece. It represents "no piece".

View Source
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 ColorIdx

func ColorIdx(color Color) int

ColorIdx returns 0 for white, 1 for black

func DuelingRank

func DuelingRank(t PieceType) int

DuelingRank computes the dueling rank of the given piece type. Dueling a piece of a higher dueling rank requires paying a stone.

func EncodeEpd

func EncodeEpd(game Game) string

EncodeEpd returns the EPD of the given game object

func EncodeFen

func EncodeFen(board Board) string

EncodeFen takes a board and returns the FEN that represents it.

func EncodeFenPiece

func EncodeFenPiece(p Piece) rune

EncodeFenPiece returns the FEN code for a specific piece

func Perft

func Perft(game Game, depth int) []uint64

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

func PerftBruteforce(game Game, depth int) []uint64

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

func SquareDistance(a, b Square) int

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

func FindArmySymbol(symbol rune) (Army, bool)

FindArmySymbol takes the EPD symbol for an army and converts it to the proper Army value.

func (Army) String

func (a Army) String() string

type Board

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

Board represents the pieces on a chess board.

func ParseFen

func ParseFen(fen string) (Board, error)

ParseFen takes a fen string and returns the Board that is represented by it.

func (*Board) ClearPieceAt

func (b *Board) ClearPieceAt(s Square)

ClearPieceAt adjusts the receiver to have an empty square at the provided space.

func (*Board) MovePiece

func (b *Board) MovePiece(from, to Square)

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

func (b *Board) PieceAt(s Square) (Piece, bool)

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

func (b *Board) ReplacePieces(color Color, find, replace PieceType)

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

func (b *Board) SetPieceAt(s Square, p Piece)

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

func OtherColor(color Color) Color

OtherColor returns black for white and vice versa

func (Color) String

func (c Color) String() string

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

func DuelWithChallenge(bid int) Duel

DuelWithChallenge creates an incomplete duel with the given challenge value.

func DuelWithResponse

func DuelWithResponse(duel Duel, bid int, gain bool) Duel

DuelWithResponse creates a complete duel from the passed incomplete duel with the attacker's response included.

func NewDuel

func NewDuel(challenge, response int, gain bool) Duel

NewDuel creates a complete Duel from scratch.

func ParseDuel

func ParseDuel(str string) (Duel, error)

ParseDuel takes a duel string like "20+" and returns Duel object.

func (Duel) Challenge

func (d Duel) Challenge() int

Challenge returns the number of stones bid by the defender.

func (Duel) Gain

func (d Duel) Gain() bool

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

func (d Duel) IsComplete() bool

IsComplete indicates that the attacker has responded to the duel. This is necessary to differentiate from a 0 Response value.

func (Duel) IsStarted

func (d Duel) IsStarted() bool

IsStarted indicates that the defender has initiated a duel. This is necessary to differentiate from the default value of a Duel.

func (Duel) Response

func (d Duel) Response() int

Response returns the number of stones bid by the attacker.

func (Duel) String

func (d Duel) String() string

type Game

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

A Game fully describes a Chess 2 game.

func GameFromArmies

func GameFromArmies(white, black Army) Game

GameFromArmies initializes a new Game with the provided armies.

func ParseEpd

func ParseEpd(epd string) (Game, error)

ParseEpd parses an EPD string and returns a Game object.

func ParseEpdClassic

func ParseEpdClassic(epd string) (Game, error)

ParseEpdClassic parses a classic Chess EPD string and returns a classic chess Game object.

func ParseEpdFlags

func ParseEpdFlags(epd string, flags GameFlags) (Game, error)

ParseEpdFlags parses and EPD string and returns a game object. The flags on the game can be adjusted.

func (*Game) ApplyMove

func (g *Game) ApplyMove(move Move) Game

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

func (g *Game) FullmoveNumber() int

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) GameState

func (g *Game) GameState() GameState

GameState returns the current state of the game.

func (*Game) GenerateDuels

func (g *Game) GenerateDuels(move Move) []Move

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

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

GenerateLegalMoves returns an array of all legal moves from the current board state.

func (*Game) IsInCheck

func (g *Game) IsInCheck(color Color) bool

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) ToMove

func (g *Game) ToMove() Color

ToMove returns the color who should make the next move.

func (*Game) ValidateDuels

func (g *Game) ValidateDuels(move Move) error

ValidateDuels returns an error describing why the duels on given move are not legal.

func (*Game) ValidateLegalMove

func (g *Game) ValidateLegalMove(move Move) error

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

func (g *Game) ValidatePseudoLegalMove(move Move) error

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 GameFlags

type GameFlags uint

GameFlags can be used to change the rules of the game.

type GameState

type GameState int

GameState describes if the game is in progress, and the winner

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

type Move struct {
	From, To Square
	Piece    Piece
	Duels    [3]Duel
}

Move represents a move, including the duels that resulted from it.

var MovePass Move = Move{From: InvalidSquare, To: InvalidSquare}

MovePass represents a pass move.

func ParseUci

func ParseUci(uci string) (Move, error)

ParseUci takes a UCI string and returns the Move it represents.

func (Move) IsDrop

func (m Move) IsDrop() bool

IsDrop returns true if the move is a drop move.

func (Move) IsPass

func (m Move) IsPass() bool

IsPass returns true if the move is a pass move.

func (Move) String

func (m Move) String() string

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 NewPiece

func NewPiece(pieceType PieceType, army Army, color Color) Piece

NewPiece returns a piece with the given properties

func ParseFenPiece

func ParseFenPiece(code rune) (Piece, error)

ParseFenPiece takes the FEN for a specific piece and converts it to a Piece in the Classic army.

func (Piece) Army

func (p Piece) Army() Army

Army returns the piece army of the receiver.

func (Piece) Color

func (p Piece) Color() Color

Color returns the piece color of the receiver.

func (Piece) Name

func (p Piece) Name() PieceName

Name returns one of the PieceName* constants. It's a combination of the Army and Type, but pieces which aren't special are converted to ArmyBasic.

func (Piece) String

func (p Piece) String() string

func (Piece) Type

func (p Piece) Type() PieceType

Type returns the piece type of the receiver.

func (Piece) WithArmy

func (p Piece) WithArmy(army Army) Piece

WithArmy returns a copied Piece with the army set to the given value.

type PieceName

type PieceName int

PieceName is a combination of PieceType and Army

func (PieceName) String

func (p PieceName) String() string

type PieceType

type PieceType int

PieceType is King, Queen, etc.

func (PieceType) String

func (t PieceType) String() string

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

func SquareFromCoords(x, y int) Square

SquareFromCoords takes an x, y pair and returns a Square.

func SquareFromName

func SquareFromName(name string) Square

SquareFromName takes a name like A1 and returns a Square. Will return InvalidSquare if the name is not valid.

func (Square) String

func (s Square) String() string

func (Square) X

func (s Square) X() int

X value of the receiver. Corresponds to x = file of the square - 'a'.

func (Square) Y

func (s Square) Y() int

Y value of the receiver. Corresponds to y = 8 - rank of the square.

Jump to

Keyboard shortcuts

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