2013-05-25 121 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