home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / ANSWERS.ARC / ANS_121B.PRO < prev    next >
Encoding:
Prolog Source  |  1988-06-21  |  1.6 KB  |  78 lines

  1. /*
  2.    Turbo Prolog 2.0, Answer to second Exercise on page 121.
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5. */
  6.  
  7. Domains
  8.   name = string
  9.   league = integer
  10.  
  11. Predicates
  12.   run
  13.   matches( name, league )
  14.   challenges( name, league, league )
  15.   show( name, league, name, league )
  16.   player( name, league )
  17.  
  18. Clauses
  19.  
  20. /* 
  21.  * List the matches for each player in the Club. 
  22.  */
  23.  
  24.   run :-
  25.     player( P, L ) ,
  26.     matches( P, L ) ,
  27.     fail ; true.
  28.  
  29. /* 
  30.  * Each player can challenge a player in 
  31.  *   the same league or in the league above,
  32.  *   league 1 being the highest league.
  33.  */
  34.  
  35.   matches( P, L ) :-
  36.     challenges( P, L, L ) ,
  37.     L1 = L-1 ,
  38.     challenges( P, L, L1 ).
  39.  
  40. /* 
  41.  * Find all challenge pairs. 
  42.  */
  43.  
  44.   challenges( P1, L1, L2 ) :-  
  45.     player( P2, L2 ) ,
  46.     show( P1, L1, P2, L2 ) ,    % show() succeeds after all matches are found
  47.     !.                 % Cut when show() succeeds.
  48.  
  49. /* 
  50.  * Write out the possible challenges, until 
  51.  *   the two players to write are the same.
  52.  */
  53.  
  54.   show( P, _, P, _ ) :- !.    % show() succeeds when players are same
  55.   show( P1, L, P2, L ) :-
  56.     writef("% (%) can challenge (or can be challenged by) % (%).\n", 
  57.            P1, L, P2, L ) ,
  58.     !, fail.
  59.   show( P1, L1, P2, L2 ) :-
  60.     writef("% (%) can challenge % (%).\n", P1, L1, P2, L2 ) ,
  61.     fail.
  62.  
  63. /* 
  64.  * List of players and their leagues. 
  65.  */
  66.  
  67.   player( tom, 2 ).
  68.   player( liz, 1 ).
  69.   player( ann, 3 ).
  70.   player( sam, 1 ).
  71.   player( jim, 2 ).
  72.   player( ron, 1 ).
  73.   player( pat, 3 ).
  74.  
  75. GOAL
  76.   makewindow(1,2,3," Squash Club ",0,0,25,60) ,
  77.   run.
  78.