home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / findwinr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-02  |  2.0 KB  |  85 lines  |  [TEXT/R*ch]

  1. #include "comment.header"
  2.  
  3. #define EMPTY 0
  4.  
  5. extern unsigned char p[19][19], l[19][19];
  6. extern int currentStone, opposingStone, MAXX, MAXY;
  7. extern int lib;
  8. extern void countlib(int,int,int);
  9. extern void initmark();
  10. extern int findopen(int,int,int[],int[],int,int,int*);
  11. extern int getcsize(int,int);
  12. extern int throwin(int,int);
  13. extern unsigned char capturedmat[19][19];
  14. extern int atari;
  15.  
  16. int findwinner(int *i, int *j, int *val)
  17.      /* find opponent piece to capture or attack */
  18. {
  19.   int m, n, ti[7], tj[7], tval, ct, u, v, lib1;
  20.   int csize;
  21.   
  22.   *i = -1;   *j = -1;   *val = -1;
  23.  
  24.   /* find opponent with liberty less than four */
  25.   for (m = 0; m < MAXX; m++)
  26.     for (n = 0; n < MAXY; n++)
  27.       if ( (p[m][n] == opposingStone) &&
  28.        ((l[m][n] < 4) || (l[m][n] < 8 && l[m][n] < csize - 2)) )
  29.     {
  30.       csize = getcsize(m,n);
  31.       ct = 0;
  32.       initmark();
  33.       if (findopen(m, n, ti, tj, opposingStone, l[m][n], &ct))
  34.         {
  35.           if (l[m][n] == 1)
  36.         {
  37.           tval = 120 + (20 * csize);
  38.           if (*val < tval)
  39.             {
  40.               *val = tval;
  41.               *i = ti[0];
  42.               *j = tj[0];
  43.             }
  44.         }
  45.           else
  46.         for (u = 0; u < l[m][n]; u++)
  47.           for (v = 0; v < l[m][n]; v++)
  48.             if (u != v)
  49.               {
  50.             lib = 0;
  51.             countlib(ti[u], tj[u], currentStone);
  52.             if (l[m][n] >= 4 && lib < 5) lib = 0;
  53.             if (lib > 0) /* valid move */
  54.               {
  55.                 lib1 = lib;
  56.                 p[ti[u]][tj[u]] = currentStone;
  57.                 /* look ahead opponent move */
  58.                 lib = 0;
  59.                 countlib(ti[v], tj[v], opposingStone);
  60.                 if ((lib1 == 1) && (lib > 0))
  61.                   tval = 0;
  62.                 else
  63.                   tval = 110 - (20 * lib) + (10 * csize);
  64.                 if (throwin(m,n)) tval -= 40;
  65.                 if (tval <= 80 &&
  66.                 capturedmat[ti[u]][tj[u]] == currentStone)
  67.                   tval = -1;
  68.                 if (*val < tval)
  69.                   {
  70.                 *val = tval;
  71.                 *i = ti[u];
  72.                 *j = tj[u];
  73.                   }
  74.                 p[ti[u]][tj[u]] = EMPTY;
  75.               }
  76.               }
  77.         }
  78.     }
  79.   if (*val > 0)    /* find move */
  80.     return 1;
  81.   else  /* fail to find winner */
  82.     return 0;
  83. }  /* end findwinner */
  84.  
  85.