bishop library

A chess logic package with flexible variant support.

Classes

Action
ActionAddToHand
Adds count of piece to the acting player's hand. If forOpponent is true, the piece(s) will be added to the other player's hand.
ActionBlockOrigin
ActionCheckPieceCount
Checks if at least count pieces of type pieceType are present for player. If player is null, then both players will be checked. If both players have less than count, it will be a draw. If one player has less, then it will be a win for their opponent, unless draw is true in which case it will be a draw. If drawsInvalidate is true (default), then any drawn result will invalidate the move. In other words, moves that cause both players to not have enough pieces will not be allowed.
ActionCheckRoyalsAlive
Checks if either side's royal pieces are alive and ends the game if one or both is dead. If allowDraw is true, both royals being dead will be a draw. If false, the move will be invalidated.
ActionDefinitions
Some common actions.
ActionEffect
ActionExitRegionEnding
ActionExplodeOnCapture
Creates an explosion in area whenever a piece is captured.
ActionExplosionRadius
Creates an explosion with radius whenever a piece is captured.
ActionFillRegionEnding
ActionFlyingGenerals
The flying generals rule from Xiangqi. If the generals/kings are facing each other, with no pieces between, the move will be invalidated. Set activeCondition to true if you have other actions that might modify the board before this.
ActionImmortality
Prevents pieces from being captured (or otherwise destroyed by actions that occur before this one). For example, if pieceType is 'Q', queens will not be capturable. It's also possible to specify a flag, which will make any pieces with that flag immortal. flag and pieceType can be combined. If adding this action to a piece type's actions field, it is not necessary to specify the piece type, as it will be added when BuiltVariant is created.
ActionMoveProcessor<T extends Move>
A move processor that modifies the state using an action.
ActionPointsEnding
ActionRemoveFromHand
Removes count of piece from the acting player's hand. If forOpponent is true, the piece(s) will be removed from the other player's hand.
ActionTransferOwnership
Transfers the ownership of the moving piece to the opponent. Royal pieces will not be transferred. The default behaviour is for this to be executed for any move, but capture or quiet can be set to false.
ActionTrigger
A class that contains all the data relevant to the action being triggered.
AddToHandAdapter
Area
Atom
A single component of a piece's move set.
BasicAdapter<T>
A simplified adapter for types that don't take any parameters.
Betza
Bishop
BishopException
BishopSerialisation
BishopState
A record of the game's state at a particular move.
BishopTypeAdapter<T>
An adapter to allow serialisation of class-based variant parameters, such as actions, or promotion options.
BlockOriginAdapter
BoardRegion
BoardRegionAdapter
BoardSize
Specifies the dimensions of a board.
BuiltRegion
A region that is created from a BoardRegion when BuiltVariant is built.
BuiltVariant
Castling
CastlingOptions
CastlingSetup
CheckPieceCountAdapter
CheckRoyalsAliveAdapter
Chess960StartPosAdapter
Chess960StartPosBuilder
CommonVariants
Common chess variants with different rules.
Conditions
Some common conditions for actions.
CustomPromotion
DeepAdapter<T>
A type adapter that also takes a list of adapters, allowing deep serialisation. Use this for classes that contain other serialisable classes.
DetonateMoveProcessor
A move processor that handles a StaticMove. On moving, the piece will be detonated, destroying all pieces in area around it. Essentially this is like an atomic move without moving. Used for Beirut Chess.
Direction
Represents a direction on a chessboard.
DirectionSetRegion
Dobutsu
DrawnGame
DrawnGameBothRoyalsDead
DrawnGameElimination
DrawnGameEnteredRegion
DrawnGameExitedRegion
DrawnGameInsufficientMaterial
DrawnGameLength
DrawnGamePoints
DrawnGameRepetition
DrawnGameStalemate
DropBuilder
DropMove
A move where a piece is dropped from the player's hand onto the board. This is only for hand drops, not gating moves, which are currently still part of StandardMove.
DropMoveFormatter
Drops
EffectAddToHand
Causes piece to be added to player's hand.
EffectInvalidateMove
This effect will cause the move being processed to be marked as invalid, meaning that it won't appear in a legal moves list.
EffectModifySquare
Causes square to be set to content.
EffectMoveProcessor<T extends Move>
A move processor that modifies the state using the effects system. You should probably use this or ActionMoveProcessor unless you have a complex special case
EffectRemoveFromHand
Causes piece to be removed from player's hand. If such a piece doesn't exist to be removed, nothing will happen.
EffectSetCustomState
Sets the custom state variable at index variable to value. You can set a number of variables equal to the size of your board, so on an 8x8 board, the highest variable is 63. value can be up to 8191, or 2^45 if you assume your code will never execute on a 32-bit vm.
EffectSetGameResult
Sets the result of the game to result. This will end the game.
Engine
EngineResult
ExitRegionEndingAdapter
ExplodeOnCaptureAdapter
ExplosionRadiusAdapter
FairyVariants
Variants where the focus is on novel piece types.
FillRegionAdapter
FirstMoveOptions
FirstMoveOptionsPair
FirstMoveOptionsSet
FirstMovePairAdapter
FirstMoveSetAdapter
FlyingGeneralsAdapter
Game
Tracks the state of the game, handles move generation and validation, and generates output.
GameEndConditions
GameEndConditionSet
GameInfo
GameNavigator
GameResult
GatingMove
A move that involves a piece being moved out of the player's gate, and onto the player's first rank.
GatingMoveFormatter
HandOptions
Defines behaviour of hands in a variant.
HideFlagsAdapter
HideFlagsStateTransformer
ImmortalityAdapter
InitialFirstMoveAdapter
This one is not exported if it's at the top level (not in a pair/set).
InitialStateFirstMoveOptions
IntersectRegion
A region that contains the intersection of regions, i.e. all squares that are in all of the regions.
InvalidMoveResult
A special case to propagate invalidation through to makeMove.
LargeVariants
Variants of chess played on larger boards.
MaskedState
MaskStateTransformer
MaterialConditions<T>
Specifies the minimum sets of pieces required for the game not to be a draw. T should be String for definitions, and is converted to int when the Variant is built.
MiscVariants
Move
MoveDefinition
MoveFormatter<T extends Move>
MoveGenerator
MoveGenParams
MoveMeta
Contains move names.
MoveParams
MoveProcessor<T extends Move>
MoveProcessorParams<T extends Move>
MultiMove
A move that consists of multiple steps. Not yet fully implemented.
Musketeer
NoPass
Never generates a pass move.
NoPassAdapter
NoPromotion
No pieces are ever promoted.
NoPromotionAdapter
OptionalPromotion
Rules where pieces can optionally promote on ranks, and if forced is true, then they will be forced to promote on forcedRanks. This allows for rules such as those in Shogi or Grand Chess.
OptionalPromotionAdapter
Orda
OtherGames
Games that don't really resemble Chess or any of the other main variants.
OutputOptions
Allows output options (FEN, PGN) to be specified for variants. This will become more significant when we support variants with more complex FENs.
PairTransformAdapter
ParseFenResult
PassMove
A move that simply passes the turn for the active player.
PassMoveFormatter
PassOptions
PgnData
The information parsed from a PGN file. This format is quite subject to change, since branch parsing support is coming.
PieceDefinition
A definition of a PieceType, specific to a Variant. You don't ever need to build these, they are generated in variant initialisation.
PieceMoveParams
PieceOptimisationData
Contains precomputed flags that help move generation run faster.
PiecePromoOptions
Defines whether a piece can promote or be promoted to.
PieceType
Specifies a piece type, with all of its moves and attributes.
Promotion
PromotionOptions
PromotionParams
RandomChessStartPosAdapter
RandomChessStartPosBuilder
RanksFirstMoveAdapter
RanksFirstMoveOptions
RectRegion
A region on a board used to define area-specific piece behaviour.
Region
RegionDropAdapter
RegionDropBuilder
Allows drops only within specified regions. It is possible to specify BoardRegions (whiteRegion, blackRegion), or reference regions in Variant.regions with whiteId and blackId.
RegionEffect
Defines an effect that a region will have on a piece type.
RegionPromotion
Allows promotion only within specified regions. It is possible to specify BoardRegions (whiteRegion, blackRegion), or reference regions in Variant.regions with whiteId and blackId.
RegionPromotionAdapter
RemoveFromHandAdapter
SetRegion
ShapeVariants
Variants that have an unusual board shape.
Shogi
This is a work in progress. Many rules, specifically those related to dropping, are not implemented yet.
SmallVariants
Variants of chess played on smaller boards.
StandardDropAdapter
StandardDropBuilder
StandardMove
A representation of a single move. This is a move that is made in a game, not a definition of a type of move that can be made by a piece. For that, see MoveDefinition.
StandardMoveDefinition
Specifies a group of moves.
StandardPass
Always generates a pass move.
StandardPassAdapter
StandardPromotion
The standard chess promotion rules. If ranks are specified (white, black), these will be used, otherwise the final ranks of the board will be used. Pieces will be able to promote on the promotion rank, or anything past it.
StandardPromotionAdapter
StartPositionBuilder
StateMeta
StateTransformer
StateTransformerPair
StaticMove
A move that starts and ends on the same square. This doesn't have any default implementation in the engine; it is intended for use in games that have a special move that doesn't move a piece, with its own move processor.
SubtractRegion
A region that consists of positive, with negative cut out of it.
TeleportMoveDefinition
TransferOwnershipAdapter
UnionRegion
A region that contains all of the squares of each region in regions.
UnrestrictedDropAdapter
UnrestrictedDropBuilder
Variant
Specifies the rules and pieces to be used, size of the board, information on how FENs are outputted, and so on and so on.
VisionAreaAdapter
VisionAreaStateTransformer
WonGame
WonGameCheckLimit
WonGameCheckmate
WonGameElimination
WonGameEnteredRegion
WonGameExitedRegion
WonGamePoints
WonGameRoyalDead
WonGameStalemate
WrapperMove
A type of move that wraps another move and extends its functionality. Currently child is limited to StandardMoves only.
Xiangqi
Xiangqi / Cờ Tướng / Chinese Chess defintions. All rules are implemented and working, except for chasing restrictions.
XorRegion
A region that contains all squares that are in left or right, but not in both of them, i.e. XOR.
Zobrist
Used for hashing and storing board states. https://en.wikipedia.org/wiki/Zobrist_hashing

