bdinit.c revision 1.24 1 1.24 rillig /* $NetBSD: bdinit.c,v 1.24 2022/05/28 08:32:55 rillig 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.5 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 tls * may be used to endorse or promote products derived from this software
20 1.1 tls * without specific prior written permission.
21 1.1 tls *
22 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 tls * SUCH DAMAGE.
33 1.1 tls */
34 1.1 tls
35 1.4 lukem #include <sys/cdefs.h>
36 1.18 rillig /* from: @(#)bdinit.c 8.2 (Berkeley) 5/3/95 */
37 1.24 rillig __RCSID("$NetBSD: bdinit.c,v 1.24 2022/05/28 08:32:55 rillig Exp $");
38 1.1 tls
39 1.1 tls #include <string.h>
40 1.1 tls #include "gomoku.h"
41 1.1 tls
42 1.8 dholland static void init_overlap(void);
43 1.8 dholland
44 1.4 lukem void
45 1.6 dholland bdinit(struct spotstr *bp)
46 1.1 tls {
47 1.4 lukem struct spotstr *sp;
48 1.4 lukem struct combostr *cbp;
49 1.1 tls
50 1.23 rillig game.nmoves = 0;
51 1.24 rillig game.winning_spot = 0;
52 1.1 tls
53 1.1 tls /* mark the borders as such */
54 1.1 tls sp = bp;
55 1.17 rillig for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
56 1.10 rillig sp->s_occ = BORDER; /* top border */
57 1.7 dholland sp->s_flags = BFLAGALL;
58 1.1 tls }
59 1.1 tls
60 1.1 tls /* fill entire board with EMPTY spots */
61 1.1 tls memset(frames, 0, sizeof(frames));
62 1.1 tls cbp = frames;
63 1.15 rillig for (int j = 0; ++j < BSZ + 1; sp++) { /* for each row */
64 1.15 rillig for (int i = 0; ++i < BSZ + 1; sp++) { /* for each column */
65 1.1 tls sp->s_occ = EMPTY;
66 1.7 dholland sp->s_flags = 0;
67 1.1 tls sp->s_wval = 0;
68 1.1 tls if (j < 5) {
69 1.1 tls /* directions 1, 2, 3 are blocked */
70 1.7 dholland sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
71 1.10 rillig (BFLAG << 3);
72 1.20 rillig sp->s_fval[BLACK][1].s = 0x600;
73 1.20 rillig sp->s_fval[BLACK][2].s = 0x600;
74 1.20 rillig sp->s_fval[BLACK][3].s = 0x600;
75 1.20 rillig sp->s_fval[WHITE][1].s = 0x600;
76 1.20 rillig sp->s_fval[WHITE][2].s = 0x600;
77 1.20 rillig sp->s_fval[WHITE][3].s = 0x600;
78 1.1 tls } else if (j == 5) {
79 1.1 tls /* five spaces, blocked on one side */
80 1.1 tls sp->s_fval[BLACK][1].s = 0x500;
81 1.1 tls sp->s_fval[BLACK][2].s = 0x500;
82 1.1 tls sp->s_fval[BLACK][3].s = 0x500;
83 1.1 tls sp->s_fval[WHITE][1].s = 0x500;
84 1.1 tls sp->s_fval[WHITE][2].s = 0x500;
85 1.1 tls sp->s_fval[WHITE][3].s = 0x500;
86 1.1 tls } else {
87 1.1 tls /* six spaces, not blocked */
88 1.1 tls sp->s_fval[BLACK][1].s = 0x401;
89 1.1 tls sp->s_fval[BLACK][2].s = 0x401;
90 1.1 tls sp->s_fval[BLACK][3].s = 0x401;
91 1.1 tls sp->s_fval[WHITE][1].s = 0x401;
92 1.1 tls sp->s_fval[WHITE][2].s = 0x401;
93 1.1 tls sp->s_fval[WHITE][3].s = 0x401;
94 1.1 tls }
95 1.1 tls if (i > (BSZ - 4)) {
96 1.1 tls /* directions 0, 1 are blocked */
97 1.7 dholland sp->s_flags |= BFLAG | (BFLAG << 1);
98 1.20 rillig sp->s_fval[BLACK][0].s = 0x600;
99 1.20 rillig sp->s_fval[BLACK][1].s = 0x600;
100 1.20 rillig sp->s_fval[WHITE][0].s = 0x600;
101 1.20 rillig sp->s_fval[WHITE][1].s = 0x600;
102 1.1 tls } else if (i == (BSZ - 4)) {
103 1.1 tls sp->s_fval[BLACK][0].s = 0x500;
104 1.1 tls sp->s_fval[WHITE][0].s = 0x500;
105 1.1 tls /* if direction 1 is not blocked */
106 1.13 rillig if ((sp->s_flags & (BFLAG << 1)) == 0) {
107 1.1 tls sp->s_fval[BLACK][1].s = 0x500;
108 1.1 tls sp->s_fval[WHITE][1].s = 0x500;
109 1.1 tls }
110 1.1 tls } else {
111 1.1 tls sp->s_fval[BLACK][0].s = 0x401;
112 1.1 tls sp->s_fval[WHITE][0].s = 0x401;
113 1.1 tls if (i < 5) {
114 1.1 tls /* direction 3 is blocked */
115 1.7 dholland sp->s_flags |= (BFLAG << 3);
116 1.20 rillig sp->s_fval[BLACK][3].s = 0x600;
117 1.20 rillig sp->s_fval[WHITE][3].s = 0x600;
118 1.1 tls } else if (i == 5 &&
119 1.13 rillig (sp->s_flags & (BFLAG << 3)) == 0) {
120 1.1 tls sp->s_fval[BLACK][3].s = 0x500;
121 1.1 tls sp->s_fval[WHITE][3].s = 0x500;
122 1.1 tls }
123 1.1 tls }
124 1.1 tls /*
125 1.16 rillig * Allocate a frame structure for non-blocked frames.
126 1.1 tls */
127 1.15 rillig for (int r = 4; --r >= 0; ) {
128 1.13 rillig if ((sp->s_flags & (BFLAG << r)) != 0)
129 1.1 tls continue;
130 1.1 tls cbp->c_combo.s = sp->s_fval[BLACK][r].s;
131 1.12 rillig cbp->c_vertex = (u_short)(sp - board);
132 1.1 tls cbp->c_nframes = 1;
133 1.1 tls cbp->c_dir = r;
134 1.1 tls sp->s_frame[r] = cbp;
135 1.1 tls cbp++;
136 1.1 tls }
137 1.1 tls }
138 1.1 tls sp->s_occ = BORDER; /* left & right border */
139 1.7 dholland sp->s_flags = BFLAGALL;
140 1.1 tls }
141 1.1 tls
142 1.1 tls /* mark the borders as such */
143 1.17 rillig for (int i = 0; i < BSZ + 1; i++, sp++) {
144 1.11 rillig sp->s_occ = BORDER; /* bottom border */
145 1.7 dholland sp->s_flags = BFLAGALL;
146 1.1 tls }
147 1.1 tls
148 1.19 rillig sortframes[BLACK] = NULL;
149 1.19 rillig sortframes[WHITE] = NULL;
150 1.1 tls init_overlap();
151 1.1 tls }
152 1.1 tls
153 1.1 tls /*
154 1.1 tls * Initialize the overlap array.
155 1.1 tls * Each entry in the array is a bit mask with eight bits corresponding
156 1.1 tls * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
157 1.16 rillig * The eight bits correspond to whether A and B are open-ended (length 6) or
158 1.1 tls * closed (length 5).
159 1.1 tls * 0 A closed and B closed
160 1.1 tls * 1 A closed and B open
161 1.1 tls * 2 A open and B closed
162 1.1 tls * 3 A open and B open
163 1.1 tls * 4 A closed and B closed and overlaps in more than one spot
164 1.1 tls * 5 A closed and B open and overlaps in more than one spot
165 1.1 tls * 6 A open and B closed and overlaps in more than one spot
166 1.1 tls * 7 A open and B open and overlaps in more than one spot
167 1.1 tls * As pieces are played, it can make frames not overlap if there are no
168 1.1 tls * common open spaces shared between the two frames.
169 1.1 tls */
170 1.8 dholland static void
171 1.6 dholland init_overlap(void)
172 1.1 tls {
173 1.1 tls
174 1.1 tls memset(overlap, 0, sizeof(overlap));
175 1.1 tls memset(intersect, 0, sizeof(intersect));
176 1.16 rillig u_char *op = &overlap[FAREA * FAREA];
177 1.16 rillig short *ip = &intersect[FAREA * FAREA];
178 1.16 rillig
179 1.15 rillig for (unsigned fi = FAREA; fi-- > 0; ) { /* each frame */
180 1.16 rillig struct combostr *cbp = &frames[fi];
181 1.14 rillig op -= FAREA;
182 1.1 tls ip -= FAREA;
183 1.16 rillig int vertex = cbp->c_vertex;
184 1.16 rillig struct spotstr *sp1 = &board[vertex];
185 1.16 rillig int d1 = dd[cbp->c_dir];
186 1.1 tls /*
187 1.1 tls * s = 5 if closed, 6 if open.
188 1.1 tls * At this point black & white are the same.
189 1.1 tls */
190 1.21 rillig int s = 5 + sp1->s_fval[BLACK][cbp->c_dir].cv_win;
191 1.1 tls /* for each spot in frame A */
192 1.15 rillig for (int i = 0; i < s; i++, sp1 += d1, vertex += d1) {
193 1.1 tls /* the sixth spot in frame A only overlaps if it is open */
194 1.16 rillig int mask = (i == 5) ? 0xC : 0xF;
195 1.1 tls /* for each direction */
196 1.15 rillig for (int r = 4; --r >= 0; ) {
197 1.16 rillig struct spotstr *sp2 = sp1;
198 1.16 rillig int d2 = dd[r];
199 1.1 tls /* for each frame that intersects at spot sp1 */
200 1.15 rillig for (int f = 0; f < 6; f++, sp2 -= d2) {
201 1.1 tls if (sp2->s_occ == BORDER)
202 1.1 tls break;
203 1.16 rillig if ((sp2->s_flags & BFLAG << r) != 0)
204 1.1 tls continue;
205 1.16 rillig int n = (int)(sp2->s_frame[r] - frames);
206 1.16 rillig ip[n] = (short)vertex;
207 1.14 rillig op[n] |= (f == 5) ? mask & 0xA : mask;
208 1.1 tls if (r == cbp->c_dir) {
209 1.1 tls /* compute the multiple spot overlap values */
210 1.1 tls switch (i) {
211 1.1 tls case 0: /* sp1 is the first spot in A */
212 1.1 tls if (f == 4)
213 1.14 rillig op[n] |= 0xA0;
214 1.1 tls else if (f != 5)
215 1.14 rillig op[n] |= 0xF0;
216 1.1 tls break;
217 1.1 tls case 1: /* sp1 is the second spot in A */
218 1.1 tls if (f == 5)
219 1.14 rillig op[n] |= 0xA0;
220 1.1 tls else
221 1.14 rillig op[n] |= 0xF0;
222 1.1 tls break;
223 1.1 tls case 4: /* sp1 is the penultimate spot in A */
224 1.1 tls if (f == 0)
225 1.14 rillig op[n] |= 0xC0;
226 1.1 tls else
227 1.14 rillig op[n] |= 0xF0;
228 1.1 tls break;
229 1.1 tls case 5: /* sp1 is the last spot in A */
230 1.1 tls if (f == 1)
231 1.14 rillig op[n] |= 0xC0;
232 1.1 tls else if (f != 0)
233 1.14 rillig op[n] |= 0xF0;
234 1.1 tls break;
235 1.1 tls default:
236 1.14 rillig op[n] |= 0xF0;
237 1.1 tls }
238 1.1 tls }
239 1.1 tls }
240 1.1 tls }
241 1.1 tls }
242 1.1 tls }
243 1.1 tls }
244