Skip to content

Commit

Permalink
Rename Rules to Rule and add rule getter to Position
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Nov 24, 2023
1 parent 606e444 commit 720128d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 10 additions & 10 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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':
Expand All @@ -359,40 +359,40 @@ 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':
case 'three check chess':
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;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/pgn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ class PgnGame<T extends PgnNodeData> {
/// 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;
Expand Down
70 changes: 48 additions & 22 deletions lib/src/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ abstract class Position<T extends Position<T>> {
/// Current move number.
final int fullmoves;

Rule get rule;

/// Abstract const constructor to be used by subclasses.
const Position._initial()
: board = Board.standard,
Expand Down Expand Up @@ -74,55 +76,55 @@ abstract class Position<T extends Position<T>> {
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;
}
}
Expand Down Expand Up @@ -1020,6 +1022,9 @@ abstract class Position<T extends Position<T>> {
/// A standard chess position.
@immutable
class Chess extends Position<Chess> {
@override
Rule get rule => Rule.chess;

const Chess({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1078,6 +1083,9 @@ class Chess extends Position<Chess> {
/// A variant of chess where you lose all your pieces or get stalemated to win.
@immutable
class Antichess extends Position<Antichess> {
@override
Rule get rule => Rule.antichess;

const Antichess({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1216,6 +1224,9 @@ class Antichess extends Position<Antichess> {
/// A variant of chess where captures cause an explosion to the surrounding pieces.
@immutable
class Atomic extends Position<Atomic> {
@override
Rule get rule => Rule.atomic;

const Atomic({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1431,6 +1442,9 @@ class Atomic extends Position<Atomic> {
/// A variant where captured pieces can be dropped back on the board instead of moving a piece.
@immutable
class Crazyhouse extends Position<Crazyhouse> {
@override
Rule get rule => Rule.crazyhouse;

const Crazyhouse({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1554,6 +1568,9 @@ class Crazyhouse extends Position<Crazyhouse> {
/// of the board.
@immutable
class KingOfTheHill extends Position<KingOfTheHill> {
@override
Rule get rule => Rule.kingofthehill;

const KingOfTheHill({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1623,6 +1640,9 @@ class KingOfTheHill extends Position<KingOfTheHill> {
/// into the third check.
@immutable
class ThreeCheck extends Position<ThreeCheck> {
@override
Rule get rule => Rule.threecheck;

const ThreeCheck({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1742,6 +1762,9 @@ class ThreeCheck extends Position<ThreeCheck> {
/// A variant where the goal is to put your king on the eigth rank
@immutable
class RacingKings extends Position<RacingKings> {
@override
Rule get rule => Rule.racingKings;

const RacingKings({
required super.board,
super.pockets,
Expand Down Expand Up @@ -1872,6 +1895,9 @@ class RacingKings extends Position<RacingKings> {

@immutable
class Horde extends Position<Horde> {
@override
Rule get rule => Rule.horde;

const Horde({
required super.board,
super.pockets,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 720128d

Please sign in to comment.