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