bdinit.c revision 1.1 1 /*
2 * Copyright (c) 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ralph Campbell.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)bdinit.c 8.2 (Berkeley) 5/3/95";
39 #endif /* not lint */
40
41 #include <string.h>
42 #include "gomoku.h"
43
44 bdinit(bp)
45 struct spotstr *bp;
46 {
47 register int i, j, r;
48 register struct spotstr *sp;
49 register struct combostr *cbp;
50
51 movenum = 1;
52
53 /* mark the borders as such */
54 sp = bp;
55 for (i = BSZ2; --i >= 0; sp++) {
56 sp->s_occ = BORDER; /* top border */
57 sp->s_flg = BFLAGALL;
58 }
59
60 /* fill entire board with EMPTY spots */
61 memset(frames, 0, sizeof(frames));
62 cbp = frames;
63 for (j = 0; ++j < BSZ1; sp++) { /* for each row */
64 for (i = 0; ++i < BSZ1; sp++) { /* for each column */
65 sp->s_occ = EMPTY;
66 sp->s_flg = 0;
67 sp->s_wval = 0;
68 if (j < 5) {
69 /* directions 1, 2, 3 are blocked */
70 sp->s_flg |= (BFLAG << 1) | (BFLAG << 2) |
71 (BFLAG << 3);
72 sp->s_fval[BLACK][1].s = MAXCOMBO;
73 sp->s_fval[BLACK][2].s = MAXCOMBO;
74 sp->s_fval[BLACK][3].s = MAXCOMBO;
75 sp->s_fval[WHITE][1].s = MAXCOMBO;
76 sp->s_fval[WHITE][2].s = MAXCOMBO;
77 sp->s_fval[WHITE][3].s = MAXCOMBO;
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_flg |= BFLAG | (BFLAG << 1);
98 sp->s_fval[BLACK][0].s = MAXCOMBO;
99 sp->s_fval[BLACK][1].s = MAXCOMBO;
100 sp->s_fval[WHITE][0].s = MAXCOMBO;
101 sp->s_fval[WHITE][1].s = MAXCOMBO;
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_flg & (BFLAG << 1))) {
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_flg |= (BFLAG << 3);
116 sp->s_fval[BLACK][3].s = MAXCOMBO;
117 sp->s_fval[WHITE][3].s = MAXCOMBO;
118 } else if (i == 5 &&
119 !(sp->s_flg & (BFLAG << 3))) {
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 (r = 4; --r >= 0; ) {
128 if (sp->s_flg & (BFLAG << r))
129 continue;
130 cbp->c_combo.s = sp->s_fval[BLACK][r].s;
131 cbp->c_vertex = 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_flg = BFLAGALL;
140 }
141
142 /* mark the borders as such */
143 for (i = BSZ1; --i >= 0; sp++) {
144 sp->s_occ = BORDER; /* bottom border */
145 sp->s_flg = BFLAGALL;
146 }
147
148 sortframes[BLACK] = (struct combostr *)0;
149 sortframes[WHITE] = (struct combostr *)0;
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 coorespond 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 init_overlap()
171 {
172 register struct spotstr *sp1, *sp2;
173 register struct combostr *cbp;
174 register int i, f, r, n, d1, d2;
175 int mask, bmask, vertex, s;
176 u_char *str;
177 short *ip;
178
179 memset(overlap, 0, sizeof(overlap));
180 memset(intersect, 0, sizeof(intersect));
181 str = &overlap[FAREA * FAREA];
182 ip = &intersect[FAREA * FAREA];
183 for (cbp = frames + FAREA; --cbp >= frames; ) { /* each frame */
184 str -= FAREA;
185 ip -= FAREA;
186 sp1 = &board[vertex = cbp->c_vertex];
187 d1 = dd[r = cbp->c_dir];
188 /*
189 * s = 5 if closed, 6 if open.
190 * At this point black & white are the same.
191 */
192 s = 5 + sp1->s_fval[BLACK][r].c.b;
193 /* for each spot in frame A */
194 for (i = 0; i < s; i++, sp1 += d1, vertex += d1) {
195 /* the sixth spot in frame A only overlaps if it is open */
196 mask = (i == 5) ? 0xC : 0xF;
197 /* for each direction */
198 for (r = 4; --r >= 0; ) {
199 bmask = BFLAG << r;
200 sp2 = sp1;
201 d2 = dd[r];
202 /* for each frame that intersects at spot sp1 */
203 for (f = 0; f < 6; f++, sp2 -= d2) {
204 if (sp2->s_occ == BORDER)
205 break;
206 if (sp2->s_flg & bmask)
207 continue;
208 n = sp2->s_frame[r] - frames;
209 ip[n] = vertex;
210 str[n] |= (f == 5) ? mask & 0xA : mask;
211 if (r == cbp->c_dir) {
212 /* compute the multiple spot overlap values */
213 switch (i) {
214 case 0: /* sp1 is the first spot in A */
215 if (f == 4)
216 str[n] |= 0xA0;
217 else if (f != 5)
218 str[n] |= 0xF0;
219 break;
220 case 1: /* sp1 is the second spot in A */
221 if (f == 5)
222 str[n] |= 0xA0;
223 else
224 str[n] |= 0xF0;
225 break;
226 case 4: /* sp1 is the penultimate spot in A */
227 if (f == 0)
228 str[n] |= 0xC0;
229 else
230 str[n] |= 0xF0;
231 break;
232 case 5: /* sp1 is the last spot in A */
233 if (f == 1)
234 str[n] |= 0xC0;
235 else if (f != 0)
236 str[n] |= 0xF0;
237 break;
238 default:
239 str[n] |= 0xF0;
240 }
241 }
242 }
243 }
244 }
245 }
246 }
247