home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / findnext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1977-12-27  |  4.1 KB  |  199 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], ma[19][19];
  51. extern int currentStone, MAXX, MAXY;
  52. extern int lib;
  53. extern countlib(int,int,int);
  54.  
  55. int fval(int newlib, int minlib)
  56.      /* evaluate new move */
  57. {
  58.   int k, val;
  59.   
  60.   if (newlib <= minlib)
  61.     val = -1;
  62.   else
  63.     {
  64.       k = newlib - minlib;
  65.       val = 40 + (k - 1) * 50 / (minlib * minlib * minlib);
  66.     }
  67.   return val;
  68. }  /* end fval */
  69.  
  70.  
  71. int findnextmove(int m, int n, int *i, int *j, int *val, int minlib)
  72.      /* find new move i, j from group containing m, n */
  73. {
  74.   int ti, tj, tval;
  75.   int found = 0;
  76.   
  77.   *i = -1;   *j = -1;    *val = -1;
  78.   /* mark current position */
  79.   ma[m][n] = 1;
  80.   
  81.   /* check North neighbor */
  82.   if (m != 0)
  83.     if (p[m - 1][n] == EMPTY)
  84.       {
  85.     ti = m - 1;
  86.     tj = n;
  87.     lib = 0;
  88.     countlib(ti, tj, currentStone);
  89.     tval = fval(lib, minlib);
  90.     found = 1;
  91.       }
  92.     else
  93.       if ((p[m - 1][n] == currentStone) && !ma[m - 1][n])
  94.     if (findnextmove(m - 1, n, &ti, &tj, &tval, minlib))
  95.       found = 1;
  96.   
  97.   if (found)
  98.     {
  99.       found = 0;
  100.       if (tval > *val)
  101.     {
  102.       *val = tval;
  103.       *i = ti;
  104.       *j = tj;
  105.     }
  106.       if (minlib == 1) return 1;
  107.     }
  108.   
  109.   /* check South neighbor */
  110.   if (m != MAXY - 1)
  111.     if (p[m + 1][n] == EMPTY)
  112.       {
  113.     ti = m + 1;
  114.     tj = n;
  115.     lib = 0;
  116.     countlib(ti, tj, currentStone);
  117.     tval = fval(lib, minlib);
  118.     found = 1;
  119.       }
  120.     else
  121.       if ((p[m + 1][n] == currentStone) && !ma[m + 1][n])
  122.     if (findnextmove(m + 1, n, &ti, &tj, &tval, minlib))
  123.       found = 1;
  124.   
  125.   if (found)
  126.     {
  127.       found = 0;
  128.       if (tval > *val)
  129.     {
  130.       *val = tval;
  131.       *i = ti;
  132.       *j = tj;
  133.     }
  134.       if (minlib == 1) return 1;
  135.     }
  136.   
  137.   /* check West neighbor */
  138.   if (n != 0)
  139.     if (p[m][n - 1] == EMPTY)
  140.       {
  141.     ti = m;
  142.     tj = n - 1;
  143.     lib = 0;
  144.     countlib(ti, tj, currentStone);
  145.     tval = fval(lib, minlib);
  146.     found = 1;
  147.       }
  148.     else
  149.       if ((p[m][n - 1] == currentStone) && !ma[m][n - 1])
  150.     if (findnextmove(m, n - 1, &ti, &tj, &tval, minlib))
  151.       found = 1;
  152.   
  153.   if (found)
  154.     {
  155.       found = 0;
  156.       if (tval > *val)
  157.     {
  158.       *val = tval;
  159.       *i = ti;
  160.       *j = tj;
  161.     }
  162.       if (minlib == 1) return 1;
  163.     }
  164.   
  165.   /* check East neighbor */
  166.   if (n != MAXX - 1)
  167.     if (p[m][n + 1] == EMPTY)
  168.       {
  169.     ti = m;
  170.     tj = n + 1;
  171.     lib = 0;
  172.     countlib(ti, tj, currentStone);
  173.     tval = fval(lib, minlib);
  174.     found = 1;
  175.       }
  176.     else
  177.       if ((p[m][n + 1] == currentStone) && !ma[m][n + 1])
  178.     if (findnextmove(m, n + 1, &ti, &tj, &tval, minlib))
  179.       found = 1;
  180.   
  181.   if (found)
  182.     {
  183.       found = 0;
  184.       if (tval > *val)
  185.     {
  186.       *val = tval;
  187.       *i = ti;
  188.       *j = tj;
  189.     }
  190.       if (minlib == 1) return 1;
  191.     }
  192.   
  193.   if (*val > 0)    /* found next move */
  194.     return 1;
  195.   else    /* next move failed */
  196.     return 0;
  197. }  /* end findnextmove */
  198.  
  199.