-1

I have implemented Phutball in CLIPS. I dont know why but i have the feeling i have some redundant , "dangerous" things written in here. I'll post part of the programs and hopefully you can help me cleanup a bit or make it more compact. Though the program works and passes all tests i still want another set of eyes.

Here is the world template :

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

My coordinates are these :

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

And here is one of my movement functions that checks if a position doesnt have men on it , it cant go any further.

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

I have one more movement function(that moves as long as there are players to jump over) , but the one above is bothering me. If you are familliar with Philosopher's Football or just a good CLIPS programmer , i hope you can help me cleanup a bit. Thank you

user1272703
  • 89
  • 1
  • 2
  • 9

2 Answers2

0

I don't quite understand how are you managing the moves, but here are my thoughts.

If you have only one world at the same time, I would not use a template for it and have different facts for its info:

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

And use a fact (man ?x ?y) for each man in the field (this is just an initial idea, maybe a list is easier to manage in the real case), so the rule for a valid move would be like this:

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

So there is no need to make a rule for a blocked move since the rule for the valid one won't be fired for a wrong move

Evans
  • 1,589
  • 1
  • 14
  • 25
0

I recently implemented phutball in F#, part of it because I couldn't find any other implementation of this game to play with. If you're interested, here's the code: https://github.com/htoma/phutball-fs

Horia Toma
  • 1,099
  • 2
  • 17
  • 29