PgnGame<T extends PgnNodeData> class
A Portable Game Notation (PGN) representation.
A PGN game is composed of PgnHeaders and moves represented by a PgnNode tree.
Parser
This class provide 2 parsers: parsePgn
to create a single PgnGame and
parseMultiGamePgn
that can handle a string containing multiple games.
const pgn = '1. d4 d5 *';
final game = PgnGame.parsePgn(pgn);
Position position = PgnGame.startingPosition(game.headers);
for (final node in game.moves.mainline()) {
final move = position.parseSan(node.san);
if (move == null) break; // Illegal move
position = position.play(move);
}
Augmenting game tree
You can use PgnNode.transform to augment all nodes in the game tree with user data.
It allows you to provide context. You update the context inside the
callback. Context object itself should be immutable to prevent any unwanted mutation.
In the example below, the current Position pos
is provided as context.
class PgnNodeWithFen extends PgnNodeData {
final String fen;
const PgnNodeWithFen(
{required this.fen,
required super.san,
super.startingComments,
super.comments,
super.nags});
// Override == and hashCode
// ...
}
final game = PgnGame.parsePgn('1. e4 e5 *');
final pos = PgnGame.startingPosition(game.headers);
final PgnNode<NodeWithFen> res = game.moves.transform<NodeWithFen, Position>(pos,
(pos, data, _) {
final move = pos.parseSan(data.san);
if (move != null) {
final newPos = pos.play(move);
return (
newPos, NodeWithFen(fen: newPos.fen, san: data.san, comments: data.comments, nags: data.nags));
}
return null;
},
);
Constructors
Properties
-
comments
→ List<
String> -
Initial comments of the game.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- headers → PgnHeaders
-
Headers of the game.
final
-
moves
→ PgnNode<
T> -
Parent node containing the game.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
makePgn(
) → String - Make a PGN String from PgnGame.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
defaultHeaders(
) → PgnHeaders - Create default headers of a PGN.
-
emptyHeaders(
) → PgnHeaders - Create empty headers of a PGN.
-
parseMultiGamePgn(
String pgn, {PgnHeaders initHeaders() = defaultHeaders}) → List< PgnGame< PgnNodeData> > - Parse a multi game PGN string.
-
parsePgn(
String pgn, {PgnHeaders initHeaders() = defaultHeaders}) → PgnGame< PgnNodeData> - Parse a PGN string and return a PgnGame.
-
startingPosition(
PgnHeaders headers, {bool? ignoreImpossibleCheck}) → Position< Position> - Create a Position for a Variant from the headers.