attacked method

bool attacked(
  1. Color color,
  2. int square
)

Implementation

bool attacked(Color color, int square) {
  for (var i = SQUARES_A8; i <= SQUARES_H1; i++) {
    /* did we run off the end of the board */
    if ((i & 0x88) != 0) {
      i += 7;
      continue;
    }

    /* if empty square or wrong color */
    final piece = board[i];
    if (piece == null || piece.color != color) continue;

    final difference = i - square;
    final index = difference + 119;
    final type = piece.type;

    if ((ATTACKS[index] & (1 << type.shift)) != 0) {
      if (type == PAWN) {
        if (difference > 0) {
          if (color == WHITE) return true;
        } else {
          if (color == BLACK) return true;
        }
        continue;
      }

      /* if the piece is a knight or a king */
      if (type == KNIGHT || type == KING) return true;

      final offset = RAYS[index];
      var j = i + offset;

      var blocked = false;
      while (j != square) {
        if (board[j] != null) {
          blocked = true;
          break;
        }
        j += offset;
      }

      if (!blocked) return true;
    }
  }

  return false;
}