parseLongAlgebraicNotation function

ChessMove parseLongAlgebraicNotation(
  1. String notation,
  2. ChessGameState state
)

Implementation

ChessMove parseLongAlgebraicNotation(String notation, ChessGameState state) {
  Point<int> begin = _parseCoord(notation.substring(0, 2));
  Point<int> end = _parseCoord(notation.substring(2, 4));
  ChessPiece promotion = ChessPiece.none;
  if (notation.length == 5) {
    String promotionChar = notation.substring(4);
    for (ChessPiece p in ChessPiece.values) {
      if (p.pieceType.lowercaseChar == promotionChar) {
        promotion = p;
        break;
      }
    }
  }
  if (state.moves.isEmpty) {
    print("NO MOVES");
  }
  return state.moves.firstWhere(
      (m) => m.start == begin && m.end == end && m.promotion == promotion,
      orElse: () {
    print(
        "NO MOVE WAS FOUND FOR NOTATION ${notation} and state ${state.forsythEdwardsNotation}. Returning first move.");
    return state.moves.first;
  });
}