### Artificial Intelligence TUTORIAL : 8

1.  Write a program to implement Tower of Hanoi.
domains
location = right;left;middle
right,left,middle = symbol

predicates
hanoi(integer)
move(integer,location,location,location)
inform(integer,location,location)

clauses
hanoi(N):-
N=0,
!,
write("Enter positive number : "),nl.

hanoi(N):-
move(N,left,middle,right).

move(1,A,_,C):-
inform(1,A,C).

move(N,A,B,C):-
NN=N-1,
move(NN,A,C,B),
inform(N,A,C),
move(NN,B,A,C).

inform(N,L1,L2):-
write("Move disk ",N,"From ",L1," to ",L2),nl.

2.  Write a program to implement Monkey & Banana (M&B) Problem. M&B Problem: A Monkey is standing at the door of a Room. Banana is hanging in the middle of the Room. Monkey wants to eat the banana but banana is not reachable by the monkey. To get the banana monkey has to use a table lying in a corner of the Room. Monkey need to push the table in the middle of the Room. Then it has to get on the table and get the Banana.
DOMAINS
st = state(symbol,symbol,symbol,symbol).
PREDICATES

canget(st).
move(st,symbol,st).

CLAUSES
canget(state(_,_,_,has)).
canget(State):-move(State1,Move,State2),
canget(State2).
move(state(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)).

move(state(P1,atfloor,P1,Has),climb,state(P1,onbox,P1,Has)).

move(state(P1,atfloor,P1,Has),push,state(P2,atfloor,P2,Has)).

move(state(P1,onfloor,P2,Has),walk,state(P2,atfloor,P2,Has)).
or

A program for the monkey and banana problem.

% move( State1, Move, State2): making Move in State1 results in State2;
%    a state is represented by a term:
%    state( MonkeyHorizontal, MonkeyVertical, BoxPosition, HasBanana)

move( state( middle, onbox, middle, hasnot),   % Before move
grasp,                                   % Grasp banana
state( middle, onbox, middle, has) ).    % After move

move( state( P, onfloor, P, H),
climb,                                   % Climb box
state( P, onbox, P, H) ).

move( state( P1, onfloor, P1, H),
push( P1, P2),                           % Push box from P1 to P2
state( P2, onfloor, P2, H) ).

move( state( P1, onfloor, B, H),
walk( P1, P2),                           % Walk from P1 to P2
state( P2, onfloor, B, H) ).

% canget( State): monkey can get banana in State

canget( state( _, _, _, has) ).       % can 1: Monkey already has it

canget( State1)  :-                   % can 2: Do some work to get it
move( State1, Move, State2),        % Do something
canget( State2).                    % Get it now

3.  Write a prolog program to solve 8-queen problem.
domains
List = integer*
Queen = integer

predicates
del(integer,List,List)

solution(List)
safe(List)
noattack(integer,List,integer)
perm(List,List)

clauses
del(X,[X|Y],Y).
del(X,[Y|T],[Y|T1]):-
del(X,T,T1).
perm([],[]).
perm(L,[X|P]):-
del(X,L,L1),
perm(L1,P).

solution(S):-
perm([1,2,3,4,5,6,7,8],S),
safe(S).
safe([]).
safe([Queen|Others]):-
noattack(Queen,Others,1),
safe(Others).

noattack(_,[],_).

noattack(Y,[Y1|List],X_dist):-
Y-Y1 <> X_dist,
Y1-Y <> X_dist,
Dist=X_dist+1,
noattack(Y,List,Dist).