-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.lgt
158 lines (116 loc) · 3.56 KB
/
snake.lgt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
:- object(snake,
imports([actor, fluent_predicates])).
action(move/0).
action(eat/0).
action(up/0).
action(down/0).
action(left/0).
action(right/0).
fluent(head/2).
fluent(body/2).
fluent(body_dl/2).
fluent(whole_body/2).
fluent(direction/2).
:- public(head/2).
head(Head, Sit) :-
bedsit::holds(snake_head(Head), Sit).
:- public(body/2).
body(Body, Sit) :-
bedsit::holds(snake_body(Body-[]), Sit).
:- public(body_dl/2).
body_dl(Body, Sit) :-
bedsit::holds(snake_body(Body), Sit).
:- public(whole_body/2).
whole_body([Head|Body], Sit) :-
body(Body, Sit),
head(Head, Sit).
:- public(direction/2).
direction(Direction, Sit) :-
bedsit::holds(snake_direction(Direction), Sit).
:- public(move/5).
move(up, coord(X, Y), Body, coord(X, NY), NewBody) :-
YY is Y - 1,
( YY < 1, board::size(NY)
; YY = NY
), !,
difflist::add(coord(X, Y), Body, NewBody).
move(down, coord(X, Y), Body, coord(X, NY), NewBody) :-
YY is Y + 1,
( board::size(H), YY > H, NY = 1
; YY = NY
), !,
difflist::add(coord(X, Y), Body, NewBody).
move(left, coord(X, Y), Body, coord(NX, Y), NewBody) :-
XX is X - 1,
( XX < 1, board::size(NX)
; NX = XX
), !,
difflist::add(coord(X, Y), Body, NewBody).
move(right, coord(X, Y), Body, coord(NX, Y), NewBody) :-
XX is X + 1,
( board::size(W), XX > W, NX = 1
; NX = XX
), !,
difflist::add(coord(X, Y), Body, NewBody).
:- end_object.
:- object(eat,
imports(action)).
poss(Sit) :-
snake::head(Location, Sit),
apple::location(Location, Sit).
retract_assert([ snake_body(_)
, snake_head(_)
]
,
[ snake_body(NewBody)
, snake_head(NewHead)
]) :-
snake::holds(head(Head)),
snake::holds(body_dl(Body)),
snake::holds(direction(Direction)),
snake::move(Direction, Head, Body, NewHead, NewBody).
:- end_object.
:- object(move,
imports(action)).
poss(Sit) :-
\+ eat::poss(Sit).
retract_assert([ snake_body(_)
, snake_head(_)
]
,
[ snake_body(NewBody)
, snake_head(NewHead)
]) :-
snake::holds(head(Head)),
snake::holds(body_dl([_|Body]-Hole)),
snake::holds(direction(Direction)),
snake::move(Direction, Head, Body-Hole, NewHead, NewBody).
:- end_object.
:- object(up,
imports(action)).
poss(Sit) :-
snake::direction(Direction, Sit),
( Direction == left ; Direction == right ).
retract_assert([snake_direction(_)], [snake_direction(up)]).
:- end_object.
:- object(down,
imports(action)).
poss(Sit) :-
snake::direction(Direction, Sit),
( Direction == left ; Direction == right ).
retract_assert([snake_direction(_)], [snake_direction(down)]).
:- end_object.
:- object(left,
imports(action)).
poss(Sit) :-
snake::direction(Direction, Sit),
( Direction == up ; Direction == down ).
retract_assert([snake_direction(_)], [snake_direction(left)]).
:- end_object.
:- object(right,
imports(action)).
poss(Sit) :-
snake::direction(Direction, Sit),
( Direction == up ; Direction == down ).
retract_assert([snake_direction(_)], [snake_direction(right)]).
:- end_object.