home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / genmove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1977-12-27  |  3.8 KB  |  155 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 MAXTRY 400
  50. #define BLACKSTONE 2
  51.  
  52. extern unsigned char p[19][19];
  53. extern int currentStone, opposingStone, MAXX, MAXY;
  54. extern int rd, lib, blackPassed, whitePassed;
  55. extern void eval(int);
  56. extern int findwinner(int*,int*,int*);
  57. extern int findsaver(int*,int*,int*);
  58. extern int findpatn(int*,int*,int*);
  59. extern void countlib(int,int,int);
  60. extern int fioe(int,int);
  61. extern void Random(int*);
  62.  
  63. void genmove(int *i, int *j)
  64.      /* generate computer move */
  65. {
  66.   int ti, tj, tval;
  67.   int val;
  68.   int try = 0;   /* number of try */
  69.   
  70.   /* initialize move and value */
  71.   *i = -1;  *j = -1;  val = -1;
  72.   
  73.   /* re-evaluate liberty of opponent pieces */
  74.   eval(opposingStone);
  75.   
  76.   /* find opponent piece to capture or attack */
  77.   if (findwinner(&ti, &tj, &tval))
  78.     if (tval > val)
  79.       {
  80.     val = tval;
  81.     *i = ti;
  82.     *j = tj;
  83.       }
  84.   
  85.   /* save any piece if threaten */
  86.   if (findsaver(&ti, &tj, &tval))
  87.     if (tval > val)
  88.       {
  89.     val = tval;
  90.     *i = ti;
  91.     *j = tj;
  92.       }
  93.   
  94.   /* try match local play pattern for new move */
  95.   if (findpatn(&ti, &tj, &tval))
  96.     if (tval > val)
  97.       {
  98.     val = tval;
  99.     *i = ti;
  100.     *j = tj;
  101.       }
  102.   
  103.   /* no urgent move then do random move */
  104.   if (val < 0)
  105.     do {
  106.       Random(&rd);
  107.       *i = rd % MAXX;
  108.       /* avoid low line  and center region */
  109.       if ((*i < 2) || (*i > MAXX - 3) || ((*i > (MAXX/2) - 2) && (*i < (MAXX/2) + 2)))
  110.     {
  111.       Random(&rd);
  112.       *i = rd % MAXX;
  113.       if ((*i < 2) || (*i > MAXX - 3))
  114.         {
  115.           Random(&rd);
  116.           *i = rd % MAXX;
  117.         }
  118.     }
  119.       Random(&rd);
  120.       *j = rd % MAXY;
  121.       /* avoid low line and center region */
  122.       if ((*j < 2) || (*j > MAXY - 3) || ((*j > (MAXX/2) - 2) && (*j < (MAXY/2) + 2)))
  123.     {
  124.       Random(&rd);
  125.       *j = rd % MAXY;
  126.       if ((*j < 2) || (*j > MAXY - 3))
  127.         {
  128.           Random(&rd);
  129.           *j = rd % MAXY;
  130.         }
  131.     }
  132.       lib = 0;
  133.       countlib(*i, *j, currentStone);
  134.     }
  135.   /* avoid illegal move, liberty one or suicide, fill in own eye */
  136.   while ((++try < MAXTRY)
  137.      && ((p[*i][*j] != EMPTY) || (lib < 3) || fioe(*i, *j)));
  138.   
  139.   if (try >= MAXTRY)  /* computer pass */
  140.     {
  141.       if (currentStone == BLACKSTONE)
  142.     blackPassed = 1;
  143.       else
  144.     whitePassed = 1;
  145.       *i = -1;
  146.     }
  147.   else   /* find valid move */
  148.     {
  149.       if (currentStone == BLACKSTONE)
  150.     blackPassed = 0;
  151.       else
  152.     whitePassed = 0;
  153.     }
  154. }  /* end genmove */
  155.