attacked method

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

Implementation

bool attacked(Color color, int square) {
  for (int 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 */
    Piece? piece = board[i];
    if (piece == null || piece.color != color) continue;

    var difference = i - square;
    var index = difference + 119;
    PieceType 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;

      int offset = RAYS[index];
      int j = i + offset;

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

      if (!blocked) return true;
    }
  }

  return false;
}