home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / PIE.ARC / FWGC.PIE next >
Encoding:
Text File  |  1988-06-21  |  1.9 KB  |  67 lines

  1. /*
  2.    Copyright (c) 1986, 88 by Borland International, Inc
  3.    
  4.    See also the program FWGC.PRO on the directory \PROGRAMS
  5. */
  6.  
  7. DOMAINS
  8.   LOC   = east ; west
  9.   STATE = state(LOC,LOC,LOC,LOC)
  10.   PATH  = STATE*
  11.   
  12. PREDICATES
  13.   go
  14.   go(STATE,STATE)
  15.   path(STATE,STATE,PATH,PATH)
  16.   move(STATE,STATE)
  17.   opposite(LOC,LOC)
  18.   unsafe(STATE)
  19.   member(STATE,PATH)
  20.   write_path(PATH)
  21.   write_move(STATE,STATE)
  22.  
  23. CLAUSES
  24. */
  25.   go:-    go(state(east,east,east,east),state(west,west,west,west)).
  26.  
  27.   go(S,G):-
  28.       path(S,G,[S],L),
  29.       nl,write("A solution is:"),nl,
  30.       write_path(L),
  31.       fail.
  32.  go(_,_).
  33.  
  34.   path(S,G,L,L1):-
  35.     move(S,S1),
  36.     not( unsafe(S1) ),
  37.     not( member(S1,L) ),
  38.     path( S1,G,[S1|L],L1),!.
  39.   path(G,G,T,T):-!.   /* The final state is reached  */
  40.                     
  41.   move(state(X,X,G,C),state(Y,Y,G,C)):-opposite(X,Y). /* FARMER + WOLF    */
  42.   move(state(X,W,X,C),state(Y,W,Y,C)):-opposite(X,Y). /* FARMER + GOAT    */
  43.   move(state(X,W,G,X),state(Y,W,G,Y)):-opposite(X,Y). /* FARMER + CABBAGE */
  44.   move(state(X,W,G,C),state(Y,W,G,C)):-opposite(X,Y). /* ONLY FARMER      */
  45.  
  46.   opposite(east,west).
  47.   opposite(west,east).
  48.  
  49.   unsafe( state(F,X,X,_) ):- opposite(F,X).  /* The wolf eats the goat    */
  50.   unsafe( state(F,_,X,X) ):- opposite(F,X).  /* The goat eats the cabbage */
  51.   
  52.   member(X,[X|_]).
  53.   member(X,[_|L]):-member(X,L).
  54.  
  55.   write_move( state(X,W,G,C), state(Y,W,G,C) ) :-!,
  56.     write("The farmer crosses the river from ",X," to ",Y),nl.
  57.   write_move( state(X,X,G,C), state(Y,Y,G,C) ) :-!,
  58.     write("The farmer takes the Wolf from ",X," of the river to ",Y),nl.
  59.   write_move( state(X,W,X,C), state(Y,W,Y,C) ) :-!,
  60.     write("The farmer takes the Goat from ",X," of the river to ",Y),nl.
  61.   write_move( state(X,W,G,X), state(Y,W,G,Y) ) :-!,
  62.     write("The farmer takes the cabbage from ",X," of the river to ",Y),nl.
  63.  
  64.   write_path( [H1,H2|T] ) :- !,
  65.     write_move(H1,H2),write_path([H2|T]).
  66.   write_path( _ ).
  67.