From 720128d22e55e613c8acb84db313a2beca061f2d Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Fri, 24 Nov 2023 17:12:11 +0100 Subject: [PATCH] Rename Rules to Rule and add rule getter to Position --- CHANGELOG.md | 4 +++ lib/src/models.dart | 20 ++++++------- lib/src/pgn.dart | 8 ++--- lib/src/position.dart | 70 +++++++++++++++++++++++++++++-------------- pubspec.yaml | 2 +- 5 files changed, 67 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf07e6..8dc804c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.0 + +- Rename Rules to Rule and add rule getter to Position + ## 0.5.1 - Fix parsing PGN from smartchess diff --git a/lib/src/models.dart b/lib/src/models.dart index dd98a89..9b0a140 100644 --- a/lib/src/models.dart +++ b/lib/src/models.dart @@ -326,7 +326,7 @@ class FenError implements Exception { } /// Represents the different possible rules of chess and its variants -enum Rules { +enum Rule { chess, antichess, kingofthehill, @@ -337,7 +337,7 @@ enum Rules { crazyhouse; /// Parses a PGN header variant tag - static Rules? fromPgn(String? variant) { + static Rule? fromPgn(String? variant) { switch ((variant ?? 'chess').toLowerCase()) { case 'chess': case 'chess960': @@ -359,16 +359,16 @@ enum Rules { case 'wild/7': case 'wild/8': case 'wild/8a': - return Rules.chess; + return Rule.chess; case 'crazyhouse': case 'crazy house': case 'house': case 'zh': - return Rules.crazyhouse; + return Rule.crazyhouse; case 'king of the hill': case 'koth': case 'kingofthehill': - return Rules.kingofthehill; + return Rule.kingofthehill; case 'three-check': case 'three check': case 'threecheck': @@ -376,23 +376,23 @@ enum Rules { case '3-check': case '3 check': case '3check': - return Rules.threecheck; + return Rule.threecheck; case 'antichess': case 'anti chess': case 'anti': - return Rules.antichess; + return Rule.antichess; case 'atomic': case 'atom': case 'atomic chess': - return Rules.atomic; + return Rule.atomic; case 'horde': case 'horde chess': - return Rules.horde; + return Rule.horde; case 'racing kings': case 'racingkings': case 'racing': case 'race': - return Rules.racingKings; + return Rule.racingKings; default: return null; } diff --git a/lib/src/pgn.dart b/lib/src/pgn.dart index 732d27f..6671ec9 100644 --- a/lib/src/pgn.dart +++ b/lib/src/pgn.dart @@ -139,14 +139,14 @@ class PgnGame { /// Throws a [PositionError] if it does not meet basic validity requirements. static Position startingPosition(PgnHeaders headers, {bool? ignoreImpossibleCheck}) { - final rules = Rules.fromPgn(headers['Variant']); - if (rules == null) throw PositionError.variant; + final rule = Rule.fromPgn(headers['Variant']); + if (rule == null) throw PositionError.variant; if (!headers.containsKey('FEN')) { - return Position.initialPosition(rules); + return Position.initialPosition(rule); } final fen = headers['FEN']!; try { - return Position.setupPosition(rules, Setup.parseFen(fen), + return Position.setupPosition(rule, Setup.parseFen(fen), ignoreImpossibleCheck: ignoreImpossibleCheck); } catch (err) { rethrow; diff --git a/lib/src/position.dart b/lib/src/position.dart index abcb90b..34b4ede 100644 --- a/lib/src/position.dart +++ b/lib/src/position.dart @@ -45,6 +45,8 @@ abstract class Position> { /// Current move number. final int fullmoves; + Rule get rule; + /// Abstract const constructor to be used by subclasses. const Position._initial() : board = Board.standard, @@ -74,55 +76,55 @@ abstract class Position> { int? fullmoves, }); - /// Create a [Position] from a [Setup] and [Rules]. - static Position setupPosition(Rules rules, Setup setup, + /// Create a [Position] from a [Setup] and [Rule]. + static Position setupPosition(Rule rule, Setup setup, {bool? ignoreImpossibleCheck}) { - switch (rules) { - case Rules.chess: + switch (rule) { + case Rule.chess: return Chess.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.antichess: + case Rule.antichess: return Antichess.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.atomic: + case Rule.atomic: return Atomic.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.kingofthehill: + case Rule.kingofthehill: return KingOfTheHill.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.crazyhouse: + case Rule.crazyhouse: return Crazyhouse.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.threecheck: + case Rule.threecheck: return ThreeCheck.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.horde: + case Rule.horde: return Horde.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); - case Rules.racingKings: + case Rule.racingKings: return RacingKings.fromSetup(setup, ignoreImpossibleCheck: ignoreImpossibleCheck); } } - /// Returns the initial [Position] for the corresponding [Rules]. - static Position initialPosition(Rules rules) { - switch (rules) { - case Rules.chess: + /// Returns the initial [Position] for the corresponding [Rule]. + static Position initialPosition(Rule rule) { + switch (rule) { + case Rule.chess: return Chess.initial; - case Rules.antichess: + case Rule.antichess: return Antichess.initial; - case Rules.atomic: + case Rule.atomic: return Atomic.initial; - case Rules.kingofthehill: + case Rule.kingofthehill: return KingOfTheHill.initial; - case Rules.threecheck: + case Rule.threecheck: return ThreeCheck.initial; - case Rules.crazyhouse: + case Rule.crazyhouse: return Crazyhouse.initial; - case Rules.horde: + case Rule.horde: return Horde.initial; - case Rules.racingKings: + case Rule.racingKings: return RacingKings.initial; } } @@ -1020,6 +1022,9 @@ abstract class Position> { /// A standard chess position. @immutable class Chess extends Position { + @override + Rule get rule => Rule.chess; + const Chess({ required super.board, super.pockets, @@ -1078,6 +1083,9 @@ class Chess extends Position { /// A variant of chess where you lose all your pieces or get stalemated to win. @immutable class Antichess extends Position { + @override + Rule get rule => Rule.antichess; + const Antichess({ required super.board, super.pockets, @@ -1216,6 +1224,9 @@ class Antichess extends Position { /// A variant of chess where captures cause an explosion to the surrounding pieces. @immutable class Atomic extends Position { + @override + Rule get rule => Rule.atomic; + const Atomic({ required super.board, super.pockets, @@ -1431,6 +1442,9 @@ class Atomic extends Position { /// A variant where captured pieces can be dropped back on the board instead of moving a piece. @immutable class Crazyhouse extends Position { + @override + Rule get rule => Rule.crazyhouse; + const Crazyhouse({ required super.board, super.pockets, @@ -1554,6 +1568,9 @@ class Crazyhouse extends Position { /// of the board. @immutable class KingOfTheHill extends Position { + @override + Rule get rule => Rule.kingofthehill; + const KingOfTheHill({ required super.board, super.pockets, @@ -1623,6 +1640,9 @@ class KingOfTheHill extends Position { /// into the third check. @immutable class ThreeCheck extends Position { + @override + Rule get rule => Rule.threecheck; + const ThreeCheck({ required super.board, super.pockets, @@ -1742,6 +1762,9 @@ class ThreeCheck extends Position { /// A variant where the goal is to put your king on the eigth rank @immutable class RacingKings extends Position { + @override + Rule get rule => Rule.racingKings; + const RacingKings({ required super.board, super.pockets, @@ -1872,6 +1895,9 @@ class RacingKings extends Position { @immutable class Horde extends Position { + @override + Rule get rule => Rule.horde; + const Horde({ required super.board, super.pockets, diff --git a/pubspec.yaml b/pubspec.yaml index 91368d5..d9f84c3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: dartchess description: Provides chess and chess variants rules and operations including chess move generation, read and write FEN, read and write PGN. repository: https://github.com/lichess-org/dartchess -version: 0.5.1 +version: 0.6.0 platforms: android: ios: