]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/pickmove.c
1 /* $NetBSD: pickmove.c,v 1.20 2010/03/29 05:16:08 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.20 2010/03/29 05:16:08 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 static struct combostr
*hashcombos
[FAREA
];/* hash list for finding duplicates */
59 static struct combostr
*sortcombos
; /* combos at higher levels */
60 static int combolen
; /* number of combos in sortcombos */
61 static int nextcolor
; /* color of next move */
62 static int elistcnt
; /* count of struct elist allocated */
63 static int combocnt
; /* count of struct combostr allocated */
64 static int forcemap
[MAPSZ
]; /* map for blocking <1,x> combos */
65 static int tmpmap
[MAPSZ
]; /* map for blocking <1,x> combos */
66 static int nforce
; /* count of opponent <1,x> combos */
68 static int better(const struct spotstr
*, const struct spotstr
*, int);
69 static void scanframes(int);
70 static void makecombo2(struct combostr
*, struct spotstr
*, int, int);
71 static void addframes(int);
72 static void makecombo(struct combostr
*, struct spotstr
*, int, int);
73 static void appendcombo(struct combostr
*, int);
74 static void updatecombo(struct combostr
*, int);
75 static void makeempty(struct combostr
*);
76 static int checkframes(struct combostr
*, struct combostr
*, struct spotstr
*,
77 int, struct overlap_info
*);
78 static int sortcombo(struct combostr
**, struct combostr
**, struct combostr
*);
79 static void printcombo(struct combostr
*, char *, size_t);
84 struct spotstr
*sp
, *sp1
, *sp2
;
85 union comboval
*Ocp
, *Tcp
;
88 /* first move is easy */
92 /* initialize all the board values */
93 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
94 sp
->s_combo
[BLACK
].s
= MAXCOMBO
+ 1;
95 sp
->s_combo
[WHITE
].s
= MAXCOMBO
+ 1;
96 sp
->s_level
[BLACK
] = 255;
97 sp
->s_level
[WHITE
] = 255;
98 sp
->s_nforce
[BLACK
] = 0;
99 sp
->s_nforce
[WHITE
] = 0;
100 sp
->s_flags
&= ~(FFLAGALL
| MFLAGALL
);
103 memset(forcemap
, 0, sizeof(forcemap
));
105 /* compute new values */
110 /* find the spot with the highest value */
111 for (sp
= sp1
= sp2
= &board
[PT(T
,19)]; --sp
>= &board
[PT(A
,1)]; ) {
112 if (sp
->s_occ
!= EMPTY
)
114 if (debug
&& (sp
->s_combo
[BLACK
].c
.a
== 1 ||
115 sp
->s_combo
[WHITE
].c
.a
== 1)) {
116 debuglog("- %s %x/%d %d %x/%d %d %d", stoc(sp
- board
),
117 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
119 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
123 /* pick the best black move */
124 if (better(sp
, sp1
, BLACK
))
126 /* pick the best white move */
127 if (better(sp
, sp2
, WHITE
))
132 debuglog("B %s %x/%d %d %x/%d %d %d",
134 sp1
->s_combo
[BLACK
].s
, sp1
->s_level
[BLACK
],
135 sp1
->s_nforce
[BLACK
],
136 sp1
->s_combo
[WHITE
].s
, sp1
->s_level
[WHITE
],
137 sp1
->s_nforce
[WHITE
], sp1
->s_wval
);
138 debuglog("W %s %x/%d %d %x/%d %d %d",
140 sp2
->s_combo
[WHITE
].s
, sp2
->s_level
[WHITE
],
141 sp2
->s_nforce
[WHITE
],
142 sp2
->s_combo
[BLACK
].s
, sp2
->s_level
[BLACK
],
143 sp2
->s_nforce
[BLACK
], sp2
->s_wval
);
145 * Check for more than one force that can't
146 * all be blocked with one move.
148 sp
= (us
== BLACK
) ? sp2
: sp1
;
150 if (sp
->s_combo
[!us
].c
.a
== 1 && !BIT_TEST(forcemap
, m
))
151 debuglog("*** Can't be blocked");
154 Ocp
= &sp1
->s_combo
[BLACK
];
155 Tcp
= &sp2
->s_combo
[WHITE
];
157 Tcp
= &sp1
->s_combo
[BLACK
];
158 Ocp
= &sp2
->s_combo
[WHITE
];
164 * Block their combo only if we have to (i.e., if they are one move
165 * away from completing a force and we don't have a force that
166 * we can complete which takes fewer moves to win).
168 if (Tcp
->c
.a
<= 1 && (Ocp
->c
.a
> 1 ||
169 Tcp
->c
.a
+ Tcp
->c
.b
< Ocp
->c
.a
+ Ocp
->c
.b
))
170 return (sp2
- board
);
171 return (sp1
- board
);
175 * Return true if spot 'sp' is better than spot 'sp1' for color 'us'.
178 better(const struct spotstr
*sp
, const struct spotstr
*sp1
, int us
)
182 if (sp
->s_combo
[us
].s
< sp1
->s_combo
[us
].s
)
184 if (sp
->s_combo
[us
].s
!= sp1
->s_combo
[us
].s
)
186 if (sp
->s_level
[us
] < sp1
->s_level
[us
])
188 if (sp
->s_level
[us
] != sp1
->s_level
[us
])
190 if (sp
->s_nforce
[us
] > sp1
->s_nforce
[us
])
192 if (sp
->s_nforce
[us
] != sp1
->s_nforce
[us
])
198 if (BIT_TEST(forcemap
, s
) && !BIT_TEST(forcemap
, s1
))
200 if (!BIT_TEST(forcemap
, s
) && BIT_TEST(forcemap
, s1
))
202 if (sp
->s_combo
[them
].s
< sp1
->s_combo
[them
].s
)
204 if (sp
->s_combo
[them
].s
!= sp1
->s_combo
[them
].s
)
206 if (sp
->s_level
[them
] < sp1
->s_level
[them
])
208 if (sp
->s_level
[them
] != sp1
->s_level
[them
])
210 if (sp
->s_nforce
[them
] > sp1
->s_nforce
[them
])
212 if (sp
->s_nforce
[them
] != sp1
->s_nforce
[them
])
215 if (sp
->s_wval
> sp1
->s_wval
)
217 if (sp
->s_wval
!= sp1
->s_wval
)
220 return (random() & 1);
223 static int curcolor
; /* implicit parameter to makecombo() */
224 static int curlevel
; /* implicit parameter to makecombo() */
227 * Scan the sorted list of non-empty frames and
228 * update the minimum combo values for each empty spot.
229 * Also, try to combine frames to find more complex (chained) moves.
232 scanframes(int color
)
234 struct combostr
*cbp
, *ecbp
;
237 struct elist
*ep
, *nep
;
243 /* check for empty list of frames */
244 cbp
= sortframes
[color
];
245 if (cbp
== (struct combostr
*)0)
248 /* quick check for four in a row */
249 sp
= &board
[cbp
->c_vertex
];
250 cb
.s
= sp
->s_fval
[color
][d
= cbp
->c_dir
].s
;
253 for (i
= 5 + cb
.c
.b
; --i
>= 0; sp
+= d
) {
254 if (sp
->s_occ
!= EMPTY
)
256 sp
->s_combo
[color
].s
= cb
.s
;
257 sp
->s_level
[color
] = 1;
263 * Update the minimum combo value for each spot in the frame
264 * and try making all combinations of two frames intersecting at
270 sp
= &board
[cbp
->c_vertex
];
271 cp
= &sp
->s_fval
[color
][r
= cbp
->c_dir
];
275 * Since this is the first spot of an open ended
276 * frame, we treat it as a closed frame.
278 cb
.c
.a
= cp
->c
.a
+ 1;
280 if (cb
.s
< sp
->s_combo
[color
].s
) {
281 sp
->s_combo
[color
].s
= cb
.s
;
282 sp
->s_level
[color
] = 1;
285 * Try combining other frames that intersect
288 makecombo2(cbp
, sp
, 0, cb
.s
);
291 else if (color
!= nextcolor
)
292 memset(tmpmap
, 0, sizeof(tmpmap
));
299 for (; i
< 5; i
++, sp
+= d
) { /* for each spot */
300 if (sp
->s_occ
!= EMPTY
)
302 if (cp
->s
< sp
->s_combo
[color
].s
) {
303 sp
->s_combo
[color
].s
= cp
->s
;
304 sp
->s_level
[color
] = 1;
306 if (cp
->s
== 0x101) {
307 sp
->s_nforce
[color
]++;
308 if (color
!= nextcolor
) {
314 * Try combining other frames that intersect
317 makecombo2(cbp
, sp
, i
, cb
.s
);
319 if (cp
->s
== 0x101 && color
!= nextcolor
) {
321 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
323 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
324 forcemap
[i
] &= tmpmap
[i
];
327 /* mark frame as having been processed */
328 board
[cbp
->c_vertex
].s_flags
|= MFLAG
<< r
;
329 } while ((cbp
= cbp
->c_next
) != ecbp
);
332 * Try to make new 3rd level combos, 4th level, etc.
333 * Limit the search depth early in the game.
336 while (d
<= ((movenum
+ 1) >> 1) && combolen
> n
) {
338 debuglog("%cL%d %d %d %d", "BW"[color
],
339 d
, combolen
- n
, combocnt
, elistcnt
);
347 /* scan for combos at empty spots */
348 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
349 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
351 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
352 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
353 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
354 sp
->s_level
[color
] = cbp
->c_nframes
;
355 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
356 sp
->s_level
[color
] = cbp
->c_nframes
;
362 sp
->s_empty
= (struct elist
*)0;
363 for (ep
= sp
->s_nempty
; ep
; ep
= nep
) {
365 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
366 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
367 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
368 sp
->s_level
[color
] = cbp
->c_nframes
;
369 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
370 sp
->s_level
[color
] = cbp
->c_nframes
;
376 sp
->s_nempty
= (struct elist
*)0;
379 /* remove old combos */
380 if ((cbp
= sortcombos
) != (struct combostr
*)0) {
381 struct combostr
*ncbp
;
389 } while ((cbp
= ncbp
) != ecbp
);
390 sortcombos
= (struct combostr
*)0;
396 debuglog("scanframes: %c combocnt %d", "BW"[color
],
401 debuglog("scanframes: %c elistcnt %d", "BW"[color
],
409 * Compute all level 2 combos of frames intersecting spot 'osp'
410 * within the frame 'ocbp' and combo value 's'.
413 makecombo2(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
416 struct combostr
*ncbp
;
418 int baseB
, fcnt
, emask
, bmask
, n
;
419 union comboval ocb
, fcb
;
420 struct combostr
**scbpp
, *fcbp
;
423 /* try to combine a new frame with those found so far */
425 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
427 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
428 for (r
= 4; --r
>= 0; ) { /* for each direction */
429 /* don't include frames that overlap in the same direction */
430 if (r
== ocbp
->c_dir
)
434 * Frame A combined with B is the same value as B combined with A
435 * so skip frames that have already been processed (MFLAG).
436 * Also skip blocked frames (BFLAG) and frames that are <1,x>
437 * since combining another frame with it isn't valid.
439 bmask
= (BFLAG
| FFLAG
| MFLAG
) << r
;
441 for (f
= 0; f
< 5; f
++, fsp
-= d
) { /* for each frame */
442 if (fsp
->s_occ
== BORDER
)
444 if (fsp
->s_flags
& bmask
)
447 /* don't include frames of the wrong color */
448 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
453 * Get the combo value for this frame.
454 * If this is the end point of the frame,
455 * use the closed ended value for the frame.
457 if ((f
== 0 && fcb
.c
.b
) || fcb
.s
== 0x101) {
462 /* compute combo value */
463 c
= fcb
.c
.a
+ ocb
.c
.a
- 3;
466 n
= fcb
.c
.a
+ fcb
.c
.b
- 1;
470 /* make a new combo! */
471 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
472 2 * sizeof(struct combostr
*));
474 panic("Out of memory!");
475 scbpp
= (struct combostr
**)(ncbp
+ 1);
476 fcbp
= fsp
->s_frame
[r
];
484 ncbp
->c_combo
.c
.a
= c
;
485 ncbp
->c_combo
.c
.b
= n
;
486 ncbp
->c_link
[0] = ocbp
;
487 ncbp
->c_link
[1] = fcbp
;
488 ncbp
->c_linkv
[0].s
= ocb
.s
;
489 ncbp
->c_linkv
[1].s
= fcb
.s
;
490 ncbp
->c_voff
[0] = off
;
492 ncbp
->c_vertex
= osp
- board
;
495 ncbp
->c_frameindex
= 0;
496 ncbp
->c_flags
= (ocb
.c
.b
) ? C_OPEN_0
: 0;
498 ncbp
->c_flags
|= C_OPEN_1
;
499 ncbp
->c_framecnt
[0] = fcnt
;
500 ncbp
->c_emask
[0] = emask
;
501 ncbp
->c_framecnt
[1] = fcb
.c
.a
- 2;
502 ncbp
->c_emask
[1] = ncbp
->c_framecnt
[1] ?
503 ((fcb
.c
.b
? 0x1E : 0x1F) & ~(1 << f
)) : 0;
506 if ((c
== 1 && debug
> 1) || debug
> 3) {
507 debuglog("%c c %d %d m %x %x o %d %d",
509 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
510 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
511 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
512 printcombo(ncbp
, tmp
, sizeof(tmp
));
516 /* record the empty spots that will complete this combo */
519 /* add the new combo to the end of the list */
520 appendcombo(ncbp
, curcolor
);
522 updatecombo(ncbp
, curcolor
);
527 if ((c
== 1 && debug
> 1) || debug
> 5) {
539 * Scan the sorted list of frames and try to add a frame to
540 * combinations of 'level' number of frames.
545 struct combostr
*cbp
, *ecbp
;
546 struct spotstr
*sp
, *fsp
;
547 struct elist
*ep
, *nep
;
549 struct combostr
**cbpp
, *pcbp
;
550 union comboval fcb
, cb
;
554 /* scan for combos at empty spots */
556 for (sp
= &board
[PT(T
,20)]; --sp
>= &board
[PT(A
,1)]; ) {
557 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
559 if (cbp
->c_combo
.s
<= sp
->s_combo
[i
].s
) {
560 if (cbp
->c_combo
.s
!= sp
->s_combo
[i
].s
) {
561 sp
->s_combo
[i
].s
= cbp
->c_combo
.s
;
562 sp
->s_level
[i
] = cbp
->c_nframes
;
563 } else if (cbp
->c_nframes
< sp
->s_level
[i
])
564 sp
->s_level
[i
] = cbp
->c_nframes
;
570 sp
->s_empty
= sp
->s_nempty
;
571 sp
->s_nempty
= (struct elist
*)0;
574 /* try to add frames to the uncompleted combos at level curlevel */
575 cbp
= ecbp
= sortframes
[curcolor
];
577 fsp
= &board
[cbp
->c_vertex
];
579 /* skip frames that are part of a <1,x> combo */
580 if (fsp
->s_flags
& (FFLAG
<< r
))
584 * Don't include <1,x> combo frames,
585 * treat it as a closed three in a row instead.
587 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
592 * If this is an open ended frame, use
593 * the combo value with the end closed.
595 if (fsp
->s_occ
== EMPTY
) {
597 cb
.c
.a
= fcb
.c
.a
+ 1;
601 makecombo(cbp
, fsp
, 0, cb
.s
);
605 * The next four spots are handled the same for both
606 * open and closed ended frames.
610 for (i
= 1; i
< 5; i
++, sp
+= d
) {
611 if (sp
->s_occ
!= EMPTY
)
613 makecombo(cbp
, sp
, i
, fcb
.s
);
615 } while ((cbp
= cbp
->c_next
) != ecbp
);
617 /* put all the combos in the hash list on the sorted list */
618 cbpp
= &hashcombos
[FAREA
];
621 if (cbp
== (struct combostr
*)0)
623 *cbpp
= (struct combostr
*)0;
625 if (ecbp
== (struct combostr
*)0)
628 /* append to sort list */
631 ecbp
->c_prev
= cbp
->c_prev
;
632 cbp
->c_prev
->c_next
= ecbp
;
635 } while (cbpp
!= hashcombos
);
639 * Compute all level N combos of frames intersecting spot 'osp'
640 * within the frame 'ocbp' and combo value 's'.
643 makecombo(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
645 struct combostr
*cbp
, *ncbp
;
650 struct combostr
**scbpp
;
651 int baseB
, fcnt
, emask
, verts
;
653 struct overlap_info vertices
[1];
657 * XXX: when I made functions static gcc started warning about
658 * some members of vertices[0] maybe being used uninitialized.
659 * For now I'm just going to clear it rather than wade through
660 * the logic to find out whether gcc or the code is wrong. I
661 * wouldn't be surprised if it were the code though. - dholland
663 memset(vertices
, 0, sizeof(vertices
));
666 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
668 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
669 for (ep
= osp
->s_empty
; ep
; ep
= ep
->e_next
) {
670 /* check for various kinds of overlap */
672 verts
= checkframes(cbp
, ocbp
, osp
, s
, vertices
);
676 /* check to see if this frame forms a valid loop */
678 sp
= &board
[vertices
[0].o_intersect
];
680 if (sp
->s_occ
!= EMPTY
) {
681 debuglog("loop: %c %s", "BW"[curcolor
],
687 * It is a valid loop if the intersection spot
688 * of the frame we are trying to attach is one
689 * of the completion spots of the combostr
690 * we are trying to attach the frame to.
692 for (nep
= sp
->s_empty
; nep
; nep
= nep
->e_next
) {
693 if (nep
->e_combo
== cbp
)
695 if (nep
->e_combo
->c_nframes
< cbp
->c_nframes
)
698 /* frame overlaps but not at a valid spot */
704 /* compute the first half of the combo value */
705 c
= cbp
->c_combo
.c
.a
+ ocb
.c
.a
- verts
- 3;
709 /* compute the second half of the combo value */
710 n
= ep
->e_fval
.c
.a
+ ep
->e_fval
.c
.b
- 1;
714 /* make a new combo! */
715 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
716 (cbp
->c_nframes
+ 1) * sizeof(struct combostr
*));
718 panic("Out of memory!");
719 scbpp
= (struct combostr
**)(ncbp
+ 1);
720 if (sortcombo(scbpp
, (struct combostr
**)(cbp
+ 1), ocbp
)) {
726 ncbp
->c_combo
.c
.a
= c
;
727 ncbp
->c_combo
.c
.b
= n
;
728 ncbp
->c_link
[0] = cbp
;
729 ncbp
->c_link
[1] = ocbp
;
730 ncbp
->c_linkv
[1].s
= ocb
.s
;
731 ncbp
->c_voff
[1] = off
;
732 ncbp
->c_vertex
= osp
- board
;
733 ncbp
->c_nframes
= cbp
->c_nframes
+ 1;
734 ncbp
->c_flags
= ocb
.c
.b
? C_OPEN_1
: 0;
735 ncbp
->c_frameindex
= ep
->e_frameindex
;
737 * Update the completion spot mask of the frame we
738 * are attaching 'ocbp' to so the intersection isn't
741 ncbp
->c_framecnt
[0] = ep
->e_framecnt
;
742 ncbp
->c_emask
[0] = ep
->e_emask
;
744 ncbp
->c_flags
|= C_LOOP
;
745 ncbp
->c_dir
= vertices
[0].o_frameindex
;
746 ncbp
->c_framecnt
[1] = fcnt
- 1;
747 if (ncbp
->c_framecnt
[1]) {
748 n
= (vertices
[0].o_intersect
- ocbp
->c_vertex
) /
750 ncbp
->c_emask
[1] = emask
& ~(1 << n
);
752 ncbp
->c_emask
[1] = 0;
753 ncbp
->c_voff
[0] = vertices
[0].o_off
;
756 ncbp
->c_framecnt
[1] = fcnt
;
757 ncbp
->c_emask
[1] = emask
;
758 ncbp
->c_voff
[0] = ep
->e_off
;
761 if ((c
== 1 && debug
> 1) || debug
> 3) {
762 debuglog("%c v%d i%d d%d c %d %d m %x %x o %d %d",
763 "bw"[curcolor
], verts
, ncbp
->c_frameindex
, ncbp
->c_dir
,
764 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
765 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
766 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
767 printcombo(ncbp
, tmp
, sizeof(tmp
));
771 /* record the empty spots that will complete this combo */
775 /* update board values */
776 updatecombo(ncbp
, curcolor
);
779 if ((c
== 1 && debug
> 1) || debug
> 4) {
790 static struct elist einfo
[MAXDEPTH
];
791 static struct combostr
*ecombo
[MAXDEPTH
]; /* separate from elist to save space */
794 * Add the combostr 'ocbp' to the empty spots list for each empty spot
795 * in 'ocbp' that will complete the combo.
798 makeempty(struct combostr
*ocbp
)
800 struct combostr
*cbp
, *tcbp
, **cbpp
;
801 struct elist
*ep
, *nep
;
803 int s
, d
, m
, emask
, i
;
808 printcombo(ocbp
, tmp
, sizeof(tmp
));
809 debuglog("E%c %s", "bw"[curcolor
], tmp
);
812 /* should never happen but check anyway */
813 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
817 * The lower level combo can be pointed to by more than one
818 * higher level 'struct combostr' so we can't modify the
819 * lower level. Therefore, higher level combos store the
820 * real mask of the lower level frame in c_emask[0] and the
821 * frame number in c_frameindex.
823 * First we traverse the tree from top to bottom and save the
824 * connection info. Then we traverse the tree from bottom to
825 * top overwriting lower levels with the newer emask information.
827 ep
= &einfo
[nframes
];
828 cbpp
= &ecombo
[nframes
];
829 for (cbp
= ocbp
; (tcbp
= cbp
->c_link
[1]) != NULL
;
830 cbp
= cbp
->c_link
[0]) {
833 *--cbpp
= cbp
->c_link
[1];
834 ep
->e_off
= cbp
->c_voff
[1];
835 ep
->e_frameindex
= cbp
->c_frameindex
;
836 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
837 ep
->e_framecnt
= cbp
->c_framecnt
[1];
838 ep
->e_emask
= cbp
->c_emask
[1];
843 *--cbpp
= cbp
->c_link
[0];
844 ep
->e_off
= cbp
->c_voff
[0];
845 ep
->e_frameindex
= 0;
846 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
847 ep
->e_framecnt
= cbp
->c_framecnt
[0];
848 ep
->e_emask
= cbp
->c_emask
[0];
850 /* now update the emask info */
852 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
854 nep
= &einfo
[ep
->e_frameindex
];
855 nep
->e_framecnt
= cbp
->c_framecnt
[0];
856 nep
->e_emask
= cbp
->c_emask
[0];
858 if (cbp
->c_flags
& C_LOOP
) {
861 * Account for the fact that this frame connects
862 * to a previous one (thus forming a loop).
864 nep
= &einfo
[cbp
->c_dir
];
865 if (--nep
->e_framecnt
)
866 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
873 * We only need to update the emask values of "complete" loops
874 * to include the intersection spots.
876 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
877 /* process loops from the top down */
878 ep
= &einfo
[nframes
];
882 if (!(cbp
->c_flags
& C_LOOP
))
886 * Update the emask values to include the
887 * intersection spots.
889 nep
= &einfo
[cbp
->c_dir
];
891 nep
->e_emask
= 1 << cbp
->c_voff
[0];
893 ep
->e_emask
= 1 << ep
->e_off
;
894 ep
= &einfo
[ep
->e_frameindex
];
897 ep
->e_emask
= 1 << ep
->e_off
;
898 ep
= &einfo
[ep
->e_frameindex
];
900 } while (ep
!= einfo
);
903 /* check all the frames for completion spots */
904 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
905 /* skip this frame if there are no incomplete spots in it */
906 if ((emask
= ep
->e_emask
) == 0)
909 sp
= &board
[cbp
->c_vertex
];
911 for (s
= 0, m
= 1; s
< 5; s
++, sp
+= d
, m
<<= 1) {
912 if (sp
->s_occ
!= EMPTY
|| !(emask
& m
))
915 /* add the combo to the list of empty spots */
916 nep
= (struct elist
*)malloc(sizeof(struct elist
));
918 panic("Out of memory!");
921 nep
->e_frameindex
= i
;
922 if (ep
->e_framecnt
> 1) {
923 nep
->e_framecnt
= ep
->e_framecnt
- 1;
924 nep
->e_emask
= emask
& ~m
;
929 nep
->e_fval
.s
= ep
->e_fval
.s
;
931 debuglog("e %s o%d i%d c%d m%x %x",
940 /* sort by the number of frames in the combo */
941 nep
->e_next
= sp
->s_nempty
;
949 * Update the board value based on the combostr.
950 * This is called only if 'cbp' is a <1,x> combo.
951 * We handle things differently depending on whether the next move
952 * would be trying to "complete" the combo or trying to block it.
955 updatecombo(struct combostr
*cbp
, int color
)
958 struct combostr
*tcbp
;
960 int nframes
, flags
, s
;
964 /* save the top level value for the whole combo */
965 cb
.c
.a
= cbp
->c_combo
.c
.a
;
966 nframes
= cbp
->c_nframes
;
968 if (color
!= nextcolor
)
969 memset(tmpmap
, 0, sizeof(tmpmap
));
971 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
972 flags
= cbp
->c_flags
;
973 cb
.c
.b
= cbp
->c_combo
.c
.b
;
974 if (color
== nextcolor
) {
975 /* update the board value for the vertex */
976 sp
= &board
[cbp
->c_vertex
];
977 sp
->s_nforce
[color
]++;
978 if (cb
.s
<= sp
->s_combo
[color
].s
) {
979 if (cb
.s
!= sp
->s_combo
[color
].s
) {
980 sp
->s_combo
[color
].s
= cb
.s
;
981 sp
->s_level
[color
] = nframes
;
982 } else if (nframes
< sp
->s_level
[color
])
983 sp
->s_level
[color
] = nframes
;
986 /* update the board values for each spot in frame */
987 sp
= &board
[s
= tcbp
->c_vertex
];
989 i
= (flags
& C_OPEN_1
) ? 6 : 5;
990 for (; --i
>= 0; sp
+= d
, s
+= d
) {
991 if (sp
->s_occ
!= EMPTY
)
993 sp
->s_nforce
[color
]++;
994 if (cb
.s
<= sp
->s_combo
[color
].s
) {
995 if (cb
.s
!= sp
->s_combo
[color
].s
) {
996 sp
->s_combo
[color
].s
= cb
.s
;
997 sp
->s_level
[color
] = nframes
;
998 } else if (nframes
< sp
->s_level
[color
])
999 sp
->s_level
[color
] = nframes
;
1005 /* mark the frame as being part of a <1,x> combo */
1006 board
[tcbp
->c_vertex
].s_flags
|= FFLAG
<< tcbp
->c_dir
;
1009 if (color
!= nextcolor
) {
1010 /* update the board values for each spot in frame */
1011 sp
= &board
[s
= cbp
->c_vertex
];
1013 i
= (flags
& C_OPEN_0
) ? 6 : 5;
1014 for (; --i
>= 0; sp
+= d
, s
+= d
) {
1015 if (sp
->s_occ
!= EMPTY
)
1017 sp
->s_nforce
[color
]++;
1018 if (cb
.s
<= sp
->s_combo
[color
].s
) {
1019 if (cb
.s
!= sp
->s_combo
[color
].s
) {
1020 sp
->s_combo
[color
].s
= cb
.s
;
1021 sp
->s_level
[color
] = nframes
;
1022 } else if (nframes
< sp
->s_level
[color
])
1023 sp
->s_level
[color
] = nframes
;
1028 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
1030 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
1031 forcemap
[i
] &= tmpmap
[i
];
1036 /* mark the frame as being part of a <1,x> combo */
1037 board
[cbp
->c_vertex
].s_flags
|= FFLAG
<< cbp
->c_dir
;
1041 * Add combo to the end of the list.
1044 appendcombo(struct combostr
*cbp
, int color __unused
)
1046 struct combostr
*pcbp
, *ncbp
;
1050 if (ncbp
== (struct combostr
*)0) {
1056 pcbp
= ncbp
->c_prev
;
1064 * Return zero if it is valid to combine frame 'fcbp' with the frames
1065 * in 'cbp' and forms a linked chain of frames (i.e., a tree; no loops).
1066 * Return positive if combining frame 'fcbp' to the frames in 'cbp'
1067 * would form some kind of valid loop. Also return the intersection spots
1068 * in 'vertices[]' beside the known intersection at spot 'osp'.
1069 * Return -1 if 'fcbp' should not be combined with 'cbp'.
1070 * 's' is the combo value for frame 'fcpb'.
1073 checkframes(struct combostr
*cbp
, struct combostr
*fcbp
, struct spotstr
*osp
,
1074 int s
, struct overlap_info
*vertices
)
1076 struct combostr
*tcbp
, *lcbp
;
1077 int i
, n
, mask
, flags
, verts
, loop
, myindex
, fcnt
;
1089 myindex
= cbp
->c_nframes
;
1090 n
= (fcbp
- frames
) * FAREA
;
1094 * i == which overlap bit to test based on whether 'fcbp' is
1095 * an open or closed frame.
1098 for (; (tcbp
= cbp
->c_link
[1]) != NULL
;
1099 lcbp
= cbp
, cbp
= cbp
->c_link
[0]) {
1101 return (-1); /* fcbp is already included */
1103 /* check for intersection of 'tcbp' with 'fcbp' */
1105 mask
= str
[tcbp
- frames
];
1106 flags
= cbp
->c_flags
;
1107 n
= i
+ ((flags
& C_OPEN_1
) != 0);
1108 if (mask
& (1 << n
)) {
1110 * The two frames are not independent if they
1111 * both lie in the same line and intersect at
1112 * more than one point.
1114 if (tcbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1117 * If this is not the spot we are attaching
1118 * 'fcbp' to and it is a reasonable intersection
1119 * spot, then there might be a loop.
1121 n
= ip
[tcbp
- frames
];
1122 if (osp
!= &board
[n
]) {
1123 /* check to see if this is a valid loop */
1126 if (fcnt
== 0 || cbp
->c_framecnt
[1] == 0)
1129 * Check to be sure the intersection is not
1130 * one of the end points if it is an open
1133 if ((flags
& C_OPEN_1
) &&
1134 (n
== tcbp
->c_vertex
||
1135 n
== tcbp
->c_vertex
+ 5 * dd
[tcbp
->c_dir
]))
1136 return (-1); /* invalid overlap */
1138 (n
== fcbp
->c_vertex
||
1139 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1140 return (-1); /* invalid overlap */
1142 vertices
->o_intersect
= n
;
1143 vertices
->o_fcombo
= cbp
;
1144 vertices
->o_link
= 1;
1145 vertices
->o_off
= (n
- tcbp
->c_vertex
) /
1147 vertices
->o_frameindex
= myindex
;
1151 n
= i
+ ((flags
& C_OPEN_0
) != 0);
1154 return (-1); /* fcbp is already included */
1156 /* check for intersection of 'cbp' with 'fcbp' */
1157 mask
= str
[cbp
- frames
];
1158 if (mask
& (1 << n
)) {
1160 * The two frames are not independent if they
1161 * both lie in the same line and intersect at
1162 * more than one point.
1164 if (cbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1167 * If this is not the spot we are attaching
1168 * 'fcbp' to and it is a reasonable intersection
1169 * spot, then there might be a loop.
1171 n
= ip
[cbp
- frames
];
1172 if (osp
!= &board
[n
]) {
1173 /* check to see if this is a valid loop */
1176 if (fcnt
== 0 || lcbp
->c_framecnt
[0] == 0)
1179 * Check to be sure the intersection is not
1180 * one of the end points if it is an open
1183 if ((flags
& C_OPEN_0
) &&
1184 (n
== cbp
->c_vertex
||
1185 n
== cbp
->c_vertex
+ 5 * dd
[cbp
->c_dir
]))
1186 return (-1); /* invalid overlap */
1188 (n
== fcbp
->c_vertex
||
1189 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1190 return (-1); /* invalid overlap */
1192 vertices
->o_intersect
= n
;
1193 vertices
->o_fcombo
= lcbp
;
1194 vertices
->o_link
= 0;
1195 vertices
->o_off
= (n
- cbp
->c_vertex
) /
1197 vertices
->o_frameindex
= 0;
1205 * Merge sort the frame 'fcbp' and the sorted list of frames 'cbpp' and
1206 * store the result in 'scbpp'. 'curlevel' is the size of the 'cbpp' array.
1207 * Return true if this list of frames is already in the hash list.
1208 * Otherwise, add the new combo to the hash list.
1211 sortcombo(struct combostr
**scbpp
, struct combostr
**cbpp
,
1212 struct combostr
*fcbp
)
1214 struct combostr
**spp
, **cpp
;
1215 struct combostr
*cbp
, *ecbp
;
1223 debuglog("sortc: %s%c l%d", stoc(fcbp
->c_vertex
),
1224 pdir
[fcbp
->c_dir
], curlevel
);
1226 for (cpp
= cbpp
; cpp
< cbpp
+ curlevel
; cpp
++) {
1227 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1228 stoc((*cpp
)->c_vertex
), pdir
[(*cpp
)->c_dir
]);
1229 pos
+= strlen(buf
+ pos
);
1231 debuglog("%s", buf
);
1235 /* first build the new sorted list */
1238 cpp
= cbpp
+ curlevel
;
1245 while (cpp
-- != cbpp
);
1249 } while (cpp
!= cbpp
);
1253 /* now check to see if this list of frames has already been seen */
1254 cbp
= hashcombos
[inx
= *scbpp
- frames
];
1255 if (cbp
== (struct combostr
*)0) {
1257 * Easy case, this list hasn't been seen.
1258 * Add it to the hash list.
1260 fcbp
= (struct combostr
*)
1261 ((char *)scbpp
- sizeof(struct combostr
));
1262 hashcombos
[inx
] = fcbp
;
1263 fcbp
->c_next
= fcbp
->c_prev
= fcbp
;
1268 cbpp
= (struct combostr
**)(cbp
+ 1);
1271 cbpp
++; /* first frame is always the same */
1273 if (*--spp
!= *--cpp
)
1275 } while (cpp
!= cbpp
);
1276 /* we found a match */
1282 debuglog("sort1: n%d", n
);
1284 for (cpp
= scbpp
; cpp
< scbpp
+ n
; cpp
++) {
1285 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1286 stoc((*cpp
)->c_vertex
),
1287 pdir
[(*cpp
)->c_dir
]);
1288 pos
+= strlen(buf
+ pos
);
1290 debuglog("%s", buf
);
1291 printcombo(cbp
, buf
, sizeof(buf
));
1292 debuglog("%s", buf
);
1295 for (cpp
= cbpp
; cpp
< cbpp
+ n
; cpp
++) {
1296 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1297 stoc((*cpp
)->c_vertex
),
1298 pdir
[(*cpp
)->c_dir
]);
1299 pos
+= strlen(buf
+ pos
);
1301 debuglog("%s", buf
);
1307 } while ((cbp
= cbp
->c_next
) != ecbp
);
1309 * This list of frames hasn't been seen.
1310 * Add it to the hash list.
1313 fcbp
= (struct combostr
*)((char *)scbpp
- sizeof(struct combostr
));
1315 fcbp
->c_prev
= ecbp
;
1317 ecbp
->c_next
= fcbp
;
1322 * Print the combo into string buffer 'buf'.
1325 printcombo(struct combostr
*cbp
, char *buf
, size_t max
)
1327 struct combostr
*tcbp
;
1330 snprintf(buf
+ pos
, max
- pos
, "%x/%d",
1331 cbp
->c_combo
.s
, cbp
->c_nframes
);
1332 pos
+= strlen(buf
+ pos
);
1334 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1335 snprintf(buf
+ pos
, max
- pos
, " %s%c%x",
1336 stoc(tcbp
->c_vertex
), pdir
[tcbp
->c_dir
], cbp
->c_flags
);
1337 pos
+= strlen(buf
+ pos
);
1339 snprintf(buf
+ pos
, max
- pos
, " %s%c",
1340 stoc(cbp
->c_vertex
), pdir
[cbp
->c_dir
]);
1345 markcombo(struct combostr
*ocbp
)
1347 struct combostr
*cbp
, *tcbp
, **cbpp
;
1348 struct elist
*ep
, *nep
;
1354 /* should never happen but check anyway */
1355 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
1359 * The lower level combo can be pointed to by more than one
1360 * higher level 'struct combostr' so we can't modify the
1361 * lower level. Therefore, higher level combos store the
1362 * real mask of the lower level frame in c_emask[0] and the
1363 * frame number in c_frameindex.
1365 * First we traverse the tree from top to bottom and save the
1366 * connection info. Then we traverse the tree from bottom to
1367 * top overwriting lower levels with the newer emask information.
1369 ep
= &einfo
[nframes
];
1370 cbpp
= &ecombo
[nframes
];
1371 for (cbp
= ocbp
; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1374 *--cbpp
= cbp
->c_link
[1];
1375 ep
->e_off
= cbp
->c_voff
[1];
1376 ep
->e_frameindex
= cbp
->c_frameindex
;
1377 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
1378 ep
->e_framecnt
= cbp
->c_framecnt
[1];
1379 ep
->e_emask
= cbp
->c_emask
[1];
1384 *--cbpp
= cbp
->c_link
[0];
1385 ep
->e_off
= cbp
->c_voff
[0];
1386 ep
->e_frameindex
= 0;
1387 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
1388 ep
->e_framecnt
= cbp
->c_framecnt
[0];
1389 ep
->e_emask
= cbp
->c_emask
[0];
1391 /* now update the emask info */
1393 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
1395 nep
= &einfo
[ep
->e_frameindex
];
1396 nep
->e_framecnt
= cbp
->c_framecnt
[0];
1397 nep
->e_emask
= cbp
->c_emask
[0];
1399 if (cbp
->c_flags
& C_LOOP
) {
1402 * Account for the fact that this frame connects
1403 * to a previous one (thus forming a loop).
1405 nep
= &einfo
[cbp
->c_dir
];
1406 if (--nep
->e_framecnt
)
1407 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
1414 * We only need to update the emask values of "complete" loops
1415 * to include the intersection spots.
1417 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
1418 /* process loops from the top down */
1419 ep
= &einfo
[nframes
];
1423 if (!(cbp
->c_flags
& C_LOOP
))
1427 * Update the emask values to include the
1428 * intersection spots.
1430 nep
= &einfo
[cbp
->c_dir
];
1431 nep
->e_framecnt
= 1;
1432 nep
->e_emask
= 1 << cbp
->c_voff
[0];
1434 ep
->e_emask
= 1 << ep
->e_off
;
1435 ep
= &einfo
[ep
->e_frameindex
];
1438 ep
->e_emask
= 1 << ep
->e_off
;
1439 ep
= &einfo
[ep
->e_frameindex
];
1441 } while (ep
!= einfo
);
1444 /* mark all the frames with the completion spots */
1445 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
1448 sp
= &board
[cbp
->c_vertex
];
1449 d
= dd
[s
= cbp
->c_dir
];
1451 omask
= (IFLAG
| CFLAG
) << s
;
1452 s
= ep
->e_fval
.c
.b
? 6 : 5;
1453 for (; --s
>= 0; sp
+= d
, m
>>= 1)
1454 sp
->s_flags
|= (m
& 1) ? omask
: cmask
;
1459 clearcombo(struct combostr
*cbp
, int open
)
1462 struct combostr
*tcbp
;
1465 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1466 clearcombo(tcbp
, cbp
->c_flags
& C_OPEN_1
);
1467 open
= cbp
->c_flags
& C_OPEN_0
;
1469 sp
= &board
[cbp
->c_vertex
];
1470 d
= dd
[n
= cbp
->c_dir
];
1471 mask
= ~((IFLAG
| CFLAG
) << n
);
1473 for (; --n
>= 0; sp
+= d
)
1474 sp
->s_flags
&= mask
;
1478 list_eq(struct combostr
**scbpp
, struct combostr
**cbpp
, int n
)
1480 struct combostr
**spp
, **cpp
;
1485 if (*--spp
!= *--cpp
)
1487 } while (cpp
!= cbpp
);
1488 /* we found a match */