home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / findwinr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1977-12-27  |  3.0 KB  |  117 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], l[19][19];
  51. extern int currentStone, opposingStone, MAXX, MAXY;
  52. extern int lib;
  53. extern void countlib(int,int,int);
  54. extern void initmark();
  55. extern int findopen(int,int,int[],int[],int,int,int*);
  56.  
  57. int findwinner(int *i, int *j, int *val)
  58.      /* find opponent piece to capture or attack */
  59. {
  60.   int m, n, ti[3], tj[3], tval, ct, u, v, lib1;
  61.   
  62.   *i = -1;   *j = -1;   *val = -1;
  63.   
  64.   /* find opponent with liberty less than four */
  65.   for (m = 0; m < MAXX; m++)
  66.     for (n = 0; n < MAXY; n++)
  67.       if ((p[m][n] == opposingStone) && (l[m][n] < 4))
  68.     {
  69.       ct = 0;
  70.       initmark();
  71.       if (findopen(m, n, ti, tj, opposingStone, l[m][n], &ct))
  72.         {
  73.           if (l[m][n] == 1)
  74.         {
  75.           if (*val < 120)
  76.             {
  77.               *val = 120;
  78.               *i = ti[0];
  79.               *j = tj[0];
  80.             }
  81.         }
  82.           else
  83.         for (u = 0; u < l[m][n]; u++)
  84.           for (v = 0; v < l[m][n]; v++)
  85.             if (u != v)
  86.               {
  87.             lib = 0;
  88.             countlib(ti[u], tj[u], currentStone);
  89.             if (lib > 0) /* valid move */
  90.               {
  91.                 lib1 = lib;
  92.                 p[ti[u]][tj[u]] = currentStone;
  93.                 /* look ahead opponent move */
  94.                 lib = 0;
  95.                 countlib(ti[v], tj[v], opposingStone);
  96.                 if ((lib1 == 1) && (lib > 0))
  97.                   tval = 0;
  98.                 else
  99.                   tval = 120 - 20 * lib;
  100.                 if (*val < tval)
  101.                   {
  102.                 *val = tval;
  103.                 *i = ti[u];
  104.                 *j = tj[u];
  105.                   }
  106.                 p[ti[u]][tj[u]] = EMPTY;
  107.               }
  108.               }
  109.         }
  110.     }
  111.   if (*val > 0)    /* find move */
  112.     return 1;
  113.   else  /* fail to find winner */
  114.     return 0;
  115. }  /* end findwinner */
  116.  
  117.