forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.c
1576 lines (1309 loc) · 42.9 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: game.c
Created: September 20, 1997
Modified: December 31, 2002
Author: Gunnar Andersson ([email protected])
Contents: All the routines needed to play a game.
*/
#include "porting.h"
#if !defined( _WIN32_WCE ) && !defined( __linux__ ) && !defined( __CYGWIN__ )
#include "dir.h"
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32_WCE
#include <assert.h>
#include <time.h>
#endif
#include "bitboard.h"
#include "constant.h"
#include "display.h"
#include "end.h"
#include "error.h"
#include "eval.h"
#include "game.h"
#include "getcoeff.h"
#include "globals.h"
#include "hash.h"
#include "macros.h"
#include "midgame.h"
#include "moves.h"
#include "myrandom.h"
#include "osfbook.h"
#include "patterns.h"
#include "probcut.h"
#include "search.h"
#include "stable.h"
#include "texts.h"
#include "thordb.h"
#include "timer.h"
#include "unflip.h"
/* The pre-ordering depth used when guessing the opponent's move */
#define PONDER_DEPTH 8
/* The depth to abandon the book move and try to solve the position
in time control games. */
#define FORCE_BOOK_SOLVE 30
/* The somewhat arbitrary point where endgame typically commences
when Zebra is running on today's hardware */
#define TYPICAL_SOLVE 27
/* The recommended extra depth gained by switching to the endgame searcher;
i.e., use the endgame solver with n+ENDGAME_OFFSET empty instead of the
midgame module with depth n. */
#define ENDGAME_OFFSET 7 /* WAS: 4 */
/* Same as ENDGAME_OFFSET but forces a solve to begin */
#define ENDGAME_FORCE_OFFSET 3
/* Solve earlier for lopsided positions? */
#define ADAPTIVE_SOLVE_DEPTH FALSE
/* The depth where no time is to be gained by using the fact that
there is only one move available. */
#ifdef _WIN32_WCE
#define DISABLE_FORCED_MOVES 70
#else
#define DISABLE_FORCED_MOVES 60
#endif
/* The file to which compute_move() writes its status reports */
#define LOG_FILE_NAME "zebra.log"
/* The maximum length of any system path. */
#define MAX_PATH_LENGTH 2048
static const char *forced_opening = NULL;
static char log_file_path[MAX_PATH_LENGTH];
static double last_time_used;
static int max_depth_reached;
#ifdef _WIN32_WCE
static int use_log_file = FALSE;
#else
static int use_log_file = TRUE;
#endif
static int play_human_openings = TRUE;
static int play_thor_match_openings = TRUE;
static int game_evaluated_count;
static int komi = 0;
static int prefix_move = 0;
static int endgame_performed[3];
static EvaluatedMove evaluated_list[60];
/*
TOGGLE_STATUS_LOG
Enable/disable the use of logging all the output that the
text version of Zebra would output to the screen.
*/
void
toggle_status_log( int write_log ) {
use_log_file = write_log;
}
/*
GLOBAL_SETUP
Initialize the different sub-systems.
*/
void
global_setup( int use_random, int hash_bits ) {
FILE *log_file;
time_t timer;
/* Clear the log file. No error handling done. */
#ifdef __linux__
strcpy( log_file_path, LOG_FILE_NAME );
#else
char directory_buffer[MAX_PATH_LENGTH];
getcwd( directory_buffer, MAX_PATH_LENGTH );
if ( directory_buffer[strlen( directory_buffer ) - 1] == '\\' )
sprintf( log_file_path, "%s%s", directory_buffer, LOG_FILE_NAME );
else
sprintf( log_file_path, "%s\\%s", directory_buffer, LOG_FILE_NAME );
#endif
if ( use_log_file ) {
log_file = fopen( log_file_path, "w" );
if ( log_file != NULL ) {
time( &timer );
fprintf( log_file, "%s %s\n", LOG_TEXT, ctime( &timer ) );
fprintf( log_file, "%s %s %s\n", ENGINE_TEXT, __DATE__, __TIME__ );
fclose( log_file );
}
}
if ( use_random ) {
time( &timer );
my_srandom( timer );
}
else
my_srandom( 1 );
init_hash( hash_bits );
init_bitboard();
init_moves();
init_patterns();
init_coeffs();
init_timer();
init_probcut();
init_stable();
setup_search();
}
/*
GLOBAL_TERMINATE
Free all dynamically allocated memory.
*/
void
global_terminate( void ) {
free_hash();
clear_coeffs();
clear_osf();
}
/*
SETUP_GAME
Prepares the board.
*/
static void
setup_game( const char *file_name, int *side_to_move ) {
int BUFFER_SIZE = 70;
char buffer[BUFFER_SIZE];
int i, j;
int pos, token;
FILE *stream;
for ( i = 0; i < 10; i++ )
for ( j = 0; j < 10; j++ ) {
pos = 10 * i + j;
if ( (i == 0) || (i == 9) || (j == 0) || (j == 9) )
board[pos] = OUTSIDE;
else
board[pos] = EMPTY;
}
if ( file_name == NULL ) {
board[45] = board[54] = BLACKSQ;
board[44] = board[55] = WHITESQ;
*side_to_move = BLACKSQ;
}
else {
stream = fopen( file_name, "r" );
if ( stream == NULL )
fatal_error( "%s '%s'\n", GAME_LOAD_ERROR, file_name );
fgets(buffer, BUFFER_SIZE, stream );
token = 0;
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
switch ( buffer[token] ) {
case '*':
case 'X':
board[pos] = BLACKSQ;
break;
case 'O':
case '0':
board[pos] = WHITESQ;
break;
case '-':
case '.':
break;
default:
#if TEXT_BASED
if (buffer[token] != '\0') // Don't log if string ended
printf( "%s '%c' %s\n", BAD_CHARACTER_ERROR, buffer[token],
GAME_FILE_TEXT);
#endif
break;
}
if (buffer[token] != '\0') // Don't increment if string ended
token++;
}
fgets( buffer, 10, stream );
if ( buffer[0] == 'B' )
*side_to_move = BLACKSQ;
else if ( buffer[0] == 'W' )
*side_to_move = WHITESQ;
else
fatal_error( "%s '%c' %s\n", BAD_CHARACTER_ERROR, buffer[0],
GAME_FILE_TEXT);
if (board[45] == EMPTY || board[54] == EMPTY || board[44] == EMPTY || board[55] == EMPTY) {
// various pieces of the program are not ready for this, even though we have end solve routines for it
fatal_error( "Initial squares (d4, e4, d5, e5) from the board file should not be empty.\n");
}
}
disks_played = disc_count( BLACKSQ ) + disc_count( WHITESQ ) - 4;
determine_hash_values( *side_to_move, board );
/* Make the game score look right */
if ( *side_to_move == BLACKSQ )
score_sheet_row = -1;
else {
black_moves[0] = PASS;
score_sheet_row = 0;
}
}
/*
GAME_INIT
Prepare the relevant data structures so that a game
can be played. The position is read from the file
specified by FILE_NAME.
*/
void
game_init( const char *file_name, int *side_to_move ) {
setup_game( file_name, side_to_move );
setup_search();
setup_midgame();
setup_end();
init_eval();
clear_ponder_times();
reset_counter( &total_nodes );
reset_counter( &total_evaluations );
init_flip_stack();
total_time = 0.0;
max_depth_reached = 0;
last_time_used = 0.0;
endgame_performed[BLACKSQ] = endgame_performed[WHITESQ] = FALSE;
}
/*
SET_KOMI
Set the endgame komi value.
*/
void
set_komi( int in_komi ) {
komi = in_komi;
}
/*
TOGGLE_HUMAN_OPENINGS
Specifies whether the Thor statistics should be queried for
openings moves before resorting to the usual opening book.
*/
void
toggle_human_openings( int toggle ) {
play_human_openings = toggle;
}
/*
TOGGLE_THOR_MATCH_OPENINGS
Specifies whether matching Thor games are used as opening book
before resorting to the usual opening book.
*/
void
toggle_thor_match_openings( int toggle ) {
play_thor_match_openings = toggle;
}
/*
SET_FORCED_OPENING
Specifies an opening line that Zebra is forced to follow when playing.
*/
void
set_forced_opening( const char *opening_str ) {
forced_opening = opening_str;
}
/*
PONDER_MOVE
Perform searches in response to the opponent's next move.
The results are not returned, but the hash table is filled
with useful scores and moves.
*/
void
ponder_move( int side_to_move, int book, int mid, int exact, int wld ) {
EvaluationType eval_info;
HashEntry entry;
double move_start_time, move_stop_time;
int i, j;
int this_move, hash_move;
int expect_count;
int stored_echo;
int best_pv_depth;
int expect_list[64];
int best_pv[61];
/* Disable all time control mechanisms as it's the opponent's
time we're using */
toggle_abort_check( FALSE );
toggle_midgame_abort_check( FALSE );
start_move( 0, 0, disc_count( BLACKSQ ) + disc_count( WHITESQ ) );
clear_ponder_times();
determine_hash_values( side_to_move, board );
reset_counter( &nodes );
/* Find the scores for the moves available to the opponent. */
hash_move = 0;
find_hash( &entry, ENDGAME_MODE );
if ( entry.draft != NO_HASH_MOVE )
hash_move = entry.move[0];
else {
find_hash( &entry, MIDGAME_MODE );
if ( entry.draft != NO_HASH_MOVE )
hash_move = entry.move[0];
}
stored_echo = echo;
echo = FALSE;
(void) compute_move( side_to_move, FALSE, 0, 0, FALSE, FALSE,
MIN( PONDER_DEPTH, mid ), 0, 0, FALSE, &eval_info );
echo = stored_echo;
/* Sort the opponents on the score and push the table move (if any)
to the front of the list */
if ( force_return )
expect_count = 0;
else {
sort_moves( move_count[disks_played] );
(void) float_move( hash_move, move_count[disks_played] );
expect_count = move_count[disks_played];
for ( i = 0; i < expect_count; i++ )
expect_list[i] = move_list[disks_played][i];
#if TEXT_BASED
printf( "%s=%d\n", HASH_MOVE_TEXT, hash_move );
for ( i = 0; i < expect_count; i++ ) {
printf( "%c%c %-6.2f ", TO_SQUARE( move_list[disks_played][i] ),
evals[disks_played][move_list[disks_played][i]] / 128.0 );
if ( (i % 7 == 6) || (i == expect_count - 1) )
puts( "" );
}
#endif
}
/* Go through the expected moves in order and prepare responses. */
best_pv_depth = 0;
for ( i = 0; !force_return && (i < expect_count); i++ ) {
move_start_time = get_real_timer();
set_ponder_move( expect_list[i] );
this_move = expect_list[i];
prefix_move = this_move;
(void) make_move( side_to_move, this_move, TRUE );
(void) compute_move( OPP( side_to_move ), FALSE, 0, 0, TRUE, FALSE,
mid, exact, wld, FALSE, &eval_info );
unmake_move( side_to_move, this_move );
clear_ponder_move();
move_stop_time = get_real_timer();
add_ponder_time( expect_list[i], move_stop_time - move_start_time );
ponder_depth[expect_list[i]] =
MAX( ponder_depth[expect_list[i]], max_depth_reached - 1 );
if ( (i == 0) && !force_return ) { /* Store the PV for the first move */
best_pv_depth = pv_depth[0];
for ( j = 0; j < pv_depth[0]; j++ )
best_pv[j] = pv[0][j];
}
}
/* Make sure the PV looks reasonable when leaving - either by
clearing it altogether or, preferrably, using the stored PV for
the first move if it is available. */
max_depth_reached++;
prefix_move = 0;
if ( best_pv_depth == 0 )
pv_depth[0] = 0;
else {
pv_depth[0] = best_pv_depth + 1;
pv[0][0] = expect_list[0];
for ( i = 0; i < best_pv_depth; i++ )
pv[0][i + 1] = best_pv[i];
}
/* Don't forget to enable the time control mechanisms when leaving */
toggle_abort_check( TRUE );
toggle_midgame_abort_check( TRUE );
}
/*
COMPARE_EVAL
Comparison function for two evals. Same return value conventions
as QuickSort.
*/
static int
compare_eval( EvaluationType e1, EvaluationType e2 ) {
if ( (e1.type == WLD_EVAL) || (e1.type == EXACT_EVAL) )
if ( e1.score > 0 )
e1.score += 100000;
if ( (e2.type == WLD_EVAL) || (e2.type == EXACT_EVAL) )
if ( e2.score > 0 )
e2.score += 100000;
return e1.score - e2.score;
}
/*
EXTENDED_COMPUTE_MOVE
This wrapper on top of compute_move() calculates the evaluation
of all moves available as opposed to upper bounds for all moves
except for the best.
*/
int
extended_compute_move( int side_to_move, int book_only,
int book, int mid, int exact, int wld ) {
int i, j;
int index;
int changed;
int this_move;
int disc_diff, corrected_diff;
int best_move, temp_move;
int best_score;
int best_pv_depth;
int stored_echo;
int shallow_eval;
int empties;
int current_mid, current_exact, current_wld;
int first_iteration;
int unsearched;
int unsearched_count;
int unsearched_move[61];
int best_pv[60];
unsigned int transform1[60], transform2[60];
CandidateMove book_move;
EvaluatedMove temp;
EvaluationType book_eval_info;
EvalResult res;
/* Disable all time control mechanisms and randomization */
toggle_abort_check( FALSE );
toggle_midgame_abort_check( FALSE );
toggle_perturbation_usage( FALSE );
start_move( 0, 0, disc_count( BLACKSQ ) + disc_count( WHITESQ ) );
clear_ponder_times();
determine_hash_values( side_to_move, board );
empties = 60 - disks_played;
best_move = 0;
game_evaluated_count = 0;
reset_counter( &nodes );
generate_all( side_to_move );
if ( book_only || book ) { /* Evaluations for database moves */
int flags = 0;
if ( empties <= exact )
flags = FULL_SOLVED;
else if ( empties <= wld )
flags = WLD_SOLVED;
fill_move_alternatives( side_to_move, flags );
game_evaluated_count = get_candidate_count();
for ( i = 0; i < game_evaluated_count; i++ ) {
int child_flags;
book_move = get_candidate( i );
evaluated_list[i].side_to_move = side_to_move;
evaluated_list[i].move = book_move.move;
evaluated_list[i].pv_depth = 1;
evaluated_list[i].pv[0] = book_move.move;
evaluated_list[i].eval =
create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION,
book_move.score, 0.0, 0, TRUE );
child_flags = book_move.flags & book_move.parent_flags;
if ( child_flags & (FULL_SOLVED | WLD_SOLVED) ) {
if ( child_flags & FULL_SOLVED )
evaluated_list[i].eval.type = EXACT_EVAL;
else
evaluated_list[i].eval.type = WLD_EVAL;
if ( book_move.score > 0 ) {
evaluated_list[i].eval.res = WON_POSITION;
/* Normalize the scores so that e.g. 33-31 becomes +256 */
evaluated_list[i].eval.score -= CONFIRMED_WIN;
evaluated_list[i].eval.score *= 128;
}
else if ( book_move.score == 0 )
evaluated_list[i].eval.res = DRAWN_POSITION;
else { /* score < 0 */
evaluated_list[i].eval.res = LOST_POSITION;
/* Normalize the scores so that e.g. 30-34 becomes -512 */
evaluated_list[i].eval.score += CONFIRMED_WIN;
evaluated_list[i].eval.score *= 128;
}
}
else
evaluated_list[i].eval.type = MIDGAME_EVAL;
}
}
if ( book_only ) { /* Only book moves are to be considered */
if ( game_evaluated_count > 0 ) {
best_move = get_book_move( side_to_move, FALSE, &book_eval_info );
set_current_eval( book_eval_info );
}
else {
pv_depth[0] = 0;
best_move = PASS;
book_eval_info = create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION,
0, 0.0, 0, FALSE );
set_current_eval( book_eval_info );
}
}
else { /* Make searches for moves not in the database */
int shallow_depth;
int empties = 60 - disks_played;
book = FALSE;
best_score = -INFINITE_EVAL;
if ( game_evaluated_count > 0 ) { /* Book PV available */
best_score = evaluated_list[0].eval.score;
best_move = evaluated_list[0].move;
}
negate_current_eval( TRUE );
/* Store the available moves, clear their evaluations and sort
them on shallow evaluation. */
if ( empties < 12 )
shallow_depth = 1;
else {
int max_depth = MAX( mid, MAX( exact, wld ) );
if ( max_depth >= 16 )
shallow_depth = 6;
else
shallow_depth = 4;
}
unsearched_count = 0;
for ( i = 0; i < move_count[disks_played]; i++ ) {
this_move = move_list[disks_played][i];
unsearched = TRUE;
for ( j = 0; j < game_evaluated_count; j++ )
if ( evaluated_list[j].move == this_move )
unsearched = FALSE;
if ( !unsearched )
continue;
unsearched_move[unsearched_count] = this_move;
unsearched_count++;
(void) make_move( side_to_move, this_move, TRUE );
if ( shallow_depth == 1 ) /* Compute move doesn't allow depth 0 */
shallow_eval = -static_evaluation( OPP( side_to_move ) );
else {
EvaluationType shallow_info;
(void) compute_move( OPP( side_to_move ), FALSE, 0, 0, FALSE, book,
shallow_depth - 1, 0, 0, TRUE, &shallow_info );
if ( shallow_info.type == PASS_EVAL ) {
/* Don't allow pass */
(void) compute_move( side_to_move, FALSE, 0, 0, FALSE, book,
shallow_depth - 1, 0, 0, TRUE, &shallow_info );
if ( shallow_info.type == PASS_EVAL ) { /* Game over */
disc_diff =
disc_count( side_to_move ) - disc_count( OPP( side_to_move ) );
if ( disc_diff > 0 )
corrected_diff = 64 - 2 * disc_count( OPP( side_to_move) );
else if ( disc_diff == 0 )
corrected_diff = 0;
else
corrected_diff = 2 * disc_count( side_to_move ) - 64;
shallow_eval = 128 * corrected_diff;
}
else
shallow_eval = shallow_info.score;
}
else /* Sign-correct the score produced */
shallow_eval = -shallow_info.score;
}
unmake_move( side_to_move, this_move );
evals[disks_played][this_move] = shallow_eval;
}
do {
changed = FALSE;
for ( i = 0; i < unsearched_count - 1; i++ )
if ( evals[disks_played][unsearched_move[i]] <
evals[disks_played][unsearched_move[i + 1]] ) {
temp_move = unsearched_move[i];
unsearched_move[i] = unsearched_move[i + 1];
unsearched_move[i + 1] = temp_move;
changed = TRUE;
}
} while ( changed );
/* Initialize the entire list as being empty */
for ( i = 0, index = game_evaluated_count; i < unsearched_count;
i++, index++ ) {
evaluated_list[index].side_to_move = side_to_move;
evaluated_list[index].move = unsearched_move[i];
evaluated_list[index].eval =
create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION,
0, 0.0, 0, FALSE );
evaluated_list[index].pv_depth = 1;
evaluated_list[index].pv[0] = unsearched_move[i];
if ( empties > MAX( wld, exact ) ) {
transform1[i] = abs( my_random() );
transform2[i] = abs( my_random() );
}
else {
transform1[i] = 0;
transform2[i] = 0;
}
}
stored_echo = echo;
echo = FALSE;
best_pv_depth = 0;
if ( mid == 1 ) { /* compute_move won't be called */
pv_depth[0] = 0;
piece_count[BLACKSQ][disks_played] = disc_count( BLACKSQ );
piece_count[WHITESQ][disks_played] = disc_count( WHITESQ );
}
/* Perform iterative deepening if the search depth is large enough */
#define ID_STEP 2
if ( exact > empties )
exact = empties;
if ( (exact < 12) || (empties > exact) )
current_exact = exact;
else
current_exact = (8 + (exact % 2)) - ID_STEP;
if ( wld > empties )
wld = empties;
if ( (wld < 14) || (empties > wld) )
current_wld = wld;
else
current_wld = (10 + (wld % 2)) - ID_STEP;
if ( ((empties == exact) || (empties == wld)) &&
(empties > 16) && (mid < empties - 12) )
mid = empties - 12;
if ( mid < 10 )
current_mid = mid;
else
current_mid = (6 + (mid % 2)) - ID_STEP;
first_iteration = TRUE;
do {
if ( current_mid < mid ) {
current_mid += ID_STEP;
/* Avoid performing deep midgame searches if the endgame
is reached anyway. */
if ( (empties <= wld) && (current_mid + 7 >= empties) ) {
current_wld = wld;
current_mid = mid;
}
if ( (empties <= exact) && (current_mid + 7 >= empties) ) {
current_exact = exact;
current_mid = mid;
}
}
else if ( current_wld < wld )
current_wld = wld;
else
current_exact = exact;
for ( i = 0; (i < unsearched_count) && !force_return; i++ ) {
EvaluationType this_eval;
this_move = unsearched_move[i];
/* Locate the current move in the list. This has to be done
because the moves might have been reordered during the
iterative deepening. */
index = 0;
while ( evaluated_list[index].move != this_move )
index++;
/* To avoid strange effects when browsing back and forth through
a game during the midgame, rehash the hash transformation masks
for each move unless the endgame is reached */
set_hash_transformation( transform1[i], transform2[i] );
/* Determine the score for the ith move */
prefix_move = this_move;
(void) make_move( side_to_move, this_move, TRUE );
if ( current_mid == 1 ) {
/* compute_move doesn't like 0-ply searches */
shallow_eval = static_evaluation( OPP( side_to_move ) );
this_eval =
create_eval_info( MIDGAME_EVAL, UNSOLVED_POSITION,
shallow_eval, 0.0, 0, FALSE );
}
else
(void) compute_move( OPP( side_to_move ), FALSE, 0, 0, FALSE, book,
current_mid - 1, current_exact - 1,
current_wld - 1, TRUE,
&this_eval );
if ( force_return ) { /* Clear eval and exit search immediately */
this_eval =
create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION,
0, 0.0, 0, FALSE );
unmake_move( side_to_move, this_move );
break;
}
if ( this_eval.type == PASS_EVAL ) {
/* Don't allow pass */
if ( current_mid == 1 ) {
/* compute_move doesn't like 0-ply searches */
shallow_eval = static_evaluation( side_to_move );
this_eval =
create_eval_info( MIDGAME_EVAL, UNSOLVED_POSITION,
shallow_eval, 0.0, 0, FALSE );
}
else
(void) compute_move( side_to_move, FALSE, 0, 0, FALSE, book,
current_mid - 1, current_exact - 1,
current_wld - 1, TRUE,
&this_eval );
if ( this_eval.type == PASS_EVAL ) { /* Game over */
disc_diff =
disc_count( side_to_move ) - disc_count( OPP( side_to_move ) );
if ( disc_diff > 0 ) {
corrected_diff = 64 - 2 * disc_count( OPP( side_to_move) );
res = WON_POSITION;
}
else if ( disc_diff == 0 ) {
corrected_diff = 0;
res = DRAWN_POSITION;
}
else {
corrected_diff = 2 * disc_count( side_to_move ) - 64;
res = LOST_POSITION;
}
this_eval =
create_eval_info( EXACT_EVAL, res, 128 * corrected_diff,
0.0, 60 - disks_played, FALSE );
}
}
else { /* Sign-correct the score produced */
this_eval.score =
-this_eval.score;
if ( this_eval.res == WON_POSITION )
this_eval.res = LOST_POSITION;
else if ( this_eval.res == LOST_POSITION )
this_eval.res = WON_POSITION;
}
if ( force_return )
break;
else
evaluated_list[index].eval = this_eval;
/* Store the PV corresponding to the move */
evaluated_list[index].pv_depth = pv_depth[0] + 1;
evaluated_list[index].pv[0] = this_move;
for ( j = 0; j < pv_depth[0]; j++ )
evaluated_list[index].pv[j + 1] = pv[0][j];
/* Store the PV corresponding to the best move */
if ( evaluated_list[index].eval.score > best_score ) {
best_score = evaluated_list[index].eval.score;
best_move = this_move;
best_pv_depth = pv_depth[0];
for ( j = 0; j < best_pv_depth; j++ )
best_pv[j] = pv[0][j];
}
unmake_move( side_to_move, this_move );
/* Sort the moves evaluated */
if ( first_iteration )
game_evaluated_count++;
if ( !force_return )
do {
changed = FALSE;
for ( j = 0; j < game_evaluated_count - 1; j++ )
if ( compare_eval( evaluated_list[j].eval,
evaluated_list[j + 1].eval ) < 0 ) {
changed = TRUE;
temp = evaluated_list[j];
evaluated_list[j] = evaluated_list[j + 1];
evaluated_list[j + 1] = temp;
}
} while ( changed );
}
first_iteration = FALSE;
/* Reorder the moves after each iteration. Each move is moved to
the front of the list, starting with the bad moves and ending
with the best move. This ensures that unsearched_move will be
sorted w.r.t. the order in evaluated_list. */
for ( i = game_evaluated_count - 1; i >= 0; i-- ) {
int this_move = evaluated_list[i].move;
j = 0;
while ( (j != unsearched_count) && (unsearched_move[j] != this_move) )
j++;
if ( j == unsearched_count ) /* Must be book move, skip */
continue;
/* Move the move to the front of the list. */
while ( j >= 1 ) {
unsearched_move[j] = unsearched_move[j - 1];
j--;
}
unsearched_move[0] = this_move;
}
} while ( !force_return &&
((current_mid != mid) ||
(current_exact != exact) || (current_wld != wld)) );
echo = stored_echo;
game_evaluated_count = move_count[disks_played];
/* Make sure that the PV and the score correspond to the best move */
pv_depth[0] = best_pv_depth + 1;
pv[0][0] = best_move;
for ( i = 0; i < best_pv_depth; i++ )
pv[0][i + 1] = best_pv[i];
negate_current_eval( FALSE );
if ( move_count[disks_played] > 0 )
set_current_eval( evaluated_list[0].eval );
}
/* Reset the hash transformation masks prior to leaving */
set_hash_transformation( 0, 0 );
/* Don't forget to enable the time control mechanisms when leaving */
toggle_abort_check( TRUE );
toggle_midgame_abort_check( TRUE );
toggle_perturbation_usage( TRUE );
max_depth_reached++;
prefix_move = 0;
return best_move;
}
/*
PERFORM_EXTENDED_SOLVE
Calculates exact score or WLD status for the move ACTUAL_MOVE as
well as for the best move in the position (if it is any other move).
*/
void
perform_extended_solve( int side_to_move, int actual_move,
int book, int exact_solve ) {
int i;
int mid, wld, exact;
int best_move;
int disc_diff, corrected_diff;
EvaluatedMove temp;
EvalResult res;
/* Disable all time control mechanisms */
toggle_abort_check( FALSE );
toggle_midgame_abort_check( FALSE );
toggle_perturbation_usage( FALSE );
start_move( 0, 0, disc_count( BLACKSQ ) + disc_count( WHITESQ ) );
clear_ponder_times();
determine_hash_values( side_to_move, board );
reset_counter( &nodes );
/* Set search depths that result in Zebra solving after a brief
midgame analysis */