Enums

ActionEvent
The type of event that triggers an action.
CastlingFormat
Determines how castling rights are represented in FEN strings. There are certain positions in variants such as Chess960, in which the standard 'KQkq' format could present an ambiguity. See this link for more details.
CheckType
DirectionType
EndType
What happens when a game end condition is met. This is from the perspective of the player the condition happens to. i.e. if a player has no legal moves and thus enters the stalemate condition, with EndType.win, the player with no moves will win. EndType.none disables a condition.
ForcedCapture
Defines forced capture behaviour. Currently only any is supported, meaning that any capture move is allowed if there are capture moves, but no non-capturing (quiet) moves.
GatingMode
Defines different types of gating for variants.
Modality
Defines whether a move definition allows captures, quiet moves or both.
Variants
All built in variants.

Functions

boardToAscii(List<int> board, {bool unicode = false, BuiltVariant? variant}) String
Generates an ASCII representation of the board.
buildMask(BoardSize size, Iterable<int> visibleSquares) List<int>
Builds a simple board mask from a list of visible squares. A value of 1 in the mask indicates a visible square, and 0 is invisible.
castlingRights(String crString) CastlingRights
countPiecesInFen(String pos) Map<String, int>
expandCountMap<T>(Map<T, int> input) List<T>
Converts a map where the values are counts to a list. For example, {'P': 3, 'B': 1} => ['P', 'P', 'P', 'B'].
fileFromSymbol(String symbol) int
fileSymbol(int file) String
formatDropMoveAlgebraic({required DropMove move, required BuiltVariant variant}) String
formatDropMovePretty({required DropMove move, required BuiltVariant variant, CheckType? checkType}) String
formatGatingMoveAlgebraic({required GatingMove move, required MoveFormatterCallback formatter, CheckType? checkType, required BuiltVariant variant, bool simplifyFixedGating = true}) String
formatGatingMovePretty({required GatingMove move, required MoveFormatterCallback formatter, CheckType? checkType, required BuiltVariant variant}) String
gameFromPgn(String pgn, {Variant? variant, String? startPosition, bool strictVariantTag = true}) Game
Parses a pgn and builds a game from it. If variant is not supplied, the parser will look for a variant tag.
gameFromPgnData(PgnData data, {Variant? variant, String? startPosition, bool strictVariantTag = true}) Game
Builds a game from already parsed data. If variant or startPosition are not supplied, tags will be used, if they exist.
getStandardDisambiguator({required StandardMove move, required List<Move> moves, required BuiltVariant variant, required BishopState state}) String
To be used in cases where, given a piece and a destination, there is more than one possible move. For example, in 'Nbxa4', this function provides the 'b'. Optionally, provide moves - a list of legal moves. This will be generated if it is not specified.
isAlpha(String str) bool
isNumeric(String s) bool
makeFlags(List<int> flags) int
makePiece(int piece, int colour, {int internalType = 0, bool initialState = false, int flags = 0}) int
maskBoard(List<int> board, List<int> mask) List<int>
Masks board with a a simple mask, such as the one built by buildMask.
parseFen({required String fen, required BuiltVariant variant, bool strict = false, bool initialPosition = false, int? seed}) ParseFenResult
Load a position from a FEN string. If strict is enabled, a full string must be provided, including turn, ep square, etc.
parsePgn(String pgn) PgnData
Parses a pgn. Supports metadata tags and comments, but not sub-lines (yet).
replaceMultiple(String source, List<String> originals, List<String> replacements) String
setupCastling({required String castlingString, required List<int> royalSquares, required List<int> board, required BuiltVariant variant}) CastlingSetup
validateFen({required Variant variant, required String fen, bool strict = false}) bool
variantFromString(String name, {bool allowIncomplete = true}) Variant?
Looks up a built in variant by name. If allowIncomplete, partial matches are allowed.
visibleSquares({required List<int> board, required BoardSize size, required int player, required Area area}) Set<int>
Gets all squares that are visible for player in state, given all of their pieces can see area around them.

