A Prolog PROGRAM example designed by us




Here is our "maze" program. It uses an external database file called: 'maze1.txt' This program will return all the possible solutions for navigating through a path in a graph. It can only go to one node once per navigation. The graph has one entry point and one exit point. The maze file specifies the attributes and complexity of the graph.


domains
	room = symbol
	roomlist = room*

facts
	hall(room, room)
	avoid(roomlist)

predicates
	nondeterm neighbor(room, room)
	nondeterm go(room, room)
	nondeterm route(room, room, roomlist)
	
	nondeterm member(room, roomlist)
	nondeterm printlist(roomlist)
	nondeterm can_leave(roomlist)

clauses
	neighbor(X, Y) :- hall(X, Y).
	neighbor(X, Y) :- hall(Y, X).

	go(Start, Finish) :- VisitedRooms = [Start], route(Start, Finish, VisitedRooms).
	
	route(exit, exit, VisitedRooms) :-
		can_leave(VisitedRooms),
		printlist(VisitedRooms), nl, fail.
	
	route(Room, Finish, VisitedRooms) :-
		neighbor(Room, NextRoom),
		avoid(DangerousRooms),
		not( member(NextRoom, DangerousRooms) ),
		not( member(NextRoom, VisitedRooms) ),
		route(NextRoom, Finish, [NextRoom|VisitedRooms]).
	
	member(X, [X|_]).
	member(X, [_|Y]) :- member(X, Y).
	
	printlist([]).
	printlist([Head|Tail]) :- printlist(Tail), write(Head), write(" ").
	
	% Gives a list of the rooms that must be visited
	can_leave(VisitedRooms) :- 
		member("5", VisitedRooms),
		member("14", VisitedRooms).

goal
	write("Maze filename--> "),
	readln(FileName), 
	consult(FileName), 
	write("-----------------------------------------------------"), nl,
	go(entry, exit).