home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / games / gnugo / c / genmove < prev    next >
Encoding:
Text File  |  1995-03-11  |  3.2 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. */
  9. /*
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation - version 1.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License in file COPYING for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Please report any bug/fix, modification, suggestion to
  24.  
  25. mail address:   Man L. Li
  26.                 Dept. of Computer Science
  27.                 University of Houston
  28.                 4800 Calhoun Road
  29.                 Houston, TX 77004
  30.  
  31. e-mail address: manli@cs.uh.edu         (Internet)
  32.                 coscgbn@uhvax1.bitnet   (BITNET)
  33.                 70070,404               (CompuServe)
  34. */
  35.  
  36. #include <stdio.h>
  37. #include "header.h"
  38. int rd,pass;
  39. void genmove(i, j)
  40. /* generate computer move */
  41. int *i, *j;
  42.   {
  43.    int ti, tj, tval;
  44.    char a;
  45.    int ii, /*m, n,*/ val;
  46.    int try = 0;   /* number of try */
  47.  
  48. /* initialize move and value */
  49.    *i = -1;  *j = -1;  val = -1;
  50.  
  51. /* re-evaluate liberty of opponent pieces */
  52.    eval(umove);
  53.  
  54. /* find opponent piece to capture or attack */
  55.    if (findwinner(&ti, &tj, &tval))
  56.        if (tval > val)
  57.      {
  58.       val = tval;
  59.       *i = ti;
  60.       *j = tj;
  61.     }
  62.  
  63. /* save any piece if threaten */
  64.    if (findsaver(&ti, &tj, &tval))
  65.        if (tval > val)
  66.      {
  67.       val = tval;
  68.       *i = ti;
  69.       *j = tj;
  70.     }
  71.  
  72. /* try match local play pattern for new move */
  73.    if (findpatn(&ti, &tj, &tval))
  74.        if (tval > val)
  75.      {
  76.       val = tval;
  77.       *i = ti;
  78.       *j = tj;
  79.     }
  80.  
  81. /* no urgent move then do random move */
  82.    if (val < 0)
  83.        do {
  84.        random(&rd);
  85.        *i = rd % 19;
  86. /* avoid low line  and center region */
  87.        if ((*i < 2) || (*i > 16) || ((*i > 5) && (*i < 13)))
  88.          {
  89.           random(&rd);
  90.           *i = rd % 19;
  91.           if ((*i < 2) || (*i > 16))
  92.         {
  93.          random(&rd);
  94.          *i = rd % 19;
  95.            }
  96.         }
  97.        random(&rd);
  98.        *j = rd % 19;
  99. /* avoid low line and center region */
  100.        if ((*j < 2) || (*j > 16) || ((*j > 5) && (*j < 13)))
  101.          {
  102.           random(&rd);
  103.           *j = rd % 19;
  104.           if ((*j < 2) || (*j > 16))
  105.         {
  106.          random(&rd);
  107.          *j = rd % 19;
  108.            }
  109.         }
  110.         lib = 0;
  111.         countlib(*i, *j, mymove);
  112.        }
  113. /* avoid illegal move, liberty one or suicide, fill in own eye */
  114.        while ((++try < MAXTRY)
  115.           && ((p[*i][*j] != EMPTY) || (lib < 2) || fioe(*i, *j)));
  116.  
  117.    if (try >= MAXTRY)  /* computer pass */
  118.      {
  119.       pass++;
  120.       printf("I pass.\n");
  121.       *i = -1;
  122.     }
  123.    else   /* find valid move */
  124.      {
  125.       pass = 0;      
  126.       printf("my move: ");
  127.       if (*j < 8)
  128.      a = *j + 65;
  129.       else
  130.      a = *j + 66;
  131.       printf("%c", a);
  132.       ii = 19 - *i;
  133.       if (ii < 10)
  134.      printf("%1d\n", ii);
  135.       else
  136.      printf("%2d\n", ii);
  137.     }
  138. }  /* end genmove */
  139.