1

Hello i have a problem with if statement. i have this

final(C1-W1,C2-W2,C3-W3):- 
    retractall(end_jug), 
    asserta( end_jug(C1,W1) ),
    asserta( end_jug(C2,W2) ),
    asserta( end_jug(C3,W3) ).

and this one

katastasi(L) :- 
    findall(X-W, jug(X,W), L0), sort(L0,L).

How can i have this check????:

    if(jug(C1,W1) == end_jug(C1,W1) && jug(C2,W2) == end_jug(C2,W2) && jug(C3,W3) == end_jug(C3,W3)) write('Congrats').

Thanks in advance!!

false
  • 10,264
  • 13
  • 101
  • 209
tetartos
  • 171
  • 1
  • 1
  • 10
  • You should've mentioned [your previous question](http://stackoverflow.com/questions/9030462/3-jugs-of-water-in-prolog-doesnt-work). :) – Will Ness Feb 23 '12 at 20:08

2 Answers2

2

Like this:

is_final_state :- 
  katastasi(S), writeln(S),
  S=[C1-W1,C2-W2,C3-W3],
  (  end_jug(C1,W1),
     end_jug(C2,W2),
     end_jug(C3,W3)
  -> writeln('Congrats!')
  ;  W1+W2+W3 < 6
  -> writeln('WARNING: not enough water left!'),
     fail
  ).

You should've mentioned your previous question. This code is a part of the code in the answer there.

Your code is in Prolog, but the check you asked about was in "Basic". Let go of the Basic mentality. :) Prolog does the checks for you, as part of unification.

The value 6 in the code above should be calculated really, according to the final values the user specifies by calling final at the start fo the game. The final value (of each end_jug fact) can be retrieved just like the current value (of each jug fact) is retrieved by jugsState predicate (which I assume is now called katastasi).

Now you must complete your game by writing the stop predicate, which should do the cleanup (i.e. call retract on all the asserted facts). You can even make an undo predicate. :)

Community
  • 1
  • 1
Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • and the [previouser](http://stackoverflow.com/questions/8748867/whats-wrong-with-my-prolog-program-for-solving-the-3-jugs-of-water-puzzle) question too x) – m09 Feb 23 '12 at 22:07
  • @Mog I meant that this new question refers to the code from the answer to that preious question, so must be (is easier) seen in that context. He wanted a "game", it turns out. Ah, and now I see there's a naming conflict there. :) will edit. – Will Ness Feb 24 '12 at 06:07
0

Just write the conditions joined by , under a new predicate:

win(C1, W1, C2, W2, C3, W3):-
    jug(C1,W1) \== end_jug(C1,W1),
    jug(C2,W2) \== end_jug(C2,W2),
    jug(C3,W3) \== end_jug(C3,W3).

Then use this predicate when needed.

finish(C1, W1, C2, W2, C3, W3):-
    win(C1, W1, C2, W2, C3, W3),
    write('Congrats').

Or write it as a whole:

finish(C1, W1, C2, W2, C3, W3):-
    jug(C1,W1) \== end_jug(C1,W1),
    jug(C2,W2) \== end_jug(C2,W2),
    jug(C3,W3) \== end_jug(C3,W3),
    write('Congrats').
Sufian Latif
  • 13,086
  • 3
  • 33
  • 70