国际象棋:主教用 CLIPS 走棋

发布于 2024-11-07 18:03:13 字数 984 浏览 5 评论 0原文

我正在尝试在国际象棋桌上实现主教的可能走法,该棋盘可以在随机单元格上放置其他棋子。我已经能够画出答案的草图,但它没有检测到其他部分。

在此规则之前,我编写了一些代码,为表格的每个单元格创建如下所示的事实,指示其内容:

(cell-info (coor {i} {j}) (contents {empty|black|white})) 

以及显示棋子位置的事实:

 (piece (row {r}) (column {c}) (type {t}) (color {col}))

到目前为止,这是我的规则(可能也不是)太高效了):

(defrule bishop-moves
    (declare (salience 30))
    (piece (row ?rb) (column ?cb) (type bishop) (color black))
    (cell-info (coor ?i ?j) (contents empty|white))
=>
    (loop-for-count (?n 1 8)
       (if (or (and (= ?i (+ ?rb ?n)) (= ?j (+ ?cb ?n)))
           (and (= ?i (- ?rb ?n)) (= ?j (- ?cb ?n)))
           (and (= ?i (+ ?rb ?n)) (= ?j (- ?cb ?n)))
           (and (= ?i (- ?rb ?n)) (= ?j (+ ?cb ?n))))
         then (assert (movement-allowed
                     (destination-cell ?i ?j)
                     (type bishop)
                     (start-cell ?rb ?cb))))))

现在有人知道我能做什么吗?提前致谢。

I'm trying to implement the possible moves of a bishop on a chess table, which can have other pieces on random cells. I've been able to make a sketch of an answer, but it doesn't detect other pieces.

Previously to this rule I've written some code that creates a fact like the following for each cell of the table, indicating its contents:

(cell-info (coor {i} {j}) (contents {empty|black|white})) 

and a fact that shows the position of a piece:

 (piece (row {r}) (column {c}) (type {t}) (color {col}))

And here's my rule so far (probably it's also not too efficient):

(defrule bishop-moves
    (declare (salience 30))
    (piece (row ?rb) (column ?cb) (type bishop) (color black))
    (cell-info (coor ?i ?j) (contents empty|white))
=>
    (loop-for-count (?n 1 8)
       (if (or (and (= ?i (+ ?rb ?n)) (= ?j (+ ?cb ?n)))
           (and (= ?i (- ?rb ?n)) (= ?j (- ?cb ?n)))
           (and (= ?i (+ ?rb ?n)) (= ?j (- ?cb ?n)))
           (and (= ?i (- ?rb ?n)) (= ?j (+ ?cb ?n))))
         then (assert (movement-allowed
                     (destination-cell ?i ?j)
                     (type bishop)
                     (start-cell ?rb ?cb))))))

Does anybody now what could I do? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2024-11-14 18:03:13
;;; Added deftemplates and deffacts
;;; Replaced rule variable ?i with ?r and ?j with ?c.
;;; Made rule applicable for both black or white bishop
;;; Moved diagonal logic from actions of rule to conditions
;;; Added logic to rule for intervening pieces

(deftemplate piece (slot row) (slot column) (slot type) (slot color))
(deftemplate cell-info (multislot coor) (slot contents))
(deftemplate movement-allowed (multislot destination-cell) (slot type) (multislot start-cell))

(deffacts test-data
   (piece (row 1) (column 1) (type pawn) (color black))
   (cell-info (coor 1 1) (contents black))  ; Invalid - friendly piece
   (cell-info (coor 1 2) (contents empty))  ; Invalid - not on diagonal
   (cell-info (coor 1 3) (contents empty))  ; Valid
   (piece (row 2) (column 2) (type bishop) (color black))
   (cell-info (coor 2 2) (contents black))  ; Invalid - friendly piece
   (cell-info (coor 2 8) (contents empty))  ; Invalid - not on diagonal
   (cell-info (coor 3 1) (contents empty))  ; Valid
   (cell-info (coor 3 3) (contents empty))  ; Valid
   (cell-info (coor 4 4) (contents empty))  ; Valid
   (cell-info (coor 5 5) (contents empty))  ; Valid
   (piece (row 6) (column 6) (type pawn) (color white))
   (cell-info (coor 6 6) (contents white))  ; Valid
   (cell-info (coor 7 7) (contents empty))  ; Invalid - blocked by pawn
   (piece (row 8) (column 8) (type pawn) (color white))
   (cell-info (coor 8 8) (contents white))) ; Invalid - blocked by pawn

