/*mandc Missionaries and Cannibals program Alan Bundy 6.11.79*/ /*top level*/ mandc :- gofrom([3,3,1],[0,0,0],[]). gofrom([0,0,0],Right,OldLefts) :- writef('which is goal state\n \n',[]). gofrom(Left,Right,OldLefts) :- legal(Left), legal(Right), not(member(Left,OldLefts)), applymove(Left,Right,[ML,CL,BL],[MR,CR,BR]), writef('\nThe left bank now contains %t missionaries and %t cannibals \n',[ML,CL]), writef('The right bank now contains %t missionaries and %t cannibals \n',[MR,CR]), gofrom([ML,CL,BL],[MR,CR,BR],[Left|OldLefts]). gofrom(_,_,_) :- writef('Backing up one move \n',[]), fail. /*decide which way to move*/ applymove([ML,CL,BL],Right,NewL,NewR) :- (BL=1 -> moveload([ML,CL,BL],Right,NewL,NewR); moveload(Right,[ML,CL,BL],NewR,NewL) ). /*move boatload over*/ moveload(Source,Target,NewS,NewT) :- move(BoatLoad), maplist(>=, Source, BoatLoad), mlmaplist(minus,[Source,BoatLoad,NewS]), mlmaplist(add,[Target,BoatLoad,NewT]), BoatLoad=[M,C,B], writef('\n move %t missionaries and %t cannibals \n',[M,C]). /*the moves*/ move([0,1,1]). move([2,0,1]). move([0,2,1]). move([1,1,1]). move([1,0,1]). /*is situation legal*/ legal([M,C,B]) :- (M=0; M >= C), !. legal([M,C,B]) :- writef('%t cannibals can eat %t missionaries \n',[C,M]), fail. /*add and minus are + and - on lists*/ add([X,Y,Z]) :- Z is X+Y. minus([X,Y,Z]) :- Z is X-Y.