Advertisement
Guest User

Untitled

a guest
May 27th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. % w9_1.pl
  2. % Farmer - wolf - goat - cabbage
  3. program:-
  4. nl,write("All stages of the passage:"),nl,nl,
  5. write("Alek | Bolek | Czarek | Darek | Bagaz | Lodka"),nl,
  6. write("--------------------------------------------------"),
  7. write("--------------------------------------------------"),
  8. passage(position(east,east,east,east,east,boat_left_side),position(west,west,west,west,west,_)),
  9. nl,write("End of all solutions."),nl.
  10.  
  11. passage(SI,SF):-
  12. nl,
  13. write("position(east, east, east, east, east, boat_left_side)"),
  14. crossing_allowed(SI,SF,[SI],Stages,boat_left_side),nl,nl,
  15. %write(Stages),nl,nl,
  16. %write_crossing_allowed(Stages),
  17. %write(" west | west | west | west | west | west "),
  18. write(" All were safely brought across the river :)"),nl.
  19. passage(_,_).
  20.  
  21.  
  22.  
  23. crossing_allowed(SF,SF,Stages,Stages,_):- !.
  24. crossing_allowed(position(A,B,C,D,L,Boat),SF,Acc,Stages, Location_before):-
  25. crossing(position(A,B,C,D,L,Boat),position(A1,B1,C1,D1,L1,Boat1),Location_before),
  26. not(member(position(A1,B1,C1,D1,L1,Boat1),Acc)),nl,
  27. write(position(A1,B1,C1,D1,L1,Boat1)),
  28. change_boat_location(Location_before,Location_after),
  29. crossing_allowed(position(A1,B1,C1,D1,L1,Boat1),SF,[position(A1,B1,C1,D1,L1,Boat1)|Acc],Stages, Location_after).
  30.  
  31.  
  32. % all possible crossings
  33. % z lewej na prawo samotnie
  34.  
  35. crossing(position(X,B,C,D,L,_),position(Y,B,C,D,L,boat_right_side),boat_left_side):-
  36. opposite_shores(X,Y). % the Alek is crossing alone
  37.  
  38.  
  39. crossing(position(A,X,C,D,L,_),position(A,Y,C,D,L,boat_right_side),boat_left_side):-
  40. opposite_shores(X,Y). % the Bolek is crossing alone
  41.  
  42.  
  43. crossing(position(A,B,X,D,L,_),position(A,B,Y,D,L,boat_right_side),boat_left_side):-
  44. opposite_shores(X,Y). % the Czarek is crossing alone
  45.  
  46.  
  47. crossing(position(A,B,C,X,L,_),position(A,B,C,Y,L,boat_right_side),boat_left_side):-
  48. opposite_shores(X,Y). % the Darek is crossing alone
  49.  
  50. % z prawej na lewo samotnie
  51.  
  52. crossing(position(X,B,C,D,L,_),position(Y,B,C,D,L,boat_left_side),boat_right_side):-
  53. opposite_shores(X,Y). % the Alek is crossing alone
  54.  
  55.  
  56. crossing(position(A,X,C,D,L,_),position(A,Y,C,D,L,boat_left_side),boat_right_side):-
  57. opposite_shores(X,Y). % the Bolek is crossing alone
  58.  
  59.  
  60. crossing(position(A,B,X,D,L,_),position(A,B,Y,D,L,boat_left_side),boat_right_side):-
  61. opposite_shores(X,Y). % the Czarek is crossing alone
  62.  
  63.  
  64. crossing(position(A,B,C,X,L,_),position(A,B,C,Y,L,boat_left_side),boat_right_side):-
  65. opposite_shores(X,Y). % the Darek is crossing alone
  66.  
  67.  
  68. % z lewej na prawo niesamotnie
  69.  
  70. crossing(position(A,X,C,D,X,_),position(A,Y,C,D,Y,boat_right_side),boat_left_side):-
  71. opposite_shores(X,Y). % the Alek is crossing alone
  72.  
  73. crossing(position(A,B,X,D,X,_),position(A,B,Y,D,Y,boat_right_side),boat_left_side):-
  74. opposite_shores(X,Y). % the Alek is crossing alone
  75.  
  76. crossing(position(A,B,C,X,X,_),position(A,B,C,Y,Y,boat_right_side),boat_left_side):-
  77. opposite_shores(X,Y). % the Alek is crossing alone
  78.  
  79. crossing(position(A,B,X,X,L,_),position(A,B,Y,Y,L,boat_right_side),boat_left_side):-
  80. opposite_shores(X,Y). % the Alek is crossing alone
  81.  
  82. % z prawej na lewo niesamotnie
  83.  
  84. crossing(position(A,X,C,D,X,_),position(A,Y,C,D,Y,boat_left_side),boat_right_side):-
  85. opposite_shores(X,Y). % the Alek is crossing alone
  86.  
  87. crossing(position(A,B,X,D,X,_),position(A,B,Y,D,Y,boat_left_side),boat_right_side):-
  88. opposite_shores(X,Y). % the Alek is crossing alone
  89.  
  90. crossing(position(A,B,C,X,X,_),position(A,B,C,Y,Y,boat_left_side),boat_right_side):-
  91. opposite_shores(X,Y). % the Alek is crossing alone
  92.  
  93. crossing(position(A,B,X,X,L,_),position(A,B,Y,Y,L,boat_left_side),boat_right_side):-
  94. opposite_shores(X,Y). % the Alek is crossing alone
  95.  
  96. %WSZYSTKIE MOZLIWOSC WYLICZONE
  97.  
  98.  
  99.  
  100. /*
  101. crossing(position(X,X,G,C),position(Y,Y,G,C)):-
  102. opposite_shores(X,Y). % the farmer is crossing with the wolf
  103.  
  104. crossing(position(X,W,X,C),position(Y,W,Y,C)):-
  105. opposite_shores(X,Y). % the farmer is crossing with the goat
  106.  
  107. crossing(position(X,W,G,X),position(Y,W,G,Y)):-
  108. opposite_shores(X,Y). % the farmer is crossing with the cabbage*/
  109.  
  110. /*
  111. dangerous(position(Y,X,X,_)):-
  112. opposite_shores(Y,X). % the wolf can eat the goat
  113. dangerous( position(Y,_,X,X) ):-
  114. opposite_shores(Y,X). % the goat can eat the cabbage*/
  115.  
  116. opposite_shores(east,west).
  117. opposite_shores(west,east).
  118.  
  119. change_boat_location(boat_right_side,boat_left_side).
  120. change_boat_location(boat_left_side,boat_right_side).
  121.  
  122.  
  123.  
  124. %writing the consecutive stages
  125. write_crossing_allowed([_]):- !.
  126. write_crossing_allowed([H1,H2|T]):-
  127. write_crossing_allowed([H2|T]),
  128. write_crossing(H2,H1).
  129.  
  130. write_crossing(position(A,B,C,D,L,Boat),position(A,B,C,D,L,Boat)):-!,
  131. write_all(A,B,C,D,L, Boat).
  132.  
  133. write_crossing(position(X,X,G,C),position(Y,Y,G,C)):-!,
  134. write_all(X,Y,X,G,C," The farmer takes the wolf from the ").
  135.  
  136. write_crossing(position(X,W,X,C),position(Y,W,Y,C)):-!,
  137. write_all(X,Y,W,X,C," The farmer takes the goat from the ").
  138.  
  139. write_crossing(position(X,W,G,X),position(Y,W,G,Y)):-!,
  140. write_all(X,Y,W,G,X," The farmer takes the cabbage from the ").
  141.  
  142. write_all(A,B,C,D,L,Boat):-
  143. 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