1

I'm creating a text adventure game on SWI-Prolog and I want to include (some kind of) dialogs.

What I have so far is:

dialog1(1):- nl,write('blah blah'),nl
             write('a: this is answer a'),nl,
             write('b: this is answer b'),nl.

a:- write('respond to answer a'),nl.
b:- write('respond to answer b'),nl. 

That's pretty much the first dialog. Now I want to create a second dialog similar to the first one.

dialog1(2):- nl,write('blah blah'),nl,
             write('a: this is answer a'),nl,
             write('b: this is answer b'),nl.

a:- write('respond to answer a'),nl.
b:- write('respond to answer b'),nl. 

How can I check if the dialog is the first or the second one? I want to do that because when the user types a., I need the right answer a to be shown.

I thought I could use something like

a(1):- write('respond to answer a'),nl.
b(1):- write('respond to answer b'),nl. 

/* and on the second dialog*/
a(2):- write('respond to answer a'),nl.
b(2):- write('respond to answer b'),nl. 

But still, if the user is on dialog 2 and he types a(1),the first answer will appear.

false
  • 10,264
  • 13
  • 101
  • 209
Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43
  • 1
    Do you want to create global state like this: `?- dialog(1).` `?- a.` a now shows the answer for dialog 1 an not for dialog 2. – User Nov 06 '13 at 22:05
  • Yeah. That's what I want to do. On the `dialog(1)` the `a` to show the answer for `dialog(1)` and on `dialog2` the `a` to show the answer for `dialog(2)` – Shevliaskovic Nov 06 '13 at 22:07
  • 1
    use `apropos(assert).` for information about `assert(statement)`. with this you can add a new `statement` to the database like `assert(a(1):- write('...'))`. Use `retract` to forget about a statement. – User Nov 06 '13 at 22:18
  • I can use assert like that? Like give a couple of commands? `assert(a(1)):- write('aa'),nl,write('qweq').` ? – Shevliaskovic Nov 06 '13 at 22:30
  • Look here: http://stackoverflow.com/questions/2435237/prolog-assert-and-retract – User Nov 06 '13 at 22:33
  • You can do this: `assert(a(1):- write('aa'),nl,write('qweq'))` – User Nov 07 '13 at 14:32
  • When I run that, I get `true` as a result,but it doesn't write `aa` and `qweq`. and when I run `a(1).` I get `false.` – Shevliaskovic Nov 07 '13 at 19:33

1 Answers1

0

You can use the -> implication as kind of if statement:

?- true -> write('t') ; write('f').
t
true.
?- false -> write('t') ; write('f').
f
true.

You can apropos(assert). for information about assert(statement). With assert you can add a new statements to the database like assert(a(1):- write('...')). Use retract to forget about a statement.

User
  • 14,131
  • 2
  • 40
  • 59
  • Like how could I include that in my code? dialog(1) == true -> write('t') ; write('f'). ? something like that? – Shevliaskovic Nov 06 '13 at 21:55
  • I am pretty sure that I just did not get the question.. The -> is the if statement but you may need something else. – User Nov 06 '13 at 22:03