bdinit.c revision 1.33 1 /* $NetBSD: bdinit.c,v 1.33 2022/05/29 14:01:57 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
35 #include <sys/cdefs.h>
36 /* from: @(#)bdinit.c 8.2 (Berkeley) 5/3/95 */
37 __RCSID("$NetBSD: bdinit.c,v 1.33 2022/05/29 14:01:57 rillig Exp $");
38
39 #include <string.h>
40 #include "gomoku.h"
41
42 static void init_overlap(void);
43
44 static void
45 init_spot_flags_and_fval(struct spotstr *sp, int i, int j)
46 {
47
48 sp->s_flags = 0;
49 if (j < 5) {
50 /* directions 1, 2, 3 are blocked */
51 sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
52 (BFLAG << 3);
53 sp->s_fval[BLACK][1].s = 0x600;
54 sp->s_fval[BLACK][2].s = 0x600;
55 sp->s_fval[BLACK][3].s = 0x600;
56 sp->s_fval[WHITE][1].s = 0x600;
57 sp->s_fval[WHITE][2].s = 0x600;
58 sp->s_fval[WHITE][3].s = 0x600;
59 } else if (j == 5) {
60 /* five spaces, blocked on one side */
61 sp->s_fval[BLACK][1].s = 0x500;
62 sp->s_fval[BLACK][2].s = 0x500;
63 sp->s_fval[BLACK][3].s = 0x500;
64 sp->s_fval[WHITE][1].s = 0x500;
65 sp->s_fval[WHITE][2].s = 0x500;
66 sp->s_fval[WHITE][3].s = 0x500;
67 } else {
68 /* six spaces, not blocked */
69 sp->s_fval[BLACK][1].s = 0x401;
70 sp->s_fval[BLACK][2].s = 0x401;
71 sp->s_fval[BLACK][3].s = 0x401;
72 sp->s_fval[WHITE][1].s = 0x401;
73 sp->s_fval[WHITE][2].s = 0x401;
74 sp->s_fval[WHITE][3].s = 0x401;
75 }
76 if (i > (BSZ - 4)) {
77 /* directions 0, 1 are blocked */
78 sp->s_flags |= BFLAG | (BFLAG << 1);
79 sp->s_fval[BLACK][0].s = 0x600;
80 sp->s_fval[BLACK][1].s = 0x600;
81 sp->s_fval[WHITE][0].s = 0x600;
82 sp->s_fval[WHITE][1].s = 0x600;
83 } else if (i == (BSZ - 4)) {
84 sp->s_fval[BLACK][0].s = 0x500;
85 sp->s_fval[WHITE][0].s = 0x500;
86 /* if direction 1 is not blocked */
87 if ((sp->s_flags & (BFLAG << 1)) == 0) {
88 sp->s_fval[BLACK][1].s = 0x500;
89 sp->s_fval[WHITE][1].s = 0x500;
90 }
91 } else {
92 sp->s_fval[BLACK][0].s = 0x401;
93 sp->s_fval[WHITE][0].s = 0x401;
94 if (i < 5) {
95 /* direction 3 is blocked */
96 sp->s_flags |= (BFLAG << 3);
97 sp->s_fval[BLACK][3].s = 0x600;
98 sp->s_fval[WHITE][3].s = 0x600;
99 } else if (i == 5 &&
100 (sp->s_flags & (BFLAG << 3)) == 0) {
101 sp->s_fval[BLACK][3].s = 0x500;
102 sp->s_fval[WHITE][3].s = 0x500;
103 }
104 }
105 }
106
107 /* Allocate one of the pre-allocated frames for each non-blocked frame. */
108 static void
109 init_spot_frame(struct spotstr *sp, frame_index *fip)
110 {
111
112 for (direction r = 4; r-- > 0; ) {
113 if ((sp->s_flags & (BFLAG << r)) != 0)
114 continue;
115
116 frame_index fi = (*fip)++;
117 sp->s_frame[r] = fi;
118
119 struct combostr *cbp = &frames[fi];
120 cbp->c_combo.s = sp->s_fval[BLACK][r].s;
121 cbp->c_vertex = (spot_index)(sp - board);
122 cbp->c_nframes = 1;
123 cbp->c_dir = r;
124 }
125 }
126
127 void
128 init_board(void)
129 {
130
131 game.nmoves = 0;
132 game.winning_spot = 0;
133
134 struct spotstr *sp = board;
135 for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
136 sp->s_occ = BORDER; /* bottom border and corners */
137 sp->s_flags = BFLAGALL;
138 }
139
140 /* fill the playing area of the board with EMPTY spots */
141 frame_index fi = 0;
142 memset(frames, 0, sizeof(frames));
143 for (int row = 1; row <= BSZ; row++, sp++) {
144 for (int col = 1; col <= BSZ; col++, sp++) {
145 sp->s_occ = EMPTY;
146 sp->s_wval = 0;
147 init_spot_flags_and_fval(sp, col, row);
148 init_spot_frame(sp, &fi);
149 }
150 sp->s_occ = BORDER; /* combined left and right border */
151 sp->s_flags = BFLAGALL;
152 }
153
154 for (int i = 0; i < BSZ + 1; i++, sp++) {
155 sp->s_occ = BORDER; /* top border and top-right corner */
156 sp->s_flags = BFLAGALL;
157 }
158
159 sortframes[BLACK] = NULL;
160 sortframes[WHITE] = NULL;
161
162 init_overlap();
163 }
164
165 /*
166 * Variable names for frames A and B:
167 *
168 * fi index of the frame in the global 'frames'
169 * d direction delta, difference between adjacent spot indexes
170 * off index of the spot in the frame, 0 to 5
171 */
172
173 /*
174 * Each entry in the overlap array is a bit mask with eight bits corresponding
175 * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
176 *
177 * The eight bits correspond to whether A and B are open-ended (length 6) or
178 * closed (length 5).
179 *
180 * 0 A closed and B closed
181 * 1 A closed and B open
182 * 2 A open and B closed
183 * 3 A open and B open
184 * 4 A closed and B closed and overlaps in more than one spot
185 * 5 A closed and B open and overlaps in more than one spot
186 * 6 A open and B closed and overlaps in more than one spot
187 * 7 A open and B open and overlaps in more than one spot
188 *
189 * As pieces are played during the game, frames that no longer share an empty
190 * spot will be removed from the overlap array by setting the entry to 0.
191 */
192 static u_char
193 adjust_overlap(u_char ov, int ra, int offa, int rb, int offb, int mask)
194 {
195 ov |= (offb == 5) ? mask & 0xA : mask;
196 if (rb != ra)
197 return ov;
198
199 /* compute the multiple spot overlap values */
200 switch (offa) {
201 case 0:
202 if (offb == 4)
203 ov |= 0xA0;
204 else if (offb != 5)
205 ov |= 0xF0;
206 break;
207 case 1:
208 if (offb == 5)
209 ov |= 0xA0;
210 else
211 ov |= 0xF0;
212 break;
213 case 4:
214 if (offb == 0)
215 ov |= 0xC0;
216 else
217 ov |= 0xF0;
218 break;
219 case 5:
220 if (offb == 1)
221 ov |= 0xC0;
222 else if (offb != 0)
223 ov |= 0xF0;
224 break;
225 default:
226 ov |= 0xF0;
227 }
228
229 return ov;
230 }
231
232 /*
233 * Given a single spot 's' of frame A, update the overlap information for
234 * each frame B that overlaps frame A in that spot.
235 */
236 static void
237 init_overlap_frame(int fia, int ra, int offa, spot_index s, int mask)
238 {
239
240 for (int rb = 4; --rb >= 0;) {
241 int db = dd[rb];
242
243 for (int offb = 0; offb < 6; offb++) {
244 /* spb0 is the spot where frame B starts. */
245 const struct spotstr *spb0 = &board[s - offb * db];
246 if (spb0->s_occ == BORDER)
247 break;
248 if ((spb0->s_flags & BFLAG << rb) != 0)
249 continue;
250
251 frame_index fib = spb0->s_frame[rb];
252 intersect[fia * FAREA + fib] = s;
253 u_char *op = &overlap[fia * FAREA + fib];
254 *op = adjust_overlap(*op, ra, offa, rb, offb, mask);
255 }
256 }
257 }
258
259 static void
260 init_overlap(void)
261 {
262
263 memset(overlap, 0, sizeof(overlap));
264 memset(intersect, 0, sizeof(intersect));
265
266 for (int fia = FAREA; fia-- > 0;) {
267 const struct combostr *fa = &frames[fia];
268 spot_index s = fa->c_vertex;
269 u_char ra = fa->c_dir;
270 int da = dd[ra];
271
272 /*
273 * len = 5 if closed, 6 if open. At this early stage, Black
274 * and White have the same value for cv_win.
275 */
276 int len = 5 + board[s].s_fval[BLACK][ra].cv_win;
277
278 for (int offa = 0; offa < len; offa++) {
279 /* spot[5] in frame A only overlaps if it is open */
280 int mask = (offa == 5) ? 0xC : 0xF;
281
282 init_overlap_frame(fia, ra, offa, s + offa * da, mask);
283 }
284 }
285 }
286