home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume9 / othello2 / part03 / remark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  4.3 KB  |  193 lines

  1. /*LINTLIBRARY*/
  2.  
  3. /*  @(#)remark.c 1.2 90/04/09
  4.  *
  5.  *  Remark making code used by othello program.
  6.  *
  7.  *  Copyright: Chris Miller, 1979, 1981, 1984
  8.  *
  9.  *  Included with this othello game by
  10.  *          Rich Burridge, Sun Microsystems, Australia.
  11.  *
  12.  *  Permission is given to distribute these sources, as long as the
  13.  *  introductory messages are not removed, and no monies are exchanged.
  14.  *
  15.  *  No responsibility is taken for any errors on inaccuracies inherent
  16.  *  either to the comments or the code of this program, but if reported
  17.  *  to me, then an attempt will be made to fix them.
  18.  */
  19.  
  20. #include "othello.h"
  21. #include "extern.h"
  22.  
  23.  
  24. make_remark(machine_piece, value)
  25. int machine_piece,value ;
  26. {
  27.   char mes[100] ;
  28.  
  29.   value *= machine_piece ;
  30.   if (value < -INFINITY+64)
  31.     {
  32.       last_remark = FIRST_REMARK ;     /* Always remark next time */
  33.       if (aspire == MAXASPIRE)
  34.         SPRINTF(mes,"You should win  (by at most %d)",-value-INFINITY+64) ;
  35.       else STRCPY(mes,"You should win.") ;
  36.     }
  37.   else if (value < -1000)
  38.     {
  39.       if (last_remark != -INFINITY)
  40.         {
  41.           cretin_flag = TRUE ;
  42.           last_remark = -INFINITY ;
  43.           STRCPY(mes,"Only a cretin could lose now.") ;
  44.         }
  45.     }
  46.   else if (value <= 1000) printr(value, mes) ;
  47.   else if (value < INFINITY-63)
  48.     {
  49.       if (last_remark != INFINITY)
  50.         {
  51.           STRCPY(mes,"Resign, you dolt! Resistance is futile.") ;
  52.           last_remark = INFINITY ;
  53.         }
  54.     }
  55.   else
  56.     {
  57.       last_remark = FIRST_REMARK ;
  58.       if (aspire == MAXASPIRE)
  59.         SPRINTF(mes, "I shall win  (by at least %d).", value-INFINITY+64) ;
  60.       else STRCPY(mes, "I shall win") ;
  61.     }
  62.   message(REMARK_MES, mes) ;
  63. }
  64.  
  65.  
  66. /*  Set up file pointers to remarks; the format of the remarks file is:
  67.  *
  68.  *  Remarks to the effect that machine is winning, in increasing order of
  69.  *  strength, one per line.
  70.  *     
  71.  *  A line beginning with the character "@".
  72.  *     
  73.  *  Remarks to the effect that human is winning, in increasing order of
  74.  *  strength, one per line.
  75.  */
  76.  
  77. get_remarks()
  78. {
  79.   long fp = 0l ;
  80.   int ch ;
  81.  
  82.   open_remfile(rem_file) ;
  83.   if (rem_fp == NULL)
  84.     {
  85.       message(REMARK_MES, "Remarks not available") ;
  86.       return ;
  87.     }
  88.  
  89.   while ((ch = getc(rem_fp)) != EOF)
  90.     {
  91.       if (ch == '@') break ;
  92.       good_remarks[max_good++] = fp++ ;
  93.       if (max_good == MAX_REMARKS)
  94.   {
  95.     message(REMARK_MES, "Too many remarks") ;
  96.     return ;
  97.   }
  98.       while (ch != '\n')
  99.   {
  100.     ch = getc(rem_fp) ;
  101.     fp++ ;
  102.   }
  103.     }
  104.   max_good-- ;
  105.  
  106.   fp++ ;
  107.   while (ch != '\n')
  108.     {
  109.       ch = getc(rem_fp) ;
  110.       fp++ ;
  111.     }
  112.  
  113.   while ((ch = getc(rem_fp)) != EOF)
  114.     {
  115.       bad_remarks[max_bad++] = fp++ ;
  116.       if (max_bad == MAX_REMARKS)
  117.   {
  118.     message(REMARK_MES, "Too many remarks") ;
  119.     return ;
  120.   }
  121.       while (ch != '\n')
  122.   {
  123.     ch = fgetc(rem_fp) ;
  124.     fp++ ;
  125.   }
  126.     }
  127.   max_bad-- ;
  128. }
  129.  
  130.  
  131. open_remfile(remname)     /* Open remark file if present. */
  132. char *remname ;
  133. {
  134.   char *getenv(), name[MAXLINE], *paths, *ptr ;
  135.   int i ;
  136.  
  137.   i = 0 ;
  138.   if ((rem_fp = fopen(remname, "r")) == NULL)
  139.     {
  140.       paths = getenv("PATH") ;
  141.       if ((ptr = paths) && remname[0] != '/')
  142.         for (;;)
  143.           if (*ptr == ':' || *ptr == 0)
  144.             {
  145.               if (*ptr == 0) break ;
  146.               name[i++] = '/' ;
  147.               name[i] = 0 ;
  148.               STRCAT(name, remname) ;
  149.               if ((rem_fp = fopen(name, "r")) != NULL) return ;
  150.               if (*ptr == '\0') break ;
  151.               *ptr++ ;
  152.               i = 0 ;
  153.             }
  154.           else name[i++] = *ptr++ ;
  155.     }
  156. }
  157.  
  158.  
  159. /*  Locate file-pointer and print appropriate line. The strange
  160.  *  computation is to cluster remarks towards low evaluations,
  161.  *  since the evaluation function changes more rapidly with small
  162.  *  changes in position as the evaluation gets larger
  163.  */
  164.  
  165. printr(n, string)
  166. int n ;
  167. char string[100] ;
  168. {
  169.   int ch ;
  170.   int index ;
  171.   float findex ;
  172.   int sign_n = (n < 0 ? -1 : 1) ;
  173.   char *stringp = string ;
  174.  
  175.   if (rem_fp == NULL) return ;
  176.  
  177.   findex = (float) abs(n) / 1000.0 ;
  178.   index = findex * (2-findex) * (n < 0 ? max_bad : max_good) + 0.5 ;
  179.  
  180. /* Don't make the same remark twice in a row */
  181.  
  182.   if (index * sign_n == last_remark) return ;
  183.  
  184.   last_remark = index * sign_n ;
  185.   FSEEK(rem_fp, n < 0 ? bad_remarks[index] : good_remarks[index], 0) ;
  186.   while ((ch = getc(rem_fp)) != EOF)
  187.     {
  188.       if (ch == '\n') break ;
  189.       *stringp++ = ch ;
  190.     }
  191.   *stringp = '\0' ;
  192. }
  193.