plan(P) :-
      start(Start),
      action(Start,[Start],Q),
      reverse(Q,P).

action(S,P,P) :- goal(S).%%, !.      
action(S,Visited,P) :-
     next(S,N),             
     safe(N),               
     no_loop(N,Visited),          
     action(N,[N|Visited],P).   

no_loop(S,Visited) :- \+member(S,Visited).

/* je Aufgabe zu implementieren:
   start(...).
   goal(...).
   safe(S) :- ...
   next(S,T) :- ...
*/

/* Bsp: Münzspiel */

start([0,0,0,6,0,0,0]).

all_le1([]).
all_le1([0|Xs]) :-  all_le1(Xs).
all_le1([1|Xs]) :-  all_le1(Xs). 
%% all_le1([X|Xs]) :- (X = 0 ; X = 1), all_le1(Xs).

goal(S) :- all_le1(S).

safe(S).

append3(Xs, Ys, Zs, XsYsZs) :-
	append(Xs, YsZs, XsYsZs), append(Ys, Zs, YsZs).

replace([X,Y,Z], [Xn,Yn,Zn]) :- Y >= 2, Xn is X+1, Zn is Z+1, Yn is Y-2.

next(Xs,Ys) :- append3(P,L,S,Xs), replace(L,R), append3(P,R,S,Ys).

/* Bsp: Kannibalen */
/*
start(s(l,3,3)).
goal(s(r,0,0)).

safe(s(_,X,X)).
safe(s(_,3,_)).
safe(s(_,0,_)).
%% safe(s(_,X,Y)) :- (X= 3; X= 0; X=Y).

boat(0,1).
boat(0,2).
boat(1,1).
boat(1,0).
boat(2,0).
%% boat(X,Y) :- (X = 0 ; X = 1 ; X = 2), (Y = 0 ; Y = 1 ; Y = 2), X + Y > 0, X + Y < 3.

next(s(l,Ml,Cl),s(r,Mr,Cr)) :-
	boat(M,C),
	Ml >= M, Cl >= C,
	Mr is Ml - M, Cr is Cl - C.
next(s(r,Ml,Cl),s(l,Mr,Cr)) :-
	boat(M,C),
	Ml =< 3-M, Cl =< 3-C,
	Mr is Ml + M, Cr is Cl + C.
*/
