-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endgame.pl
122 lines (108 loc) · 3.13 KB
/
Endgame.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
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
% Load file KB1.pl. TRIED IT BUT IF USED THE PROGRAM DOES NOT TERMINATE.
% :- ['KB1'].
% Load file KB2.pl TRIED IT BUT IF USED THE PROGRAM DOES NOT TERMINATE.
% :- ['KB2'].
% --------------------------------------------------------------------------------
% KB 1
% Optimal Solution:-
% result(snap,result(right,result(collect, result(down,result(right,result(collect,result(right, result(collect,result(down,result(collect,result(left, s0))))))))))).
dimensions(5, 5).
thanos_location(3, 4).
stone(1, 1).
stone(2, 1).
stone(2, 2).
stone(3, 3).
iron_man_location(1, 2, [stone(1, 1), stone(2, 1), stone(2, 2), stone(3, 3)], s0).
% ---------------------------------------------------------------------------------
% KB 2
% Optimal Solution:-
% result(snap, result(down, result(left, result(left, result(left, result(collect, result(right, result(collect, result(right, result(collect, result(down, result(collect, result(left, s0))))))))))))),
%
% dimensions(4, 5).
%
% thanos_location(3, 0).
%
% stone(1, 1).
% stone(2, 1).
% stone(2, 2).
% stone(2, 3).
%
% iron_man_location(1, 2, [stone(1, 1), stone(2, 1), stone(2, 2), stone(2, 3)], s0).
% --------------------------------------------------------------------------------
% Main program
iron_man_location(I, J, Stones, result(A, S)):-
(
( % He was not on the top border and he was one cell down
A = up,
I1 is I + 1,
not(dimensions(I1, _)),
iron_man_location(I1, J, Stones, S)
);
( % He was not on the bottom border and he was one cell up
A = down,
I \= 0,
I1 is I - 1,
iron_man_location(I1, J, Stones, S)
);
( % He was not on the left border and he was one cell to the right
A = left,
J1 is J + 1,
not(dimensions(_, J1)),
iron_man_location(I, J1, Stones, S)
);
( % He was not on the right border and he was one cell to the left
A = right,
J \= 0,
J1 is J - 1,
iron_man_location(I, J1, Stones, S)
)
);
( % Iron Man did not change his location
( % Either Iron Man did not move or he was on the border
(
A = collect,
stone(I, J),
\+member(stone(I, J), Stones),
select(stone(I, J), New_stones, Stones),
iron_man_location(I, J, New_stones, S)
);
( % Upper border
iron_man_location(0, J, Stones, S),
A = up
);
% Bottom border
(
iron_man_location(I, J, Stones, S),
A = down,
I1 is I + 1,
dimensions(I1, _)
);
% Left border
(
iron_man_location(I, 0, Stones, S),
A = left
);
% Right border
(
iron_man_location(I, J, Stones, S),
A = right,
J1 is J + 1,
dimensions(_, J1)
)
)
).
snapped(result(snap, S)):-
( % Iron Man is in the same cell as Thanos and there are no stones left
thanos_location(I, J),
iron_man_location(I, J, [], S)
).
ids(Predicate, Initial_limit, Result):-
(
call_with_depth_limit(Predicate, Initial_limit, Result1),
Result1 \= depth_limit_exceeded,
Result = Result1
);
(
New_limit is Initial_limit + 1,
ids(Predicate, New_limit, Result)
).