]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/pickmove.c
1 /* $NetBSD: pickmove.c,v 1.15 2009/06/04 05:43:29 dholland Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)pickmove.c 8.2 (Berkeley) 5/3/95";
40 __RCSID("$NetBSD: pickmove.c,v 1.15 2009/06/04 05:43:29 dholland Exp $");
51 #define BITS_PER_INT (sizeof(int) * CHAR_BIT)
52 #define MAPSZ (BAREA / BITS_PER_INT)
54 #define BIT_SET(a, b) ((a)[(b)/BITS_PER_INT] |= (1 << ((b) % BITS_PER_INT)))
55 #define BIT_CLR(a, b) ((a)[(b)/BITS_PER_INT] &= ~(1 << ((b) % BITS_PER_INT)))
56 #define BIT_TEST(a, b) ((a)[(b)/BITS_PER_INT] & (1 << ((b) % BITS_PER_INT)))
58 struct combostr
*hashcombos
[FAREA
]; /* hash list for finding duplicates */
59 struct combostr
*sortcombos
; /* combos at higher levels */
60 int combolen
; /* number of combos in sortcombos */
61 int nextcolor
; /* color of next move */
62 int elistcnt
; /* count of struct elist allocated */
63 int combocnt
; /* count of struct combostr allocated */
64 int forcemap
[MAPSZ
]; /* map for blocking <1,x> combos */
65 int tmpmap
[MAPSZ
]; /* map for blocking <1,x> combos */
66 int nforce
; /* count of opponent <1,x> combos */
71 struct spotstr
*sp
, *sp1
, *sp2
;
72 union comboval
*Ocp
, *Tcp
;
75 /* first move is easy */
79 /* initialize all the board values */
80 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
81 sp
->s_combo
[BLACK
].s
= MAXCOMBO
+ 1;
82 sp
->s_combo
[WHITE
].s
= MAXCOMBO
+ 1;
83 sp
->s_level
[BLACK
] = 255;
84 sp
->s_level
[WHITE
] = 255;
85 sp
->s_nforce
[BLACK
] = 0;
86 sp
->s_nforce
[WHITE
] = 0;
87 sp
->s_flags
&= ~(FFLAGALL
| MFLAGALL
);
90 memset(forcemap
, 0, sizeof(forcemap
));
92 /* compute new values */
97 /* find the spot with the highest value */
98 for (sp
= sp1
= sp2
= &board
[PT(T
,19)]; --sp
>= &board
[PT(A
,1)]; ) {
99 if (sp
->s_occ
!= EMPTY
)
101 if (debug
&& (sp
->s_combo
[BLACK
].c
.a
== 1 ||
102 sp
->s_combo
[WHITE
].c
.a
== 1)) {
103 sprintf(fmtbuf
, "- %s %x/%d %d %x/%d %d %d", stoc(sp
- board
),
104 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
106 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
111 /* pick the best black move */
112 if (better(sp
, sp1
, BLACK
))
114 /* pick the best white move */
115 if (better(sp
, sp2
, WHITE
))
120 sprintf(fmtbuf
, "B %s %x/%d %d %x/%d %d %d",
122 sp1
->s_combo
[BLACK
].s
, sp1
->s_level
[BLACK
],
123 sp1
->s_nforce
[BLACK
],
124 sp1
->s_combo
[WHITE
].s
, sp1
->s_level
[WHITE
],
125 sp1
->s_nforce
[WHITE
], sp1
->s_wval
);
127 sprintf(fmtbuf
, "W %s %x/%d %d %x/%d %d %d",
129 sp2
->s_combo
[WHITE
].s
, sp2
->s_level
[WHITE
],
130 sp2
->s_nforce
[WHITE
],
131 sp2
->s_combo
[BLACK
].s
, sp2
->s_level
[BLACK
],
132 sp2
->s_nforce
[BLACK
], sp2
->s_wval
);
135 * Check for more than one force that can't
136 * all be blocked with one move.
138 sp
= (us
== BLACK
) ? sp2
: sp1
;
140 if (sp
->s_combo
[!us
].c
.a
== 1 && !BIT_TEST(forcemap
, m
))
141 dlog("*** Can't be blocked");
144 Ocp
= &sp1
->s_combo
[BLACK
];
145 Tcp
= &sp2
->s_combo
[WHITE
];
147 Tcp
= &sp1
->s_combo
[BLACK
];
148 Ocp
= &sp2
->s_combo
[WHITE
];
154 * Block their combo only if we have to (i.e., if they are one move
155 * away from completing a force and we don't have a force that
156 * we can complete which takes fewer moves to win).
158 if (Tcp
->c
.a
<= 1 && (Ocp
->c
.a
> 1 ||
159 Tcp
->c
.a
+ Tcp
->c
.b
< Ocp
->c
.a
+ Ocp
->c
.b
))
160 return (sp2
- board
);
161 return (sp1
- board
);
165 * Return true if spot 'sp' is better than spot 'sp1' for color 'us'.
168 better(const struct spotstr
*sp
, const struct spotstr
*sp1
, int us
)
172 if (sp
->s_combo
[us
].s
< sp1
->s_combo
[us
].s
)
174 if (sp
->s_combo
[us
].s
!= sp1
->s_combo
[us
].s
)
176 if (sp
->s_level
[us
] < sp1
->s_level
[us
])
178 if (sp
->s_level
[us
] != sp1
->s_level
[us
])
180 if (sp
->s_nforce
[us
] > sp1
->s_nforce
[us
])
182 if (sp
->s_nforce
[us
] != sp1
->s_nforce
[us
])
188 if (BIT_TEST(forcemap
, s
) && !BIT_TEST(forcemap
, s1
))
190 if (!BIT_TEST(forcemap
, s
) && BIT_TEST(forcemap
, s1
))
192 if (sp
->s_combo
[them
].s
< sp1
->s_combo
[them
].s
)
194 if (sp
->s_combo
[them
].s
!= sp1
->s_combo
[them
].s
)
196 if (sp
->s_level
[them
] < sp1
->s_level
[them
])
198 if (sp
->s_level
[them
] != sp1
->s_level
[them
])
200 if (sp
->s_nforce
[them
] > sp1
->s_nforce
[them
])
202 if (sp
->s_nforce
[them
] != sp1
->s_nforce
[them
])
205 if (sp
->s_wval
> sp1
->s_wval
)
207 if (sp
->s_wval
!= sp1
->s_wval
)
213 return (random() & 1);
217 int curcolor
; /* implicit parameter to makecombo() */
218 int curlevel
; /* implicit parameter to makecombo() */
221 * Scan the sorted list of non-empty frames and
222 * update the minimum combo values for each empty spot.
223 * Also, try to combine frames to find more complex (chained) moves.
226 scanframes(int color
)
228 struct combostr
*cbp
, *ecbp
;
231 struct elist
*ep
, *nep
;
237 /* check for empty list of frames */
238 cbp
= sortframes
[color
];
239 if (cbp
== (struct combostr
*)0)
242 /* quick check for four in a row */
243 sp
= &board
[cbp
->c_vertex
];
244 cb
.s
= sp
->s_fval
[color
][d
= cbp
->c_dir
].s
;
247 for (i
= 5 + cb
.c
.b
; --i
>= 0; sp
+= d
) {
248 if (sp
->s_occ
!= EMPTY
)
250 sp
->s_combo
[color
].s
= cb
.s
;
251 sp
->s_level
[color
] = 1;
257 * Update the minimum combo value for each spot in the frame
258 * and try making all combinations of two frames intersecting at
264 sp
= &board
[cbp
->c_vertex
];
265 cp
= &sp
->s_fval
[color
][r
= cbp
->c_dir
];
269 * Since this is the first spot of an open ended
270 * frame, we treat it as a closed frame.
272 cb
.c
.a
= cp
->c
.a
+ 1;
274 if (cb
.s
< sp
->s_combo
[color
].s
) {
275 sp
->s_combo
[color
].s
= cb
.s
;
276 sp
->s_level
[color
] = 1;
279 * Try combining other frames that intersect
282 makecombo2(cbp
, sp
, 0, cb
.s
);
285 else if (color
!= nextcolor
)
286 memset(tmpmap
, 0, sizeof(tmpmap
));
293 for (; i
< 5; i
++, sp
+= d
) { /* for each spot */
294 if (sp
->s_occ
!= EMPTY
)
296 if (cp
->s
< sp
->s_combo
[color
].s
) {
297 sp
->s_combo
[color
].s
= cp
->s
;
298 sp
->s_level
[color
] = 1;
300 if (cp
->s
== 0x101) {
301 sp
->s_nforce
[color
]++;
302 if (color
!= nextcolor
) {
308 * Try combining other frames that intersect
311 makecombo2(cbp
, sp
, i
, cb
.s
);
313 if (cp
->s
== 0x101 && color
!= nextcolor
) {
315 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
317 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
318 forcemap
[i
] &= tmpmap
[i
];
321 /* mark frame as having been processed */
322 board
[cbp
->c_vertex
].s_flags
|= MFLAG
<< r
;
323 } while ((cbp
= cbp
->c_next
) != ecbp
);
326 * Try to make new 3rd level combos, 4th level, etc.
327 * Limit the search depth early in the game.
330 while (d
<= ((movenum
+ 1) >> 1) && combolen
> n
) {
332 sprintf(fmtbuf
, "%cL%d %d %d %d", "BW"[color
],
333 d
, combolen
- n
, combocnt
, elistcnt
);
342 /* scan for combos at empty spots */
343 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
344 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
346 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
347 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
348 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
349 sp
->s_level
[color
] = cbp
->c_nframes
;
350 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
351 sp
->s_level
[color
] = cbp
->c_nframes
;
357 sp
->s_empty
= (struct elist
*)0;
358 for (ep
= sp
->s_nempty
; ep
; ep
= nep
) {
360 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
361 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
362 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
363 sp
->s_level
[color
] = cbp
->c_nframes
;
364 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
365 sp
->s_level
[color
] = cbp
->c_nframes
;
371 sp
->s_nempty
= (struct elist
*)0;
374 /* remove old combos */
375 if ((cbp
= sortcombos
) != (struct combostr
*)0) {
376 struct combostr
*ncbp
;
384 } while ((cbp
= ncbp
) != ecbp
);
385 sortcombos
= (struct combostr
*)0;
391 sprintf(fmtbuf
, "scanframes: %c combocnt %d", "BW"[color
],
397 sprintf(fmtbuf
, "scanframes: %c elistcnt %d", "BW"[color
],
406 * Compute all level 2 combos of frames intersecting spot 'osp'
407 * within the frame 'ocbp' and combo value 's'.
410 makecombo2(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
413 struct combostr
*ncbp
;
415 int baseB
, fcnt
, emask
, bmask
, n
;
416 union comboval ocb
, fcb
;
417 struct combostr
**scbpp
, *fcbp
;
419 /* try to combine a new frame with those found so far */
421 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
423 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
424 for (r
= 4; --r
>= 0; ) { /* for each direction */
425 /* don't include frames that overlap in the same direction */
426 if (r
== ocbp
->c_dir
)
430 * Frame A combined with B is the same value as B combined with A
431 * so skip frames that have already been processed (MFLAG).
432 * Also skip blocked frames (BFLAG) and frames that are <1,x>
433 * since combining another frame with it isn't valid.
435 bmask
= (BFLAG
| FFLAG
| MFLAG
) << r
;
437 for (f
= 0; f
< 5; f
++, fsp
-= d
) { /* for each frame */
438 if (fsp
->s_occ
== BORDER
)
440 if (fsp
->s_flags
& bmask
)
443 /* don't include frames of the wrong color */
444 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
449 * Get the combo value for this frame.
450 * If this is the end point of the frame,
451 * use the closed ended value for the frame.
453 if ((f
== 0 && fcb
.c
.b
) || fcb
.s
== 0x101) {
458 /* compute combo value */
459 c
= fcb
.c
.a
+ ocb
.c
.a
- 3;
462 n
= fcb
.c
.a
+ fcb
.c
.b
- 1;
466 /* make a new combo! */
467 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
468 2 * sizeof(struct combostr
*));
470 panic("Out of memory!");
471 scbpp
= (struct combostr
**)(ncbp
+ 1);
472 fcbp
= fsp
->s_frame
[r
];
480 ncbp
->c_combo
.c
.a
= c
;
481 ncbp
->c_combo
.c
.b
= n
;
482 ncbp
->c_link
[0] = ocbp
;
483 ncbp
->c_link
[1] = fcbp
;
484 ncbp
->c_linkv
[0].s
= ocb
.s
;
485 ncbp
->c_linkv
[1].s
= fcb
.s
;
486 ncbp
->c_voff
[0] = off
;
488 ncbp
->c_vertex
= osp
- board
;
491 ncbp
->c_frameindex
= 0;
492 ncbp
->c_flags
= (ocb
.c
.b
) ? C_OPEN_0
: 0;
494 ncbp
->c_flags
|= C_OPEN_1
;
495 ncbp
->c_framecnt
[0] = fcnt
;
496 ncbp
->c_emask
[0] = emask
;
497 ncbp
->c_framecnt
[1] = fcb
.c
.a
- 2;
498 ncbp
->c_emask
[1] = ncbp
->c_framecnt
[1] ?
499 ((fcb
.c
.b
? 0x1E : 0x1F) & ~(1 << f
)) : 0;
502 if ((c
== 1 && debug
> 1) || debug
> 3) {
503 sprintf(fmtbuf
, "%c c %d %d m %x %x o %d %d",
505 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
506 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
507 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
509 printcombo(ncbp
, fmtbuf
);
513 /* record the empty spots that will complete this combo */
516 /* add the new combo to the end of the list */
517 appendcombo(ncbp
, curcolor
);
519 updatecombo(ncbp
, curcolor
);
524 if (c
== 1 && debug
> 1 || debug
> 5) {
536 * Scan the sorted list of frames and try to add a frame to
537 * combinations of 'level' number of frames.
542 struct combostr
*cbp
, *ecbp
;
543 struct spotstr
*sp
, *fsp
;
544 struct elist
*ep
, *nep
;
546 struct combostr
**cbpp
, *pcbp
;
547 union comboval fcb
, cb
;
551 /* scan for combos at empty spots */
553 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
554 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
556 if (cbp
->c_combo
.s
<= sp
->s_combo
[i
].s
) {
557 if (cbp
->c_combo
.s
!= sp
->s_combo
[i
].s
) {
558 sp
->s_combo
[i
].s
= cbp
->c_combo
.s
;
559 sp
->s_level
[i
] = cbp
->c_nframes
;
560 } else if (cbp
->c_nframes
< sp
->s_level
[i
])
561 sp
->s_level
[i
] = cbp
->c_nframes
;
567 sp
->s_empty
= sp
->s_nempty
;
568 sp
->s_nempty
= (struct elist
*)0;
571 /* try to add frames to the uncompleted combos at level curlevel */
572 cbp
= ecbp
= sortframes
[curcolor
];
574 fsp
= &board
[cbp
->c_vertex
];
576 /* skip frames that are part of a <1,x> combo */
577 if (fsp
->s_flags
& (FFLAG
<< r
))
581 * Don't include <1,x> combo frames,
582 * treat it as a closed three in a row instead.
584 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
589 * If this is an open ended frame, use
590 * the combo value with the end closed.
592 if (fsp
->s_occ
== EMPTY
) {
594 cb
.c
.a
= fcb
.c
.a
+ 1;
598 makecombo(cbp
, fsp
, 0, cb
.s
);
602 * The next four spots are handled the same for both
603 * open and closed ended frames.
607 for (i
= 1; i
< 5; i
++, sp
+= d
) {
608 if (sp
->s_occ
!= EMPTY
)
610 makecombo(cbp
, sp
, i
, fcb
.s
);
612 } while ((cbp
= cbp
->c_next
) != ecbp
);
614 /* put all the combos in the hash list on the sorted list */
615 cbpp
= &hashcombos
[FAREA
];
618 if (cbp
== (struct combostr
*)0)
620 *cbpp
= (struct combostr
*)0;
622 if (ecbp
== (struct combostr
*)0)
625 /* append to sort list */
628 ecbp
->c_prev
= cbp
->c_prev
;
629 cbp
->c_prev
->c_next
= ecbp
;
632 } while (cbpp
!= hashcombos
);
636 * Compute all level N combos of frames intersecting spot 'osp'
637 * within the frame 'ocbp' and combo value 's'.
640 makecombo(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
642 struct combostr
*cbp
, *ncbp
;
647 struct combostr
**scbpp
;
648 int baseB
, fcnt
, emask
, verts
;
650 struct overlap_info vertices
[1];
653 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
655 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
656 for (ep
= osp
->s_empty
; ep
; ep
= ep
->e_next
) {
657 /* check for various kinds of overlap */
659 verts
= checkframes(cbp
, ocbp
, osp
, s
, vertices
);
663 /* check to see if this frame forms a valid loop */
665 sp
= &board
[vertices
[0].o_intersect
];
667 if (sp
->s_occ
!= EMPTY
) {
668 sprintf(fmtbuf
, "loop: %c %s", "BW"[curcolor
],
675 * It is a valid loop if the intersection spot
676 * of the frame we are trying to attach is one
677 * of the completion spots of the combostr
678 * we are trying to attach the frame to.
680 for (nep
= sp
->s_empty
; nep
; nep
= nep
->e_next
) {
681 if (nep
->e_combo
== cbp
)
683 if (nep
->e_combo
->c_nframes
< cbp
->c_nframes
)
686 /* frame overlaps but not at a valid spot */
692 /* compute the first half of the combo value */
693 c
= cbp
->c_combo
.c
.a
+ ocb
.c
.a
- verts
- 3;
697 /* compute the second half of the combo value */
698 n
= ep
->e_fval
.c
.a
+ ep
->e_fval
.c
.b
- 1;
702 /* make a new combo! */
703 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
704 (cbp
->c_nframes
+ 1) * sizeof(struct combostr
*));
706 panic("Out of memory!");
707 scbpp
= (struct combostr
**)(ncbp
+ 1);
708 if (sortcombo(scbpp
, (struct combostr
**)(cbp
+ 1), ocbp
)) {
714 ncbp
->c_combo
.c
.a
= c
;
715 ncbp
->c_combo
.c
.b
= n
;
716 ncbp
->c_link
[0] = cbp
;
717 ncbp
->c_link
[1] = ocbp
;
718 ncbp
->c_linkv
[1].s
= ocb
.s
;
719 ncbp
->c_voff
[1] = off
;
720 ncbp
->c_vertex
= osp
- board
;
721 ncbp
->c_nframes
= cbp
->c_nframes
+ 1;
722 ncbp
->c_flags
= ocb
.c
.b
? C_OPEN_1
: 0;
723 ncbp
->c_frameindex
= ep
->e_frameindex
;
725 * Update the completion spot mask of the frame we
726 * are attaching 'ocbp' to so the intersection isn't
729 ncbp
->c_framecnt
[0] = ep
->e_framecnt
;
730 ncbp
->c_emask
[0] = ep
->e_emask
;
732 ncbp
->c_flags
|= C_LOOP
;
733 ncbp
->c_dir
= vertices
[0].o_frameindex
;
734 ncbp
->c_framecnt
[1] = fcnt
- 1;
735 if (ncbp
->c_framecnt
[1]) {
736 n
= (vertices
[0].o_intersect
- ocbp
->c_vertex
) /
738 ncbp
->c_emask
[1] = emask
& ~(1 << n
);
740 ncbp
->c_emask
[1] = 0;
741 ncbp
->c_voff
[0] = vertices
[0].o_off
;
744 ncbp
->c_framecnt
[1] = fcnt
;
745 ncbp
->c_emask
[1] = emask
;
746 ncbp
->c_voff
[0] = ep
->e_off
;
749 if ((c
== 1 && debug
> 1) || debug
> 3) {
750 sprintf(fmtbuf
, "%c v%d i%d d%d c %d %d m %x %x o %d %d",
751 "bw"[curcolor
], verts
, ncbp
->c_frameindex
, ncbp
->c_dir
,
752 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
753 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
754 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
756 printcombo(ncbp
, fmtbuf
);
760 /* record the empty spots that will complete this combo */
764 /* update board values */
765 updatecombo(ncbp
, curcolor
);
768 if (c
== 1 && debug
> 1 || debug
> 4) {
779 struct elist einfo
[MAXDEPTH
];
780 struct combostr
*ecombo
[MAXDEPTH
]; /* separate from elist to save space */
783 * Add the combostr 'ocbp' to the empty spots list for each empty spot
784 * in 'ocbp' that will complete the combo.
787 makeempty(struct combostr
*ocbp
)
789 struct combostr
*cbp
, *tcbp
, **cbpp
;
790 struct elist
*ep
, *nep
;
792 int s
, d
, m
, emask
, i
;
796 sprintf(fmtbuf
, "E%c ", "bw"[curcolor
]);
797 printcombo(ocbp
, fmtbuf
+ 3);
801 /* should never happen but check anyway */
802 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
806 * The lower level combo can be pointed to by more than one
807 * higher level 'struct combostr' so we can't modify the
808 * lower level. Therefore, higher level combos store the
809 * real mask of the lower level frame in c_emask[0] and the
810 * frame number in c_frameindex.
812 * First we traverse the tree from top to bottom and save the
813 * connection info. Then we traverse the tree from bottom to
814 * top overwriting lower levels with the newer emask information.
816 ep
= &einfo
[nframes
];
817 cbpp
= &ecombo
[nframes
];
818 for (cbp
= ocbp
; (tcbp
= cbp
->c_link
[1]) != NULL
;
819 cbp
= cbp
->c_link
[0]) {
822 *--cbpp
= cbp
->c_link
[1];
823 ep
->e_off
= cbp
->c_voff
[1];
824 ep
->e_frameindex
= cbp
->c_frameindex
;
825 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
826 ep
->e_framecnt
= cbp
->c_framecnt
[1];
827 ep
->e_emask
= cbp
->c_emask
[1];
832 *--cbpp
= cbp
->c_link
[0];
833 ep
->e_off
= cbp
->c_voff
[0];
834 ep
->e_frameindex
= 0;
835 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
836 ep
->e_framecnt
= cbp
->c_framecnt
[0];
837 ep
->e_emask
= cbp
->c_emask
[0];
839 /* now update the emask info */
841 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
843 nep
= &einfo
[ep
->e_frameindex
];
844 nep
->e_framecnt
= cbp
->c_framecnt
[0];
845 nep
->e_emask
= cbp
->c_emask
[0];
847 if (cbp
->c_flags
& C_LOOP
) {
850 * Account for the fact that this frame connects
851 * to a previous one (thus forming a loop).
853 nep
= &einfo
[cbp
->c_dir
];
854 if (--nep
->e_framecnt
)
855 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
862 * We only need to update the emask values of "complete" loops
863 * to include the intersection spots.
865 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
866 /* process loops from the top down */
867 ep
= &einfo
[nframes
];
871 if (!(cbp
->c_flags
& C_LOOP
))
875 * Update the emask values to include the
876 * intersection spots.
878 nep
= &einfo
[cbp
->c_dir
];
880 nep
->e_emask
= 1 << cbp
->c_voff
[0];
882 ep
->e_emask
= 1 << ep
->e_off
;
883 ep
= &einfo
[ep
->e_frameindex
];
886 ep
->e_emask
= 1 << ep
->e_off
;
887 ep
= &einfo
[ep
->e_frameindex
];
889 } while (ep
!= einfo
);
892 /* check all the frames for completion spots */
893 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
894 /* skip this frame if there are no incomplete spots in it */
895 if ((emask
= ep
->e_emask
) == 0)
898 sp
= &board
[cbp
->c_vertex
];
900 for (s
= 0, m
= 1; s
< 5; s
++, sp
+= d
, m
<<= 1) {
901 if (sp
->s_occ
!= EMPTY
|| !(emask
& m
))
904 /* add the combo to the list of empty spots */
905 nep
= (struct elist
*)malloc(sizeof(struct elist
));
907 panic("Out of memory!");
910 nep
->e_frameindex
= i
;
911 if (ep
->e_framecnt
> 1) {
912 nep
->e_framecnt
= ep
->e_framecnt
- 1;
913 nep
->e_emask
= emask
& ~m
;
918 nep
->e_fval
.s
= ep
->e_fval
.s
;
920 sprintf(fmtbuf
, "e %s o%d i%d c%d m%x %x",
930 /* sort by the number of frames in the combo */
931 nep
->e_next
= sp
->s_nempty
;
939 * Update the board value based on the combostr.
940 * This is called only if 'cbp' is a <1,x> combo.
941 * We handle things differently depending on whether the next move
942 * would be trying to "complete" the combo or trying to block it.
945 updatecombo(struct combostr
*cbp
, int color
)
948 struct combostr
*tcbp
;
950 int nframes
, flags
, s
;
954 /* save the top level value for the whole combo */
955 cb
.c
.a
= cbp
->c_combo
.c
.a
;
956 nframes
= cbp
->c_nframes
;
958 if (color
!= nextcolor
)
959 memset(tmpmap
, 0, sizeof(tmpmap
));
961 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
962 flags
= cbp
->c_flags
;
963 cb
.c
.b
= cbp
->c_combo
.c
.b
;
964 if (color
== nextcolor
) {
965 /* update the board value for the vertex */
966 sp
= &board
[cbp
->c_vertex
];
967 sp
->s_nforce
[color
]++;
968 if (cb
.s
<= sp
->s_combo
[color
].s
) {
969 if (cb
.s
!= sp
->s_combo
[color
].s
) {
970 sp
->s_combo
[color
].s
= cb
.s
;
971 sp
->s_level
[color
] = nframes
;
972 } else if (nframes
< sp
->s_level
[color
])
973 sp
->s_level
[color
] = nframes
;
976 /* update the board values for each spot in frame */
977 sp
= &board
[s
= tcbp
->c_vertex
];
979 i
= (flags
& C_OPEN_1
) ? 6 : 5;
980 for (; --i
>= 0; sp
+= d
, s
+= d
) {
981 if (sp
->s_occ
!= EMPTY
)
983 sp
->s_nforce
[color
]++;
984 if (cb
.s
<= sp
->s_combo
[color
].s
) {
985 if (cb
.s
!= sp
->s_combo
[color
].s
) {
986 sp
->s_combo
[color
].s
= cb
.s
;
987 sp
->s_level
[color
] = nframes
;
988 } else if (nframes
< sp
->s_level
[color
])
989 sp
->s_level
[color
] = nframes
;
995 /* mark the frame as being part of a <1,x> combo */
996 board
[tcbp
->c_vertex
].s_flags
|= FFLAG
<< tcbp
->c_dir
;
999 if (color
!= nextcolor
) {
1000 /* update the board values for each spot in frame */
1001 sp
= &board
[s
= cbp
->c_vertex
];
1003 i
= (flags
& C_OPEN_0
) ? 6 : 5;
1004 for (; --i
>= 0; sp
+= d
, s
+= d
) {
1005 if (sp
->s_occ
!= EMPTY
)
1007 sp
->s_nforce
[color
]++;
1008 if (cb
.s
<= sp
->s_combo
[color
].s
) {
1009 if (cb
.s
!= sp
->s_combo
[color
].s
) {
1010 sp
->s_combo
[color
].s
= cb
.s
;
1011 sp
->s_level
[color
] = nframes
;
1012 } else if (nframes
< sp
->s_level
[color
])
1013 sp
->s_level
[color
] = nframes
;
1018 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
1020 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
1021 forcemap
[i
] &= tmpmap
[i
];
1026 /* mark the frame as being part of a <1,x> combo */
1027 board
[cbp
->c_vertex
].s_flags
|= FFLAG
<< cbp
->c_dir
;
1031 * Add combo to the end of the list.
1034 appendcombo(struct combostr
*cbp
, int color __unused
)
1036 struct combostr
*pcbp
, *ncbp
;
1040 if (ncbp
== (struct combostr
*)0) {
1046 pcbp
= ncbp
->c_prev
;
1054 * Return zero if it is valid to combine frame 'fcbp' with the frames
1055 * in 'cbp' and forms a linked chain of frames (i.e., a tree; no loops).
1056 * Return positive if combining frame 'fcbp' to the frames in 'cbp'
1057 * would form some kind of valid loop. Also return the intersection spots
1058 * in 'vertices[]' beside the known intersection at spot 'osp'.
1059 * Return -1 if 'fcbp' should not be combined with 'cbp'.
1060 * 's' is the combo value for frame 'fcpb'.
1063 checkframes(struct combostr
*cbp
, struct combostr
*fcbp
, struct spotstr
*osp
,
1064 int s
, struct overlap_info
*vertices
)
1066 struct combostr
*tcbp
, *lcbp
;
1067 int i
, n
, mask
, flags
, verts
, loop
, myindex
, fcnt
;
1079 myindex
= cbp
->c_nframes
;
1080 n
= (fcbp
- frames
) * FAREA
;
1084 * i == which overlap bit to test based on whether 'fcbp' is
1085 * an open or closed frame.
1088 for (; (tcbp
= cbp
->c_link
[1]) != NULL
;
1089 lcbp
= cbp
, cbp
= cbp
->c_link
[0]) {
1091 return (-1); /* fcbp is already included */
1093 /* check for intersection of 'tcbp' with 'fcbp' */
1095 mask
= str
[tcbp
- frames
];
1096 flags
= cbp
->c_flags
;
1097 n
= i
+ ((flags
& C_OPEN_1
) != 0);
1098 if (mask
& (1 << n
)) {
1100 * The two frames are not independent if they
1101 * both lie in the same line and intersect at
1102 * more than one point.
1104 if (tcbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1107 * If this is not the spot we are attaching
1108 * 'fcbp' to and it is a reasonable intersection
1109 * spot, then there might be a loop.
1111 n
= ip
[tcbp
- frames
];
1112 if (osp
!= &board
[n
]) {
1113 /* check to see if this is a valid loop */
1116 if (fcnt
== 0 || cbp
->c_framecnt
[1] == 0)
1119 * Check to be sure the intersection is not
1120 * one of the end points if it is an open
1123 if ((flags
& C_OPEN_1
) &&
1124 (n
== tcbp
->c_vertex
||
1125 n
== tcbp
->c_vertex
+ 5 * dd
[tcbp
->c_dir
]))
1126 return (-1); /* invalid overlap */
1128 (n
== fcbp
->c_vertex
||
1129 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1130 return (-1); /* invalid overlap */
1132 vertices
->o_intersect
= n
;
1133 vertices
->o_fcombo
= cbp
;
1134 vertices
->o_link
= 1;
1135 vertices
->o_off
= (n
- tcbp
->c_vertex
) /
1137 vertices
->o_frameindex
= myindex
;
1141 n
= i
+ ((flags
& C_OPEN_0
) != 0);
1144 return (-1); /* fcbp is already included */
1146 /* check for intersection of 'cbp' with 'fcbp' */
1147 mask
= str
[cbp
- frames
];
1148 if (mask
& (1 << n
)) {
1150 * The two frames are not independent if they
1151 * both lie in the same line and intersect at
1152 * more than one point.
1154 if (cbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1157 * If this is not the spot we are attaching
1158 * 'fcbp' to and it is a reasonable intersection
1159 * spot, then there might be a loop.
1161 n
= ip
[cbp
- frames
];
1162 if (osp
!= &board
[n
]) {
1163 /* check to see if this is a valid loop */
1166 if (fcnt
== 0 || lcbp
->c_framecnt
[0] == 0)
1169 * Check to be sure the intersection is not
1170 * one of the end points if it is an open
1173 if ((flags
& C_OPEN_0
) &&
1174 (n
== cbp
->c_vertex
||
1175 n
== cbp
->c_vertex
+ 5 * dd
[cbp
->c_dir
]))
1176 return (-1); /* invalid overlap */
1178 (n
== fcbp
->c_vertex
||
1179 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1180 return (-1); /* invalid overlap */
1182 vertices
->o_intersect
= n
;
1183 vertices
->o_fcombo
= lcbp
;
1184 vertices
->o_link
= 0;
1185 vertices
->o_off
= (n
- cbp
->c_vertex
) /
1187 vertices
->o_frameindex
= 0;
1195 * Merge sort the frame 'fcbp' and the sorted list of frames 'cbpp' and
1196 * store the result in 'scbpp'. 'curlevel' is the size of the 'cbpp' array.
1197 * Return true if this list of frames is already in the hash list.
1198 * Otherwise, add the new combo to the hash list.
1201 sortcombo(struct combostr
**scbpp
, struct combostr
**cbpp
,
1202 struct combostr
*fcbp
)
1204 struct combostr
**spp
, **cpp
;
1205 struct combostr
*cbp
, *ecbp
;
1212 sprintf(fmtbuf
, "sortc: %s%c l%d", stoc(fcbp
->c_vertex
),
1213 pdir
[fcbp
->c_dir
], curlevel
);
1216 for (cpp
= cbpp
; cpp
< cbpp
+ curlevel
; cpp
++) {
1217 sprintf(str
, " %s%c", stoc((*cpp
)->c_vertex
),
1218 pdir
[(*cpp
)->c_dir
]);
1225 /* first build the new sorted list */
1228 cpp
= cbpp
+ curlevel
;
1235 while (cpp
-- != cbpp
);
1239 } while (cpp
!= cbpp
);
1243 /* now check to see if this list of frames has already been seen */
1244 cbp
= hashcombos
[inx
= *scbpp
- frames
];
1245 if (cbp
== (struct combostr
*)0) {
1247 * Easy case, this list hasn't been seen.
1248 * Add it to the hash list.
1250 fcbp
= (struct combostr
*)
1251 ((char *)scbpp
- sizeof(struct combostr
));
1252 hashcombos
[inx
] = fcbp
;
1253 fcbp
->c_next
= fcbp
->c_prev
= fcbp
;
1258 cbpp
= (struct combostr
**)(cbp
+ 1);
1261 cbpp
++; /* first frame is always the same */
1263 if (*--spp
!= *--cpp
)
1265 } while (cpp
!= cbpp
);
1266 /* we found a match */
1271 sprintf(fmtbuf
, "sort1: n%d", n
);
1274 for (cpp
= scbpp
; cpp
< scbpp
+ n
; cpp
++) {
1275 sprintf(str
, " %s%c", stoc((*cpp
)->c_vertex
),
1276 pdir
[(*cpp
)->c_dir
]);
1280 printcombo(cbp
, fmtbuf
);
1284 for (cpp
= cbpp
; cpp
< cbpp
+ n
; cpp
++) {
1285 sprintf(str
, " %s%c", stoc((*cpp
)->c_vertex
),
1286 pdir
[(*cpp
)->c_dir
]);
1295 } while ((cbp
= cbp
->c_next
) != ecbp
);
1297 * This list of frames hasn't been seen.
1298 * Add it to the hash list.
1301 fcbp
= (struct combostr
*)((char *)scbpp
- sizeof(struct combostr
));
1303 fcbp
->c_prev
= ecbp
;
1305 ecbp
->c_next
= fcbp
;
1310 * Print the combo into string 'str'.
1313 printcombo(struct combostr
*cbp
, char *str
)
1315 struct combostr
*tcbp
;
1317 sprintf(str
, "%x/%d", cbp
->c_combo
.s
, cbp
->c_nframes
);
1319 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1320 sprintf(str
, " %s%c%x", stoc(tcbp
->c_vertex
), pdir
[tcbp
->c_dir
],
1324 sprintf(str
, " %s%c", stoc(cbp
->c_vertex
), pdir
[cbp
->c_dir
]);
1329 markcombo(struct combostr
*ocbp
)
1331 struct combostr
*cbp
, *tcbp
, **cbpp
;
1332 struct elist
*ep
, *nep
, **epp
;
1336 int r
, n
, flags
, cmask
, omask
;
1338 /* should never happen but check anyway */
1339 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
1343 * The lower level combo can be pointed to by more than one
1344 * higher level 'struct combostr' so we can't modify the
1345 * lower level. Therefore, higher level combos store the
1346 * real mask of the lower level frame in c_emask[0] and the
1347 * frame number in c_frameindex.
1349 * First we traverse the tree from top to bottom and save the
1350 * connection info. Then we traverse the tree from bottom to
1351 * top overwriting lower levels with the newer emask information.
1353 ep
= &einfo
[nframes
];
1354 cbpp
= &ecombo
[nframes
];
1355 for (cbp
= ocbp
; tcbp
= cbp
->c_link
[1]; cbp
= cbp
->c_link
[0]) {
1358 *--cbpp
= cbp
->c_link
[1];
1359 ep
->e_off
= cbp
->c_voff
[1];
1360 ep
->e_frameindex
= cbp
->c_frameindex
;
1361 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
1362 ep
->e_framecnt
= cbp
->c_framecnt
[1];
1363 ep
->e_emask
= cbp
->c_emask
[1];
1368 *--cbpp
= cbp
->c_link
[0];
1369 ep
->e_off
= cbp
->c_voff
[0];
1370 ep
->e_frameindex
= 0;
1371 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
1372 ep
->e_framecnt
= cbp
->c_framecnt
[0];
1373 ep
->e_emask
= cbp
->c_emask
[0];
1375 /* now update the emask info */
1377 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
1379 nep
= &einfo
[ep
->e_frameindex
];
1380 nep
->e_framecnt
= cbp
->c_framecnt
[0];
1381 nep
->e_emask
= cbp
->c_emask
[0];
1383 if (cbp
->c_flags
& C_LOOP
) {
1386 * Account for the fact that this frame connects
1387 * to a previous one (thus forming a loop).
1389 nep
= &einfo
[cbp
->c_dir
];
1390 if (--nep
->e_framecnt
)
1391 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
1398 * We only need to update the emask values of "complete" loops
1399 * to include the intersection spots.
1401 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
1402 /* process loops from the top down */
1403 ep
= &einfo
[nframes
];
1407 if (!(cbp
->c_flags
& C_LOOP
))
1411 * Update the emask values to include the
1412 * intersection spots.
1414 nep
= &einfo
[cbp
->c_dir
];
1415 nep
->e_framecnt
= 1;
1416 nep
->e_emask
= 1 << cbp
->c_voff
[0];
1418 ep
->e_emask
= 1 << ep
->e_off
;
1419 ep
= &einfo
[ep
->e_frameindex
];
1422 ep
->e_emask
= 1 << ep
->e_off
;
1423 ep
= &einfo
[ep
->e_frameindex
];
1425 } while (ep
!= einfo
);
1428 /* mark all the frames with the completion spots */
1429 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
1432 sp
= &board
[cbp
->c_vertex
];
1433 d
= dd
[s
= cbp
->c_dir
];
1435 omask
= (IFLAG
| CFLAG
) << s
;
1436 s
= ep
->e_fval
.c
.b
? 6 : 5;
1437 for (; --s
>= 0; sp
+= d
, m
>>= 1)
1438 sp
->s_flags
|= (m
& 1) ? omask
: cmask
;
1443 clearcombo(struct combostr
*cbp
, int open
)
1446 struct combostr
*tcbp
;
1449 for (; tcbp
= cbp
->c_link
[1]; cbp
= cbp
->c_link
[0]) {
1450 clearcombo(tcbp
, cbp
->c_flags
& C_OPEN_1
);
1451 open
= cbp
->c_flags
& C_OPEN_0
;
1453 sp
= &board
[cbp
->c_vertex
];
1454 d
= dd
[n
= cbp
->c_dir
];
1455 mask
= ~((IFLAG
| CFLAG
) << n
);
1457 for (; --n
>= 0; sp
+= d
)
1458 sp
->s_flags
&= mask
;
1462 list_eq(struct combostr
**scbpp
, struct combostr
**cbpp
, int n
)
1464 struct combostr
**spp
, **cpp
;
1469 if (*--spp
!= *--cpp
)
1471 } while (cpp
!= cbpp
);
1472 /* we found a match */