home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / findpatn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1977-12-27  |  4.6 KB  |  203 lines

  1. /*
  2.   GNU GO - the game of Go (Wei-Chi)
  3.   Version 1.1   last revised 3-1-89
  4.   Copyright (C) Free Software Foundation, Inc.
  5.   written by Man L. Li
  6.   modified by Wayne Iba
  7.   documented by Bob Webber
  8.   NeXT version by John Neil
  9.   */
  10. /*
  11.   This program is free software; you can redistribute it and/or modify
  12.   it under the terms of the GNU General Public License as published by
  13.   the Free Software Foundation - version 1.
  14.   
  15.   This program is distributed in the hope that it will be useful,
  16.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.   GNU General Public License in file COPYING for more details.
  19.   
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.   
  24.   Please report any bug/fix, modification, suggestion to
  25.   
  26.   mail address:   Man L. Li
  27.   Dept. of Computer Science
  28.   University of Houston
  29.   4800 Calhoun Road
  30.   Houston, TX 77004
  31.   
  32.   e-mail address: manli@cs.uh.edu         (Internet)
  33.   coscgbn@uhvax1.bitnet   (BITNET)
  34.   70070,404               (CompuServe)
  35.  
  36. For the NeXT version, please report any bug/fix, modification, suggestion to
  37.  
  38. mail address:   John Neil
  39.                 Mathematics Department
  40.                 Portland State University
  41.                 PO Box 751
  42.                 Portland, OR  97207
  43.  
  44. e-mail address: neil@math.mth.pdx.edu  (Internet)
  45.                 neil@psuorvm.bitnet    (BITNET)
  46.   */
  47.  
  48. #define EMPTY 0
  49.  
  50. extern unsigned char p[19][19];
  51. extern int opn[9];
  52. extern int MAXX, MAXY, currentStone;
  53. extern int opening(int*,int*,int*,int);
  54. extern int openregion(int,int,int,int);
  55. extern int matchpat(int,int,int*,int*,int*);
  56.  
  57. int findpatn(int *i, int *j, int *val)
  58.      /* find pattern to match for next move */
  59. {
  60.   int m, n;
  61.   int ti, tj, tval;
  62.   static int cnd, mtype;  /* game tree node number, move type */
  63.   /* mtype = 0, basic; 1, inverted; 2, reflected; 3, inverted & reflected */
  64.   
  65.   /* open game then occupy corners */
  66.   if (opn[4])   /* continue last move */
  67.     {
  68.       opn[4] = 0;  /* clear flag */
  69.       if (opening(i, j, &cnd, mtype)) opn[4] = 1; /* more move then reset flag */
  70.       if (p[*i][*j] == EMPTY)  /* valid move */
  71.     {
  72.       *val = 80;
  73.       return 1;
  74.     }
  75.       else
  76.     opn[4] = 0;
  77.     }
  78.   
  79.   if (opn[0])   /* Northwest corner */
  80.     {
  81.       opn[0] = 0;  /* clear flag */
  82.       if (openregion(0, 0, 5, 5))
  83.     {
  84.       cnd = 0;
  85.       mtype = 0;
  86.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  87.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  88.       *val = 80;
  89.       return 1;
  90.     }
  91.     }
  92.   
  93.   if (opn[1])   /* Southwest corner */
  94.     {
  95.       opn[1] = 0;
  96.       if (openregion(MAXX - 6, 0, MAXX - 1, 5))
  97.     {
  98.       cnd = 0;
  99.       mtype = 1;
  100.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  101.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  102.       *val = 80;
  103.       return 1;
  104.     }
  105.     }
  106.   
  107.   if (opn[2])   /* Northeast corner */
  108.     {
  109.       opn[2] = 0;
  110.       if (openregion(0, MAXY - 6, 5, MAXY - 1))
  111.     {
  112.       cnd = 0;
  113.       mtype = 2;
  114.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  115.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  116.       *val = 80;
  117.       return 1;
  118.     }
  119.     }
  120.   
  121.   if (opn[3])   /* Northeast corner */
  122.     {
  123.       opn[3] = 0;
  124.       if (openregion(MAXX - 6, MAXY - 6, MAXX - 1, MAXY - 1))
  125.     {
  126.       cnd = 0;
  127.       mtype = 3;
  128.       opening(i, j, &cnd, mtype);  /* get new node for next move */
  129.       if (opening(i, j, &cnd, mtype)) opn[4] = 1;
  130.       *val = 80;
  131.       return 1;
  132.     }
  133.     }
  134.   
  135.   /* occupy edges */
  136.   if (opn[5])   /* North edge */
  137.     {
  138.       opn[5] = 0;
  139.       if (openregion(0, (MAXY/2) - 3, 4, (MAXY/2) + 2))
  140.     {
  141.       *i = 3;
  142.       *j = MAXY/2;
  143.       *val = 80;
  144.       return 1;
  145.     }
  146.     }
  147.   
  148.   if (opn[6])   /* South edge */
  149.     {
  150.       opn[6] = 0;
  151.       if (openregion(MAXX - 1, (MAXY/2) - 3, MAXX - 5, (MAXY/2) + 2))
  152.     {
  153.       *i = MAXX - 4;
  154.       *j = MAXY/2;
  155.       *val = 80;
  156.       return 1;
  157.     }
  158.     }
  159.   
  160.   if (opn[7])   /* West edge */
  161.     {
  162.       opn[7] = 0;
  163.       if (openregion((MAXX/2) - 3, 0, (MAXX/2) + 2, 4))
  164.     {
  165.       *i = MAXX/2;
  166.       *j = 3;
  167.       *val = 80;
  168.       return 1;
  169.     }
  170.     }
  171.   
  172.   if (opn[8])   /* East edge */
  173.     {
  174.       opn[8] = 0;
  175.       if (openregion((MAXX/2) - 3, MAXY - 1, (MAXX/2) + 2, MAXY - 5))
  176.     {
  177.       *i = MAXX/2;
  178.       *j = MAXY - 4;
  179.       *val = 80;
  180.       return 1;
  181.     }
  182.     }
  183.   
  184.   *i = -1;
  185.   *j = -1;
  186.   *val = -1;
  187.   
  188.   /* find local pattern */
  189.   for (m = 0; m < MAXX; m++)
  190.     for (n = 0; n < MAXY; n++)
  191.       if ((p[m][n] == currentStone) &&
  192.       (matchpat(m, n, &ti, &tj, &tval) && (tval > *val)))
  193.     {
  194.       *val = tval;
  195.       *i = ti;
  196.       *j = tj;
  197.     }
  198.   if (*val > 0)  /* pattern found */
  199.     return 1;
  200.   else  /* no match found */
  201.     return 0;
  202. }  /* end findpatn */
  203.