]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/pickmove.c
1 /* $NetBSD: pickmove.c,v 1.22 2013/10/19 17:23:08 christos 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.22 2013/10/19 17:23:08 christos 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
;
89 /* first move is easy */
93 /* initialize all the board values */
94 for (pos
= PT(T
,20); pos
-- > PT(A
,1); ) {
96 sp
->s_combo
[BLACK
].s
= MAXCOMBO
+ 1;
97 sp
->s_combo
[WHITE
].s
= MAXCOMBO
+ 1;
98 sp
->s_level
[BLACK
] = 255;
99 sp
->s_level
[WHITE
] = 255;
100 sp
->s_nforce
[BLACK
] = 0;
101 sp
->s_nforce
[WHITE
] = 0;
102 sp
->s_flags
&= ~(FFLAGALL
| MFLAGALL
);
105 memset(forcemap
, 0, sizeof(forcemap
));
107 /* compute new values */
112 /* find the spot with the highest value */
114 sp1
= sp2
= &board
[pos
];
115 for ( ; pos
-- > PT(A
,1); ) {
117 if (sp
->s_occ
!= EMPTY
)
119 if (debug
&& (sp
->s_combo
[BLACK
].c
.a
== 1 ||
120 sp
->s_combo
[WHITE
].c
.a
== 1)) {
121 debuglog("- %s %x/%d %d %x/%d %d %d", stoc(sp
- board
),
122 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
124 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
128 /* pick the best black move */
129 if (better(sp
, sp1
, BLACK
))
131 /* pick the best white move */
132 if (better(sp
, sp2
, WHITE
))
137 debuglog("B %s %x/%d %d %x/%d %d %d",
139 sp1
->s_combo
[BLACK
].s
, sp1
->s_level
[BLACK
],
140 sp1
->s_nforce
[BLACK
],
141 sp1
->s_combo
[WHITE
].s
, sp1
->s_level
[WHITE
],
142 sp1
->s_nforce
[WHITE
], sp1
->s_wval
);
143 debuglog("W %s %x/%d %d %x/%d %d %d",
145 sp2
->s_combo
[WHITE
].s
, sp2
->s_level
[WHITE
],
146 sp2
->s_nforce
[WHITE
],
147 sp2
->s_combo
[BLACK
].s
, sp2
->s_level
[BLACK
],
148 sp2
->s_nforce
[BLACK
], sp2
->s_wval
);
150 * Check for more than one force that can't
151 * all be blocked with one move.
153 sp
= (us
== BLACK
) ? sp2
: sp1
;
155 if (sp
->s_combo
[!us
].c
.a
== 1 && !BIT_TEST(forcemap
, m
))
156 debuglog("*** Can't be blocked");
159 Ocp
= &sp1
->s_combo
[BLACK
];
160 Tcp
= &sp2
->s_combo
[WHITE
];
162 Tcp
= &sp1
->s_combo
[BLACK
];
163 Ocp
= &sp2
->s_combo
[WHITE
];
169 * Block their combo only if we have to (i.e., if they are one move
170 * away from completing a force and we don't have a force that
171 * we can complete which takes fewer moves to win).
173 if (Tcp
->c
.a
<= 1 && (Ocp
->c
.a
> 1 ||
174 Tcp
->c
.a
+ Tcp
->c
.b
< Ocp
->c
.a
+ Ocp
->c
.b
))
175 return (sp2
- board
);
176 return (sp1
- board
);
180 * Return true if spot 'sp' is better than spot 'sp1' for color 'us'.
183 better(const struct spotstr
*sp
, const struct spotstr
*sp1
, int us
)
187 if (sp
->s_combo
[us
].s
< sp1
->s_combo
[us
].s
)
189 if (sp
->s_combo
[us
].s
!= sp1
->s_combo
[us
].s
)
191 if (sp
->s_level
[us
] < sp1
->s_level
[us
])
193 if (sp
->s_level
[us
] != sp1
->s_level
[us
])
195 if (sp
->s_nforce
[us
] > sp1
->s_nforce
[us
])
197 if (sp
->s_nforce
[us
] != sp1
->s_nforce
[us
])
203 if (BIT_TEST(forcemap
, s
) && !BIT_TEST(forcemap
, s1
))
205 if (!BIT_TEST(forcemap
, s
) && BIT_TEST(forcemap
, s1
))
207 if (sp
->s_combo
[them
].s
< sp1
->s_combo
[them
].s
)
209 if (sp
->s_combo
[them
].s
!= sp1
->s_combo
[them
].s
)
211 if (sp
->s_level
[them
] < sp1
->s_level
[them
])
213 if (sp
->s_level
[them
] != sp1
->s_level
[them
])
215 if (sp
->s_nforce
[them
] > sp1
->s_nforce
[them
])
217 if (sp
->s_nforce
[them
] != sp1
->s_nforce
[them
])
220 if (sp
->s_wval
> sp1
->s_wval
)
222 if (sp
->s_wval
!= sp1
->s_wval
)
225 return (random() & 1);
228 static int curcolor
; /* implicit parameter to makecombo() */
229 static int curlevel
; /* implicit parameter to makecombo() */
232 * Scan the sorted list of non-empty frames and
233 * update the minimum combo values for each empty spot.
234 * Also, try to combine frames to find more complex (chained) moves.
237 scanframes(int color
)
239 struct combostr
*cbp
, *ecbp
;
242 struct elist
*ep
, *nep
;
249 /* check for empty list of frames */
250 cbp
= sortframes
[color
];
251 if (cbp
== (struct combostr
*)0)
254 /* quick check for four in a row */
255 sp
= &board
[cbp
->c_vertex
];
256 cb
.s
= sp
->s_fval
[color
][d
= cbp
->c_dir
].s
;
259 for (i
= 5 + cb
.c
.b
; --i
>= 0; sp
+= d
) {
260 if (sp
->s_occ
!= EMPTY
)
262 sp
->s_combo
[color
].s
= cb
.s
;
263 sp
->s_level
[color
] = 1;
269 * Update the minimum combo value for each spot in the frame
270 * and try making all combinations of two frames intersecting at
276 sp
= &board
[cbp
->c_vertex
];
277 cp
= &sp
->s_fval
[color
][r
= cbp
->c_dir
];
281 * Since this is the first spot of an open ended
282 * frame, we treat it as a closed frame.
284 cb
.c
.a
= cp
->c
.a
+ 1;
286 if (cb
.s
< sp
->s_combo
[color
].s
) {
287 sp
->s_combo
[color
].s
= cb
.s
;
288 sp
->s_level
[color
] = 1;
291 * Try combining other frames that intersect
294 makecombo2(cbp
, sp
, 0, cb
.s
);
297 else if (color
!= nextcolor
)
298 memset(tmpmap
, 0, sizeof(tmpmap
));
305 for (; i
< 5; i
++, sp
+= d
) { /* for each spot */
306 if (sp
->s_occ
!= EMPTY
)
308 if (cp
->s
< sp
->s_combo
[color
].s
) {
309 sp
->s_combo
[color
].s
= cp
->s
;
310 sp
->s_level
[color
] = 1;
312 if (cp
->s
== 0x101) {
313 sp
->s_nforce
[color
]++;
314 if (color
!= nextcolor
) {
320 * Try combining other frames that intersect
323 makecombo2(cbp
, sp
, i
, cb
.s
);
325 if (cp
->s
== 0x101 && color
!= nextcolor
) {
327 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
329 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
330 forcemap
[i
] &= tmpmap
[i
];
333 /* mark frame as having been processed */
334 board
[cbp
->c_vertex
].s_flags
|= MFLAG
<< r
;
335 } while ((cbp
= cbp
->c_next
) != ecbp
);
338 * Try to make new 3rd level combos, 4th level, etc.
339 * Limit the search depth early in the game.
342 while (d
<= ((movenum
+ 1) >> 1) && combolen
> n
) {
344 debuglog("%cL%d %d %d %d", "BW"[color
],
345 d
, combolen
- n
, combocnt
, elistcnt
);
353 /* scan for combos at empty spots */
354 for (pos
= PT(T
,20); pos
-- > PT(A
,1); ) {
356 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
358 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
359 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
360 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
361 sp
->s_level
[color
] = cbp
->c_nframes
;
362 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
363 sp
->s_level
[color
] = cbp
->c_nframes
;
369 sp
->s_empty
= (struct elist
*)0;
370 for (ep
= sp
->s_nempty
; ep
; ep
= nep
) {
372 if (cbp
->c_combo
.s
<= sp
->s_combo
[color
].s
) {
373 if (cbp
->c_combo
.s
!= sp
->s_combo
[color
].s
) {
374 sp
->s_combo
[color
].s
= cbp
->c_combo
.s
;
375 sp
->s_level
[color
] = cbp
->c_nframes
;
376 } else if (cbp
->c_nframes
< sp
->s_level
[color
])
377 sp
->s_level
[color
] = cbp
->c_nframes
;
383 sp
->s_nempty
= (struct elist
*)0;
386 /* remove old combos */
387 if ((cbp
= sortcombos
) != (struct combostr
*)0) {
388 struct combostr
*ncbp
;
396 } while ((cbp
= ncbp
) != ecbp
);
397 sortcombos
= (struct combostr
*)0;
403 debuglog("scanframes: %c combocnt %d", "BW"[color
],
408 debuglog("scanframes: %c elistcnt %d", "BW"[color
],
416 * Compute all level 2 combos of frames intersecting spot 'osp'
417 * within the frame 'ocbp' and combo value 's'.
420 makecombo2(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
423 struct combostr
*ncbp
;
425 int baseB
, fcnt
, emask
, bmask
, n
;
426 union comboval ocb
, fcb
;
427 struct combostr
**scbpp
, *fcbp
;
430 /* try to combine a new frame with those found so far */
432 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
434 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
435 for (r
= 4; --r
>= 0; ) { /* for each direction */
436 /* don't include frames that overlap in the same direction */
437 if (r
== ocbp
->c_dir
)
441 * Frame A combined with B is the same value as B combined with A
442 * so skip frames that have already been processed (MFLAG).
443 * Also skip blocked frames (BFLAG) and frames that are <1,x>
444 * since combining another frame with it isn't valid.
446 bmask
= (BFLAG
| FFLAG
| MFLAG
) << r
;
448 for (f
= 0; f
< 5; f
++, fsp
-= d
) { /* for each frame */
449 if (fsp
->s_occ
== BORDER
)
451 if (fsp
->s_flags
& bmask
)
454 /* don't include frames of the wrong color */
455 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
460 * Get the combo value for this frame.
461 * If this is the end point of the frame,
462 * use the closed ended value for the frame.
464 if ((f
== 0 && fcb
.c
.b
) || fcb
.s
== 0x101) {
469 /* compute combo value */
470 c
= fcb
.c
.a
+ ocb
.c
.a
- 3;
473 n
= fcb
.c
.a
+ fcb
.c
.b
- 1;
477 /* make a new combo! */
478 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
479 2 * sizeof(struct combostr
*));
481 panic("Out of memory!");
482 scbpp
= (struct combostr
**)(ncbp
+ 1);
483 fcbp
= fsp
->s_frame
[r
];
491 ncbp
->c_combo
.c
.a
= c
;
492 ncbp
->c_combo
.c
.b
= n
;
493 ncbp
->c_link
[0] = ocbp
;
494 ncbp
->c_link
[1] = fcbp
;
495 ncbp
->c_linkv
[0].s
= ocb
.s
;
496 ncbp
->c_linkv
[1].s
= fcb
.s
;
497 ncbp
->c_voff
[0] = off
;
499 ncbp
->c_vertex
= osp
- board
;
502 ncbp
->c_frameindex
= 0;
503 ncbp
->c_flags
= (ocb
.c
.b
) ? C_OPEN_0
: 0;
505 ncbp
->c_flags
|= C_OPEN_1
;
506 ncbp
->c_framecnt
[0] = fcnt
;
507 ncbp
->c_emask
[0] = emask
;
508 ncbp
->c_framecnt
[1] = fcb
.c
.a
- 2;
509 ncbp
->c_emask
[1] = ncbp
->c_framecnt
[1] ?
510 ((fcb
.c
.b
? 0x1E : 0x1F) & ~(1 << f
)) : 0;
513 if ((c
== 1 && debug
> 1) || debug
> 3) {
514 debuglog("%c c %d %d m %x %x o %d %d",
516 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
517 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
518 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
519 printcombo(ncbp
, tmp
, sizeof(tmp
));
523 /* record the empty spots that will complete this combo */
526 /* add the new combo to the end of the list */
527 appendcombo(ncbp
, curcolor
);
529 updatecombo(ncbp
, curcolor
);
534 if ((c
== 1 && debug
> 1) || debug
> 5) {
546 * Scan the sorted list of frames and try to add a frame to
547 * combinations of 'level' number of frames.
552 struct combostr
*cbp
, *ecbp
;
553 struct spotstr
*sp
, *fsp
;
554 struct elist
*ep
, *nep
;
556 struct combostr
**cbpp
, *pcbp
;
557 union comboval fcb
, cb
;
562 /* scan for combos at empty spots */
564 for (pos
= PT(T
,20); pos
-- > PT(A
,1); ) {
566 for (ep
= sp
->s_empty
; ep
; ep
= nep
) {
568 if (cbp
->c_combo
.s
<= sp
->s_combo
[i
].s
) {
569 if (cbp
->c_combo
.s
!= sp
->s_combo
[i
].s
) {
570 sp
->s_combo
[i
].s
= cbp
->c_combo
.s
;
571 sp
->s_level
[i
] = cbp
->c_nframes
;
572 } else if (cbp
->c_nframes
< sp
->s_level
[i
])
573 sp
->s_level
[i
] = cbp
->c_nframes
;
579 sp
->s_empty
= sp
->s_nempty
;
580 sp
->s_nempty
= (struct elist
*)0;
583 /* try to add frames to the uncompleted combos at level curlevel */
584 cbp
= ecbp
= sortframes
[curcolor
];
586 fsp
= &board
[cbp
->c_vertex
];
588 /* skip frames that are part of a <1,x> combo */
589 if (fsp
->s_flags
& (FFLAG
<< r
))
593 * Don't include <1,x> combo frames,
594 * treat it as a closed three in a row instead.
596 fcb
.s
= fsp
->s_fval
[curcolor
][r
].s
;
601 * If this is an open ended frame, use
602 * the combo value with the end closed.
604 if (fsp
->s_occ
== EMPTY
) {
606 cb
.c
.a
= fcb
.c
.a
+ 1;
610 makecombo(cbp
, fsp
, 0, cb
.s
);
614 * The next four spots are handled the same for both
615 * open and closed ended frames.
619 for (i
= 1; i
< 5; i
++, sp
+= d
) {
620 if (sp
->s_occ
!= EMPTY
)
622 makecombo(cbp
, sp
, i
, fcb
.s
);
624 } while ((cbp
= cbp
->c_next
) != ecbp
);
626 /* put all the combos in the hash list on the sorted list */
627 cbpp
= &hashcombos
[FAREA
];
630 if (cbp
== (struct combostr
*)0)
632 *cbpp
= (struct combostr
*)0;
634 if (ecbp
== (struct combostr
*)0)
637 /* append to sort list */
640 ecbp
->c_prev
= cbp
->c_prev
;
641 cbp
->c_prev
->c_next
= ecbp
;
644 } while (cbpp
!= hashcombos
);
648 * Compute all level N combos of frames intersecting spot 'osp'
649 * within the frame 'ocbp' and combo value 's'.
652 makecombo(struct combostr
*ocbp
, struct spotstr
*osp
, int off
, int s
)
654 struct combostr
*cbp
, *ncbp
;
659 struct combostr
**scbpp
;
660 int baseB
, fcnt
, emask
, verts
;
662 struct overlap_info vertices
[1];
666 * XXX: when I made functions static gcc started warning about
667 * some members of vertices[0] maybe being used uninitialized.
668 * For now I'm just going to clear it rather than wade through
669 * the logic to find out whether gcc or the code is wrong. I
670 * wouldn't be surprised if it were the code though. - dholland
672 memset(vertices
, 0, sizeof(vertices
));
675 baseB
= ocb
.c
.a
+ ocb
.c
.b
- 1;
677 emask
= fcnt
? ((ocb
.c
.b
? 0x1E : 0x1F) & ~(1 << off
)) : 0;
678 for (ep
= osp
->s_empty
; ep
; ep
= ep
->e_next
) {
679 /* check for various kinds of overlap */
681 verts
= checkframes(cbp
, ocbp
, osp
, s
, vertices
);
685 /* check to see if this frame forms a valid loop */
687 sp
= &board
[vertices
[0].o_intersect
];
689 if (sp
->s_occ
!= EMPTY
) {
690 debuglog("loop: %c %s", "BW"[curcolor
],
696 * It is a valid loop if the intersection spot
697 * of the frame we are trying to attach is one
698 * of the completion spots of the combostr
699 * we are trying to attach the frame to.
701 for (nep
= sp
->s_empty
; nep
; nep
= nep
->e_next
) {
702 if (nep
->e_combo
== cbp
)
704 if (nep
->e_combo
->c_nframes
< cbp
->c_nframes
)
707 /* frame overlaps but not at a valid spot */
713 /* compute the first half of the combo value */
714 c
= cbp
->c_combo
.c
.a
+ ocb
.c
.a
- verts
- 3;
718 /* compute the second half of the combo value */
719 n
= ep
->e_fval
.c
.a
+ ep
->e_fval
.c
.b
- 1;
723 /* make a new combo! */
724 ncbp
= (struct combostr
*)malloc(sizeof(struct combostr
) +
725 (cbp
->c_nframes
+ 1) * sizeof(struct combostr
*));
727 panic("Out of memory!");
728 scbpp
= (struct combostr
**)(ncbp
+ 1);
729 if (sortcombo(scbpp
, (struct combostr
**)(cbp
+ 1), ocbp
)) {
735 ncbp
->c_combo
.c
.a
= c
;
736 ncbp
->c_combo
.c
.b
= n
;
737 ncbp
->c_link
[0] = cbp
;
738 ncbp
->c_link
[1] = ocbp
;
739 ncbp
->c_linkv
[1].s
= ocb
.s
;
740 ncbp
->c_voff
[1] = off
;
741 ncbp
->c_vertex
= osp
- board
;
742 ncbp
->c_nframes
= cbp
->c_nframes
+ 1;
743 ncbp
->c_flags
= ocb
.c
.b
? C_OPEN_1
: 0;
744 ncbp
->c_frameindex
= ep
->e_frameindex
;
746 * Update the completion spot mask of the frame we
747 * are attaching 'ocbp' to so the intersection isn't
750 ncbp
->c_framecnt
[0] = ep
->e_framecnt
;
751 ncbp
->c_emask
[0] = ep
->e_emask
;
753 ncbp
->c_flags
|= C_LOOP
;
754 ncbp
->c_dir
= vertices
[0].o_frameindex
;
755 ncbp
->c_framecnt
[1] = fcnt
- 1;
756 if (ncbp
->c_framecnt
[1]) {
757 n
= (vertices
[0].o_intersect
- ocbp
->c_vertex
) /
759 ncbp
->c_emask
[1] = emask
& ~(1 << n
);
761 ncbp
->c_emask
[1] = 0;
762 ncbp
->c_voff
[0] = vertices
[0].o_off
;
765 ncbp
->c_framecnt
[1] = fcnt
;
766 ncbp
->c_emask
[1] = emask
;
767 ncbp
->c_voff
[0] = ep
->e_off
;
770 if ((c
== 1 && debug
> 1) || debug
> 3) {
771 debuglog("%c v%d i%d d%d c %d %d m %x %x o %d %d",
772 "bw"[curcolor
], verts
, ncbp
->c_frameindex
, ncbp
->c_dir
,
773 ncbp
->c_framecnt
[0], ncbp
->c_framecnt
[1],
774 ncbp
->c_emask
[0], ncbp
->c_emask
[1],
775 ncbp
->c_voff
[0], ncbp
->c_voff
[1]);
776 printcombo(ncbp
, tmp
, sizeof(tmp
));
780 /* record the empty spots that will complete this combo */
784 /* update board values */
785 updatecombo(ncbp
, curcolor
);
788 if ((c
== 1 && debug
> 1) || debug
> 4) {
799 static struct elist einfo
[MAXDEPTH
];
800 static struct combostr
*ecombo
[MAXDEPTH
]; /* separate from elist to save space */
803 * Add the combostr 'ocbp' to the empty spots list for each empty spot
804 * in 'ocbp' that will complete the combo.
807 makeempty(struct combostr
*ocbp
)
809 struct combostr
*cbp
, *tcbp
, **cbpp
;
810 struct elist
*ep
, *nep
;
812 int s
, d
, m
, emask
, i
;
817 printcombo(ocbp
, tmp
, sizeof(tmp
));
818 debuglog("E%c %s", "bw"[curcolor
], tmp
);
821 /* should never happen but check anyway */
822 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
826 * The lower level combo can be pointed to by more than one
827 * higher level 'struct combostr' so we can't modify the
828 * lower level. Therefore, higher level combos store the
829 * real mask of the lower level frame in c_emask[0] and the
830 * frame number in c_frameindex.
832 * First we traverse the tree from top to bottom and save the
833 * connection info. Then we traverse the tree from bottom to
834 * top overwriting lower levels with the newer emask information.
836 ep
= &einfo
[nframes
];
837 cbpp
= &ecombo
[nframes
];
838 for (cbp
= ocbp
; (tcbp
= cbp
->c_link
[1]) != NULL
;
839 cbp
= cbp
->c_link
[0]) {
842 *--cbpp
= cbp
->c_link
[1];
843 ep
->e_off
= cbp
->c_voff
[1];
844 ep
->e_frameindex
= cbp
->c_frameindex
;
845 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
846 ep
->e_framecnt
= cbp
->c_framecnt
[1];
847 ep
->e_emask
= cbp
->c_emask
[1];
852 *--cbpp
= cbp
->c_link
[0];
853 ep
->e_off
= cbp
->c_voff
[0];
854 ep
->e_frameindex
= 0;
855 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
856 ep
->e_framecnt
= cbp
->c_framecnt
[0];
857 ep
->e_emask
= cbp
->c_emask
[0];
859 /* now update the emask info */
861 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
863 nep
= &einfo
[ep
->e_frameindex
];
864 nep
->e_framecnt
= cbp
->c_framecnt
[0];
865 nep
->e_emask
= cbp
->c_emask
[0];
867 if (cbp
->c_flags
& C_LOOP
) {
870 * Account for the fact that this frame connects
871 * to a previous one (thus forming a loop).
873 nep
= &einfo
[cbp
->c_dir
];
874 if (--nep
->e_framecnt
)
875 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
882 * We only need to update the emask values of "complete" loops
883 * to include the intersection spots.
885 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
886 /* process loops from the top down */
887 ep
= &einfo
[nframes
];
891 if (!(cbp
->c_flags
& C_LOOP
))
895 * Update the emask values to include the
896 * intersection spots.
898 nep
= &einfo
[cbp
->c_dir
];
900 nep
->e_emask
= 1 << cbp
->c_voff
[0];
902 ep
->e_emask
= 1 << ep
->e_off
;
903 ep
= &einfo
[ep
->e_frameindex
];
906 ep
->e_emask
= 1 << ep
->e_off
;
907 ep
= &einfo
[ep
->e_frameindex
];
909 } while (ep
!= einfo
);
912 /* check all the frames for completion spots */
913 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
914 /* skip this frame if there are no incomplete spots in it */
915 if ((emask
= ep
->e_emask
) == 0)
918 sp
= &board
[cbp
->c_vertex
];
920 for (s
= 0, m
= 1; s
< 5; s
++, sp
+= d
, m
<<= 1) {
921 if (sp
->s_occ
!= EMPTY
|| !(emask
& m
))
924 /* add the combo to the list of empty spots */
925 nep
= (struct elist
*)malloc(sizeof(struct elist
));
927 panic("Out of memory!");
930 nep
->e_frameindex
= i
;
931 if (ep
->e_framecnt
> 1) {
932 nep
->e_framecnt
= ep
->e_framecnt
- 1;
933 nep
->e_emask
= emask
& ~m
;
938 nep
->e_fval
.s
= ep
->e_fval
.s
;
940 debuglog("e %s o%d i%d c%d m%x %x",
949 /* sort by the number of frames in the combo */
950 nep
->e_next
= sp
->s_nempty
;
958 * Update the board value based on the combostr.
959 * This is called only if 'cbp' is a <1,x> combo.
960 * We handle things differently depending on whether the next move
961 * would be trying to "complete" the combo or trying to block it.
964 updatecombo(struct combostr
*cbp
, int color
)
967 struct combostr
*tcbp
;
969 int nframes
, flags
, s
;
973 /* save the top level value for the whole combo */
974 cb
.c
.a
= cbp
->c_combo
.c
.a
;
975 nframes
= cbp
->c_nframes
;
977 if (color
!= nextcolor
)
978 memset(tmpmap
, 0, sizeof(tmpmap
));
980 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
981 flags
= cbp
->c_flags
;
982 cb
.c
.b
= cbp
->c_combo
.c
.b
;
983 if (color
== nextcolor
) {
984 /* update the board value for the vertex */
985 sp
= &board
[cbp
->c_vertex
];
986 sp
->s_nforce
[color
]++;
987 if (cb
.s
<= sp
->s_combo
[color
].s
) {
988 if (cb
.s
!= sp
->s_combo
[color
].s
) {
989 sp
->s_combo
[color
].s
= cb
.s
;
990 sp
->s_level
[color
] = nframes
;
991 } else if (nframes
< sp
->s_level
[color
])
992 sp
->s_level
[color
] = nframes
;
995 /* update the board values for each spot in frame */
996 sp
= &board
[s
= tcbp
->c_vertex
];
998 i
= (flags
& C_OPEN_1
) ? 6 : 5;
999 for (; --i
>= 0; sp
+= d
, s
+= d
) {
1000 if (sp
->s_occ
!= EMPTY
)
1002 sp
->s_nforce
[color
]++;
1003 if (cb
.s
<= sp
->s_combo
[color
].s
) {
1004 if (cb
.s
!= sp
->s_combo
[color
].s
) {
1005 sp
->s_combo
[color
].s
= cb
.s
;
1006 sp
->s_level
[color
] = nframes
;
1007 } else if (nframes
< sp
->s_level
[color
])
1008 sp
->s_level
[color
] = nframes
;
1014 /* mark the frame as being part of a <1,x> combo */
1015 board
[tcbp
->c_vertex
].s_flags
|= FFLAG
<< tcbp
->c_dir
;
1018 if (color
!= nextcolor
) {
1019 /* update the board values for each spot in frame */
1020 sp
= &board
[s
= cbp
->c_vertex
];
1022 i
= (flags
& C_OPEN_0
) ? 6 : 5;
1023 for (; --i
>= 0; sp
+= d
, s
+= d
) {
1024 if (sp
->s_occ
!= EMPTY
)
1026 sp
->s_nforce
[color
]++;
1027 if (cb
.s
<= sp
->s_combo
[color
].s
) {
1028 if (cb
.s
!= sp
->s_combo
[color
].s
) {
1029 sp
->s_combo
[color
].s
= cb
.s
;
1030 sp
->s_level
[color
] = nframes
;
1031 } else if (nframes
< sp
->s_level
[color
])
1032 sp
->s_level
[color
] = nframes
;
1037 memcpy(forcemap
, tmpmap
, sizeof(tmpmap
));
1039 for (i
= 0; (unsigned int)i
< MAPSZ
; i
++)
1040 forcemap
[i
] &= tmpmap
[i
];
1045 /* mark the frame as being part of a <1,x> combo */
1046 board
[cbp
->c_vertex
].s_flags
|= FFLAG
<< cbp
->c_dir
;
1050 * Add combo to the end of the list.
1053 appendcombo(struct combostr
*cbp
, int color __unused
)
1055 struct combostr
*pcbp
, *ncbp
;
1059 if (ncbp
== (struct combostr
*)0) {
1065 pcbp
= ncbp
->c_prev
;
1073 * Return zero if it is valid to combine frame 'fcbp' with the frames
1074 * in 'cbp' and forms a linked chain of frames (i.e., a tree; no loops).
1075 * Return positive if combining frame 'fcbp' to the frames in 'cbp'
1076 * would form some kind of valid loop. Also return the intersection spots
1077 * in 'vertices[]' beside the known intersection at spot 'osp'.
1078 * Return -1 if 'fcbp' should not be combined with 'cbp'.
1079 * 's' is the combo value for frame 'fcpb'.
1082 checkframes(struct combostr
*cbp
, struct combostr
*fcbp
, struct spotstr
*osp
,
1083 int s
, struct overlap_info
*vertices
)
1085 struct combostr
*tcbp
, *lcbp
;
1086 int i
, n
, mask
, flags
, verts
, myindex
, fcnt
;
1097 myindex
= cbp
->c_nframes
;
1098 n
= (fcbp
- frames
) * FAREA
;
1102 * i == which overlap bit to test based on whether 'fcbp' is
1103 * an open or closed frame.
1106 for (; (tcbp
= cbp
->c_link
[1]) != NULL
;
1107 lcbp
= cbp
, cbp
= cbp
->c_link
[0]) {
1109 return (-1); /* fcbp is already included */
1111 /* check for intersection of 'tcbp' with 'fcbp' */
1113 mask
= str
[tcbp
- frames
];
1114 flags
= cbp
->c_flags
;
1115 n
= i
+ ((flags
& C_OPEN_1
) != 0);
1116 if (mask
& (1 << n
)) {
1118 * The two frames are not independent if they
1119 * both lie in the same line and intersect at
1120 * more than one point.
1122 if (tcbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1125 * If this is not the spot we are attaching
1126 * 'fcbp' to and it is a reasonable intersection
1127 * spot, then there might be a loop.
1129 n
= ip
[tcbp
- frames
];
1130 if (osp
!= &board
[n
]) {
1131 /* check to see if this is a valid loop */
1134 if (fcnt
== 0 || cbp
->c_framecnt
[1] == 0)
1137 * Check to be sure the intersection is not
1138 * one of the end points if it is an open
1141 if ((flags
& C_OPEN_1
) &&
1142 (n
== tcbp
->c_vertex
||
1143 n
== tcbp
->c_vertex
+ 5 * dd
[tcbp
->c_dir
]))
1144 return (-1); /* invalid overlap */
1146 (n
== fcbp
->c_vertex
||
1147 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1148 return (-1); /* invalid overlap */
1150 vertices
->o_intersect
= n
;
1151 vertices
->o_fcombo
= cbp
;
1152 vertices
->o_link
= 1;
1153 vertices
->o_off
= (n
- tcbp
->c_vertex
) /
1155 vertices
->o_frameindex
= myindex
;
1159 n
= i
+ ((flags
& C_OPEN_0
) != 0);
1162 return (-1); /* fcbp is already included */
1164 /* check for intersection of 'cbp' with 'fcbp' */
1165 mask
= str
[cbp
- frames
];
1166 if (mask
& (1 << n
)) {
1168 * The two frames are not independent if they
1169 * both lie in the same line and intersect at
1170 * more than one point.
1172 if (cbp
->c_dir
== fcbp
->c_dir
&& (mask
& (0x10 << n
)))
1175 * If this is not the spot we are attaching
1176 * 'fcbp' to and it is a reasonable intersection
1177 * spot, then there might be a loop.
1179 n
= ip
[cbp
- frames
];
1180 if (osp
!= &board
[n
]) {
1181 /* check to see if this is a valid loop */
1184 if (fcnt
== 0 || lcbp
->c_framecnt
[0] == 0)
1187 * Check to be sure the intersection is not
1188 * one of the end points if it is an open
1191 if ((flags
& C_OPEN_0
) &&
1192 (n
== cbp
->c_vertex
||
1193 n
== cbp
->c_vertex
+ 5 * dd
[cbp
->c_dir
]))
1194 return (-1); /* invalid overlap */
1196 (n
== fcbp
->c_vertex
||
1197 n
== fcbp
->c_vertex
+ 5 * dd
[fcbp
->c_dir
]))
1198 return (-1); /* invalid overlap */
1200 vertices
->o_intersect
= n
;
1201 vertices
->o_fcombo
= lcbp
;
1202 vertices
->o_link
= 0;
1203 vertices
->o_off
= (n
- cbp
->c_vertex
) /
1205 vertices
->o_frameindex
= 0;
1213 * Merge sort the frame 'fcbp' and the sorted list of frames 'cbpp' and
1214 * store the result in 'scbpp'. 'curlevel' is the size of the 'cbpp' array.
1215 * Return true if this list of frames is already in the hash list.
1216 * Otherwise, add the new combo to the hash list.
1219 sortcombo(struct combostr
**scbpp
, struct combostr
**cbpp
,
1220 struct combostr
*fcbp
)
1222 struct combostr
**spp
, **cpp
;
1223 struct combostr
*cbp
, *ecbp
;
1231 debuglog("sortc: %s%c l%d", stoc(fcbp
->c_vertex
),
1232 pdir
[fcbp
->c_dir
], curlevel
);
1234 for (cpp
= cbpp
; cpp
< cbpp
+ curlevel
; cpp
++) {
1235 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1236 stoc((*cpp
)->c_vertex
), pdir
[(*cpp
)->c_dir
]);
1237 pos
+= strlen(buf
+ pos
);
1239 debuglog("%s", buf
);
1243 /* first build the new sorted list */
1246 cpp
= cbpp
+ curlevel
;
1253 while (cpp
-- != cbpp
);
1257 } while (cpp
!= cbpp
);
1261 /* now check to see if this list of frames has already been seen */
1262 cbp
= hashcombos
[inx
= *scbpp
- frames
];
1263 if (cbp
== (struct combostr
*)0) {
1265 * Easy case, this list hasn't been seen.
1266 * Add it to the hash list.
1268 fcbp
= (struct combostr
*)
1269 ((char *)scbpp
- sizeof(struct combostr
));
1270 hashcombos
[inx
] = fcbp
;
1271 fcbp
->c_next
= fcbp
->c_prev
= fcbp
;
1276 cbpp
= (struct combostr
**)(cbp
+ 1);
1279 cbpp
++; /* first frame is always the same */
1281 if (*--spp
!= *--cpp
)
1283 } while (cpp
!= cbpp
);
1284 /* we found a match */
1290 debuglog("sort1: n%d", n
);
1292 for (cpp
= scbpp
; cpp
< scbpp
+ n
; cpp
++) {
1293 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1294 stoc((*cpp
)->c_vertex
),
1295 pdir
[(*cpp
)->c_dir
]);
1296 pos
+= strlen(buf
+ pos
);
1298 debuglog("%s", buf
);
1299 printcombo(cbp
, buf
, sizeof(buf
));
1300 debuglog("%s", buf
);
1303 for (cpp
= cbpp
; cpp
< cbpp
+ n
; cpp
++) {
1304 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " %s%c",
1305 stoc((*cpp
)->c_vertex
),
1306 pdir
[(*cpp
)->c_dir
]);
1307 pos
+= strlen(buf
+ pos
);
1309 debuglog("%s", buf
);
1315 } while ((cbp
= cbp
->c_next
) != ecbp
);
1317 * This list of frames hasn't been seen.
1318 * Add it to the hash list.
1321 fcbp
= (struct combostr
*)((char *)scbpp
- sizeof(struct combostr
));
1323 fcbp
->c_prev
= ecbp
;
1325 ecbp
->c_next
= fcbp
;
1330 * Print the combo into string buffer 'buf'.
1333 printcombo(struct combostr
*cbp
, char *buf
, size_t max
)
1335 struct combostr
*tcbp
;
1338 snprintf(buf
+ pos
, max
- pos
, "%x/%d",
1339 cbp
->c_combo
.s
, cbp
->c_nframes
);
1340 pos
+= strlen(buf
+ pos
);
1342 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1343 snprintf(buf
+ pos
, max
- pos
, " %s%c%x",
1344 stoc(tcbp
->c_vertex
), pdir
[tcbp
->c_dir
], cbp
->c_flags
);
1345 pos
+= strlen(buf
+ pos
);
1347 snprintf(buf
+ pos
, max
- pos
, " %s%c",
1348 stoc(cbp
->c_vertex
), pdir
[cbp
->c_dir
]);
1353 markcombo(struct combostr
*ocbp
)
1355 struct combostr
*cbp
, *tcbp
, **cbpp
;
1356 struct elist
*ep
, *nep
;
1362 /* should never happen but check anyway */
1363 if ((nframes
= ocbp
->c_nframes
) >= MAXDEPTH
)
1367 * The lower level combo can be pointed to by more than one
1368 * higher level 'struct combostr' so we can't modify the
1369 * lower level. Therefore, higher level combos store the
1370 * real mask of the lower level frame in c_emask[0] and the
1371 * frame number in c_frameindex.
1373 * First we traverse the tree from top to bottom and save the
1374 * connection info. Then we traverse the tree from bottom to
1375 * top overwriting lower levels with the newer emask information.
1377 ep
= &einfo
[nframes
];
1378 cbpp
= &ecombo
[nframes
];
1379 for (cbp
= ocbp
; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1382 *--cbpp
= cbp
->c_link
[1];
1383 ep
->e_off
= cbp
->c_voff
[1];
1384 ep
->e_frameindex
= cbp
->c_frameindex
;
1385 ep
->e_fval
.s
= cbp
->c_linkv
[1].s
;
1386 ep
->e_framecnt
= cbp
->c_framecnt
[1];
1387 ep
->e_emask
= cbp
->c_emask
[1];
1392 *--cbpp
= cbp
->c_link
[0];
1393 ep
->e_off
= cbp
->c_voff
[0];
1394 ep
->e_frameindex
= 0;
1395 ep
->e_fval
.s
= cbp
->c_linkv
[0].s
;
1396 ep
->e_framecnt
= cbp
->c_framecnt
[0];
1397 ep
->e_emask
= cbp
->c_emask
[0];
1399 /* now update the emask info */
1401 for (i
= 2, ep
+= 2; i
< nframes
; i
++, ep
++) {
1403 nep
= &einfo
[ep
->e_frameindex
];
1404 nep
->e_framecnt
= cbp
->c_framecnt
[0];
1405 nep
->e_emask
= cbp
->c_emask
[0];
1407 if (cbp
->c_flags
& C_LOOP
) {
1410 * Account for the fact that this frame connects
1411 * to a previous one (thus forming a loop).
1413 nep
= &einfo
[cbp
->c_dir
];
1414 if (--nep
->e_framecnt
)
1415 nep
->e_emask
&= ~(1 << cbp
->c_voff
[0]);
1422 * We only need to update the emask values of "complete" loops
1423 * to include the intersection spots.
1425 if (s
&& ocbp
->c_combo
.c
.a
== 2) {
1426 /* process loops from the top down */
1427 ep
= &einfo
[nframes
];
1431 if (!(cbp
->c_flags
& C_LOOP
))
1435 * Update the emask values to include the
1436 * intersection spots.
1438 nep
= &einfo
[cbp
->c_dir
];
1439 nep
->e_framecnt
= 1;
1440 nep
->e_emask
= 1 << cbp
->c_voff
[0];
1442 ep
->e_emask
= 1 << ep
->e_off
;
1443 ep
= &einfo
[ep
->e_frameindex
];
1446 ep
->e_emask
= 1 << ep
->e_off
;
1447 ep
= &einfo
[ep
->e_frameindex
];
1449 } while (ep
!= einfo
);
1452 /* mark all the frames with the completion spots */
1453 for (i
= 0, ep
= einfo
, cbpp
= ecombo
; i
< nframes
; i
++, ep
++, cbpp
++) {
1456 sp
= &board
[cbp
->c_vertex
];
1457 d
= dd
[s
= cbp
->c_dir
];
1459 omask
= (IFLAG
| CFLAG
) << s
;
1460 s
= ep
->e_fval
.c
.b
? 6 : 5;
1461 for (; --s
>= 0; sp
+= d
, m
>>= 1)
1462 sp
->s_flags
|= (m
& 1) ? omask
: cmask
;
1467 clearcombo(struct combostr
*cbp
, int open
)
1470 struct combostr
*tcbp
;
1473 for (; (tcbp
= cbp
->c_link
[1]) != NULL
; cbp
= cbp
->c_link
[0]) {
1474 clearcombo(tcbp
, cbp
->c_flags
& C_OPEN_1
);
1475 open
= cbp
->c_flags
& C_OPEN_0
;
1477 sp
= &board
[cbp
->c_vertex
];
1478 d
= dd
[n
= cbp
->c_dir
];
1479 mask
= ~((IFLAG
| CFLAG
) << n
);
1481 for (; --n
>= 0; sp
+= d
)
1482 sp
->s_flags
&= mask
;
1486 list_eq(struct combostr
**scbpp
, struct combostr
**cbpp
, int n
)
1488 struct combostr
**spp
, **cpp
;
1493 if (*--spp
!= *--cpp
)
1495 } while (cpp
!= cbpp
);
1496 /* we found a match */