water-jug-problem

If statement in prolog

心已入冬 提交于 2019-12-11 07:46:58
问题 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!! 回答1: Like this: is_final_state :- katastasi(S), writeln(S), S=[C1-W1,C2-W2,C3

3-jugs of water in prolog doesn't work

跟風遠走 提交于 2019-12-11 04:47:17
问题 I have 3 jugs of water problem to solve but with a little trick.I dont have to use an algorithm but to have a 'function' that allows to user to move litres from on jug to another with an initial and final state that its written also by him. For example he writes initial(10,0,0,0,r) and the first state is 10 litres in first and zero in the other two, also he writes final(0,3,3,3,l) and the final state has 3 litres in the two smaller jugs and zero in the first one. The 'moves' between the jugs

What's wrong with my prolog program for solving the 3 jugs of water puzzle?

瘦欲@ 提交于 2019-12-02 04:10:54
问题 Can anyone find why I can't have any true answers with my 'go' at this code? For example, I write go(7,3,l) and I suppose that it should move 3 litres of water to the second jug, but it is false according to prolog. What's wrong? :- dynamic go/3. :- dynamic cur_state/1,init/5. :- dynamic end_state/1, final/5. cur_state(State):-State = state(10,0,0,7,l). end_state(State):-State = state(0,3,3,0,r). pour(state(D1,D2,D3,n,l),move(D,C,r),state(D,C,D3,n,r)) :- D is D1-n, C is D2+n. pour(state(D1,D2