home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Games / NeXTGo-3.0-MIS / findpatn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-06  |  3.3 KB  |  171 lines

  1. #include "comment.header"
  2.  
  3. /* $Id: findpatn.c,v 1.3 1997/07/06 19:34:57 ergo Exp $ */
  4.  
  5. /*
  6.  * $Log: findpatn.c,v $
  7.  * Revision 1.3  1997/07/06 19:34:57  ergo
  8.  * actual version
  9.  *
  10.  * Revision 1.2  1997/05/04 18:57:02  ergo
  11.  * added time control for moves
  12.  *
  13.  */
  14.  
  15. #define EMPTY 0
  16.  
  17. extern unsigned char p[19][19];
  18. extern int opn[9];
  19. extern int MAXX, MAXY, currentStone;
  20. extern int opening(int*,int*,int*,int);
  21. extern int openregion(int,int,int,int);
  22. extern int matchpat(int,int,int*,int*,int*);
  23.  
  24. int findpatn(int *i, int *j, int *val)
  25.      /* find pattern to match for next move */
  26. {
  27.   int m, n;
  28.   int ti, tj, tval;
  29.   static int cnd, mtype;  /* game tree node number, move type */
  30.   /* mtype = 0, basic; 1, inverted; 2, reflected; 3, inverted & reflected */
  31.   
  32.   /* open game then occupy corners */
  33.   if (opn[4])   /* continue last move */
  34.     {
  35.       opn[4] = 0;  /* clear flag */
  36.       if (opening(i, j, &cnd, mtype)) opn[4] = 1; /* more move then reset flag */
  37.       if (p[*i][*j] == EMPTY)  /* valid move */
  38.     {
  39.       *val = 80;
  40.       return 1;
  41.     }
  42.       else
  43.     opn[4] = 0;
  44.     }
  45.   
  46.   if (opn[0])   /* Northwest corner */
  47.     {
  48.       opn[0] = 0;  /* clear flag */
  49.       if (openregion(0, 0, 5, 5))
  50.     {
  51.       cnd = 0;
  52.       mtype = 0;
  53.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  54.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  55.       *val = 80;
  56.       return 1;
  57.     }
  58.     }
  59.   
  60.   if (opn[1])   /* Southwest corner */
  61.     {
  62.       opn[1] = 0;
  63.       if (openregion(MAXX - 6, 0, MAXX - 1, 5))
  64.     {
  65.       cnd = 0;
  66.       mtype = 1;
  67.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  68.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  69.       *val = 80;
  70.       return 1;
  71.     }
  72.     }
  73.   
  74.   if (opn[2])   /* Northeast corner */
  75.     {
  76.       opn[2] = 0;
  77.       if (openregion(0, MAXY - 6, 5, MAXY - 1))
  78.     {
  79.       cnd = 0;
  80.       mtype = 2;
  81.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  82.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  83.       *val = 80;
  84.       return 1;
  85.     }
  86.     }
  87.   
  88.   if (opn[3])   /* Northeast corner */
  89.     {
  90.       opn[3] = 0;
  91.       if (openregion(MAXX - 6, MAXY - 6, MAXX - 1, MAXY - 1))
  92.     {
  93.       cnd = 0;
  94.       mtype = 3;
  95.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  96.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  97.       *val = 80;
  98.       return 1;
  99.     }
  100.     }
  101.   
  102.   /* occupy edges */
  103.   if (opn[5])   /* North edge */
  104.     {
  105.       opn[5] = 0;
  106.       if (openregion(0, (MAXY/2) - 3, 4, (MAXY/2) + 2))
  107.     {
  108.       *i = 3;
  109.       *j = MAXY/2;
  110.       *val = 80;
  111.       return 1;
  112.     }
  113.     }
  114.   
  115.   if (opn[6])   /* South edge */
  116.     {
  117.       opn[6] = 0;
  118.       if (openregion(MAXX - 1, (MAXY/2) - 3, MAXX - 5, (MAXY/2) + 2))
  119.     {
  120.       *i = MAXX - 4;
  121.       *j = MAXY/2;
  122.       *val = 80;
  123.       return 1;
  124.     }
  125.     }
  126.   
  127.   if (opn[7])   /* West edge */
  128.     {
  129.       opn[7] = 0;
  130.       if (openregion((MAXX/2) - 3, 0, (MAXX/2) + 2, 4))
  131.     {
  132.       *i = MAXX/2;
  133.       *j = 3;
  134.       *val = 80;
  135.       return 1;
  136.     }
  137.     }
  138.   
  139.   if (opn[8])   /* East edge */
  140.     {
  141.       opn[8] = 0;
  142.       if (openregion((MAXX/2) - 3, MAXY - 1, (MAXX/2) + 2, MAXY - 5))
  143.     {
  144.       *i = MAXX/2;
  145.       *j = MAXY - 4;
  146.       *val = 80;
  147.       return 1;
  148.     }
  149.     }
  150.   
  151.   *i = -1;
  152.   *j = -1;
  153.   *val = -1;
  154.   
  155.   /* find local pattern */
  156.   for (m = 0; m < MAXX; m++)
  157.     for (n = 0; n < MAXY; n++)
  158.       if ((p[m][n] == currentStone) &&
  159.       (matchpat(m, n, &ti, &tj, &tval) && (tval > *val)))
  160.     {
  161.       *val = tval;
  162.       *i = ti;
  163.       *j = tj;
  164.     }
  165.   if (*val > 0)  /* pattern found */
  166.     return 1;
  167.   else  /* no match found */
  168.     return 0;
  169. }  /* end findpatn */
  170.  
  171.