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