moveFromAlgebraic method
Create a Move
from an algebraic string (e.g. a2a3, g6f3) for a board
of this size.
Implementation
Move moveFromAlgebraic(String alg) {
// A normal move - b1c3
// A normal move with promo - e7e8q
// A drop - e@g7
// A gating move - g7f6/e
// A disambiguated gating move - e1g1/eh1 (castling)
if (alg[1] == '@') {
// it's a drop
int from = Squares.hand;
int to = squareNumber(alg.substring(2));
return Move(from: from, to: to, piece: alg[0]);
}
List<String> sections = alg.split('/');
String move = sections.first;
final m = _moveRegex.firstMatch(move);
if (m == null) {
throw Exception('Invalid move string: $alg');
}
int from = squareNumber(m.group(1)!);
int to = squareNumber(m.group(2)!);
String? promo = m.group(3);
String? piece;
int? gatingSquare;
if (sections.length > 1) {
String gate = sections.last;
piece = gate[0];
gatingSquare = gate.length > 2 ? squareNumber(gate.substring(1)) : from;
}
return Move(
from: from,
to: to,
promo: promo,
piece: piece,
gatingSquare: gatingSquare,
);
}