首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接四:比特板检查算法

连接四:比特板检查算法
EN

Code Review用户
提问于 2012-09-06 09:39:52
回答 1查看 992关注 0票数 4

我对Clojure还很陌生,所以我决定编写一个连接四的程序,以获得乐趣和学习。

下面的代码是这个位板算法的Clojure实现。

完整的代码可以在这里找到:https://gist.github.com/3639220

我不确定的是函数(插入)和(位检查板):

代码语言:javascript
复制
(defn insert
  "Inserts symbol for given player (either 1 or 2) at specified x and
   calls bit-insert for corresponding bitboard."
  [boards x player-num]
  (let [y (get-y (boards 0) x)]
    (if (= player-num 1) ; TODO: improve
      (vector
       (assoc-in (boards 0) [y x] (player player-num))
       (bit-insert (boards 1) (- 5 y) x)
       (boards 2))
      (vector
       (assoc-in (boards 0) [y x] (player player-num))
       (boards 1)
       (bit-insert (boards 2) (- 5 y) x)))))

(defn bit-check-board ; TODO: improve
  "Checks whether given bitboard is a win."
  [bitboard]
  (let [diag1 (bit-and bitboard (bit-shift-right bitboard 6))
        hori  (bit-and bitboard (bit-shift-right bitboard 7))
        diag2 (bit-and bitboard (bit-shift-right bitboard 8))
        vert  (bit-and bitboard (bit-shift-right bitboard 1))]
    (bit-or (bit-and diag1 (bit-shift-right diag1 (* 2 6)))
            (bit-and hori  (bit-shift-right hori  (* 2 7)))
            (bit-and diag2 (bit-shift-right diag2 (* 2 8)))
            (bit-and vert  (bit-shift-right vert  (* 2 1))))))
  1. 插入:是否有更好的方法使函数调用依赖于某种测试?
  2. 位检查板:许多类似的行,其中只有一个数字(和一个变量名)是不同的。
EN

回答 1

Code Review用户

发布于 2012-09-06 10:38:40

bit-check-board确实可以改进。这就是我通过把共同的部分连接在一起而得到的结果,尽管我认为它还可以进一步改进:

代码语言:javascript
复制
(defn check [c x]
  (bit-and c (bit-shift-right c x)))

(defn bit-check-board
  "Checks whether given bitboard is a win."
  [bitboard]
  (let [positions [6 7 8 1]
        coords (mapv (partial check bitboard) positions)]
    (apply bit-or (map check coords (map #(* 2 %) positions)))))

此外,您还可以通过更好地模块化代码和使用带有预计算转换表的高阶函数来减少insert函数中的重复:

代码语言:javascript
复制
(defn insert* [board x y]
  (bit-insert board (- 5 y) x))

(defn get-transforms [num]
  (if (= 1 num)
    [identity insert* identity]
    [identity identity insert*]))

(defn insert
  "Inserts symbol for given player (either 1 or 2) at specified x and
   calls bit-insert for corresponding bitboard."
  [boards x player-num]
  (let [y (get-y (boards 0) x)
        board0 (assoc-in (boards 0) [y x] (player player-num))
        transfv (get-transforms player-num)]
    (mapv [board0 (boards 1) (boards 2)] transv)))

如果您在Clojure上< 1.4,则必须更改(mapv ...)中的(vector (map ...))

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/15378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档