gomoku.h revision 1.5 1 1.5 hubertf /* $NetBSD: gomoku.h,v 1.5 1998/09/13 15:27:28 hubertf Exp $ */
2 1.3 cgd
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1994
5 1.1 tls * The Regents of the University of California. All rights reserved.
6 1.1 tls *
7 1.1 tls * This code is derived from software contributed to Berkeley by
8 1.1 tls * Ralph Campbell.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.1 tls * 3. All advertising materials mentioning features or use of this software
19 1.1 tls * must display the following acknowledgement:
20 1.1 tls * This product includes software developed by the University of
21 1.1 tls * California, Berkeley and its contributors.
22 1.1 tls * 4. Neither the name of the University nor the names of its contributors
23 1.1 tls * may be used to endorse or promote products derived from this software
24 1.1 tls * without specific prior written permission.
25 1.1 tls *
26 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 tls * SUCH DAMAGE.
37 1.1 tls *
38 1.1 tls * @(#)gomoku.h 8.2 (Berkeley) 5/3/95
39 1.1 tls */
40 1.1 tls
41 1.1 tls #include <sys/types.h>
42 1.4 lukem #include <stdio.h>
43 1.1 tls
44 1.1 tls /* board dimensions */
45 1.1 tls #define BSZ 19
46 1.1 tls #define BSZ1 (BSZ+1)
47 1.1 tls #define BSZ2 (BSZ+2)
48 1.1 tls #define BAREA (BSZ2*BSZ1+1)
49 1.1 tls
50 1.1 tls /* frame dimentions (based on 5 in a row) */
51 1.1 tls #define FSZ1 BSZ
52 1.1 tls #define FSZ2 (BSZ-4)
53 1.1 tls #define FAREA (FSZ1*FSZ2 + FSZ2*FSZ2 + FSZ1*FSZ2 + FSZ2*FSZ2)
54 1.1 tls
55 1.1 tls #define MUP (BSZ1)
56 1.1 tls #define MDOWN (-BSZ1)
57 1.1 tls #define MLEFT (-1)
58 1.1 tls #define MRIGHT (1)
59 1.1 tls
60 1.1 tls /* values for s_occ */
61 1.1 tls #define BLACK 0
62 1.1 tls #define WHITE 1
63 1.1 tls #define EMPTY 2
64 1.1 tls #define BORDER 3
65 1.1 tls
66 1.1 tls /* return values for makemove() */
67 1.1 tls #define MOVEOK 0
68 1.1 tls #define RESIGN 1
69 1.1 tls #define ILLEGAL 2
70 1.1 tls #define WIN 3
71 1.1 tls #define TIE 4
72 1.1 tls #define SAVE 5
73 1.1 tls
74 1.1 tls #define A 1
75 1.1 tls #define B 2
76 1.1 tls #define C 3
77 1.1 tls #define D 4
78 1.1 tls #define E 5
79 1.1 tls #define F 6
80 1.1 tls #define G 7
81 1.1 tls #define H 8
82 1.1 tls #define J 9
83 1.1 tls #define K 10
84 1.1 tls #define L 11
85 1.1 tls #define M 12
86 1.1 tls #define N 13
87 1.1 tls #define O 14
88 1.1 tls #define P 15
89 1.1 tls #define Q 16
90 1.1 tls #define R 17
91 1.1 tls #define S 18
92 1.1 tls #define T 19
93 1.1 tls
94 1.1 tls #define PT(x,y) ((x) + BSZ1 * (y))
95 1.1 tls
96 1.1 tls /*
97 1.1 tls * A 'frame' is a group of five or six contiguous board locations.
98 1.1 tls * An open ended frame is one with spaces on both ends; otherwise, its closed.
99 1.1 tls * A 'combo' is a group of intersecting frames and consists of two numbers:
100 1.1 tls * 'A' is the number of moves to make the combo non-blockable.
101 1.1 tls * 'B' is the minimum number of moves needed to win once it can't be blocked.
102 1.1 tls * A 'force' is a combo that is one move away from being non-blockable
103 1.1 tls *
104 1.1 tls * Single frame combo values:
105 1.1 tls * <A,B> board values
106 1.1 tls * 5,0 . . . . . O
107 1.1 tls * 4,1 . . . . . .
108 1.1 tls * 4,0 . . . . X O
109 1.1 tls * 3,1 . . . . X .
110 1.1 tls * 3,0 . . . X X O
111 1.1 tls * 2,1 . . . X X .
112 1.1 tls * 2,0 . . X X X O
113 1.1 tls * 1,1 . . X X X .
114 1.1 tls * 1,0 . X X X X O
115 1.1 tls * 0,1 . X X X X .
116 1.1 tls * 0,0 X X X X X O
117 1.1 tls *
118 1.1 tls * The rule for combining two combos (<A1,B1> <A2,B2>)
119 1.1 tls * with V valid intersection points, is:
120 1.1 tls * A' = A1 + A2 - 2 - V
121 1.1 tls * B' = MIN(A1 + B1 - 1, A2 + B2 - 1)
122 1.1 tls * Each time a frame is added to the combo, the number of moves to complete
123 1.1 tls * the force is the number of moves needed to 'fill' the frame plus one at
124 1.1 tls * the intersection point. The number of moves to win is the number of moves
125 1.1 tls * to complete the best frame minus the last move to complete the force.
126 1.1 tls * Note that it doesn't make sense to combine a <1,x> with anything since
127 1.1 tls * it is already a force. Also, the frames have to be independent so a
128 1.1 tls * single move doesn't affect more than one frame making up the combo.
129 1.1 tls *
130 1.1 tls * Rules for comparing which of two combos (<A1,B1> <A2,B2>) is better:
131 1.1 tls * Both the same color:
132 1.1 tls * <A',B'> = (A1 < A2 || A1 == A2 && B1 <= B2) ? <A1,B1> : <A2,B2>
133 1.1 tls * We want to complete the force first, then the combo with the
134 1.1 tls * fewest moves to win.
135 1.1 tls * Different colors, <A1,B1> is the combo for the player with the next move:
136 1.1 tls * <A',B'> = A2 <= 1 && (A1 > 1 || A2 + B2 < A1 + B1) ? <A2,B2> : <A1,B1>
137 1.1 tls * We want to block only if we have to (i.e., if they are one move away
138 1.1 tls * from completing a force and we don't have a force that we can
139 1.1 tls * complete which takes fewer or the same number of moves to win).
140 1.1 tls */
141 1.1 tls
142 1.1 tls #define MAXA 6
143 1.1 tls #define MAXB 2
144 1.1 tls #define MAXCOMBO 0x600
145 1.1 tls
146 1.1 tls union comboval {
147 1.1 tls struct {
148 1.1 tls #if BYTE_ORDER == BIG_ENDIAN
149 1.1 tls u_char a; /* # moves to complete force */
150 1.1 tls u_char b; /* # moves to win */
151 1.1 tls #endif
152 1.1 tls #if BYTE_ORDER == LITTLE_ENDIAN
153 1.1 tls u_char b; /* # moves to win */
154 1.1 tls u_char a; /* # moves to complete force */
155 1.1 tls #endif
156 1.1 tls } c;
157 1.1 tls u_short s;
158 1.1 tls };
159 1.1 tls
160 1.1 tls /*
161 1.1 tls * This structure is used to record information about single frames (F) and
162 1.1 tls * combinations of two more frames (C).
163 1.1 tls * For combinations of two or more frames, there is an additional
164 1.1 tls * array of pointers to the frames of the combination which is sorted
165 1.1 tls * by the index into the frames[] array. This is used to prevent duplication
166 1.1 tls * since frame A combined with B is the same as B with A.
167 1.1 tls * struct combostr *c_sort[size c_nframes];
168 1.1 tls * The leaves of the tree (frames) are numbered 0 (bottom, leftmost)
169 1.1 tls * to c_nframes - 1 (top, right). This is stored in c_frameindex and
170 1.1 tls * c_dir if C_LOOP is set.
171 1.1 tls */
172 1.1 tls struct combostr {
173 1.1 tls struct combostr *c_next; /* list of combos at the same level */
174 1.1 tls struct combostr *c_prev; /* list of combos at the same level */
175 1.1 tls struct combostr *c_link[2]; /* C:previous level or F:NULL */
176 1.1 tls union comboval c_linkv[2]; /* C:combo value for link[0,1] */
177 1.1 tls union comboval c_combo; /* C:combo value for this level */
178 1.1 tls u_short c_vertex; /* C:intersection or F:frame head */
179 1.1 tls u_char c_nframes; /* number of frames in the combo */
180 1.1 tls u_char c_dir; /* C:loop frame or F:frame direction */
181 1.1 tls u_char c_flg; /* C:combo flags */
182 1.1 tls u_char c_frameindex; /* C:intersection frame index */
183 1.1 tls u_char c_framecnt[2]; /* number of frames left to attach */
184 1.1 tls u_char c_emask[2]; /* C:bit mask of completion spots for
185 1.1 tls * link[0] and link[1] */
186 1.1 tls u_char c_voff[2]; /* C:vertex offset within frame */
187 1.1 tls };
188 1.1 tls
189 1.1 tls /* flag values for c_flg */
190 1.1 tls #define C_OPEN_0 0x01 /* link[0] is an open ended frame */
191 1.1 tls #define C_OPEN_1 0x02 /* link[1] is an open ended frame */
192 1.1 tls #define C_LOOP 0x04 /* link[1] intersects previous frame */
193 1.1 tls #define C_MARK 0x08 /* indicates combo processed */
194 1.1 tls
195 1.1 tls /*
196 1.1 tls * This structure is used for recording the completion points of
197 1.1 tls * multi frame combos.
198 1.1 tls */
199 1.1 tls struct elist {
200 1.1 tls struct elist *e_next; /* list of completion points */
201 1.1 tls struct combostr *e_combo; /* the whole combo */
202 1.1 tls u_char e_off; /* offset in frame of this empty spot */
203 1.1 tls u_char e_frameindex; /* intersection frame index */
204 1.1 tls u_char e_framecnt; /* number of frames left to attach */
205 1.1 tls u_char e_emask; /* real value of the frame's emask */
206 1.1 tls union comboval e_fval; /* frame combo value */
207 1.1 tls };
208 1.1 tls
209 1.1 tls /*
210 1.1 tls * One spot structure for each location on the board.
211 1.1 tls * A frame consists of the combination for the current spot plus the five spots
212 1.1 tls * 0: right, 1: right & down, 2: down, 3: down & left.
213 1.1 tls */
214 1.1 tls struct spotstr {
215 1.1 tls short s_occ; /* color of occupant */
216 1.1 tls short s_wval; /* weighted value */
217 1.1 tls int s_flg; /* flags for graph walks */
218 1.1 tls struct combostr *s_frame[4]; /* level 1 combo for frame[dir] */
219 1.1 tls union comboval s_fval[2][4]; /* combo value for [color][frame] */
220 1.1 tls union comboval s_combo[2]; /* minimum combo value for BLK & WHT */
221 1.1 tls u_char s_level[2]; /* number of frames in the min combo */
222 1.1 tls u_char s_nforce[2]; /* number of <1,x> combos */
223 1.1 tls struct elist *s_empty; /* level n combo completion spots */
224 1.1 tls struct elist *s_nempty; /* level n+1 combo completion spots */
225 1.1 tls int dummy[2]; /* XXX */
226 1.1 tls };
227 1.1 tls
228 1.1 tls /* flag values for s_flg */
229 1.1 tls #define CFLAG 0x000001 /* frame is part of a combo */
230 1.1 tls #define CFLAGALL 0x00000F /* all frame directions marked */
231 1.1 tls #define IFLAG 0x000010 /* legal intersection point */
232 1.1 tls #define IFLAGALL 0x0000F0 /* any intersection points? */
233 1.1 tls #define FFLAG 0x000100 /* frame is part of a <1,x> combo */
234 1.1 tls #define FFLAGALL 0x000F00 /* all force frames */
235 1.1 tls #define MFLAG 0x001000 /* frame has already been seen */
236 1.1 tls #define MFLAGALL 0x00F000 /* all frames seen */
237 1.1 tls #define BFLAG 0x010000 /* frame intersects border or dead */
238 1.1 tls #define BFLAGALL 0x0F0000 /* all frames dead */
239 1.1 tls
240 1.1 tls /*
241 1.1 tls * This structure is used to store overlap information between frames.
242 1.1 tls */
243 1.1 tls struct ovlp_info {
244 1.1 tls int o_intersect; /* intersection spot */
245 1.1 tls struct combostr *o_fcombo; /* the connecting combo */
246 1.1 tls u_char o_link; /* which link to update (0 or 1) */
247 1.1 tls u_char o_off; /* offset in frame of intersection */
248 1.1 tls u_char o_frameindex; /* intersection frame index */
249 1.1 tls };
250 1.1 tls
251 1.1 tls extern char *letters;
252 1.1 tls extern char fmtbuf[];
253 1.1 tls extern char pdir[];
254 1.1 tls
255 1.1 tls extern int dd[4];
256 1.1 tls extern struct spotstr board[BAREA]; /* info for board */
257 1.1 tls extern struct combostr frames[FAREA]; /* storage for single frames */
258 1.1 tls extern struct combostr *sortframes[2]; /* sorted, non-empty frames */
259 1.1 tls extern u_char overlap[FAREA * FAREA]; /* frame [a][b] overlap */
260 1.1 tls extern short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
261 1.1 tls extern int movelog[BSZ * BSZ]; /* history of moves */
262 1.1 tls extern int movenum;
263 1.1 tls extern int debug;
264 1.1 tls
265 1.4 lukem #define ASSERT(x)
266 1.1 tls
267 1.4 lukem void bdinit __P((struct spotstr *));
268 1.4 lukem void init_overlap __P((void));
269 1.4 lukem int getline __P((char *, int));
270 1.4 lukem void ask __P((char *));
271 1.4 lukem void dislog __P((char *));
272 1.4 lukem void bdump __P((FILE *));
273 1.4 lukem void bdisp __P((void));
274 1.4 lukem void bdisp_init __P((void));
275 1.4 lukem void cursfini __P((void));
276 1.4 lukem void cursinit __P((void));
277 1.4 lukem void bdwho __P((int));
278 1.5 hubertf void panic __P((char *)) __attribute__((__noreturn__));
279 1.4 lukem void log __P((char *));
280 1.4 lukem void dlog __P((char *));
281 1.5 hubertf void quit __P((void)) __attribute__((__noreturn__));
282 1.5 hubertf void quitsig __P((int)) __attribute__((__noreturn__));
283 1.4 lukem void whatsup __P((int));
284 1.4 lukem int readinput __P((FILE *));
285 1.4 lukem char *stoc __P((int));
286 1.4 lukem int lton __P((int));
287 1.4 lukem int ctos __P((char *));
288 1.4 lukem void update_overlap __P((struct spotstr *));
289 1.4 lukem int makemove __P((int, int));
290 1.4 lukem int list_eq __P((struct combostr **, struct combostr **, int));
291 1.4 lukem void clearcombo __P((struct combostr *, int));
292 1.4 lukem void makeempty __P((struct combostr *));
293 1.4 lukem void appendcombo __P((struct combostr *, int));
294 1.4 lukem void updatecombo __P((struct combostr *, int));
295 1.4 lukem void markcombo __P((struct combostr *));
296 1.4 lukem void printcombo __P((struct combostr *, char *));
297 1.4 lukem void makecombo __P((struct combostr *, struct spotstr *, int, int));
298 1.4 lukem void makecombo2 __P((struct combostr *, struct spotstr *, int, int));
299 1.4 lukem int sortcombo __P((struct combostr **, struct combostr **, struct combostr *));
300 1.4 lukem int checkframes __P((struct combostr *, struct combostr *, struct spotstr *,
301 1.4 lukem int, struct ovlp_info *));
302 1.4 lukem void addframes __P((int));
303 1.4 lukem void scanframes __P((int));
304 1.4 lukem int better __P((struct spotstr *, struct spotstr *, int));
305 1.4 lukem int pickmove __P((int));
306