Could someone please explain how this code works

Im working on solving a task, where i have to walk thorugh a labyrinth with doors to get to a specific room and i have to find out the way. I created this program with help but don’t quite understand it, could someone please do so?

(tuer=door; richtung=direction/arrow; weg=the path; Start/Ende=start/end; Loesung=solution; Punkt =Point, Knot)

tuer(a,b).
tuer(b,e).
tuer(b,c).
tuer(d,e).
tuer(c,d).
tuer(e,f). 
tuer(g,e).

richtung(X, Y):-tuer (X,Y). 
richtung(X,Y):-tuer (Y, X).

%Now is the point of not understanding anything
weg(Start,Ziel, Loesung): -richtung (Start, X), richtung (Y, Ziel),
                                              weg (Start, [Start], Ziel, Loesung).

weg(Ziel, Weg,Ziel, Loesung):-Loesung = Weg.

weg(PunktA, Weg, Ziel, Loesung):-
         richtung (PunktA, PunktX),
         \+(member(PunktX, Weg)), 
          append(Weg, [PunktX], WegNeu),
          weg (PunktX, WegNeu, Ziel, Loesung).

Thank you in advance!

Also asked at prolog - Could someone please explain how this code works - Stack Overflow

Only you know why you don’t understand something. Anyway- something that always works in situations like this is prolog’s trace facility. Google it, use it, follow it. Don’t expect shortcuts.

While you can ask these type of homework questions here, do not expect an answer here.

To get started learning Prolog please use one of the free introduction books. :slightly_smiling_face:

1 Like