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