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 #include "comment.header"
49
50 /* $Id: findpatn.c,v 1.1 2003/01/12 04:01:52 gcasa Exp $ */
51
52 /*
53 * $Log: findpatn.c,v $
54 * Revision 1.1 2003/01/12 04:01:52 gcasa
55 * Committing the entire GNU Go and NeXT Go application to the repository.
56 * See COPYING file for GNU License.
57 *
58 * Revision 1.3 1997/07/06 19:34:57 ergo
59 * actual version
60 *
61 * Revision 1.2 1997/05/04 18:57:02 ergo
62 * added time control for moves
63 *
64 */
65
66 #define EMPTY 0
67
68 extern unsigned char p[19][19];
69 extern int opn[9];
70 extern int MAXX, MAXY, currentStone;
71 extern int opening(int*,int*,int*,int);
72 extern int openregion(int,int,int,int);
73 extern int matchpat(int,int,int*,int*,int*);
74
findpatn(int * i,int * j,int * val)75 int findpatn(int *i, int *j, int *val)
76 /* find pattern to match for next move */
77 {
78 int m, n;
79 int ti, tj, tval;
80 static int cnd, mtype; /* game tree node number, move type */
81 /* mtype = 0, basic; 1, inverted; 2, reflected; 3, inverted & reflected */
82
83 /* open game then occupy corners */
84 if (opn[4]) /* continue last move */
85 {
86 opn[4] = 0; /* clear flag */
87 if (opening(i, j, &cnd, mtype)) opn[4] = 1; /* more move then reset flag */
88 if (p[*i][*j] == EMPTY) /* valid move */
89 {
90 *val = 80;
91 return 1;
92 }
93 else
94 opn[4] = 0;
95 }
96
97 if (opn[0]) /* Northwest corner */
98 {
99 opn[0] = 0; /* clear flag */
100 if (openregion(0, 0, 5, 5))
101 {
102 cnd = 0;
103 mtype = 0;
104 opening(i, j, &cnd, mtype); /* get new node for next move */
105 if (opening(i, j, &cnd, mtype)) opn[4] = 1;
106 *val = 80;
107 return 1;
108 }
109 }
110
111 if (opn[1]) /* Southwest corner */
112 {
113 opn[1] = 0;
114 if (openregion(MAXX - 6, 0, MAXX - 1, 5))
115 {
116 cnd = 0;
117 mtype = 1;
118 opening(i, j, &cnd, mtype); /* get new node for next move */
119 if (opening(i, j, &cnd, mtype)) opn[4] = 1;
120 *val = 80;
121 return 1;
122 }
123 }
124
125 if (opn[2]) /* Northeast corner */
126 {
127 opn[2] = 0;
128 if (openregion(0, MAXY - 6, 5, MAXY - 1))
129 {
130 cnd = 0;
131 mtype = 2;
132 opening(i, j, &cnd, mtype); /* get new node for next move */
133 if (opening(i, j, &cnd, mtype)) opn[4] = 1;
134 *val = 80;
135 return 1;
136 }
137 }
138
139 if (opn[3]) /* Northeast corner */
140 {
141 opn[3] = 0;
142 if (openregion(MAXX - 6, MAXY - 6, MAXX - 1, MAXY - 1))
143 {
144 cnd = 0;
145 mtype = 3;
146 opening(i, j, &cnd, mtype); /* get new node for next move */
147 if (opening(i, j, &cnd, mtype)) opn[4] = 1;
148 *val = 80;
149 return 1;
150 }
151 }
152
153 /* occupy edges */
154 if (opn[5]) /* North edge */
155 {
156 opn[5] = 0;
157 if (openregion(0, (MAXY/2) - 3, 4, (MAXY/2) + 2))
158 {
159 *i = 3;
160 *j = MAXY/2;
161 *val = 80;
162 return 1;
163 }
164 }
165
166 if (opn[6]) /* South edge */
167 {
168 opn[6] = 0;
169 if (openregion(MAXX - 1, (MAXY/2) - 3, MAXX - 5, (MAXY/2) + 2))
170 {
171 *i = MAXX - 4;
172 *j = MAXY/2;
173 *val = 80;
174 return 1;
175 }
176 }
177
178 if (opn[7]) /* West edge */
179 {
180 opn[7] = 0;
181 if (openregion((MAXX/2) - 3, 0, (MAXX/2) + 2, 4))
182 {
183 *i = MAXX/2;
184 *j = 3;
185 *val = 80;
186 return 1;
187 }
188 }
189
190 if (opn[8]) /* East edge */
191 {
192 opn[8] = 0;
193 if (openregion((MAXX/2) - 3, MAXY - 1, (MAXX/2) + 2, MAXY - 5))
194 {
195 *i = MAXX/2;
196 *j = MAXY - 4;
197 *val = 80;
198 return 1;
199 }
200 }
201
202 *i = -1;
203 *j = -1;
204 *val = -1;
205
206 /* find local pattern */
207 for (m = 0; m < MAXX; m++)
208 for (n = 0; n < MAXY; n++)
209 if ((p[m][n] == currentStone) &&
210 (matchpat(m, n, &ti, &tj, &tval) && (tval > *val)))
211 {
212 *val = tval;
213 *i = ti;
214 *j = tj;
215 }
216 if (*val > 0) /* pattern found */
217 return 1;
218 else /* no match found */
219 return 0;
220 } /* end findpatn */
221
222