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

我想要这个最终结果:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

????????????
4

2 回答 2

0

多槽包含对象,但您无法匹配列表项中的对象。这将不起作用:

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

应该可以将坐标对存储为字符串(或折叠整数):

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

现在根据

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

(您的线路循环效果不佳,但我不想在这个绝对非声明性的规则代码上浪费我的时间。抱歉。)

于 2014-03-19T12:41:11.753 回答
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)

这个想法是模拟for loopusing only asserts/retracts

输出:

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

希望这可以帮助

于 2014-03-19T12:36:13.910 回答