Menu


Notice: Undefined index: url2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 84

Notice: Undefined index: url3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 85

Notice: Undefined index: url4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 86

Notice: Undefined index: opurl2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 89

Notice: Undefined index: opurl3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 90

Notice: Undefined index: opurl4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/aidisplay.php on line 91

Artificial Intelligence [ Lab Programs ]


Aim:

Write a program in prolog to solve Tower of Hanoi

Solution :

/* Description:

This object of this famous puzzle is to move N disks from the left peg to the right peg using the center peg as an auxiliary holding peg. At no time can a larger disk be placed upon a smaller disk. The following diagram depicts the starting setup for N=3 disks. 
*/

% Production rules:

hanoi(N) 🡪 move(N,left,middle,right).
move(1,A,_,C) 🡪 inform(A,C),fail.
move(N,A,B,C) 🡪 N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).


% Domains:

loc =right;middle;left

% Predicates:

hanoi(integer)
move(integer,loc,loc,loc)
inform(loc,loc)

% Clauses:

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

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

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

inform(Loc1, Loc2):-
	write("\nMove a disk from ", Loc1, " to ", Loc2).

Output:

% Goal:

hanoi(3).
Move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right

Yes

Related Content :

Artificial Intelligence Lab Programs

1) Write a program in prolog to implement simple facts and Queries View Solution

2) Write a program in prolog to implement simple arithmetic View Solution

3) Write a program in prolog to solve Monkey banana problem View Solution

4) Write a program in prolog to solve Tower of Hanoi View Solution

5) Write a program in prolog to solve 8 Puzzle problems View Solution

6) Write a program in prolog to solve 4-Queens problem View Solution

7) Write a program in prolog to solve Traveling salesman problem View Solution

8) Write a program in prolog for Water jug problem View Solution