国际象棋:主教与CLIPS一起移动

时间:2011-05-16 18:26:17

标签: chess clips

我正在尝试在棋盘上实施主教的可能动作,棋盘可以在随机单元格上有其他部分。我已经能够做出答案的草图,但它没有检测到其他部分。

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

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

现在有人能做什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

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