-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray_predicates.pl
68 lines (57 loc) · 1.57 KB
/
ray_predicates.pl
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
:- module(ray_predicates, [
xray_of/2,
yray_of/2
]).
xray_forward_of(cell(X, Y), cell(A, B)) :-
X1 is X + 1,
is_cell_valid(cell(X1, Y)),
\+ wall(X1, Y),
(
A is X1,
B is Y;
X1 is X + 1,
xray_forward_of(cell(X1, Y), cell(A, B))
).
xray_backward_of(cell(X, Y), cell(A, B)) :-
X1 is X - 1,
is_cell_valid(cell(X1, Y)),
\+ wall(X1, Y),
(
A is X1,
B is Y;
X1 is X - 1,
xray_backward_of(cell(X1, Y), cell(A, B))
).
% Returns all valid cells to the left and right of cell(X, Y)
% until a wall is found.
xray_of(cell(X, Y), List) :-
findall(cell(A, B), xray_forward_of(cell(X, Y), cell(A, B)), Right),
findall(cell(A, B), xray_backward_of(cell(X, Y), cell(A, B)), Left),
append(Right, Left, List).
% Returns all valid cells under and above
yray_forward_of(cell(X, Y), cell(A, B)) :-
Y1 is Y + 1,
is_cell_valid(cell(X, Y1)),
\+ wall(X, Y1),
(
A is X,
B is Y1;
Y1 is Y+1,
yray_forward_of(cell(X, Y1), cell(A, B))
).
yray_backward_of(cell(X, Y), cell(A, B)) :-
Y1 is Y - 1,
is_cell_valid(cell(X, Y1)),
\+ wall(X, Y1),
(
A is X,
B is Y1;
Y1 is Y - 1,
yray_backward_of(cell(X, Y1), cell(A, B))
).
% Returns all valid cells above and below cell(X, Y)
% until a wall is found.
yray_of(cell(X, Y), List) :-
findall(cell(A, B), yray_forward_of(cell(X, Y), cell(A, B)), Above),
findall(cell(A, B), yray_backward_of(cell(X, Y), cell(A, B)), Below),
append(Above, Below, List).