(defrule bishop-moves

   (declare (salience 30))

   (piece (row ?rb) (column ?cb) (type bishop) (color ?color))

   ;; The destination cell must be empty or contain 
   ;; an opposing piece

   (cell-info (coor ?r ?c) (contents empty | ~?color))

   ;; If the cell and piece are on the same diagonal, the
   ;; absolute difference between the two should be the same

   (test (= (abs (- ?r ?rb)) (abs (- ?c ?cb))))

   ;; Check that there is not another piece that is within
   ;; the rectangle formed by the bishop and the destination
   ;; cell and is also on the same diagonal as the bishop

   (not (and (piece (row ?ro) (column ?co))

             (test (and (or (< ?rb ?ro ?r) (< ?r ?ro ?rb))
                        (or (< ?cb ?co ?c) (< ?c ?co ?cb))))

             (test (= (abs (- ?ro ?rb)) (abs (- ?co ?cb))))))

   =>

   (assert (movement-allowed
                     (destination-cell ?r ?c)
                     (type bishop)
                     (start-cell ?rb ?cb))))
;;; Added deftemplates and deffacts
;;; Replaced rule variable ?i with ?r and ?j with ?c.
;;; Made rule applicable for both black or white bishop
;;; Moved diagonal logic from actions of rule to conditions
;;; Added logic to rule for intervening pieces

(deftemplate piece (slot row) (slot column) (slot type) (slot color))
(deftemplate cell-info (multislot coor) (slot contents))
(deftemplate movement-allowed (multislot destination-cell) (slot type) (multislot start-cell))

(deffacts test-data
   (piece (row 1) (column 1) (type pawn) (color black))
   (cell-info (coor 1 1) (contents black))  ; Invalid - friendly piece
   (cell-info (coor 1 2) (contents empty))  ; Invalid - not on diagonal
   (cell-info (coor 1 3) (contents empty))  ; Valid
   (piece (row 2) (column 2) (type bishop) (color black))
   (cell-info (coor 2 2) (contents black))  ; Invalid - friendly piece
   (cell-info (coor 2 8) (contents empty))  ; Invalid - not on diagonal
   (cell-info (coor 3 1) (contents empty))  ; Valid
   (cell-info (coor 3 3) (contents empty))  ; Valid
   (cell-info (coor 4 4) (contents empty))  ; Valid
   (cell-info (coor 5 5) (contents empty))  ; Valid
   (piece (row 6) (column 6) (type pawn) (color white))
   (cell-info (coor 6 6) (contents white))  ; Valid
   (cell-info (coor 7 7) (contents empty))  ; Invalid - blocked by pawn
   (piece (row 8) (column 8) (type pawn) (color white))
   (cell-info (coor 8 8) (contents white))) ; Invalid - blocked by pawn

(defrule bishop-moves

   (declare (salience 30))

   (piece (row ?rb) (column ?cb) (type bishop) (color ?color))

   ;; The destination cell must be empty or contain 
   ;; an opposing piece

   (cell-info (coor ?r ?c) (contents empty | ~?color))

   ;; If the cell and piece are on the same diagonal, the
   ;; absolute difference between the two should be the same

   (test (= (abs (- ?r ?rb)) (abs (- ?c ?cb))))

   ;; Check that there is not another piece that is within
   ;; the rectangle formed by the bishop and the destination
   ;; cell and is also on the same diagonal as the bishop

   (not (and (piece (row ?ro) (column ?co))

             (test (and (or (< ?rb ?ro ?r) (< ?r ?ro ?rb))
                        (or (< ?cb ?co ?c) (< ?c ?co ?cb))))

             (test (= (abs (- ?ro ?rb)) (abs (- ?co ?cb))))))

   =>

   (assert (movement-allowed
                     (destination-cell ?r ?c)
                     (type bishop)
                     (start-cell ?rb ?cb))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文