home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / findopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1977-12-27  |  3.5 KB  |  139 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. #define BLACKSTONE 2
  50.  
  51. extern unsigned char p[19][19], ma[19][19];
  52. extern int MAXX, MAXY;
  53. extern int currentStone, blackCapturedKoI, blackCapturedKoJ;  /* piece captured */
  54. extern int whiteCapturedKoI, whiteCapturedKoJ;
  55.  
  56. int findopen(int m, int n, int i[], int j[], int color, int minlib, int *ct)
  57.      /* find all open spaces i, j from m, n */
  58. {
  59.   int mik, mjk;
  60.   
  61.   if (currentStone == BLACKSTONE) {
  62.     mik = blackCapturedKoI;
  63.     mjk = blackCapturedKoJ;
  64.   } else {
  65.     mik = whiteCapturedKoI;
  66.     mjk = whiteCapturedKoJ;
  67.   }
  68.   
  69.   /* mark this one */
  70.   ma[m][n] = 1;
  71.   
  72.   /* check North neighbor */
  73.   if (m != 0)
  74.     {
  75.       if ((p[m - 1][n] == EMPTY) && (((m - 1) != mik) || (n != mjk)))
  76.     {
  77.       i[*ct] = m - 1;
  78.       j[*ct] = n;
  79.       ++*ct;
  80.       if (*ct == minlib) return 1;
  81.     }
  82.       else
  83.     if ((p[m - 1][n] == color) && !ma[m - 1][n])
  84.       if (findopen(m - 1, n, i, j, color, minlib, ct) && (*ct == minlib))
  85.         return 1;
  86.     }
  87.   
  88.   /* check South neighbor */
  89.   if (m != MAXY - 1)
  90.     {
  91.       if ((p[m + 1][n] == EMPTY) && (((m + 1) != mik) || (n != mjk)))
  92.     {
  93.       i[*ct] = m + 1;
  94.       j[*ct] = n;
  95.       ++*ct;
  96.       if (*ct == minlib) return 1;
  97.     }
  98.       else
  99.     if ((p[m + 1][n] == color) && !ma[m + 1][n])
  100.       if (findopen(m + 1, n, i, j, color, minlib, ct) && (*ct == minlib))
  101.         return 1;
  102.     }
  103.   
  104.   /* check West neighbor */
  105.   if (n != 0)
  106.     {
  107.       if ((p[m][n - 1] == EMPTY) && ((m != mik) || ((n - 1) != mjk)))
  108.     {
  109.       i[*ct] = m;
  110.       j[*ct] = n - 1;
  111.       ++*ct;
  112.       if (*ct == minlib) return 1;
  113.     }
  114.       else
  115.     if ((p[m][n - 1] == color) && !ma[m][n - 1])
  116.       if (findopen(m, n - 1, i, j, color, minlib, ct) && (*ct == minlib))
  117.         return 1;
  118.     }
  119.   
  120.   /* check East neighbor */
  121.   if (n != MAXX - 1)
  122.     {
  123.       if ((p[m][n + 1] == EMPTY) && ((m != mik) || ((n + 1) != mjk)))
  124.     {
  125.       i[*ct] = m;
  126.       j[*ct] = n + 1;
  127.       ++*ct;
  128.       if (*ct == minlib) return 1;
  129.     }
  130.       else
  131.     if ((p[m][n + 1] == color) && !ma[m][n + 1])
  132.       if (findopen(m, n + 1, i, j, color, minlib, ct) && (*ct == minlib))
  133.         return 1;
  134.     }
  135.   
  136.   /* fail to find open space */
  137.   return 0;
  138. }  /* end findopen */
  139.