tags:

views:

385

answers:

1

I'm writing a text adventure game in Prolog, and I am printing out room exits. I have code that does:

exits_from(Room) :-
  connected(Room, X),
  write(X), write('  ').

where connected/2 is:

connected(X, Y) :- path(X, Y).
connected(X, Y) :- path(Y, X).

and path is:

path(room, hallway).
path(hallway, foyer).

and so on.

When I am printing the exits for a room though, it gets the first, then wants a ';' to say that I want another solution. Is there anyway to force a predicate to compute the result entirely, so that the player wouldn't have to keep asking for more exits?

+5  A: 

one way is to do something like

print_all_solutions :-
  solution(Sol),
  write(Sol),
  fail. % this causes backtracking
print_all_solutions. % succed

another is to use special predicate forall, like follows:

forall(solution(Sol), write(Sol))
Xonix
Is forall built in for some implementations? It's not valid in gprolog apparently. But thanks for the backtracking tip!
Kai
According to this http://www.gprolog.org/manual/gprolog.html#htoc103 gprolog has findall/3.
pfctdayelise
findall/3 is not the same as forall/2.
Kaarel
SWI-Prolog has forall/2.
Kaarel