bdinit.c revision 1.24 1 /* $NetBSD: bdinit.c,v 1.24 2022/05/28 08:32:55 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.24 2022/05/28 08:32:55 rillig Exp $");
38
39 #include <string.h>
40 #include "gomoku.h"
41
42 static void init_overlap(void);
43
44 void
45 bdinit(struct spotstr *bp)
46 {
47 struct spotstr *sp;
48 struct combostr *cbp;
49
50 game.nmoves = 0;
51 game.winning_spot = 0;
52
53 /* mark the borders as such */
54 sp = bp;
55 for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
56 sp->s_occ = BORDER; /* top border */
57 sp->s_flags = BFLAGALL;
58 }
59
60 /* fill entire board with EMPTY spots */
61 memset(frames, 0, sizeof(frames));
62 cbp = frames;
63 for (int j = 0; ++j < BSZ + 1; sp++) { /* for each row */
64 for (int i = 0; ++i < BSZ + 1; sp++) { /* for each column */
65 sp->s_occ = EMPTY;
66 sp->s_flags = 0;
67 sp->s_wval = 0;
68 if (j < 5) {
69 /* directions 1, 2, 3 are blocked */
70 sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
71 (BFLAG << 3);
72 sp->s_fval[BLACK][1].s = 0x600;
73 sp->s_fval[BLACK][2].s = 0x600;
74 sp->s_fval[BLACK][3].s = 0x600;
75 sp->s_fval[WHITE][1].s = 0x600;
76 sp->s_fval[WHITE][2].s = 0x600;
77 sp->s_fval[WHITE][3].s = 0x600;
78 } else if (j == 5) {
79 /* five spaces, blocked on one side */
80 sp->s_fval[BLACK][1].s = 0x500;
81 sp->s_fval[BLACK][2].s = 0x500;
82 sp->s_fval[BLACK][3].s = 0x500;
83 sp->s_fval[WHITE][1].s = 0x500;
84 sp->s_fval[WHITE][2].s = 0x500;
85 sp->s_fval[WHITE][3].s = 0x500;
86 } else {
87 /* six spaces, not blocked */
88 sp->s_fval[BLACK][1].s = 0x401;
89 sp->s_fval[BLACK][2].s = 0x401;
90 sp->s_fval[BLACK][3].s = 0x401;
91 sp->s_fval[WHITE][1].s = 0x401;
92 sp->s_fval[WHITE][2].s = 0x401;
93 sp->s_fval[WHITE][3].s = 0x401;
94 }
95 if (i > (BSZ - 4)) {
96 /* directions 0, 1 are blocked */
97 sp->s_flags |= BFLAG | (BFLAG << 1);
98 sp->s_fval[BLACK][0].s = 0x600;
99 sp->s_fval[BLACK][1].s = 0x600;
100 sp->s_fval[WHITE][0].s = 0x600;
101 sp->s_fval[WHITE][1].s = 0x600;
102 } else if (i == (BSZ - 4)) {
103 sp->s_fval[BLACK][0].s = 0x500;
104 sp->s_fval[WHITE][0].s = 0x500;
105 /* if direction 1 is not blocked */
106 if ((sp->s_flags & (BFLAG << 1)) == 0) {
107 sp->s_fval[BLACK][1].s = 0x500;
108 sp->s_fval[WHITE][1].s = 0x500;
109 }
110 } else {
111 sp->s_fval[BLACK][0].s = 0x401;
112 sp->s_fval[WHITE][0].s = 0x401;
113 if (i < 5) {
114 /* direction 3 is blocked */
115 sp->s_flags |= (BFLAG << 3);
116 sp->s_fval[BLACK][3].s = 0x600;
117 sp->s_fval[WHITE][3].s = 0x600;
118 } else if (i == 5 &&
119 (sp->s_flags & (BFLAG << 3)) == 0) {
120 sp->s_fval[BLACK][3].s = 0x500;
121 sp->s_fval[WHITE][3].s = 0x500;
122 }
123 }
124 /*
125 * Allocate a frame structure for non-blocked frames.
126 */
127 for (int r = 4; --r >= 0; ) {
128 if ((sp->s_flags & (BFLAG << r)) != 0)
129 continue;
130 cbp->c_combo.s = sp->s_fval[BLACK][r].s;
131 cbp->c_vertex = (u_short)(sp - board);
132 cbp->c_nframes = 1;
133 cbp->c_dir = r;
134 sp->s_frame[r] = cbp;
135 cbp++;
136 }
137 }
138 sp->s_occ = BORDER; /* left & right border */
139 sp->s_flags = BFLAGALL;
140 }
141
142 /* mark the borders as such */
143 for (int i = 0; i < BSZ + 1; i++, sp++) {
144 sp->s_occ = BORDER; /* bottom border */
145 sp->s_flags = BFLAGALL;
146 }
147
148 sortframes[BLACK] = NULL;
149 sortframes[WHITE] = NULL;
150 init_overlap();
151 }
152
153 /*
154 * Initialize the overlap array.
155 * Each entry in the array is a bit mask with eight bits corresponding
156 * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
157 * The eight bits correspond to whether A and B are open-ended (length 6) or
158 * closed (length 5).
159 * 0 A closed and B closed
160 * 1 A closed and B open
161 * 2 A open and B closed
162 * 3 A open and B open
163 * 4 A closed and B closed and overlaps in more than one spot
164 * 5 A closed and B open and overlaps in more than one spot
165 * 6 A open and B closed and overlaps in more than one spot
166 * 7 A open and B open and overlaps in more than one spot
167 * As pieces are played, it can make frames not overlap if there are no
168 * common open spaces shared between the two frames.
169 */
170 static void
171 init_overlap(void)
172 {
173
174 memset(overlap, 0, sizeof(overlap));
175 memset(intersect, 0, sizeof(intersect));
176 u_char *op = &overlap[FAREA * FAREA];
177 short *ip = &intersect[FAREA * FAREA];
178
179 for (unsigned fi = FAREA; fi-- > 0; ) { /* each frame */
180 struct combostr *cbp = &frames[fi];
181 op -= FAREA;
182 ip -= FAREA;
183 int vertex = cbp->c_vertex;
184 struct spotstr *sp1 = &board[vertex];
185 int d1 = dd[cbp->c_dir];
186 /*
187 * s = 5 if closed, 6 if open.
188 * At this point black & white are the same.
189 */
190 int s = 5 + sp1->s_fval[BLACK][cbp->c_dir].cv_win;
191 /* for each spot in frame A */
192 for (int i = 0; i < s; i++, sp1 += d1, vertex += d1) {
193 /* the sixth spot in frame A only overlaps if it is open */
194 int mask = (i == 5) ? 0xC : 0xF;
195 /* for each direction */
196 for (int r = 4; --r >= 0; ) {
197 struct spotstr *sp2 = sp1;
198 int d2 = dd[r];
199 /* for each frame that intersects at spot sp1 */
200 for (int f = 0; f < 6; f++, sp2 -= d2) {
201 if (sp2->s_occ == BORDER)
202 break;
203 if ((sp2->s_flags & BFLAG << r) != 0)
204 continue;
205 int n = (int)(sp2->s_frame[r] - frames);
206 ip[n] = (short)vertex;
207 op[n] |= (f == 5) ? mask & 0xA : mask;
208 if (r == cbp->c_dir) {
209 /* compute the multiple spot overlap values */
210 switch (i) {
211 case 0: /* sp1 is the first spot in A */
212 if (f == 4)
213 op[n] |= 0xA0;
214 else if (f != 5)
215 op[n] |= 0xF0;
216 break;
217 case 1: /* sp1 is the second spot in A */
218 if (f == 5)
219 op[n] |= 0xA0;
220 else
221 op[n] |= 0xF0;
222 break;
223 case 4: /* sp1 is the penultimate spot in A */
224 if (f == 0)
225 op[n] |= 0xC0;
226 else
227 op[n] |= 0xF0;
228 break;
229 case 5: /* sp1 is the last spot in A */
230 if (f == 1)
231 op[n] |= 0xC0;
232 else if (f != 0)
233 op[n] |= 0xF0;
234 break;
235 default:
236 op[n] |= 0xF0;
237 }
238 }
239 }
240 }
241 }
242 }
243 }
244