Search code examples
prologswi-prologprolog-toplevel

How to turn off "true" and "false" outputs in Prolog?


I would like to write a small text-based adventure game using Prolog (this might be a dumb idea but I am not here to discuss that).

The only problem is that I can't manage to print text on screen without the "true" and "false" values to appear as well.

For instance if I try something like:

take(desk) :- write('This thing is way too heavy for me to carry!').

where take is a one place predicate and desk a name I get as an output:

?- take(desk).
   This thing is way too heavy for me to carry!
   true.

How can I get rid of this "true" or "false" outputs?

Just to mention that I also tried with the format/1 one place predicate for simple text output and also the format/2 two place predicate (when I want to output the name of a variable) but it gives exactly the same problem.

I have also seen this answer but first it is not detailed enough (at least not for someone like me) and second, I hope deep inside that there is a simpler manner to do it.

And finally, I am using SWI-Prolog.

Thank you.


Solution

  • A simplistic method would be to create a little REPL (read, evaluate, print loop) of your own. Something like this:

    game :-
        repeat,
        write('> '),
        read(X),
        call(X),
        fail.
    

    This will just prompt and execute whatever you enter at the prompt. In conjunction with your take fact (and another I added for illustration):

    take(desk) :- write('This thing is way too heavy for me to carry!'), nl.
    take(chair) :- write('This is easier to carry.'), nl.
    

    You would get:

    ?- game.
    > take(desk).
    This thing is way too heavy for me to carry!
    > take(chair).
    This is easier to carry.
    >
    

    You don't get the true or false because the game goal doesn't resolve until you exit the loop somehow. You could add checks for a quit or bye or whatever to exit the game loop. Ctrl-C or Ctrl-D can be used as well to abort the loop. You might need to add some other "features" to make it work to your needs or liking.