Typedefs

ActionCondition = bool Function(ActionTrigger trigger)
A function that validates an ActionTrigger.
ActionDefinition = List<ActionEffect> Function(ActionTrigger trigger)
A function that generates a List<ActionEffect> based on an ActionTrigger passed to it.
CastlingRights = int
Colour = int
DropBuilderFunction = List<Move> Function(MoveParams params)
EffectMoveFunction<T extends Move> = List<ActionEffect> Function(MoveProcessorParams<Move> params)
FenBuilder = String Function({int? seed})
Hand = List<int>
MoveChecker = bool Function(MoveParams params)
MoveFormatterCallback = String Function(Move move, CheckType? checkType)
MoveFormatterFunction<T extends Move> = String Function(Move move, MoveFormatterCallback formatter, CheckType? checkType)
MoveGenFunction<T extends Move> = List<T> Function({MoveGenParams params, required int player, required BishopState state})
MoveProcessorFunction<T extends Move> = BishopState? Function(MoveProcessorParams<Move> params)
PieceMoveChecker = bool Function(PieceMoveParams params)
PromotionBuilder = List<StandardMove>? Function(PromotionParams params)
PromotionSetup = PromotionBuilder Function(BuiltVariant variant)
Square = int
StateTransformFunction<T extends BishopState> = T? Function(BishopState state, [int? player])