1

I already made this code, but when I consult it in GNU Prolog, it shows like this.

enter image description here

here is my code

initial(left,3,3,0,0).
final(right,0,0,3,3).
P1=3000.
P2=5000.
P3=8000.
A is P1.
B is P2.
C is P3.

bad([_,A1,B1,C1,A2,B2,C2]):-
   (   A1>B1,B1>C1,A1>C1
   ;   A2>B2,B2>C2,A2>C2
   ).

start:-
   initial(S),
   rez(S,Path),
   my_write(Path).

rez(State,Sol):-
   bkt(State,[],Sol).

bkt(State,Path,[State|Path]):-
   final(State).
bkt(State,Path,Sol) :-
   arc(State,N1),
   \+bad(N1),
   \+member(N1,Path),
   bkt(N1, [State|Path],Sol).

arc([left,As,Bs,Cs,Ad,Bd,Cd], [right,As1,Bs1,Cs1,Ad1,Bd1,Cd1]) :-
   boat(A,B,C),
   As >= A, As1 is As-A,
   Bs >= B, Bs1 is Bs-B,
   Cs >= C, Cs1 is Cs-C,
   Ad1 is Ad+A,
   Bd1 is Bd+B,
   Cd1 is Cd+C.
arc([right,As,Bs,Cs,Ad,Bd,Cd], [left,As1,Bs1,Cs1,Ad1,Bd1,Cd1]):-
   boat(A,B,C),
   Ad >= A, Ad1 is Ad-A,
   Bd >= B, Bd1 is Bd-B,
   Cd >= C, Cd1 is Cd-C,
   As1 is As+A,Bs1 is Bs+B, Cs1 is Cs+C.

boat(A,B,C):-
   member([A,B,C],[[0,1],[1,0],[1,1],[2,0],[0,2]]).

my_write([]).
my_write([H|T]):-
   write(H),
   nl,
   my_write(T).

I hope you all can help me to fix this code so it can run well when I consult in GNU Prolog.

false
  • 10,264
  • 13
  • 101
  • 209
helena
  • 39
  • 1
  • 6
  • 3
    You are having a basic lack of understanding of Prolog syntax and how the language workis. Statements like, `P1=3000.` don't set `P1` to 3000 like in other languages. It just attempts to assert a fact or rule, `'='(P1, 3000).` which gives a *singleton* warning (a variable is used for no reason) and is an attempt to redefine the built-in operator, `(=)/2`. `A is P1.` which is equivalent to `is(A, P1)` attempts to redefine `is/2` and has two singletons, `A` and `P1`. – lurker Jul 11 '17 at 11:08
  • @lurker , yeah i still lack in basic... What i want to make is a game with 3 thieves with each thief has their own bag of money. that three bag has different amount, so i want to make that the P1 is belong to A, P2 belong to B, P3 belong to C. each thief cannot stay with the bigger amount of money than he had. mean, A cannot stay with P2 or P3 unless A or B present. how can i solve it? – helena Jul 11 '17 at 12:49
  • 1
    You might want to start by going through this [Prolog tutorial](http://www.learnprolognow.org/lpnpage.php?pageid=online) to learn the language basics. In your case, you need to learn a little about the language first before someone can help you with your actual problem. – lurker Jul 11 '17 at 14:26
  • 2
    Your first fact, `initial(left,3,3,0,0).` isn't correct. It should be, `initial([left,3,3,0,0]).`. What's the purpose of `P1`, `P2` and `P3`? If you want initializers for `A`, `B`, and `C`, assert them as facts: `initial_a(3000).`, `initial_b(5000).`, etc. Then you can do `initial_a(A)` followed by `As >= A`, etc. – lurker Jul 11 '17 at 14:49
  • i'm sorry for my lackness about prolog. P1 is initial for money bag that belong to A, P2 belong to B, and P3 belong to C... i will give a link to youtube, i want to make game like that but in prolog. https://www.youtube.com/watch?v=2sKMlKC2Kos – helena Jul 11 '17 at 15:01
  • 2
    Knowing the details of the game is past where you need to get to first. Walk before running. If you need `P1`, etc, then continue with the method I showed. `initial_p1(3000).` etc. Then you can unify `A = P1`, etc, within your predicate, but you can't do it as a fact. – lurker Jul 11 '17 at 15:09
  • 1
    boat/3 is **always** false. – CapelliC Jul 11 '17 at 19:22

0 Answers0