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.
If you want to exit SWI-Prolog, issue the command halt., or simply type CTRL-d at the SWI-Prolog prompt.
not(X) is the way to implement negation in Prolog; however not(X) does not mean that X is false, it means that X can't be proven true.
I looked this up on the SWI prolog website where they defined it as. The arithmetic expression X equals Y.
output in Prologwrite(X) which writes the term X to the current output stream (which means the window on your workstation unless you have done something fancy). print(X, …) which writes a variable number of arguments to the current output stream.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With