lastMoveFrom property

Position? get lastMoveFrom

Returns the Position from which a ChessPiece was moved, deduced by comparing the second last and last FEN strings.

Implementation

Position? get lastMoveFrom {
  if (history.length < 2) return null;
  String previousFen = history[history.length - 2];
  String currentFen = history.last;
  List<List<ChessPiece?>> prevBoard = _decodeBoard(previousFen);
  List<List<ChessPiece?>> currBoard = _decodeBoard(currentFen);
  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 8; col++) {
      // If a square had a piece and now is empty,
      // we assume that's where the piece moved from.
      if (prevBoard[row][col] != null && currBoard[row][col] == null) {
        return Position(row: row, col: col);
      }
    }
  }
  return null;
}