3

64ビットのロングビットボード表現のインデックスを指定して、レイアタックを計算しようとしています。

(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))))

ルーク攻撃(ファイルまたはランクに沿ったストレート)は十分に簡単です。ただし、上記のコードの問題は、斜めのビショップ攻撃の可能性が次のようになることです。

00000000
00100000
01000000
10000001
00000010
00000100
00001000
00010000

ピースがボードの端から外れた場合、どのように説明すればよいですか?ビッグエンディアンマッピング(A8 = 0、H1 = 63)を使用しています。

4

2 に答える 2

1
(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))))
于 2012-05-04T08:09:57.863 に答える
1

私はおそらくボード上のx、y座標を使用してこれを行うでしょう:これにより、ボードの端で境界条件チェックを行うのが簡単になります。

(defn se [x y]
  "Produces a ray attack from the indexed bit in the south-east direction"
  (let [initial (bit-shift-left (bit-shift-left (long 1) x) (* y 8))
        dx 1 ;; x direction
        dy 1 ;; y direction
        distance (min 
                   (- 7 x) 
                   (- 7 y))
        shift (+ dx (* 8 dy))]
    (loop [value 0
           distance distance]
      (if (<= distance 0)
        value
        (recur (bit-or value (bit-shift-left initial (* distance shift))) (dec distance))))))

(defn bits [^long bitboard]
  (map 
    #(if (> (bit-and 1 (bit-shift-right bitboard %)) 0) 1 0)
    (range 64)))

(defn display [bitboard]
  (let [bits (partition 8 (bits bitboard))]
    (doseq [ss bits]
      (println (apply str ss)))))

(display (se 1 3))

00000000
00000000
00000000
00000000
00100000
00010000
00001000
00000100

少し余分な作業を行うことで、これを一般化して、任意の(dx、dy)方向に光線をキャストできます。たとえば、東に移動するルークの場合は(1,0)です。距離制限を設定すると、騎士にも(2,1)を使用できます。

これは、ピースの方向ごとに個別の関数を定義するよりも実用的だと思います。

于 2012-05-04T01:32:53.870 に答える