2011-05-16 81 views
1

我想實現主席在棋盤上,可以有其他件隨機單元格上的可能動作。我已經能夠做出答案的草圖,但它沒有檢測到其他部分。國際象棋:主教與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)))))) 

現在有人可以做什麼嗎?提前致謝。

回答

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