2013-05-25 119 views
-1

我已经在CLIPS中实现了Phutball。我不知道为什么,但我有这种感觉,我在这里写下了一些多余的,“危险”的东西。我会发布部分程序,希望你能帮我清理一下,或者让它更加紧凑。虽然该程序运行并通过了所有测试,但我仍然希望获得另一组眼睛。CLIPS编码清理

这里是世界的模板:

(deftemplate world 
(multislot limit) ; max size (width, height) 
(multislot ball) ; the ball 
(multislot men) ; positions one after another, x y -, 
(slot id) ; id for world 
(multislot moves) ; moves list , null at start 
(slot coord) ; coordinates for next move 
) 

我的坐标是这些:

(deffacts coordinates "Direction" 

(coord 1 0 D) 
(coord -1 0 U) 
(coord 0 -1 L) 
(coord 0 1 R) 
(coord -1 -1 UL) 
(coord -1 1 UR) 
(coord 1 -1 DL) 
(coord 1 1 DR) 
) 

这里是我的运动功能之一来检查,如果位置不必须在它的人,它不能再往前走。

(defrule blocked_move 
    (coord ?gox ?goy ?poz) 

?f <-(myWorld 
     (limit $?l) 
     (ball ?x ?y) 
     (men $?men) 
     (id ?curent) 
     (moves $?mutari) 
     (coord ?poz) 
) 
;no position to go next 
(not (myWorld 
      (limit $?l) 
      (ball ?x ?y) 
      (men $?start ?mx &:(eq (+ ?x ?gox) ?mx) ?my &:(eq (+ ?y ?goy) ?my) - $?end) 
      (id ?curent) 
      (moves $?mutari) 
      (coord ?poz) 
)) 

=> 
;go back to a position with no direction 
(retract ?f) 
(assert(myWorld 
     (limit $?l) 
     (ball (+ ?x ?gox) (+ ?y ?goy)) 
     (men $?men) 
     (id ?curent) 
     (moves $?mutari (+ ?x ?gox) (+ ?y ?goy) -) 
     (coord NULL) 
)) 
) 

我有一个更多的移动功能(只要有玩家跳过就移动),但上面的一个是打扰我。如果你是哲学家足球的好朋友,或者只是一个很好的CLIPS程序员,我希望你能帮我清理一下。谢谢

回答

0

我不太明白你如何管理这些举措,但这里是我的想法。

如果您在同一时间只有一个世界,我不会用一个模板,并有不同的事实,它的信息:

(deffunction init() 
    ; Give value to the variables 
    (assert (limit ?width ?height)) 
    (assert (ball ?x ?y)) 
    ... 
) 

和使用实际上对于(人x和y?)在现场的每个人(这只是一个初步的想法,也许一个列表更容易在现实情况下,管理),所以对于一个有效的移动的规则是这样的:

(defrule valid_move 
    (coord ?gox ?goy ?poz) 
    ;there is a man 1 position away in the direction i want to move 
    ?m <- (man ?mx &:(eq (+ ?x ?gox) ?mx) ?my &:(eq (+ ?y ?goy) ?my)) 
    (test (;final position of the ball not out of bounds of the field)) 
=> 
    ;do whatever to take the move ?gox ?goy as valid (move ball, remove men, or save the movement for later use...) 
) 

所以没有需要为阻止的移动制定规则自有效规则以来一个不会因为错误的举动而被解雇

0

我最近在F#中实现了phutball,其中一部分是因为我找不到其他任何实现这个游戏的玩法。 如果你有兴趣,这里是代码:https://github.com/htoma/phutball-fs