forked from pauldepalma/CPSC427
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknight5.pl
executable file
·56 lines (46 loc) · 973 Bytes
/
knight5.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
%stops after first goal.
path(Start, Goal) :- empty(EmptyBeen),
push(Start,EmptyBeen,Been),
path1(Start, Goal, Been).
path1(Goal, Goal, Been) :-
nl,
nl,
write('Solution is: '),
nl,
display_r(Been).
path1(Start, Goal, Been) :-
move(Start, Next),
not(member_st(Next, Been)),
push(Next, Been, Newbeen),
path1(Next, Goal, Newbeen), !.
move(1,6).
move(1,8).
move(2,7).
move(2,9).
move(3,4).
move(3,8).
move(4,3).
move(4,9).
move(6,1).
move(6,7).
move(7,2).
move(7,6).
move(8,1).
move(8,3).
move(9,2).
move(9,4).
not(P) :-
P,!,fail
;
true.
mem(Elt,[Elt|_]).
mem(Elt,[_|Tail]) :- mem(Elt,Tail).
empty([]).
push(Top, Stack, [Top|Stack]).
pop(Top, Stack, [Top|Stack]).
member_st(Elt, Stack) :- mem(Elt,Stack).
display_r(Stack) :- empty(Stack).
display_r(Stack) :- pop(Top, Rest, Stack),
display_r(Rest),
write(Top),
nl.