Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % w9_1.pl
- % Farmer - wolf - goat - cabbage
- program:-
- nl,write("All stages of the passage:"),nl,nl,
- write("Alek | Bolek | Czarek | Darek | Bagaz | Lodka"),nl,
- write("--------------------------------------------------"),
- write("--------------------------------------------------"),
- passage(position(east,east,east,east,east,boat_left_side),position(west,west,west,west,west,_)),
- nl,write("End of all solutions."),nl.
- passage(SI,SF):-
- nl,
- write("position(east, east, east, east, east, boat_left_side)"),
- crossing_allowed(SI,SF,[SI],Stages,boat_left_side),nl,nl,
- %write(Stages),nl,nl,
- %write_crossing_allowed(Stages),
- %write(" west | west | west | west | west | west "),
- write(" All were safely brought across the river :)"),nl.
- passage(_,_).
- crossing_allowed(SF,SF,Stages,Stages,_):- !.
- crossing_allowed(position(A,B,C,D,L,Boat),SF,Acc,Stages, Location_before):-
- crossing(position(A,B,C,D,L,Boat),position(A1,B1,C1,D1,L1,Boat1),Location_before),
- not(member(position(A1,B1,C1,D1,L1,Boat1),Acc)),nl,
- write(position(A1,B1,C1,D1,L1,Boat1)),
- change_boat_location(Location_before,Location_after),
- crossing_allowed(position(A1,B1,C1,D1,L1,Boat1),SF,[position(A1,B1,C1,D1,L1,Boat1)|Acc],Stages, Location_after).
- % all possible crossings
- % z lewej na prawo samotnie
- crossing(position(X,B,C,D,L,_),position(Y,B,C,D,L,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,X,C,D,L,_),position(A,Y,C,D,L,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Bolek is crossing alone
- crossing(position(A,B,X,D,L,_),position(A,B,Y,D,L,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Czarek is crossing alone
- crossing(position(A,B,C,X,L,_),position(A,B,C,Y,L,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Darek is crossing alone
- % z prawej na lewo samotnie
- crossing(position(X,B,C,D,L,_),position(Y,B,C,D,L,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,X,C,D,L,_),position(A,Y,C,D,L,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Bolek is crossing alone
- crossing(position(A,B,X,D,L,_),position(A,B,Y,D,L,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Czarek is crossing alone
- crossing(position(A,B,C,X,L,_),position(A,B,C,Y,L,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Darek is crossing alone
- % z lewej na prawo niesamotnie
- crossing(position(A,X,C,D,X,_),position(A,Y,C,D,Y,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,X,D,X,_),position(A,B,Y,D,Y,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,C,X,X,_),position(A,B,C,Y,Y,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,X,X,L,_),position(A,B,Y,Y,L,boat_right_side),boat_left_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- % z prawej na lewo niesamotnie
- crossing(position(A,X,C,D,X,_),position(A,Y,C,D,Y,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,X,D,X,_),position(A,B,Y,D,Y,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,C,X,X,_),position(A,B,C,Y,Y,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- crossing(position(A,B,X,X,L,_),position(A,B,Y,Y,L,boat_left_side),boat_right_side):-
- opposite_shores(X,Y). % the Alek is crossing alone
- %WSZYSTKIE MOZLIWOSC WYLICZONE
- /*
- crossing(position(X,X,G,C),position(Y,Y,G,C)):-
- opposite_shores(X,Y). % the farmer is crossing with the wolf
- crossing(position(X,W,X,C),position(Y,W,Y,C)):-
- opposite_shores(X,Y). % the farmer is crossing with the goat
- crossing(position(X,W,G,X),position(Y,W,G,Y)):-
- opposite_shores(X,Y). % the farmer is crossing with the cabbage*/
- /*
- dangerous(position(Y,X,X,_)):-
- opposite_shores(Y,X). % the wolf can eat the goat
- dangerous( position(Y,_,X,X) ):-
- opposite_shores(Y,X). % the goat can eat the cabbage*/
- opposite_shores(east,west).
- opposite_shores(west,east).
- change_boat_location(boat_right_side,boat_left_side).
- change_boat_location(boat_left_side,boat_right_side).
- %writing the consecutive stages
- write_crossing_allowed([_]):- !.
- write_crossing_allowed([H1,H2|T]):-
- write_crossing_allowed([H2|T]),
- write_crossing(H2,H1).
- write_crossing(position(A,B,C,D,L,Boat),position(A,B,C,D,L,Boat)):-!,
- write_all(A,B,C,D,L, Boat).
- write_crossing(position(X,X,G,C),position(Y,Y,G,C)):-!,
- write_all(X,Y,X,G,C," The farmer takes the wolf from the ").
- write_crossing(position(X,W,X,C),position(Y,W,Y,C)):-!,
- write_all(X,Y,W,X,C," The farmer takes the goat from the ").
- write_crossing(position(X,W,G,X),position(Y,W,G,Y)):-!,
- write_all(X,Y,W,G,X," The farmer takes the cabbage from the ").
- write_all(A,B,C,D,L,Boat):-
- write(" "),write(A),write(" | "),write(B),write(" | "),write(C),write(" | "), write(D),write(L),write(B),nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement