English 中文(简体)
三. 轨道板的雷射
原标题: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))))

猛烈的袭击(从档案或级别上看)很容易发生。 然而,上述法典问题最终导致“大主教”攻击的可能性如下:

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

最佳回答
(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))))
问题回答

我也许会利用船上的Xy坐标做这项工作:这样就更容易在上边进行边界状况检查,就像这样的情况。

(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)夜晚。

I think this will be more practical than defining separate functions for each piece direction.





相关问题
how to develop a multi player chess?

I wana develop a multi-player chess using c# but I don t have any idea of how to implement the restriction rules of chess with c# to be honest i ve never done even a little bit of game programming in ...

Chess game in JavaScript [closed]

Is there any Chess game API , purely written in JavaScript ? No Flash! Anybody know the algorithm(in general) used in Chess games ?

C# minimax tree realization

I m trying to write C# Chess AI. At that moment I have to build my minmax tree. I try by using recursion, but my recursive functions has to call itself about 1 000 000 times for every node. I get ...

Chessboard in WPF

For years I ve developed with Winforms, now I want to switch to WPF and make a chessboard. Unfortunately I have no idea where to start. Using WPF makes me very unsure, I m feeling like a noob again. ...

Preventing cheating in online chess games? [closed]

In many online chess lobbies, I ve seen instances of engining , where a cheater would open a chess program at the same time as the main game window. He would then set it up so that the opponent s ...

Programming a chess AI

I m looking to try and write a chess AI. Is there something i can use on the .NET framework (or maybe even a chess program scripted in Lua) that will let me write and test a chess AI without worrying ...

热门标签