Skip to content

Commit

Permalink
fix alternative castling
Browse files Browse the repository at this point in the history
  • Loading branch information
HaonRekcef committed Mar 11, 2024
1 parent 027fee3 commit d3a6d84
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/src/model/common/node.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:dartchess/dartchess.dart';
import 'package:dartchess/src/utils.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
Expand Down Expand Up @@ -192,17 +193,23 @@ abstract class Node {
bool prepend = false,
}) {
final pos = nodeAt(path).position;
final (newPos, newSan) = pos.makeSan(convertAltCastlingMove(move) ?? move);
final convertedMove = convertAltCastlingMove(pos, move);
final (newPos, newSan) = pos.makeSan(convertedMove!);
final newNode = Branch(
sanMove: SanMove(newSan, convertAltCastlingMove(move) ?? move),
sanMove: SanMove(newSan, convertedMove),
position: newPos,
);
return addNodeAt(path, newNode, prepend: prepend);
}

/// The function `convertAltCastlingMove` checks if a move is an alternative
/// castling move and converts it to the corresponding standard castling move if so.
Move? convertAltCastlingMove(Move move) {
Move? convertAltCastlingMove(Position pos, Move move) {
final isKingMove =
pos.board.roleAt(parseSquare(move.uci.substring(0, 2)) ?? 0) ==
Role.king;
if (!isKingMove) return move;

return altCastles.containsValue(move.uci)
? Move.fromUci(
altCastles.entries.firstWhere((e) => e.value == move.uci).key,
Expand Down

0 comments on commit d3a6d84

Please sign in to comment.