2014-03-19 52 views
1

如何以適當的座標在板上打印對象(字符#對象I)?在板上打印座標

(deftemplate cenario 
(slot min-line) 
(slot max-line) 
(slot min-column) 
(slot max-column)) 

(deftemplate line 
(slot index)) 

(deftemplate column 
(slot index)) 

(deftemplate coordinate 
(slot line) 
(slot column)) 

(deftemplate object 
(multislot coordinate)) 

(deffacts cenario 
(cenario 
(min-line 1) 
(max-line 24) 
(min-column 1) 
(max-column 12))) 

(deffacts line 
(line (index 1)) 
(line (index 2)) 
(line (index 3)) 
(line (index 4)) 
(line (index 5)) 
(line (index 6)) 
(line (index 7)) 
(line (index 8)) 
(line (index 9)) 
(line (index 10)) 
(line (index 11)) 
(line (index 12)) 
(line (index 13)) 
(line (index 14)) 
(line (index 15)) 
(line (index 16)) 
(line (index 17)) 
(line (index 18)) 
(line (index 19)) 
(line (index 20)) 
(line (index 21)) 
(line (index 22)) 
(line (index 23)) 
(line (index 24))) 

(deffacts column 
(column (index 1)) 
(column (index 2)) 
(column (index 3)) 
(column (index 4)) 
(column (index 5)) 
(column (index 6)) 
(column (index 7)) 
(column (index 8)) 
(column (index 9)) 
(column (index 10)) 
(column (index 11)) 
(column (index 12))) 

(deffacts I 
(object (coordinate 5 24) (coordinate 6 24) (coordinate 7 24) (coordinate 8 24)))) 

(defrule startcolumn 
(not(columnCurrent)) 
(cenario (min-column ?x)) 
=> 
(assert(columnCurrent ?x))) 

(defrule startline 
(not(lineCurrent)) 
(cenario (max-line ?x)) 
=> 
(assert(lineCurrent ?x))) 

(defrule print-board 
(cenario (max-column ?maxcol)) 
?f <- (line (index ?i)) 
?g <- (columnCurrent ?ca&:(<= ?ca ?maxcol)) 
(not (object (coordinate ?i ?ca))) 
(lineCurrent ?i) 
=> 
(retract ?g) 
(assert (columnCurrent (+ ?ca 1))) 
(printout t "?")) 

(defrule print-object 
(lineCurrent ?i) 
(columnCurrent ?ca) 
(object (coordinate ?i ?ca)) 
=> 
(printout t ?i " " ?ca)) 

(defrule change-line 
(cenario (max-column ?maxcol)) 
?f <- (line (index ?i)) 
?g <- (columnCurrent 13) 
(lineCurrent ?i) 
=> 
(retract ?f) 
(assert (columnCurrent 1)) 
(assert (lineCurrent (- ?i 1))) 
(printout t crlf)) 

我想這個最終結果:

????####???? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 

???????????? 
+0

結構需要像這樣或可以不同? – rpax

+0

它可以不同! –

+0

你解決了這個問題嗎? – rpax

回答

0

你可以像這樣的東西開始。

(deftemplate point 
(slot i(type INTEGER)) 
(slot j(type INTEGER))  
) 
(defglobal ?*ROWS* = 5) 
(defglobal ?*COLS* = 5) 

(deffacts initial 
(currentRow 0) 
(currentColumn 0) 
(point(i 1)(j 1)) 
(point(i 1)(j 2)) 
(point(i 1)(j 3)) 
(point(i 2)(j 1)) 
(point(i 2)(j 3)) 
(point(i 3)(j 1)) 
(point(i 3)(j 2)) 
(point(i 3)(j 3)) 
) 



(defrule print_1 
    "Prints 1 if there's a point to print" 
    ?r<-(currentRow ?i) 
    ?c<-(currentColumn ?j) 
    (point(i ?i)(j ?j)) 
    => 
    (printout t "1 ") 
    (retract ?c) 
    (assert (currentColumn (+ ?j 1))) 
) 
(defrule print_0 
    "Prints 0 if there's not any point to print" 
    ?r<-(currentRow ?i) 
    ?c<-(currentColumn ?j&:(<= ?j ?*COLS*)) 
    (not(point(i ?i)(j ?j))) 
    => 
    (printout t "0 ") 
    (retract ?c) 
    (assert (currentColumn (+ ?j 1))) 

) 
(defrule printNextRow 
    "If we have reached the limit" 
    ?c<-(currentColumn ?j&:(> ?j ?*COLS*)) 
    ?r<-(currentRow ?i&:(< ?i ?*ROWS*)) 
    => 
    (printout t "" crlf) 
    (retract ?c) 
    (retract ?r) 
    (assert (currentColumn 0)) 
    (assert (currentRow (+ ?i 1))) 
) 
(reset) 
(run) 

的想法是隻使用asserts/retracts來模擬for loop

輸出:

Jess, the Rule Engine for the Java Platform 
Copyright (C) 2008 Sandia Corporation 
Jess Version 7.1p2 11/5/2008 

0 0 0 0 0 0 
0 1 1 1 0 0 
0 1 0 1 0 0 
0 1 1 1 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

希望這有助於

+0

@TiagoSantos如果有用,請考慮+1 – rpax

0

的多時隙包含的對象,但不能匹配列表項中的對象。這將無法工作:

(object $? (coordinate ?i ?ca) $?) 

應該可以存儲一個座標對作爲字符串(或摺疊整數):

(deffacts I 
(object (coordinate "5.23" "6.23" "7.2" "8.2"))) 

現在重寫模式根據

(object (coordinate $? ?x&=(str-cat ?i "." ?ca) $?))) 

(你的循環不能很好地工作,但我不想浪費我的時間,用這個絕對沒有聲明的規則代碼,很抱歉。)