home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / PROGRAMS / FWGC.PRO < prev    next >
Encoding:
Prolog Source  |  1990-03-26  |  4.4 KB  |  128 lines

  1. /*
  2. PROBLEM:
  3. A farmer with his goat, wolf and cabbage come to a river that they
  4. wish to cross. There is a boat, but it only has room for two, and
  5. the farmer is the only one that can row. If the goat and cabbage
  6. get in the boat at the same time, the cabbage gets eaten.
  7. Similarly, if the wolf and goat are together without the farmer,
  8. the goat is eaten. Devise a series of crossings of the river so that all
  9. concerned make it across safely.
  10.  
  11. The state of the system is indicated by stating where the farmer, the goat
  12. the wolf and the cabbage are located.
  13.    state( Farmer, Wolf, Goat, Cabbage )
  14.  
  15. The problem is that a state must only be visited once, and some states 
  16. are illegal. This is checked by 'unsafe' and 'member'.
  17.  
  18. The Predicate "go" can be called with a start state and a final state
  19.  
  20.   go( state(east,east,east,east), state(west,west,west,west) ).
  21.  
  22. */
  23.  
  24. DOMAINS
  25.   LOC   = east ; west
  26.   STATE = state(LOC,LOC,LOC,LOC)
  27.   PATH  = STATE*
  28.   
  29.  
  30. PREDICATES
  31.   go(STATE,STATE)        /* Start of the algorithm                 */
  32.   path(STATE,STATE,PATH,PATH)    /* Finds a path from one state to another */
  33.   move(STATE,STATE)    /* Transfer a system from one side to another    */
  34.   opposite(LOC,LOC)    /* Gives a location on the opposite side    */
  35.   unsafe(STATE)        /* Gives the unsafe states            */
  36.   member(STATE,PATH)    /* Checks if the state is already visited    */
  37.   write_path(PATH)
  38.   write_move(STATE,STATE)
  39.   show_move(STATE,STATE)
  40.   showside(LOC,LOC,string)
  41.   s_river
  42.  
  43. GOAL    
  44.         makewindow(4,7,0,"",0,0,24,80),        
  45.         makewindow(3,7,7," The River   ",15,0,10,80),        
  46.         makewindow(2,112,0,"",0,0,1,80),        
  47.         write("press any key for each step of solution"),
  48.         makewindow(1,7,7," Solutions   ",2,0,13,80),
  49.   
  50.     show_move(state(west,west,west,west),state(west,west,west,west)),
  51.     go(state(east,east,east,east),state(west,west,west,west)),
  52.     write("solved press any key to continue"),
  53.     readchar(_),
  54.     exit.
  55.  
  56. CLAUSES
  57.   go(S,G):-
  58.       path(S,G,[S],L),
  59.       nl,write("A solution is:"),nl,
  60.       write_path(L),
  61.       fail.
  62.  go(_,_).
  63.  
  64.   path(S,G,L,L1):-    move(S,S1),
  65.             not( unsafe(S1) ),
  66.             not( member(S1,L) ),
  67.             path( S1,G,[S1|L],L1),!.
  68.   path(G,G,T,T):-       !.            /* The final state is reached  */
  69.                        
  70.  
  71.   move(state(X,X,G,C),state(Y,Y,G,C)):-opposite(X,Y). /* FARMER + WOLF    */
  72.   move(state(X,W,X,C),state(Y,W,Y,C)):-opposite(X,Y). /* FARMER + GOAT    */
  73.   move(state(X,W,G,X),state(Y,W,G,Y)):-opposite(X,Y). /* FARMER + CABBAGE */
  74.   move(state(X,W,G,C),state(Y,W,G,C)):-opposite(X,Y). /* ONLY FARMER      */
  75.  
  76.   opposite(east,west).
  77.   opposite(west,east):-!.
  78.  
  79.   unsafe( state(F,X,X,_) ):- opposite(F,X).  /* The wolf eats the goat    */
  80.   unsafe( state(F,_,X,X) ):- opposite(F,X).  /* The goat eats the cabbage */
  81.   
  82.   member(X,[X|_]).
  83.   member(X,[_|L]):-member(X,L).
  84.  
  85.   write_path( [H1,H2|T] ) :- !,
  86.                 readchar(_),
  87.                 write_move(H1,H2),
  88.                 show_move(H1,H2), 
  89.                 write_path([H2|T]).
  90.   write_path( [] ).
  91.   
  92.  
  93.  
  94.   write_move( state(X,W,G,C), state(Y,W,G,C) ) :-!,
  95.     write("The farmer crosses the river from ",X," to ",Y),nl.
  96.   write_move( state(X,X,G,C), state(Y,Y,G,C) ) :-!,
  97.     write("The farmer takes the Wolf from ",X," of the river to ",Y),nl.
  98.   write_move( state(X,W,X,C), state(Y,W,Y,C) ) :-!,
  99.     write("The farmer takes the Goat from ",X," of the river to ",Y),nl.
  100.   write_move( state(X,W,G,X), state(Y,W,G,Y) ) :-!,
  101.     write("The farmer takes the cabbage from ",X," of the river to ",Y),nl.
  102.  
  103. /* Show them move across the river.*/
  104.  
  105.  show_move( state(F,W,G,C), state(F1,W1,G1,C1) ) :-!,
  106.     s_river,
  107.     showside(F,F1,"Farmer "),
  108.     showside(W,W1," Wolf  "),    
  109.     showside(G,G1," Goat  "),    
  110.     showside(C,C1,"Cabbage"),
  111.     shiftwindow(1).
  112.     
  113.   showside(east,east,Item):-!,
  114.        write("                                    ~~~~~                       ",Item),nl.
  115.   showside(west,west,Item):-!,
  116.        write("      ",Item,"                       ~~~~~           "),nl.
  117.   showside(east,west,Item):-!,
  118.        write("      ",Item,"          <=  <=  <=   ",Item),nl.
  119.   showside(west,east,Item):-!,
  120.        write("                                    ",Item,"  =>  =>  =>         ",Item),nl.
  121.  
  122. s_river:-
  123.     shiftwindow(3),clearwindow,
  124.     write("        WEST                        river                      EAST"),nl,
  125.     write("  ----------------------------------------------------------------------"),
  126.     nl.
  127.  
  128.