parse static method

Move? parse(
  1. String str
)

Parses a UCI string into a move.

Will return a NormalMove or a DropMove depending on the UCI string.

Returns null if UCI string is not valid.

Implementation

static Move? parse(String str) {
  if (str[1] == '@' && str.length == 4) {
    final role = Role.fromChar(str[0]);
    final to = Square.parse(str.substring(2));
    if (role != null && to != null) return DropMove(to: to, role: role);
  } else if (str.length == 4 || str.length == 5) {
    final from = Square.parse(str.substring(0, 2));
    final to = Square.parse(str.substring(2, 4));
    Role? promotion;
    if (str.length == 5) {
      promotion = Role.fromChar(str[4]);
      if (promotion == null) {
        return null;
      }
    }
    if (from != null && to != null) {
      return NormalMove(from: from, to: to, promotion: promotion);
    }
  }
  return null;
}