isValidArrangement method

bool isValidArrangement(
  1. dynamic arrangement
)

Implementation

bool isValidArrangement(dynamic arrangement) {
  String arrngs = "";
  if (arrangement is String) {
    arrngs = arrangement;
  }
  if (arrangement.length != 8) return false;

  if (arrangement is List<String>) {
    arrngs = arrangement.join('').toUpperCase();
  }

  // Check the presence of all pieces
  if (countPiece('K', arrngs) != 1 ||
      countPiece('Q', arrngs) != 1 ||
      countPiece('R', arrngs) != 2 ||
      countPiece('B', arrngs) != 2 ||
      countPiece('N', arrngs) != 2) {
    return false;
  }

  // Check the positions of bishops (on different colored squares)
  int b1 = arrngs.indexOf('B');
  int b2 = arrngs.lastIndexOf('B');
  if (!((b2 - b1) % 2 != 0)) return false;

  // Check the positions of rooks (one on each side of the king)
  int k = arrngs.indexOf('K');
  int r1 = arrngs.indexOf('R');
  if (r1 > k) return false;

  return true;
}