-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.clp
104 lines (93 loc) · 2.47 KB
/
Robot.clp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;; Static fact (Grid X Y Warehouse X Y)
;; Static fact (MaxDepth ?maxDepth) (Max depth in graph)
;; Dynamic fact (Robot X Y BULBS Lamp X X BURNED_OUT_BULBS Lamp.. Level LEVEL)
(deffacts facts
(Grid 5 4 Warehouse 2 3)
)
(defglobal ?*nod-gen* = 0)
(deffunction init ()
(reset)
(printout t "Max depth? " crlf)
(bind ?maxDepth (read))
(printout t "Type search " crlf " 1.- Breadth" crlf " 2.- Depth" crlf )
(bind ?a (read))
(if (= ?a 1)
then (set-strategy breadth)
else (set-strategy depth))
(assert (Robot 1 3 0 Lamp 3 4 3 Lamp 5 4 2 Lamp 4 2 2 Level 0))
(assert (MaxDepth ?maxDepth))
)
(defrule move-right
(Grid ?mX ?mY $?)
(Robot ?x $?aux Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (< ?x ?mX))
=>
(assert (Robot (+ ?x 1) $?aux Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule move-left
(Grid ?mX ?mY $?)
(Robot ?x $?aux Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (> ?x 1))
=>
(assert (Robot (- ?x 1) $?aux Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule move-up
(Grid ?mX ?mY $?)
(Robot ?x ?y $?aux Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (< ?y ?mY))
=>
(assert (Robot ?x (+ ?y 1) $?aux Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule move-down
(Grid ?mX ?mY $?)
(Robot ?x ?y $?aux Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (> ?y 1))
=>
(assert (Robot ?x (- ?y 1) $?aux Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule repair
(Robot ?x ?y ?bulb $?aux Lamp ?x ?y ?lBulb $?aux1 Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (>= ?bulb ?lBulb))
=>
(assert (Robot ?x ?y (- ?bulb ?lBulb) $?aux $?aux1 Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule charge
(Grid $? Warehouse ?x ?y)
(Robot ?x ?y ?bulb $?aux Lamp ?lX ?lY ?lBulb $?aux1 Level ?n)
(MaxDepth ?maxDepth)
(test (< ?n ?maxDepth))
(test (< ?bulb ?lBulb))
=>
(assert (Robot ?x ?y (+ ?bulb (- ?lBulb ?bulb)) $?aux Lamp ?lX ?lY ?lBulb $?aux1 Level (+ ?n 1)))
(bind ?*nod-gen* (+ ?*nod-gen* 1))
)
(defrule bad-gameover
(declare (salience -99))
=>
(halt)
(printout t "**** NOT SOLUTION :/ **** " crlf)
)
(defrule gameover
(declare (salience 100))
(Robot ?x ?y ?bulb Level ?n)
(test (= ?bulb 0))
=>
(halt)
(printout t "**** GAME OVER **** " crlf)
(printout t "Level : " ?n " Nodes : " ?*nod-gen* crlf)
)