Search code examples
clojurechessbitboard

Ray attacks on a bitboard


I'm trying to calculate ray attacks given the index of a 64-bit Long bitboard representation:

(defn se [board index]
  "Produces a ray attack from the indexed bit in the south-east direction"
  (reduce bit-or
    (for [bit (rest (range index 0 -7))]
      (bit-flip board bit))))

The rook attacks (straight along a file or rank) are easy enough. However, the problem with the above code is I'm ending up with the following possibility for diagonal Bishop attacks:

00000000
00100000
01000000
10000001
00000010
00000100
00001000
00010000

How should I account for the case when the piece goes off the edge of the board? I'm using big endian mapping (A8 = 0, H1 = 63).


Solution

  • (defn se [board index]
      "Produces a ray attack from the indexed bit in the south-east direction"
      (reduce bit-or 0
        (for [bit (take (- 7 (rem index 8)) (rest (range index 0 -7)))]
          (bit-flip board bit))))