Commit | Line | Data |
---|---|---|
7eeb782e AT |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ |
2 | * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see * | |
3 | * http://www.gnu.org/software/gnugo/ for more information. * | |
4 | * * | |
5 | * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * | |
6 | * 2008 and 2009 by the Free Software Foundation. * | |
7 | * * | |
8 | * This program is free software; you can redistribute it and/or * | |
9 | * modify it under the terms of the GNU General Public License as * | |
10 | * published by the Free Software Foundation - version 3 or * | |
11 | * (at your option) any later version. * | |
12 | * * | |
13 | * This program is distributed in the hope that it will be useful, * | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
16 | * GNU General Public License in file COPYING for more details. * | |
17 | * * | |
18 | * You should have received a copy of the GNU General Public * | |
19 | * License along with this program; if not, write to the Free * | |
20 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * | |
21 | * Boston, MA 02111, USA. * | |
22 | \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
23 | ||
24 | #include "gnugo.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | #include <stdlib.h> | |
29 | #include <stdarg.h> | |
30 | #include <math.h> | |
31 | ||
32 | #include "liberty.h" | |
33 | #include "sgftree.h" | |
34 | #include "random.h" | |
35 | #include "gg_utils.h" | |
36 | #include "patterns.h" | |
37 | ||
38 | /* | |
39 | * Change the status of all the stones in the dragon at (dr). | |
40 | */ | |
41 | ||
42 | void | |
43 | change_dragon_status(int dr, enum dragon_status status) | |
44 | { | |
45 | int pos; | |
46 | int origin = dragon[dr].origin; | |
47 | ||
48 | for (pos = BOARDMIN; pos < BOARDMAX; pos++) | |
49 | if (ON_BOARD(pos)) { | |
50 | if (dragon[pos].origin == origin) | |
51 | dragon[pos].status = status; | |
52 | } | |
53 | } | |
54 | ||
55 | ||
56 | /* | |
57 | * Check whether a move at (move) stops the enemy from playing at (apos). | |
58 | */ | |
59 | ||
60 | int | |
61 | defend_against(int move, int color, int apos) | |
62 | { | |
63 | if (trymove(move, color, "defend_against", NO_MOVE)) { | |
64 | if (safe_move(apos, OTHER_COLOR(color)) == 0) { | |
65 | popgo(); | |
66 | return 1; | |
67 | } | |
68 | popgo(); | |
69 | } | |
70 | return 0; | |
71 | } | |
72 | ||
73 | ||
74 | /* | |
75 | * Returns true if color can cut at (pos), or if connection through (pos) | |
76 | * is inhibited. This information is collected by find_cuts(), using the B | |
77 | * patterns in the connections database. | |
78 | */ | |
79 | ||
80 | int | |
81 | cut_possible(int pos, int color) | |
82 | { | |
83 | return (cutting_points[pos] & OTHER_COLOR(color)) != 0; | |
84 | } | |
85 | ||
86 | ||
87 | /* | |
88 | * does_attack(move, str) returns the result code for an attack on the | |
89 | * string 'str' by the move 'move'. However, if the move does not | |
90 | * improve the attack result compared to tenuki, 0 is returned. In | |
91 | * particular if the string is already captured, does_attack() always | |
92 | * returns 0. | |
93 | */ | |
94 | ||
95 | int | |
96 | does_attack(int move, int str) | |
97 | { | |
98 | int color = board[str]; | |
99 | int other = OTHER_COLOR(color); | |
100 | int result = 0; | |
101 | int acode = 0; | |
102 | int dcode = 0; | |
103 | int spos = NO_MOVE; | |
104 | ||
105 | attack_and_defend(str, &acode, NULL, &dcode, &spos); | |
106 | if (acode != 0 && dcode == 0) | |
107 | return 0; | |
108 | ||
109 | if (trymove(move, other, "does_attack-A", str)) { | |
110 | if (!board[str]) | |
111 | result = WIN; | |
112 | else | |
113 | result = REVERSE_RESULT(find_defense(str, NULL)); | |
114 | if (result != 0) { | |
115 | increase_depth_values(); | |
116 | if (spos != NO_MOVE && trymove(spos, color, "does_attack-B", str)) { | |
117 | if (board[str]) { | |
118 | int new_result = attack(str, NULL); | |
119 | if (new_result < result) | |
120 | result = new_result; | |
121 | } | |
122 | popgo(); | |
123 | } | |
124 | decrease_depth_values(); | |
125 | } | |
126 | popgo(); | |
127 | } | |
128 | ||
129 | if (result < acode) | |
130 | result = 0; | |
131 | ||
132 | return result; | |
133 | } | |
134 | ||
135 | ||
136 | /* | |
137 | * does_defend(move, str) returns true if the move at (move) | |
138 | * defends (str). This means that it defends the string, and that | |
139 | * (str) can be captured if no defense is made. | |
140 | * | |
141 | * FIXME: Make does_defend() ko aware like does_attack(). | |
142 | */ | |
143 | ||
144 | int | |
145 | does_defend(int move, int str) | |
146 | { | |
147 | int color = board[str]; | |
148 | int other = OTHER_COLOR(color); | |
149 | int result = 0; | |
150 | int spos = NO_MOVE; | |
151 | ||
152 | if (!attack(str, &spos)) | |
153 | return 0; | |
154 | ||
155 | gg_assert(spos != NO_MOVE); | |
156 | ||
157 | if (trymove(move, color, "does_defend-A", str)) { | |
158 | if (!attack(str, NULL)) { | |
159 | result = 1; | |
160 | increase_depth_values(); | |
161 | if (trymove(spos, other, "does_defend-B", str)) { | |
162 | if (!board[str] || !find_defense(str, NULL)) | |
163 | result = 0; | |
164 | popgo(); | |
165 | } | |
166 | decrease_depth_values(); | |
167 | } | |
168 | popgo(); | |
169 | } | |
170 | ||
171 | return result; | |
172 | } | |
173 | ||
174 | ||
175 | /* | |
176 | * Example: somewhere(WHITE, 2, apos, bpos, cpos). | |
177 | * | |
178 | * Returns true if one of the vertices listed | |
179 | * satisfies board[pos]==color. Here num_moves is the | |
180 | * number of moves. If check_alive is true, the dragon is not allowed | |
181 | * to be dead. This check is only valid if stackp==0. | |
182 | */ | |
183 | ||
184 | int | |
185 | somewhere(int color, int check_alive, int num_moves, ...) | |
186 | { | |
187 | va_list ap; | |
188 | int pos; | |
189 | int k; | |
190 | ||
191 | gg_assert(stackp == 0 || !check_alive); | |
192 | ||
193 | va_start(ap, num_moves); | |
194 | for (k = 0; k < num_moves; k++) { | |
195 | pos = va_arg(ap, int); | |
196 | ||
197 | if (board[pos] == color | |
198 | && (!check_alive || dragon[pos].status != DEAD)) { | |
199 | va_end(ap); | |
200 | return 1; | |
201 | } | |
202 | } | |
203 | ||
204 | va_end(ap); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | /* Search along the edge for the first visible stone. Start at apos | |
209 | * and move in the direction of bpos. Return 1 if the first visible | |
210 | * stone is of the given color. It is required that apos and bpos are | |
211 | * at the same distance from the edge. | |
212 | * | |
213 | * FIXME: The detection of the first visible stone is quite crude and | |
214 | * probably needs to be improved. | |
215 | */ | |
216 | int | |
217 | visible_along_edge(int color, int apos, int bpos) | |
218 | { | |
219 | int ai = I(apos); | |
220 | int aj = J(apos); | |
221 | int bi = I(bpos); | |
222 | int bj = J(bpos); | |
223 | int pos; | |
224 | int forward; | |
225 | int up; | |
226 | ASSERT1((ai == bi) ^ (aj == bj), apos); | |
227 | ||
228 | if (ai == bi) { | |
229 | if (aj > bj) | |
230 | forward = WEST(0); | |
231 | else | |
232 | forward = EAST(0); | |
233 | ||
234 | if (ai < board_size/2) { | |
235 | pos = POS(0, bj); | |
236 | up = SOUTH(0); | |
237 | } | |
238 | else { | |
239 | pos = POS(board_size - 1, bj); | |
240 | up = NORTH(0); | |
241 | } | |
242 | } | |
243 | else { | |
244 | if (ai > bi) | |
245 | forward = NORTH(0); | |
246 | else | |
247 | forward = SOUTH(0); | |
248 | ||
249 | if (aj < board_size/2) { | |
250 | pos = POS(bi, 0); | |
251 | up = EAST(0); | |
252 | } | |
253 | else { | |
254 | pos = POS(bi, board_size - 1); | |
255 | up = WEST(0); | |
256 | } | |
257 | } | |
258 | ||
259 | for (; ON_BOARD(pos); pos += forward) { | |
260 | int k; | |
261 | for (k = 4; k >= 0; k--) { | |
262 | ASSERT_ON_BOARD1(pos + k * up); | |
263 | if (board[pos + k * up] == color) | |
264 | return 1; | |
265 | else if (board[pos + k * up] == OTHER_COLOR(color)) | |
266 | return 0; | |
267 | } | |
268 | } | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
273 | /* Is the board symmetric (or rather antisymmetric) with respect to | |
274 | * mirroring in tengen after a specific move has been played? If the | |
275 | * move is PASS_MOVE, check the current board. | |
276 | * | |
277 | * If strict is set we require that each stone is matched by a stone | |
278 | * of the opposite color at the mirrored vertex. Otherwise we only | |
279 | * require that each stone is matched by a stone of either color. | |
280 | */ | |
281 | int | |
282 | test_symmetry_after_move(int move, int color, int strict) | |
283 | { | |
284 | int pos; | |
285 | int result = 1; | |
286 | ||
287 | if (move != PASS_MOVE) { | |
288 | if (board[move] != EMPTY) | |
289 | return 0; | |
290 | if (!trymove(move, color, "find_mirror_move", NO_MOVE)) | |
291 | return 0; | |
292 | } | |
293 | ||
294 | for (pos = BOARDMIN; pos < MIRROR_MOVE(pos); pos++) { | |
295 | int sum; | |
296 | if (!ON_BOARD(pos)) | |
297 | continue; | |
298 | ||
299 | sum = board[pos] + board[MIRROR_MOVE(pos)]; | |
300 | if (sum != EMPTY + EMPTY && sum != BLACK + WHITE) { | |
301 | if (strict || sum == EMPTY + WHITE || sum == EMPTY + BLACK) { | |
302 | result = 0; | |
303 | break; | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | if (move != PASS_MOVE) | |
309 | popgo(); | |
310 | ||
311 | return result; | |
312 | } | |
313 | ||
314 | ||
315 | /* The function play_break_through_n() plays a sequence of moves, | |
316 | * alternating between the players and starting with color. After | |
317 | * having played through the sequence, the three last coordinate pairs | |
318 | * gives a position to be analyzed by break_through(), to see whether | |
319 | * either color has managed to enclose some stones and/or connected | |
320 | * his own stones. If any of the three last positions is empty, it's | |
321 | * assumed that the enclosure has failed, as well as the attempt to | |
322 | * connect. | |
323 | * | |
324 | * If one or more of the moves to play turns out to be illegal for | |
325 | * some reason, the rest of the sequence is played anyway, and | |
326 | * break_through() is called as if nothing special happened. | |
327 | * | |
328 | * Like break_through(), this function returns 1 if the attempt to | |
329 | * break through was succesful and 2 if it only managed to cut | |
330 | * through. | |
331 | */ | |
332 | ||
333 | int | |
334 | play_break_through_n(int color, int num_moves, ...) | |
335 | { | |
336 | va_list ap; | |
337 | int mcolor = color; | |
338 | int success = 0; | |
339 | int i; | |
340 | int played_moves = 0; | |
341 | int apos; | |
342 | int xpos; | |
343 | int ypos; | |
344 | int zpos; | |
345 | ||
346 | va_start(ap, num_moves); | |
347 | ||
348 | /* Do all the moves with alternating colors. */ | |
349 | for (i = 0; i < num_moves; i++) { | |
350 | apos = va_arg(ap, int); | |
351 | ||
352 | if (apos != NO_MOVE | |
353 | && (trymove(apos, mcolor, "play_break_through_n", NO_MOVE) | |
354 | || tryko(apos, mcolor, "play_break_through_n"))) | |
355 | played_moves++; | |
356 | mcolor = OTHER_COLOR(mcolor); | |
357 | } | |
358 | ||
359 | /* Now do the real work. */ | |
360 | xpos = va_arg(ap, int); | |
361 | ypos = va_arg(ap, int); | |
362 | zpos = va_arg(ap, int); | |
363 | ||
364 | /* Temporarily increase the depth values with the number of explicitly | |
365 | * placed stones. | |
366 | */ | |
367 | #if 0 | |
368 | modify_depth_values(played_moves); | |
369 | #endif | |
370 | ||
371 | if (board[xpos] == EMPTY | |
372 | || board[ypos] == EMPTY | |
373 | || board[zpos] == EMPTY) | |
374 | success = 1; | |
375 | else | |
376 | success = break_through(xpos, ypos, zpos); | |
377 | ||
378 | #if 0 | |
379 | modify_depth_values(-played_moves); | |
380 | #endif | |
381 | ||
382 | /* Pop all the moves we could successfully play. */ | |
383 | for (i = 0; i < played_moves; i++) | |
384 | popgo(); | |
385 | ||
386 | va_end(ap); | |
387 | return success; | |
388 | } | |
389 | ||
390 | ||
391 | /* The function play_attack_defend_n() plays a sequence of moves, | |
392 | * alternating between the players and starting with color. After | |
393 | * having played through the sequence, the last coordinate pair gives | |
394 | * a target to attack or defend, depending on the value of do_attack. | |
395 | * If there is no stone present to attack or defend, it is assumed | |
396 | * that it has already been captured. If one or more of the moves to | |
397 | * play turns out to be illegal for some reason, the rest of the | |
398 | * sequence is played anyway, and attack/defense is tested as if | |
399 | * nothing special happened. | |
400 | * | |
401 | * A typical use for these functions is to set up a ladder in an | |
402 | * autohelper and see whether it works or not. | |
403 | */ | |
404 | ||
405 | int | |
406 | play_attack_defend_n(int color, int do_attack, int num_moves, ...) | |
407 | { | |
408 | va_list ap; | |
409 | int mcolor = color; | |
410 | int success = 0; | |
411 | int i; | |
412 | int played_moves = 0; | |
413 | int apos; | |
414 | int zpos; | |
415 | ||
416 | va_start(ap, num_moves); | |
417 | ||
418 | /* Do all the moves with alternating colors. */ | |
419 | for (i = 0; i < num_moves; i++) { | |
420 | apos = va_arg(ap, int); | |
421 | ||
422 | if (apos != NO_MOVE | |
423 | && (trymove(apos, mcolor, "play_attack_defend_n", NO_MOVE) | |
424 | || tryko(apos, mcolor, "play_attack_defend_n"))) | |
425 | played_moves++; | |
426 | mcolor = OTHER_COLOR(mcolor); | |
427 | } | |
428 | ||
429 | /* Now do the real work. */ | |
430 | zpos = va_arg(ap, int); | |
431 | ||
432 | /* Temporarily increase the depth values with the number of explicitly | |
433 | * placed stones. | |
434 | * | |
435 | * This improves the reading of pattern constraints but | |
436 | * unfortunately tends to be too expensive. For the time being it is | |
437 | * disabled. | |
438 | */ | |
439 | #if 0 | |
440 | modify_depth_values(played_moves); | |
441 | #endif | |
442 | ||
443 | if (do_attack) { | |
444 | if (board[zpos] == EMPTY) | |
445 | success = WIN; | |
446 | else | |
447 | success = attack(zpos, NULL); | |
448 | } | |
449 | else { | |
450 | if (board[zpos] == EMPTY) | |
451 | success = 0; | |
452 | else { | |
453 | int dcode = find_defense(zpos, NULL); | |
454 | if (dcode == 0 && !attack(zpos, NULL)) | |
455 | success = WIN; | |
456 | else | |
457 | success = dcode; | |
458 | } | |
459 | } | |
460 | ||
461 | #if 0 | |
462 | modify_depth_values(-played_moves); | |
463 | #endif | |
464 | ||
465 | /* Pop all the moves we could successfully play. */ | |
466 | for (i = 0; i < played_moves; i++) | |
467 | popgo(); | |
468 | ||
469 | va_end(ap); | |
470 | return success; | |
471 | } | |
472 | ||
473 | ||
474 | /* The function play_attack_defend2_n() plays a sequence of moves, | |
475 | * alternating between the players and starting with color. After | |
476 | * having played through the sequence, the two last coordinate pairs | |
477 | * give two targets to simultaneously attack or defend, depending on | |
478 | * the value of do_attack. If there is no stone present to attack or | |
479 | * defend, it is assumed that it has already been captured. If one or | |
480 | * more of the moves to play turns out to be illegal for some reason, | |
481 | * the rest of the sequence is played anyway, and attack/defense is | |
482 | * tested as if nothing special happened. | |
483 | * | |
484 | * A typical use for these functions is to set up a crosscut in an | |
485 | * autohelper and see whether at least one cutting stone can be | |
486 | * captured. | |
487 | */ | |
488 | ||
489 | int | |
490 | play_attack_defend2_n(int color, int do_attack, int num_moves, ...) | |
491 | { | |
492 | va_list ap; | |
493 | int mcolor = color; | |
494 | int success = 0; | |
495 | int i; | |
496 | int played_moves = 0; | |
497 | int apos; | |
498 | int ypos; | |
499 | int zpos; | |
500 | ||
501 | va_start(ap, num_moves); | |
502 | ||
503 | /* Do all the moves with alternating colors. */ | |
504 | for (i = 0; i < num_moves; i++) { | |
505 | apos = va_arg(ap, int); | |
506 | ||
507 | if (apos != NO_MOVE | |
508 | && (trymove(apos, mcolor, "play_attack_defend_n", NO_MOVE) | |
509 | || tryko(apos, mcolor, "play_attack_defend_n"))) | |
510 | played_moves++; | |
511 | mcolor = OTHER_COLOR(mcolor); | |
512 | } | |
513 | ||
514 | /* Now do the real work. */ | |
515 | ypos = va_arg(ap, int); | |
516 | zpos = va_arg(ap, int); | |
517 | ||
518 | /* Temporarily increase the depth values with the number of explicitly | |
519 | * placed stones. | |
520 | */ | |
521 | #if 0 | |
522 | modify_depth_values(played_moves); | |
523 | #endif | |
524 | ||
525 | ||
526 | /* FIXED: tm - returns ko results correctly (3.1.22) */ | |
527 | if (do_attack) { | |
528 | if (board[ypos] == EMPTY || board[zpos] == EMPTY) | |
529 | success = WIN; | |
530 | else | |
531 | success = attack_either(ypos, zpos); | |
532 | } | |
533 | else { | |
534 | if (board[ypos] == EMPTY || board[zpos] == EMPTY) | |
535 | success = 0; | |
536 | else | |
537 | success = defend_both(ypos, zpos); | |
538 | } | |
539 | ||
540 | #if 0 | |
541 | modify_depth_values(-played_moves); | |
542 | #endif | |
543 | ||
544 | /* Pop all the moves we could successfully play. */ | |
545 | for (i = 0; i < played_moves; i++) | |
546 | popgo(); | |
547 | ||
548 | va_end(ap); | |
549 | return success; | |
550 | } | |
551 | ||
552 | ||
553 | /* The function play_connect_n() plays a sequence of moves, | |
554 | * alternating between the players and starting with color. After | |
555 | * having played through the sequence, the two last coordinates | |
556 | * give two targets that should be connected or disconnected, depending on | |
557 | * the value of do_connect. If there is no stone present to connect or | |
558 | * disconnect, it is assumed that the connection has failed. If one or | |
559 | * more of the moves to play turns out to be illegal for some reason, | |
560 | * the rest of the sequence is played anyway, and connection/disconnection | |
561 | * is tested as if nothing special happened. | |
562 | */ | |
563 | ||
564 | int | |
565 | play_connect_n(int color, int do_connect, int num_moves, ...) | |
566 | { | |
567 | va_list ap; | |
568 | int mcolor = color; | |
569 | int success = 0; | |
570 | int i; | |
571 | int played_moves = 0; | |
572 | int apos; | |
573 | int ypos; | |
574 | int zpos; | |
575 | ||
576 | va_start(ap, num_moves); | |
577 | ||
578 | /* Do all the moves with alternating colors. */ | |
579 | for (i = 0; i < num_moves; i++) { | |
580 | apos = va_arg(ap, int); | |
581 | ||
582 | if (apos != NO_MOVE | |
583 | && (trymove(apos, mcolor, "play_connect_n", NO_MOVE) | |
584 | || tryko(apos, mcolor, "play_connect_n"))) | |
585 | played_moves++; | |
586 | mcolor = OTHER_COLOR(mcolor); | |
587 | } | |
588 | ||
589 | /* Now do the real work. */ | |
590 | ypos = va_arg(ap, int); | |
591 | zpos = va_arg(ap, int); | |
592 | ||
593 | /* Temporarily increase the depth values with the number of explicitly | |
594 | * placed stones. | |
595 | * | |
596 | * This improves the reading of pattern constraints but | |
597 | * unfortunately tends to be too expensive. For the time being it is | |
598 | * disabled. | |
599 | */ | |
600 | #if 0 | |
601 | modify_depth_values(played_moves); | |
602 | #endif | |
603 | ||
604 | /* See if ypos and zpos can be connected (or disconnected). */ | |
605 | if (do_connect) { | |
606 | if (board[ypos] == EMPTY || board[zpos] == EMPTY) | |
607 | success = 0; | |
608 | else | |
609 | success = string_connect(ypos, zpos, NULL); | |
610 | } | |
611 | else { | |
612 | if (board[ypos] == EMPTY || board[zpos] == EMPTY) | |
613 | success = WIN; | |
614 | else | |
615 | success = disconnect(ypos, zpos, NULL); | |
616 | } | |
617 | ||
618 | #if 0 | |
619 | modify_depth_values(-played_moves); | |
620 | #endif | |
621 | ||
622 | /* Pop all the moves we could successfully play. */ | |
623 | for (i = 0; i < played_moves; i++) | |
624 | popgo(); | |
625 | ||
626 | va_end(ap); | |
627 | return success; | |
628 | } | |
629 | ||
630 | ||
631 | /* The function play_lib_n() plays a sequence of moves, alternating | |
632 | * between the players and starting with color. After having played | |
633 | * through the sequence, the last coordinate gives a target for liberty | |
634 | * counting. The number of liberties is returned. | |
635 | * | |
636 | * If only one move is to be played and that stone is the target, | |
637 | * accuratelib (or approxlib if appropriate) is more efficient. | |
638 | */ | |
639 | ||
640 | int | |
641 | play_lib_n(int color, int num_moves, ...) | |
642 | { | |
643 | va_list ap; | |
644 | int mcolor = color; | |
645 | int libs = 0; | |
646 | int i; | |
647 | int played_moves = 0; | |
648 | int apos; | |
649 | int ypos; | |
650 | ||
651 | va_start(ap, num_moves); | |
652 | ||
653 | /* Do all the moves with alternating colors. */ | |
654 | for (i = 0; i < num_moves; i++) { | |
655 | apos = va_arg(ap, int); | |
656 | ||
657 | if (apos != NO_MOVE | |
658 | && (trymove(apos, mcolor, "play_connect_n", NO_MOVE) | |
659 | || tryko(apos, mcolor, "play_connect_n"))) | |
660 | played_moves++; | |
661 | mcolor = OTHER_COLOR(mcolor); | |
662 | } | |
663 | ||
664 | /* Now do the real work. */ | |
665 | ypos = va_arg(ap, int); | |
666 | if (board[ypos] == EMPTY) | |
667 | libs = 0; | |
668 | else | |
669 | libs = countlib(ypos); | |
670 | ||
671 | /* Pop all the moves we could successfully play. */ | |
672 | for (i = 0; i < played_moves; i++) | |
673 | popgo(); | |
674 | ||
675 | va_end(ap); | |
676 | return libs; | |
677 | } | |
678 | ||
679 | ||
680 | ||
681 | /* | |
682 | * It is assumed in reading a ladder if stackp >= depth that | |
683 | * as soon as a bounding stone is in atari, the string is safe. | |
684 | * It is used similarly at other places in reading.c to implement simplifying | |
685 | * assumptions when stackp is large. DEPTH is the default value of depth. | |
686 | * | |
687 | * Unfortunately any such scheme invites the ``horizon effect.'' Increasing | |
688 | * DEPTH will make the program stronger and slower. | |
689 | * | |
690 | */ | |
691 | ||
692 | /* Tactical reading using C functions */ | |
693 | #define DEPTH 16 | |
694 | #define BRANCH_DEPTH 13 | |
695 | #define BACKFILL_DEPTH 12 | |
696 | #define BACKFILL2_DEPTH 5 | |
697 | #define BREAK_CHAIN_DEPTH 7 | |
698 | #define SUPERSTRING_DEPTH 7 | |
699 | #define FOURLIB_DEPTH 7 | |
700 | #define KO_DEPTH 8 | |
701 | ||
702 | #if 0 | |
703 | #undef FOURLIB_DEPTH | |
704 | #define FOURLIB_DEPTH 9 | |
705 | #endif | |
706 | ||
707 | ||
708 | #define AA_DEPTH 6 | |
709 | ||
710 | /* Pattern based reading */ | |
711 | #define OWL_DISTRUST_DEPTH 6 | |
712 | #define OWL_BRANCH_DEPTH 8 | |
713 | #define OWL_READING_DEPTH 20 | |
714 | #define SEMEAI_BRANCH_DEPTH 12 | |
715 | #define SEMEAI_BRANCH_DEPTH2 6 | |
716 | ||
717 | /* Connecton reading */ | |
718 | #define CONNECT_NODE_LIMIT 2000 | |
719 | #define CONNECT_DEPTH 64 | |
720 | #define CONNECT_DEPTH2 20 | |
721 | ||
722 | #define BREAKIN_NODE_LIMIT 400 | |
723 | #define BREAKIN_DEPTH 14 | |
724 | ||
725 | /* Set the various reading depth parameters. If mandated_depth_value | |
726 | * is not -1 that value is used; otherwise the depth values are | |
727 | * set as a function of level. The parameter mandated_depth_value | |
728 | * can be set at the command line to force a particular value of | |
729 | * depth; normally it is -1. | |
730 | */ | |
731 | ||
732 | void | |
733 | set_depth_values(int level, int report_levels) | |
734 | { | |
735 | static int node_limits[] = {500, 500, 450, 400, 400, 325, 275, | |
736 | 200, 150, 100, 75, 50}; | |
737 | int depth_level; | |
738 | ||
739 | /* | |
740 | * Other policies depending on level: | |
741 | * owl.c: >= 9: use vital attack pattern database | |
742 | * >= 8: increase depth values in owl_substantial | |
743 | * >= 8: don't turn off owl_phase in semeai reading | |
744 | * reading.c: >= 8: Use superstrings and do more backfilling. | |
745 | * value_moves.c: >= 6: try to find more owl attacks/defenses | |
746 | * breakin.c: >= 10: try to find break-ins. (*) | |
747 | * worm.c: >= 10: detect unconditionally meaningless moves | |
748 | * | |
749 | * The break-in code (*) is particularly expensive. | |
750 | * | |
751 | * Speedups between levels 9 and 10 and between levels 7 and 8 | |
752 | * are obtained by turning off services, and between these | |
753 | * levels no changes are made in the depths. The parameter | |
754 | * depth_level is the correction compared to the default settings at level | |
755 | * 10 for most reading depths. | |
756 | */ | |
757 | if (level >= 10) | |
758 | depth_level = level - 10; | |
759 | else if (level == 9) | |
760 | depth_level = 0; | |
761 | else if (level == 8) | |
762 | depth_level = -1; | |
763 | else | |
764 | depth_level = level - 8; | |
765 | ||
766 | depth = gg_max(6, DEPTH + depth_level); | |
767 | branch_depth = gg_max(3, BRANCH_DEPTH + depth_level); | |
768 | backfill_depth = gg_max(2, BACKFILL_DEPTH + depth_level); | |
769 | backfill2_depth = gg_max(1, BACKFILL2_DEPTH + depth_level); | |
770 | break_chain_depth = gg_max(2, BREAK_CHAIN_DEPTH + depth_level); | |
771 | if (level >= 8) | |
772 | owl_distrust_depth = gg_max(1, (2 * OWL_DISTRUST_DEPTH + depth_level) / 2); | |
773 | else | |
774 | owl_distrust_depth = gg_max(1, (2 * OWL_DISTRUST_DEPTH - 1 | |
775 | + depth_level) / 2); | |
776 | owl_branch_depth = gg_max(2, (2 * OWL_BRANCH_DEPTH + depth_level) / 2); | |
777 | owl_reading_depth = gg_max(5, (2 * OWL_READING_DEPTH + depth_level) / 2); | |
778 | ||
779 | /* Atari-atari depth levels are unchanged only between levels 7/8, 9/10: */ | |
780 | if (level >= 10) | |
781 | aa_depth = gg_max(0, AA_DEPTH + (level - 10)); | |
782 | else if (level == 9) | |
783 | aa_depth = gg_max(0, AA_DEPTH); | |
784 | else if (level >= 7) | |
785 | aa_depth = gg_max(0, AA_DEPTH - 1); | |
786 | else | |
787 | aa_depth = gg_max(0, AA_DEPTH - (8 - level)); | |
788 | ||
789 | /* Exceptions: | |
790 | * fourlib_depth: This is constant from levels 7 to 10. | |
791 | * superstring_depth: set to 0 below level 8. | |
792 | */ | |
793 | if (level >= 10) | |
794 | ko_depth = gg_max(1, KO_DEPTH + (level - 10)); | |
795 | else if (level == 9) | |
796 | ko_depth = gg_max(1, KO_DEPTH); | |
797 | else if (level >= 7) | |
798 | ko_depth = gg_max(1, KO_DEPTH - 1); | |
799 | else | |
800 | ko_depth = gg_max(1, KO_DEPTH + (level - 8)); | |
801 | ||
802 | if (level >= 10) | |
803 | fourlib_depth = gg_max(1, FOURLIB_DEPTH + (level - 10)); | |
804 | else if (level >= 7) | |
805 | fourlib_depth = gg_max(1, FOURLIB_DEPTH); | |
806 | else | |
807 | fourlib_depth = gg_max(1, FOURLIB_DEPTH + (level - 7)); | |
808 | ||
809 | if (level >= 8) | |
810 | superstring_depth = gg_max(1, SUPERSTRING_DEPTH); | |
811 | else | |
812 | superstring_depth = 0; | |
813 | ||
814 | if (level >= 10) | |
815 | owl_node_limit = OWL_NODE_LIMIT * pow(1.5, depth_level); | |
816 | else { | |
817 | owl_node_limit = (OWL_NODE_LIMIT * node_limits[10 - level] / | |
818 | node_limits[0]); | |
819 | owl_node_limit = gg_max(20, owl_node_limit); | |
820 | } | |
821 | ||
822 | semeai_branch_depth = gg_max(2, (2*SEMEAI_BRANCH_DEPTH + depth_level) / 2); | |
823 | semeai_branch_depth2 = gg_max(2, (2*SEMEAI_BRANCH_DEPTH2 + depth_level) / 2); | |
824 | semeai_node_limit = SEMEAI_NODE_LIMIT * pow(1.5, depth_level); | |
825 | ||
826 | connect_depth = gg_max(2, CONNECT_DEPTH + 2 * depth_level); | |
827 | connect_depth2 = gg_max(2, CONNECT_DEPTH2 + 2 * depth_level); | |
828 | connection_node_limit = CONNECT_NODE_LIMIT * pow(1.45, depth_level); | |
829 | breakin_depth = gg_max(2, BREAKIN_DEPTH + 2 * depth_level); | |
830 | breakin_node_limit = BREAKIN_NODE_LIMIT * pow(1.5, depth_level); | |
831 | ||
832 | if (mandated_depth != -1) | |
833 | depth = mandated_depth; | |
834 | if (mandated_backfill_depth != -1) | |
835 | backfill_depth = mandated_backfill_depth; | |
836 | if (mandated_backfill2_depth != -1) | |
837 | backfill2_depth = mandated_backfill2_depth; | |
838 | if (mandated_break_chain_depth != -1) | |
839 | break_chain_depth = mandated_break_chain_depth; | |
840 | if (mandated_superstring_depth != -1) | |
841 | superstring_depth = mandated_superstring_depth; | |
842 | if (mandated_branch_depth != -1) | |
843 | branch_depth = mandated_branch_depth; | |
844 | if (mandated_fourlib_depth != -1) | |
845 | fourlib_depth = mandated_fourlib_depth; | |
846 | if (mandated_ko_depth != -1) | |
847 | ko_depth = mandated_ko_depth; | |
848 | if (mandated_aa_depth != -1) | |
849 | aa_depth = mandated_aa_depth; | |
850 | if (mandated_owl_distrust_depth != -1) | |
851 | owl_distrust_depth = mandated_owl_distrust_depth; | |
852 | if (mandated_owl_branch_depth != -1) | |
853 | owl_branch_depth = mandated_owl_branch_depth; | |
854 | if (mandated_owl_reading_depth != -1) | |
855 | owl_reading_depth = mandated_owl_reading_depth; | |
856 | if (mandated_owl_node_limit != -1) | |
857 | owl_node_limit = mandated_owl_node_limit; | |
858 | if (mandated_semeai_node_limit != -1) | |
859 | semeai_node_limit = mandated_semeai_node_limit; | |
860 | ||
861 | depth_offset = 0; | |
862 | ||
863 | if (report_levels) { | |
864 | fprintf(stderr, "at level %d:\n\n\ | |
865 | depth: %d\n\ | |
866 | branch_depth: %d\n\ | |
867 | backfill_depth: %d\n\ | |
868 | backfill2_depth: %d\n\ | |
869 | break_chain_depth: %d\n\ | |
870 | owl_distrust_depth: %d\n\ | |
871 | owl_branch_depth: %d\n\ | |
872 | owl_reading_depth: %d\n\ | |
873 | aa_depth: %d\n\ | |
874 | ko_depth: %d\n\ | |
875 | fourlib_depth: %d\n\ | |
876 | superstring_depth: %d\n\ | |
877 | owl_node_limit: %d\n\ | |
878 | semeai_branch_depth: %d\n\ | |
879 | semeai_branch_depth2: %d\n\ | |
880 | semeai_node_limit: %d\n\ | |
881 | connect_depth: %d\n\ | |
882 | connect_depth2: %d\n\ | |
883 | connection_node_limit: %d\n\ | |
884 | breakin_depth: %d\n\ | |
885 | breakin_node_limit: %d\n\n", | |
886 | level, depth, branch_depth, backfill_depth, backfill2_depth, | |
887 | break_chain_depth, owl_distrust_depth, owl_branch_depth, | |
888 | owl_reading_depth, aa_depth, ko_depth, fourlib_depth, | |
889 | superstring_depth, owl_node_limit, semeai_branch_depth, | |
890 | semeai_branch_depth2, semeai_node_limit, connect_depth, | |
891 | connect_depth2, connection_node_limit, breakin_depth, | |
892 | breakin_node_limit); | |
893 | } | |
894 | } | |
895 | ||
896 | ||
897 | static int depth_modification = 0; | |
898 | ||
899 | /* | |
900 | * Modify the various tactical reading depth parameters. This is | |
901 | * typically used to avoid horizon effects. By temporarily increasing | |
902 | * the depth values when trying some move, one can avoid that an | |
903 | * irrelevant move seems effective just because the reading hits a | |
904 | * depth limit earlier than it did when reading only on relevant | |
905 | * moves. | |
906 | */ | |
907 | ||
908 | void | |
909 | modify_depth_values(int n) | |
910 | { | |
911 | depth += n; | |
912 | backfill_depth += n; | |
913 | backfill2_depth += n; | |
914 | break_chain_depth += n; | |
915 | superstring_depth += n; | |
916 | branch_depth += n; | |
917 | fourlib_depth += n; | |
918 | ko_depth += n; | |
919 | breakin_depth += n; | |
920 | depth_offset += n; | |
921 | depth_modification += n; | |
922 | } | |
923 | ||
924 | void | |
925 | increase_depth_values(void) | |
926 | { | |
927 | modify_depth_values(1); | |
928 | } | |
929 | ||
930 | void | |
931 | decrease_depth_values(void) | |
932 | { | |
933 | modify_depth_values(-1); | |
934 | } | |
935 | ||
936 | int | |
937 | get_depth_modification(void) | |
938 | { | |
939 | return depth_modification; | |
940 | } | |
941 | ||
942 | ||
943 | /******************* | |
944 | * Detect blunders * | |
945 | *******************/ | |
946 | ||
947 | static int detect_owl_blunder(int move, int color, int *defense_point, | |
948 | signed char safe_stones[BOARDMAX], int liberties, | |
949 | float *return_value, int save_verbose); | |
950 | ||
951 | static void detect_tactical_blunder(int move, int color, int *defense_point, | |
952 | signed char safe_stones[BOARDMAX], | |
953 | int liberties, int *libs, | |
954 | float *return_value, int save_verbose); | |
955 | ||
956 | /* Check that the move at color doesn't involve any kind of blunder, | |
957 | * regardless of size. | |
958 | */ | |
959 | int | |
960 | confirm_safety(int move, int color, int *defense_point, | |
961 | signed char safe_stones[BOARDMAX]) | |
962 | { | |
963 | return (blunder_size(move, color, defense_point, safe_stones) == 0.0); | |
964 | } | |
965 | ||
966 | /* This function will detect some blunders. If the move reduces the | |
967 | * number of liberties of an adjacent friendly string, there is a | |
968 | * danger that the move could backfire, so the function checks that no | |
969 | * friendly worm which was formerly not attackable becomes attackable, | |
970 | * and it checks that no opposing worm which was not defendable | |
971 | * becomes defendable. | |
972 | * | |
973 | * It returns the estimated size of the blunder, or 0.0 if nothing | |
974 | * bad has happened. | |
975 | * | |
976 | * The array safe_stones[] contains the stones that are supposedly | |
977 | * safe after (move). It may be NULL. | |
978 | * | |
979 | * For use when called from fill_liberty, this function may optionally | |
980 | * return a point of defense, which, if taken, will presumably make | |
981 | * the move at (move) safe on a subsequent turn. | |
982 | */ | |
983 | ||
984 | float | |
985 | blunder_size(int move, int color, int *defense_point, | |
986 | signed char safe_stones[BOARDMAX]) | |
987 | { | |
988 | int libs[5]; | |
989 | int liberties = accuratelib(move, color, 5, libs); | |
990 | int trouble = 0; | |
991 | int save_verbose = verbose; | |
992 | float return_value = 0.0; | |
993 | int atari; | |
994 | signed char defense_moves[BOARDMAX]; | |
995 | ||
996 | if (defense_point) | |
997 | *defense_point = NO_MOVE; | |
998 | ||
999 | TRACE("Checking safety of a %s move at %1m\n", color_to_string(color), move); | |
1000 | ||
1001 | if (verbose > 0) | |
1002 | verbose--; | |
1003 | ||
1004 | /* We start by checking whether we have accidentally killed an own | |
1005 | * dragon. | |
1006 | */ | |
1007 | trouble = detect_owl_blunder(move, color, defense_point, | |
1008 | safe_stones, liberties, | |
1009 | &return_value, save_verbose); | |
1010 | ||
1011 | ||
1012 | /* Next we see whether the move has caused tactical complications. | |
1013 | * The trouble variable is set if a string next to the move with few | |
1014 | * liberties has not gained liberties by the move. | |
1015 | */ | |
1016 | if (trouble) | |
1017 | detect_tactical_blunder(move, color, defense_point, safe_stones, | |
1018 | liberties, libs, &return_value, save_verbose); | |
1019 | ||
1020 | /* FIXME: We would also need a detect_semeai_blunder() to check | |
1021 | * against moves which make the outcome of a semeai worse, e.g. by | |
1022 | * letting the opponent live in seki. | |
1023 | */ | |
1024 | ||
1025 | ||
1026 | /* Finally we call the atari-atari code to see whether the move has | |
1027 | * set up some combination attack that didn't exist before. We do | |
1028 | * this last to avoid duplicate blunder reports. | |
1029 | */ | |
1030 | atari = atari_atari_blunder_size(color, move, defense_moves, safe_stones); | |
1031 | if (atari) { | |
1032 | if (defense_point) { | |
1033 | /* FIXME: Choose defense point more systematically. */ | |
1034 | int pos; | |
1035 | *defense_point = NO_MOVE; | |
1036 | for (pos = BOARDMIN; pos < BOARDMAX; pos++) | |
1037 | if (ON_BOARD(pos) && defense_moves[pos]) { | |
1038 | *defense_point = pos; | |
1039 | break; | |
1040 | } | |
1041 | } | |
1042 | verbose = save_verbose; | |
1043 | TRACE("Combination attack appears.\n"); | |
1044 | return_value += (float) atari; | |
1045 | } | |
1046 | ||
1047 | verbose = save_verbose; | |
1048 | return return_value; | |
1049 | } | |
1050 | ||
1051 | /* Check whether we have accidentally killed an own dragon adjacent to | |
1052 | * move. If this happens, we mark its stones as no longer safe, and | |
1053 | * remember the dragon's size. | |
1054 | */ | |
1055 | ||
1056 | static int | |
1057 | detect_owl_blunder(int move, int color, int *defense_point, | |
1058 | signed char safe_stones[BOARDMAX], int liberties, | |
1059 | float *return_value, int save_verbose) | |
1060 | { | |
1061 | int k; | |
1062 | int ii; | |
1063 | int trouble = 0; | |
1064 | int dragon_analyzed[4] = {0, 0, 0, 0}; | |
1065 | int current_verbose = verbose; | |
1066 | ||
1067 | for (k = 0; k < 4; k++) { | |
1068 | int bpos = move + delta[k]; | |
1069 | int j; | |
1070 | /* We get worried if there is a liberty problem (and in this case | |
1071 | * there might also be tactical trouble), or if we play inside | |
1072 | * our eye space and the dragon is only just alive. | |
1073 | */ | |
1074 | if (board[bpos] != color) | |
1075 | continue; | |
1076 | if (liberties <= worm[bpos].liberties | |
1077 | && liberties <= 4) | |
1078 | trouble = 1; | |
1079 | else | |
1080 | if (min_eyes(&(DRAGON2(bpos).genus)) > 2 | |
1081 | || !is_proper_eye_space(move)) | |
1082 | continue; | |
1083 | ||
1084 | /* Don't test the same dragon twice. */ | |
1085 | for (j = 0; j < k; j++) | |
1086 | if (dragon_analyzed[j] == dragon[bpos].origin) | |
1087 | break; | |
1088 | if (j < k) | |
1089 | continue; | |
1090 | dragon_analyzed[k] = dragon[bpos].origin; | |
1091 | ||
1092 | /* Don't reanalyze if (move) is an owl defense for (bpos). */ | |
1093 | if (safe_stones && safe_stones[bpos] == OWL_SAVED_STONE) | |
1094 | continue; | |
1095 | ||
1096 | if ((dragon[bpos].status == ALIVE | |
1097 | || (safe_stones | |
1098 | && safe_stones[bpos])) | |
1099 | && DRAGON2(bpos).safety != INVINCIBLE | |
1100 | && DRAGON2(bpos).safety != STRONGLY_ALIVE) { | |
1101 | int kworm = NO_MOVE; | |
1102 | int acode = owl_confirm_safety(move, bpos, defense_point, &kworm); | |
1103 | ||
1104 | /* If owl couldn't confirm safety, maybe semeai can. */ | |
1105 | if (acode != WIN) { | |
1106 | int r; | |
1107 | for (r = 0; r < DRAGON2(bpos).neighbors; r++) { | |
1108 | int neighbor = dragon2[DRAGON2(bpos).adjacent[r]].origin; | |
1109 | int resultb; | |
1110 | if (board[neighbor] == color) | |
1111 | continue; | |
1112 | owl_analyze_semeai_after_move(move, color, neighbor, bpos, | |
1113 | NULL, &resultb, NULL, 1, NULL, 0); | |
1114 | if (resultb == 0) | |
1115 | acode = WIN; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | if (acode == 0) { | |
1120 | verbose = save_verbose; | |
1121 | TRACE("Dragon at %1m becomes attackable.\n", bpos); | |
1122 | verbose = current_verbose; | |
1123 | *return_value += 2.0 * dragon[bpos].effective_size; | |
1124 | if (safe_stones) | |
1125 | mark_dragon(bpos, safe_stones, 0); | |
1126 | } | |
1127 | else if (acode == LOSS) { | |
1128 | verbose = save_verbose; | |
1129 | TRACE("Dragon at %1m becomes attackable.\n", bpos); | |
1130 | verbose = current_verbose; | |
1131 | if (kworm == move) { | |
1132 | int l; | |
1133 | /* the worm origin was messed by our own move */ | |
1134 | for (l = 0; l < 4; l++) { | |
1135 | int kworm = move + delta[l]; | |
1136 | if (board[kworm] == color) { | |
1137 | *return_value += 2.0 * worm[kworm].effective_size; | |
1138 | if (safe_stones) | |
1139 | for (ii = BOARDMIN; ii < BOARDMAX; ii++) | |
1140 | if (ON_BOARD(ii) && worm[ii].origin == worm[kworm].origin) | |
1141 | safe_stones[ii] = 0; | |
1142 | } | |
1143 | } | |
1144 | } | |
1145 | else { | |
1146 | *return_value += 2.0 * worm[kworm].effective_size; | |
1147 | if (safe_stones) | |
1148 | for (ii = BOARDMIN; ii < BOARDMAX; ii++) | |
1149 | if (ON_BOARD(ii) && worm[ii].origin == worm[kworm].origin) | |
1150 | safe_stones[ii] = 0; | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | return trouble; | |
1157 | } | |
1158 | ||
1159 | /* Check whether a move causes any unexpected and unwelcome changes in | |
1160 | * the tactical status of worms all over the board. | |
1161 | */ | |
1162 | static void | |
1163 | detect_tactical_blunder(int move, int color, int *defense_point, | |
1164 | signed char safe_stones[BOARDMAX], | |
1165 | int liberties, int *libs, | |
1166 | float *return_value, int save_verbose) | |
1167 | { | |
1168 | int other = OTHER_COLOR(color); | |
1169 | int pos; | |
1170 | int ii; | |
1171 | int current_verbose = verbose; | |
1172 | ||
1173 | if (!trymove(move, color, NULL, NO_MOVE)) | |
1174 | return; | |
1175 | ||
1176 | /* Need to increase the depth values during this reading to avoid | |
1177 | * horizon effects. | |
1178 | */ | |
1179 | increase_depth_values(); | |
1180 | ||
1181 | for (pos = BOARDMIN; pos < BOARDMAX; pos++) { | |
1182 | if (!IS_STONE(board[pos]) | |
1183 | || worm[pos].origin != pos | |
1184 | || pos == move) | |
1185 | continue; | |
1186 | ||
1187 | /* First, we look for a new tactical attack. | |
1188 | * FIXME: Verify that the tactically attacked stone matters. See | |
1189 | * e.g. the D6 move in filllib:51 which invites a harmless | |
1190 | * tactical attack of A4. | |
1191 | */ | |
1192 | if (board[pos] == color | |
1193 | && ((safe_stones && safe_stones[pos]) | |
1194 | || (!safe_stones && worm[pos].attack_codes[0] == 0)) | |
1195 | && attack(pos, NULL)) { | |
1196 | /* A safe worm of ours has become attackable. */ | |
1197 | if (defense_point) { | |
1198 | find_defense(pos, defense_point); | |
1199 | /* Check that this move is legal and effective also on the | |
1200 | * original board, otherwise find a tactical defense there | |
1201 | * instead. | |
1202 | */ | |
1203 | popgo(); | |
1204 | ||
1205 | if (!is_legal(*defense_point, color) | |
1206 | || play_attack_defend_n(color, 1, 1, *defense_point, pos)) | |
1207 | find_defense(pos, defense_point); | |
1208 | ||
1209 | /* Redo the move, we know that it won't fail. */ | |
1210 | trymove(move, color, NULL, NO_MOVE); | |
1211 | } | |
1212 | verbose = save_verbose; | |
1213 | TRACE("After %1m Worm at %1m becomes attackable.\n", move, pos); | |
1214 | verbose = current_verbose; | |
1215 | *return_value += worm[pos].effective_size; | |
1216 | if (safe_stones) /* Can't use mark_string. */ | |
1217 | for (ii = BOARDMIN; ii < BOARDMAX; ii++) | |
1218 | if (worm[ii].origin == worm[pos].origin) | |
1219 | safe_stones[ii] = 0; | |
1220 | } | |
1221 | else if (board[pos] == other | |
1222 | && worm[pos].origin == pos | |
1223 | && worm[pos].attack_codes[0] != 0 | |
1224 | && worm[pos].defense_codes[0] == 0 | |
1225 | && find_defense(pos, NULL)) { | |
1226 | /* A dead opponent's worm has become defendable. | |
1227 | * Also ask the owl code whether the string can live | |
1228 | * strategically. To do this we need to temporarily undo | |
1229 | * the trymove(). | |
1230 | */ | |
1231 | int owl_attacks; | |
1232 | int defense_effective = 0; | |
1233 | ||
1234 | popgo(); | |
1235 | decrease_depth_values(); | |
1236 | owl_attacks = owl_does_attack(move, pos, NULL); | |
1237 | if (owl_attacks != WIN) { | |
1238 | *return_value += 2 * worm[pos].effective_size; | |
1239 | defense_effective = 1; | |
1240 | verbose = save_verbose; | |
1241 | TRACE("After %1m worm at %1m becomes defendable - A.\n", move, pos); | |
1242 | verbose = current_verbose; | |
1243 | } | |
1244 | else if (dragon[pos].status != ALIVE) { | |
1245 | /* Before redoing the trymove we also check whether the worm now | |
1246 | * has a semeai defense. See blunder:26 for an example. | |
1247 | * | |
1248 | * If the worm already was alive in seki, it is generally okay | |
1249 | * that it also becomes tactically safe when the outer | |
1250 | * liberties are filled, see e.g. blunder:32,34. Thus the | |
1251 | * check above. | |
1252 | */ | |
1253 | int k; | |
1254 | int adj[MAXCHAIN]; | |
1255 | int num_adj; | |
1256 | num_adj = extended_chainlinks(pos, adj, 0); | |
1257 | for (k = 0; k < num_adj; k++) { | |
1258 | int neighbor = adj[k]; | |
1259 | int resulta; | |
1260 | owl_analyze_semeai_after_move(move, color, pos, neighbor, | |
1261 | &resulta, NULL, NULL, 1, NULL, 1); | |
1262 | if (resulta != 0) { | |
1263 | *return_value += 2 * worm[pos].effective_size; | |
1264 | defense_effective = 1; | |
1265 | verbose = save_verbose; | |
1266 | TRACE("After %1m worm at %1m becomes defendable - B.\n", | |
1267 | move, pos); | |
1268 | verbose = current_verbose; | |
1269 | break; | |
1270 | } | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | trymove(move, color, NULL, NO_MOVE); | |
1275 | increase_depth_values(); | |
1276 | ||
1277 | if (defense_effective && defense_point) { | |
1278 | int dpos; | |
1279 | if (attack(pos, &dpos)) { | |
1280 | *defense_point = dpos; | |
1281 | /* Check that this move is legal and effective also on the | |
1282 | * original board, otherwise find a tactical attack there | |
1283 | * instead. | |
1284 | */ | |
1285 | popgo(); | |
1286 | ||
1287 | if (!is_legal(dpos, color) | |
1288 | || play_attack_defend_n(color, 0, 1, dpos, pos)) | |
1289 | attack(pos, defense_point); | |
1290 | ||
1291 | /* Redo the move, we know that it won't fail. */ | |
1292 | trymove(move, color, NULL, NO_MOVE); | |
1293 | } | |
1294 | else { | |
1295 | verbose = save_verbose; | |
1296 | TRACE("No attack found (unexpectedly) on %1m after move at %1m.\n", | |
1297 | pos, move); | |
1298 | verbose = current_verbose; | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | /* Look for double atari style complications of the move. | |
1305 | * | |
1306 | * FIXME: Since we have an atari_atari check in blunder_size(), do | |
1307 | * we still need to do this step? | |
1308 | */ | |
1309 | if (liberties == 2) { | |
1310 | float d_a_blunder_size; | |
1311 | if (double_atari(libs[0], other, &d_a_blunder_size, safe_stones)) { | |
1312 | if (defense_point && safe_move(libs[0], color) == WIN) | |
1313 | *defense_point = libs[0]; | |
1314 | *return_value += d_a_blunder_size; | |
1315 | verbose = save_verbose; | |
1316 | TRACE("Double threat appears at %1m.\n", libs[0]); | |
1317 | verbose = current_verbose; | |
1318 | } | |
1319 | else if (double_atari(libs[1], other, &d_a_blunder_size, safe_stones)) { | |
1320 | if (defense_point && safe_move(libs[1], color) == WIN) | |
1321 | *defense_point = libs[1]; | |
1322 | *return_value += d_a_blunder_size; | |
1323 | verbose = save_verbose; | |
1324 | TRACE("Double threat appears at %1m.\n", libs[1]); | |
1325 | verbose = current_verbose; | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /* Reset the depth values. */ | |
1330 | decrease_depth_values(); | |
1331 | ||
1332 | popgo(); | |
1333 | } | |
1334 | ||
1335 | ||
1336 | /* Returns true if a move by (color) fits the following shape: | |
1337 | * | |
1338 | * | |
1339 | * X* (O=color) | |
1340 | * OX | |
1341 | * | |
1342 | * capturing one of the two X strings. The name is a slight | |
1343 | * misnomer since this includes attacks which are not necessarily | |
1344 | * double ataris, though the common double atari is the most | |
1345 | * important special case. | |
1346 | * | |
1347 | * If safe_stones != NULL, then only attacks on stones marked as safe are | |
1348 | * tried. | |
1349 | * | |
1350 | * The value of the double atari attack is returned in *value (unless | |
1351 | * value is NULL), and the attacked stones are marked unsafe. | |
1352 | */ | |
1353 | ||
1354 | int | |
1355 | double_atari(int move, int color, float *value, | |
1356 | signed char safe_stones[BOARDMAX]) | |
1357 | { | |
1358 | int other = OTHER_COLOR(color); | |
1359 | int k; | |
1360 | int m = I(move); | |
1361 | int n = J(move); | |
1362 | ||
1363 | if (!ON_BOARD(move)) | |
1364 | return 0; | |
1365 | ||
1366 | /* Loop over the diagonal directions. */ | |
1367 | for (k = 4; k < 8; k++) { | |
1368 | int dm = deltai[k]; | |
1369 | int dn = deltaj[k]; | |
1370 | ||
1371 | /* because (m, n) and (m+dm, n+dn) are opposite | |
1372 | * corners of a square, ON_BOARD2(m, n) && ON_BOARD2(m+dm, n+dn) | |
1373 | * implies ON_BOARD2(m+dm, n) and ON_BOARD2(n, n+dn) | |
1374 | * | |
1375 | * Only try to attack supposedly safe stones. | |
1376 | */ | |
1377 | if (BOARD(m+dm, n+dn) == color | |
1378 | && BOARD(m, n+dn) == other | |
1379 | && BOARD(m+dm, n) == other | |
1380 | && (!safe_stones | |
1381 | || (safe_stones[POS(m, n+dn)] && safe_stones[POS(m+dm, n)])) | |
1382 | && trymove(move, color, "double_atari", NO_MOVE)) { | |
1383 | if (countlib(move) > 1 | |
1384 | && (BOARD(m, n+dn) == EMPTY || BOARD(m+dm, n) == EMPTY | |
1385 | || !defend_both(POS(m, n+dn), POS(m+dm, n)))) { | |
1386 | popgo(); | |
1387 | if (value) { | |
1388 | if (worm[POS(m, n+dn)].effective_size | |
1389 | > worm[POS(m+dm, n)].effective_size) { | |
1390 | *value = 2.0 * worm[POS(m, n+dn)].effective_size; | |
1391 | if (safe_stones) | |
1392 | mark_string(POS(m, n+dn), safe_stones, 0); | |
1393 | } | |
1394 | else { | |
1395 | *value = 2.0 * worm[POS(m+dm, n)].effective_size; | |
1396 | if (safe_stones) | |
1397 | mark_string(POS(m+dm, n), safe_stones, 0); | |
1398 | } | |
1399 | } | |
1400 | return 1; | |
1401 | } | |
1402 | popgo(); | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | ||
1410 | /* Returns true if a move by (color) plays into a snapback. */ | |
1411 | int | |
1412 | playing_into_snapback(int move, int color) | |
1413 | { | |
1414 | int libs[2]; | |
1415 | int k; | |
1416 | ||
1417 | if (approxlib(move, color, 1, NULL) != 0 | |
1418 | || accuratelib(move, color, 2, libs) != 1) | |
1419 | return 0; | |
1420 | ||
1421 | for (k = 0; k < 4; k++) | |
1422 | if (board[move + delta[k]] == color | |
1423 | && adjacent_strings(libs[0], move + delta[k])) | |
1424 | return 1; | |
1425 | ||
1426 | return 0; | |
1427 | } | |
1428 | ||
1429 | ||
1430 | /* Score the game and determine the winner */ | |
1431 | ||
1432 | void | |
1433 | who_wins(int color, FILE *outfile) | |
1434 | { | |
1435 | float result; | |
1436 | ||
1437 | silent_examine_position(EXAMINE_DRAGONS); | |
1438 | ||
1439 | #if 0 | |
1440 | float white_score; | |
1441 | float black_score; | |
1442 | int winner; | |
1443 | #endif | |
1444 | ||
1445 | if (color != BLACK && color != WHITE) | |
1446 | color = BLACK; | |
1447 | ||
1448 | #if 0 | |
1449 | /* Use the aftermath code to compute the final score. (Slower but | |
1450 | * more reliable.) | |
1451 | */ | |
1452 | result = aftermath_compute_score(color, NULL); | |
1453 | if (result > 0.0) | |
1454 | winner = WHITE; | |
1455 | else { | |
1456 | winner = BLACK; | |
1457 | result = -result; | |
1458 | } | |
1459 | #endif | |
1460 | ||
1461 | result = (white_score + black_score)/2.0; | |
1462 | if (result == 0.0) | |
1463 | fprintf(outfile, "Result: jigo "); | |
1464 | else | |
1465 | fprintf(outfile, "Result: %c+%.1f ", | |
1466 | (result > 0.0) ? 'W' : 'B', gg_abs(result)); | |
1467 | } | |
1468 | ||
1469 | ||
1470 | ||
1471 | /* Find the stones of an extended string, where the extensions are | |
1472 | * through the following kinds of connections: | |
1473 | * | |
1474 | * 1. Solid connections (just like ordinary string). | |
1475 | * | |
1476 | * OO | |
1477 | * | |
1478 | * 2. Diagonal connection or one space jump through an intersection | |
1479 | * where an opponent move would be suicide or self-atari. | |
1480 | * | |
1481 | * ... | |
1482 | * O.O | |
1483 | * XOX | |
1484 | * X.X | |
1485 | * | |
1486 | * 3. Bamboo joint. | |
1487 | * | |
1488 | * OO | |
1489 | * .. | |
1490 | * OO | |
1491 | * | |
1492 | * 4. Diagonal connection where both adjacent intersections are empty. | |
1493 | * | |
1494 | * .O | |
1495 | * O. | |
1496 | * | |
1497 | * 5. Connection through adjacent or diagonal tactically captured stones. | |
1498 | * Connections of this type are omitted when the superstring code is | |
1499 | * called from reading.c, but included when the superstring code is | |
1500 | * called from owl.c | |
1501 | */ | |
1502 | ||
1503 | static void | |
1504 | do_find_superstring(int str, int *num_stones, int *stones, | |
1505 | int *num_lib, int *libs, int maxlibs, | |
1506 | int *num_adj, int *adjs, int liberty_cap, | |
1507 | int proper, int type); | |
1508 | ||
1509 | static void | |
1510 | superstring_add_string(int str, | |
1511 | int *num_my_stones, int *my_stones, | |
1512 | int *num_stones, int *stones, | |
1513 | int *num_libs, int *libs, int maxlibs, | |
1514 | int *num_adj, int *adjs, int liberty_cap, | |
1515 | signed char mx[BOARDMAX], | |
1516 | signed char ml[BOARDMAX], | |
1517 | signed char ma[BOARDMAX], | |
1518 | int do_add); | |
1519 | ||
1520 | void | |
1521 | find_superstring(int str, int *num_stones, int *stones) | |
1522 | { | |
1523 | do_find_superstring(str, num_stones, stones, | |
1524 | NULL, NULL, 0, | |
1525 | NULL, NULL, 0, | |
1526 | 0, 1); | |
1527 | } | |
1528 | ||
1529 | /* This is the same as find_superstring, except that connections of | |
1530 | * type 5 are omitted. This is used in semeai analysis. | |
1531 | */ | |
1532 | void | |
1533 | find_superstring_conservative(int str, int *num_stones, int *stones) | |
1534 | { | |
1535 | do_find_superstring(str, num_stones, stones, | |
1536 | NULL, NULL, 0, | |
1537 | NULL, NULL, 0, | |
1538 | 0, 0); | |
1539 | } | |
1540 | ||
1541 | ||
1542 | /* This function computes the superstring at (str) as described above, | |
1543 | * but omitting connections of type 5. Then it constructs a list of | |
1544 | * liberties of the superstring which are not already liberties of | |
1545 | * (str). | |
1546 | * | |
1547 | * If liberty_cap is nonzero, only liberties of substrings of the | |
1548 | * superstring which have fewer than liberty_cap liberties are | |
1549 | * generated. | |
1550 | */ | |
1551 | ||
1552 | void | |
1553 | find_superstring_liberties(int str, | |
1554 | int *num_libs, int *libs, int liberty_cap) | |
1555 | { | |
1556 | do_find_superstring(str, NULL, NULL, | |
1557 | num_libs, libs, MAX_LIBERTIES, | |
1558 | NULL, NULL, liberty_cap, | |
1559 | 0, 0); | |
1560 | } | |
1561 | ||
1562 | /* This function is the same as find_superstring_liberties, but it | |
1563 | * omits those liberties of the string (str), presumably since | |
1564 | * those have already been treated elsewhere. | |
1565 | * | |
1566 | * If liberty_cap is nonzero, only liberties of substrings of the | |
1567 | * superstring which have at most liberty_cap liberties are | |
1568 | * generated. | |
1569 | */ | |
1570 | ||
1571 | void | |
1572 | find_proper_superstring_liberties(int str, | |
1573 | int *num_libs, int *libs, | |
1574 | int liberty_cap) | |
1575 | { | |
1576 | do_find_superstring(str, NULL, NULL, | |
1577 | num_libs, libs, MAX_LIBERTIES, | |
1578 | NULL, NULL, liberty_cap, | |
1579 | 1, 0); | |
1580 | } | |
1581 | ||
1582 | /* This function computes the superstring at (str) as described above, | |
1583 | * but omitting connections of type 5. Then it constructs a list of | |
1584 | * liberties of the superstring which are not already liberties of | |
1585 | * (str). | |
1586 | * | |
1587 | * If liberty_cap is nonzero, only liberties of substrings of the | |
1588 | * superstring which have fewer than liberty_cap liberties are | |
1589 | * generated. | |
1590 | */ | |
1591 | ||
1592 | void | |
1593 | find_superstring_stones_and_liberties(int str, | |
1594 | int *num_stones, int *stones, | |
1595 | int *num_libs, int *libs, | |
1596 | int liberty_cap) | |
1597 | { | |
1598 | do_find_superstring(str, num_stones, stones, | |
1599 | num_libs, libs, MAX_LIBERTIES, | |
1600 | NULL, NULL, liberty_cap, | |
1601 | 0, 0); | |
1602 | } | |
1603 | ||
1604 | /* analogous to chainlinks, this function finds boundary chains of the | |
1605 | * superstring at (str), including those which are boundary chains of | |
1606 | * (str) itself. If liberty_cap != 0, only those boundary chains with | |
1607 | * <= liberty_cap liberties are reported. | |
1608 | */ | |
1609 | ||
1610 | void | |
1611 | superstring_chainlinks(int str, | |
1612 | int *num_adj, int adjs[MAXCHAIN], | |
1613 | int liberty_cap) | |
1614 | { | |
1615 | do_find_superstring(str, NULL, NULL, | |
1616 | NULL, NULL, 0, | |
1617 | num_adj, adjs, liberty_cap, | |
1618 | 0, 2); | |
1619 | } | |
1620 | ||
1621 | ||
1622 | /* analogous to chainlinks, this function finds boundary chains of the | |
1623 | * superstring at (str), omitting those which are boundary chains of | |
1624 | * (str) itself. If liberty_cap != 0, only those boundary chains with | |
1625 | * <= liberty_cap liberties are reported. | |
1626 | */ | |
1627 | ||
1628 | void | |
1629 | proper_superstring_chainlinks(int str, | |
1630 | int *num_adj, int adjs[MAXCHAIN], | |
1631 | int liberty_cap) | |
1632 | { | |
1633 | do_find_superstring(str, NULL, NULL, | |
1634 | NULL, NULL, 0, | |
1635 | num_adj, adjs, liberty_cap, | |
1636 | 1, 2); | |
1637 | } | |
1638 | ||
1639 | /* Do the real work finding the superstring and recording stones, | |
1640 | * liberties, and/or adjacent strings. | |
1641 | */ | |
1642 | static void | |
1643 | do_find_superstring(int str, int *num_stones, int *stones, | |
1644 | int *num_libs, int *libs, int maxlibs, | |
1645 | int *num_adj, int *adjs, int liberty_cap, | |
1646 | int proper, int type) | |
1647 | { | |
1648 | int num_my_stones; | |
1649 | int my_stones[MAX_BOARD * MAX_BOARD]; | |
1650 | ||
1651 | signed char mx[BOARDMAX]; /* stones */ | |
1652 | signed char ml[BOARDMAX]; /* liberties */ | |
1653 | signed char ma[BOARDMAX]; /* adjacent strings */ | |
1654 | ||
1655 | int k, l, r; | |
1656 | int color = board[str]; | |
1657 | int other = OTHER_COLOR(color); | |
1658 | ||
1659 | memset(mx, 0, sizeof(mx)); | |
1660 | memset(ml, 0, sizeof(ml)); | |
1661 | memset(ma, 0, sizeof(ma)); | |
1662 | ||
1663 | if (num_stones) | |
1664 | *num_stones = 0; | |
1665 | if (num_libs) | |
1666 | *num_libs = 0; | |
1667 | if (num_adj) | |
1668 | *num_adj = 0; | |
1669 | ||
1670 | /* Include the string itself in the superstring. Only record stones, | |
1671 | * liberties, and/or adjacent strings if proper==0. | |
1672 | */ | |
1673 | num_my_stones = 0; | |
1674 | superstring_add_string(str, &num_my_stones, my_stones, | |
1675 | num_stones, stones, | |
1676 | num_libs, libs, maxlibs, | |
1677 | num_adj, adjs, liberty_cap, | |
1678 | mx, ml, ma, | |
1679 | !proper); | |
1680 | ||
1681 | /* Loop over all found stones, looking for more strings to include | |
1682 | * in the superstring. The loop is automatically extended over later | |
1683 | * found stones as well. | |
1684 | */ | |
1685 | for (r = 0; r < num_my_stones; r++) { | |
1686 | int pos = my_stones[r]; | |
1687 | ||
1688 | for (k = 0; k < 4; k++) { | |
1689 | /* List of relative coordinates. (pos) is marked by *. | |
1690 | * | |
1691 | * ef. | |
1692 | * gb. | |
1693 | * *ac | |
1694 | * .d. | |
1695 | * | |
1696 | */ | |
1697 | int right = delta[k]; | |
1698 | int up = delta[(k+1)%4]; | |
1699 | ||
1700 | int apos = pos + right; | |
1701 | int bpos = pos + right + up; | |
1702 | int cpos = pos + 2*right; | |
1703 | int dpos = pos + right - up; | |
1704 | int epos = pos + 2*up; | |
1705 | int fpos = pos + right + 2*up; | |
1706 | int gpos = pos + up; | |
1707 | int unsafe_move; | |
1708 | ||
1709 | if (!ON_BOARD(apos)) | |
1710 | continue; | |
1711 | ||
1712 | /* Case 1. Nothing to do since stones are added string by string. */ | |
1713 | ||
1714 | /* Case 2. */ | |
1715 | if (board[apos] == EMPTY) { | |
1716 | if (type == 2) | |
1717 | unsafe_move = (approxlib(apos, other, 2, NULL) < 2); | |
1718 | else | |
1719 | unsafe_move = is_self_atari(apos, other); | |
1720 | ||
1721 | if (unsafe_move && type == 1 && is_ko(apos, other, NULL)) | |
1722 | unsafe_move = 0; | |
1723 | ||
1724 | if (unsafe_move) { | |
1725 | if (board[bpos] == color && !mx[bpos]) | |
1726 | superstring_add_string(bpos, &num_my_stones, my_stones, | |
1727 | num_stones, stones, | |
1728 | num_libs, libs, maxlibs, | |
1729 | num_adj, adjs, liberty_cap, | |
1730 | mx, ml, ma, 1); | |
1731 | if (board[cpos] == color && !mx[cpos]) | |
1732 | superstring_add_string(cpos, &num_my_stones, my_stones, | |
1733 | num_stones, stones, | |
1734 | num_libs, libs, maxlibs, | |
1735 | num_adj, adjs, liberty_cap, | |
1736 | mx, ml, ma, 1); | |
1737 | if (board[dpos] == color && !mx[dpos]) | |
1738 | superstring_add_string(dpos, &num_my_stones, my_stones, | |
1739 | num_stones, stones, | |
1740 | num_libs, libs, maxlibs, | |
1741 | num_adj, adjs, liberty_cap, | |
1742 | mx, ml, ma, 1); | |
1743 | } | |
1744 | } | |
1745 | ||
1746 | /* Case 3. */ | |
1747 | /* Notice that the order of these tests is significant. We must | |
1748 | * check bpos before fpos and epos to avoid accessing memory | |
1749 | * outside the board array. (Notice that fpos is two steps away | |
1750 | * from pos, which we know is on the board.) | |
1751 | */ | |
1752 | if (board[apos] == color && board[bpos] == EMPTY | |
1753 | && board[fpos] == color && board[epos] == color && !mx[epos] | |
1754 | && board[gpos] == EMPTY) | |
1755 | superstring_add_string(epos, &num_my_stones, my_stones, | |
1756 | num_stones, stones, | |
1757 | num_libs, libs, maxlibs, | |
1758 | num_adj, adjs, liberty_cap, | |
1759 | mx, ml, ma, 1); | |
1760 | /* Don't bother with f, it is part of the string just added. */ | |
1761 | ||
1762 | /* Case 4. */ | |
1763 | if (board[bpos] == color && !mx[bpos] | |
1764 | && board[apos] == EMPTY && board[gpos] == EMPTY) | |
1765 | superstring_add_string(bpos, &num_my_stones, my_stones, | |
1766 | num_stones, stones, | |
1767 | num_libs, libs, maxlibs, | |
1768 | num_adj, adjs, liberty_cap, | |
1769 | mx, ml, ma, 1); | |
1770 | ||
1771 | /* Case 5. */ | |
1772 | if (type == 1) | |
1773 | for (l = 0; l < 2; l++) { | |
1774 | int upos; | |
1775 | ||
1776 | if (l == 0) { | |
1777 | /* adjacent lunch */ | |
1778 | upos = apos; | |
1779 | } | |
1780 | else { | |
1781 | /* diagonal lunch */ | |
1782 | upos = bpos; | |
1783 | } | |
1784 | ||
1785 | if (board[upos] != other) | |
1786 | continue; | |
1787 | ||
1788 | upos = find_origin(upos); | |
1789 | ||
1790 | /* Only do the reading once. */ | |
1791 | if (mx[upos] == 1) | |
1792 | continue; | |
1793 | ||
1794 | mx[upos] = 1; | |
1795 | ||
1796 | if (attack(upos, NULL) | |
1797 | && !find_defense(upos, NULL)) { | |
1798 | int lunch_stones[MAX_BOARD*MAX_BOARD]; | |
1799 | int num_lunch_stones = findstones(upos, MAX_BOARD*MAX_BOARD, | |
1800 | lunch_stones); | |
1801 | int m, n; | |
1802 | for (m = 0; m < num_lunch_stones; m++) | |
1803 | for (n = 0; n < 8; n++) { | |
1804 | int vpos = lunch_stones[m] + delta[n]; | |
1805 | if (board[vpos] == color && !mx[vpos]) | |
1806 | superstring_add_string(vpos, | |
1807 | &num_my_stones, my_stones, | |
1808 | num_stones, stones, | |
1809 | num_libs, libs, maxlibs, | |
1810 | num_adj, adjs, liberty_cap, | |
1811 | mx, ml, ma, 1); | |
1812 | } | |
1813 | } | |
1814 | } | |
1815 | if (num_libs && maxlibs > 0 && *num_libs >= maxlibs) | |
1816 | return; | |
1817 | } | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | /* Add a new string to a superstring. Record stones, liberties, and | |
1822 | * adjacent strings as asked for. | |
1823 | */ | |
1824 | static void | |
1825 | superstring_add_string(int str, | |
1826 | int *num_my_stones, int *my_stones, | |
1827 | int *num_stones, int *stones, | |
1828 | int *num_libs, int *libs, int maxlibs, | |
1829 | int *num_adj, int *adjs, int liberty_cap, | |
1830 | signed char mx[BOARDMAX], | |
1831 | signed char ml[BOARDMAX], | |
1832 | signed char ma[BOARDMAX], | |
1833 | int do_add) | |
1834 | { | |
1835 | int num_my_libs; | |
1836 | int my_libs[MAXLIBS]; | |
1837 | int num_my_adj; | |
1838 | int my_adjs[MAXCHAIN]; | |
1839 | int new_stones; | |
1840 | int k; | |
1841 | ||
1842 | ASSERT1(mx[str] == 0, str); | |
1843 | ||
1844 | /* Pick up the stones of the new string. */ | |
1845 | new_stones = findstones(str, board_size * board_size, | |
1846 | &(my_stones[*num_my_stones])); | |
1847 | ||
1848 | mark_string(str, mx, 1); | |
1849 | if (stones) { | |
1850 | gg_assert(num_stones); | |
1851 | for (k = 0; k < new_stones; k++) { | |
1852 | if (do_add) { | |
1853 | stones[*num_stones] = my_stones[*num_my_stones + k]; | |
1854 | (*num_stones)++; | |
1855 | } | |
1856 | } | |
1857 | } | |
1858 | (*num_my_stones) += new_stones; | |
1859 | ||
1860 | /* Pick up the liberties of the new string. */ | |
1861 | if (libs) { | |
1862 | gg_assert(num_libs); | |
1863 | /* Get the liberties of the string. */ | |
1864 | num_my_libs = findlib(str, MAXLIBS, my_libs); | |
1865 | ||
1866 | /* Remove this string from the superstring if it has too many | |
1867 | * liberties. | |
1868 | */ | |
1869 | if (liberty_cap > 0 && num_my_libs > liberty_cap) | |
1870 | (*num_my_stones) -= new_stones; | |
1871 | ||
1872 | for (k = 0; k < num_my_libs; k++) { | |
1873 | if (ml[my_libs[k]]) | |
1874 | continue; | |
1875 | ml[my_libs[k]] = 1; | |
1876 | if (do_add && (liberty_cap == 0 || num_my_libs <= liberty_cap)) { | |
1877 | libs[*num_libs] = my_libs[k]; | |
1878 | (*num_libs)++; | |
1879 | if (*num_libs == maxlibs) | |
1880 | break; | |
1881 | } | |
1882 | } | |
1883 | } | |
1884 | ||
1885 | /* Pick up adjacent strings to the new string. */ | |
1886 | if (adjs) { | |
1887 | gg_assert(num_adj); | |
1888 | num_my_adj = chainlinks(str, my_adjs); | |
1889 | for (k = 0; k < num_my_adj; k++) { | |
1890 | if (liberty_cap > 0 && countlib(my_adjs[k]) > liberty_cap) | |
1891 | continue; | |
1892 | if (ma[my_adjs[k]]) | |
1893 | continue; | |
1894 | ma[my_adjs[k]] = 1; | |
1895 | if (do_add) { | |
1896 | adjs[*num_adj] = my_adjs[k]; | |
1897 | (*num_adj)++; | |
1898 | } | |
1899 | } | |
1900 | } | |
1901 | } | |
1902 | ||
1903 | /* Internal timers for assessing time spent on various tasks. */ | |
1904 | #define NUMBER_OF_TIMERS 4 | |
1905 | static double timers[NUMBER_OF_TIMERS]; | |
1906 | ||
1907 | /* Start a timer. */ | |
1908 | void | |
1909 | start_timer(int n) | |
1910 | { | |
1911 | gg_assert(n >= 0 && n < NUMBER_OF_TIMERS); | |
1912 | if (!showtime) | |
1913 | return; | |
1914 | ||
1915 | timers[n] = gg_cputime(); | |
1916 | } | |
1917 | ||
1918 | /* Report time spent and restart the timer. Make no report if elapsed | |
1919 | * time is less than mintime. | |
1920 | */ | |
1921 | double | |
1922 | time_report(int n, const char *occupation, int move, double mintime) | |
1923 | { | |
1924 | double t; | |
1925 | double dt; | |
1926 | gg_assert(n >= 0 && n < NUMBER_OF_TIMERS); | |
1927 | ||
1928 | if (!showtime) | |
1929 | return 0.0; | |
1930 | ||
1931 | t = gg_cputime(); | |
1932 | dt = t - timers[n]; | |
1933 | if (dt > mintime) { | |
1934 | gprintf("%s", occupation); | |
1935 | if (move != NO_MOVE) | |
1936 | gprintf("%1m", move); | |
1937 | fprintf(stderr, ": %.2f sec\n", dt); | |
1938 | } | |
1939 | timers[n] = t; | |
1940 | return dt; | |
1941 | } | |
1942 | ||
1943 | void | |
1944 | clearstats() | |
1945 | { | |
1946 | stats.nodes = 0; | |
1947 | stats.read_result_entered = 0; | |
1948 | stats.read_result_hits = 0; | |
1949 | stats.trusted_read_result_hits = 0; | |
1950 | } | |
1951 | ||
1952 | void | |
1953 | showstats() | |
1954 | { | |
1955 | gprintf("Nodes: %d\n", stats.nodes); | |
1956 | gprintf("Read results entered: %d\n", stats.read_result_entered); | |
1957 | gprintf("Read result hits: %d\n", stats.read_result_hits); | |
1958 | gprintf("Trusted read result hits: %d\n", stats.trusted_read_result_hits); | |
1959 | } | |
1960 | ||
1961 | ||
1962 | /* Set up a compiled in pattern database for use by the Monte Carlo | |
1963 | * code. If name is NULL, the first pattern database is used. | |
1964 | * | |
1965 | * The reason why this function and the next are placed here rather | |
1966 | * than in montecarlo.c is to keep that file free from dependency on | |
1967 | * patterns.h. | |
1968 | */ | |
1969 | int | |
1970 | choose_mc_patterns(char *name) | |
1971 | { | |
1972 | int k; | |
1973 | for (k = 0; mc_pattern_databases[k].name; k++) { | |
1974 | if (!name || strcmp(name, mc_pattern_databases[k].name) == 0) { | |
1975 | mc_init_patterns(mc_pattern_databases[k].values); | |
1976 | return 1; | |
1977 | } | |
1978 | } | |
1979 | ||
1980 | return 0; | |
1981 | } | |
1982 | ||
1983 | /* List compiled in Monte Carlo pattern databases. */ | |
1984 | void | |
1985 | list_mc_patterns(void) | |
1986 | { | |
1987 | int k; | |
1988 | printf("Available builtin Monte Carlo local patterns:\n\n"); | |
1989 | for (k = 0; mc_pattern_databases[k].name; k++) { | |
1990 | if (k == 0) | |
1991 | printf("* %s (default)\n", mc_pattern_databases[k].name); | |
1992 | else | |
1993 | printf("* %s\n", mc_pattern_databases[k].name); | |
1994 | } | |
1995 | printf("\nUse \"--mc-patterns name\" to choose one of these.\n"); | |
1996 | printf("Use \"--mc-load-patterns filename\" to directly load a pattern database.\n"); | |
1997 | } | |
1998 | ||
1999 | /* | |
2000 | * Local Variables: | |
2001 | * tab-width: 8 | |
2002 | * c-basic-offset: 2 | |
2003 | * End: | |
2004 | */ |