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