-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility.c
2855 lines (2723 loc) · 105 KB
/
utility.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
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include "chess.h"
#include "data.h"
#if defined(UNIX)
# include <unistd.h>
# include <sys/types.h>
# include <signal.h>
# include <sys/wait.h>
# include <sys/times.h>
# include <sys/time.h>
#else
# include <windows.h>
# include <winbase.h>
# include <wincon.h>
# include <io.h>
# include <time.h>
#endif
/*
*******************************************************************************
* *
* AlignedMalloc() is used to allocate memory on a precise boundary, *
* primarily to optimize cache performance by forcing the start of the *
* memory region being allocated to match up so that a structure will lie *
* on a single cache line rather than being split across two, assuming the *
* structure is 64 bytes or less of course. *
* *
*******************************************************************************
*/
void AlignedMalloc(void **pointer, int alignment, size_t size) {
segments[nsegments][0] = malloc(size + alignment - 1);
segments[nsegments][1] =
(void *) (((uintptr_t) segments[nsegments][0] + alignment -
1) & ~(alignment - 1));
*pointer = segments[nsegments][1];
nsegments++;
}
/*
*******************************************************************************
* *
* atoiKMB() is used to read in an integer value that can have a "K" or "M" *
* appended to it to multiply by 1024 or 1024*1024. It returns a 64 bit *
* value since memory sizes can exceed 4gb on modern hardware. *
* *
*******************************************************************************
*/
uint64_t atoiKMB(char *input) {
uint64_t size;
size = atoi(input);
if (strchr(input, 'K') || strchr(input, 'k'))
size *= 1 << 10;
if (strchr(input, 'M') || strchr(input, 'm'))
size *= 1 << 20;
if (strchr(input, 'B') || strchr(input, 'b') || strchr(input, 'G') ||
strchr(input, 'g'))
size *= 1 << 30;
return size;
}
/*
*******************************************************************************
* *
* AlignedRemalloc() is used to change the size of a memory block that has *
* previously been allocated using AlignedMalloc(). *
* *
*******************************************************************************
*/
void AlignedRemalloc(void **pointer, int alignment, size_t size) {
int i;
for (i = 0; i < nsegments; i++)
if (segments[i][1] == *pointer)
break;
if (i == nsegments) {
Print(4095, "ERROR AlignedRemalloc() given an invalid pointer\n");
exit(1);
}
free(segments[i][0]);
segments[i][0] = malloc(size + alignment - 1);
segments[i][1] =
(void *) (((uintptr_t) segments[i][0] + alignment - 1) & ~(alignment -
1));
*pointer = segments[i][1];
}
/*
*******************************************************************************
* *
* BookClusterIn() is used to read a cluster in as characters, then stuff *
* the data into a normal array of structures that can be used within Crafty *
* without any endian issues. *
* *
*******************************************************************************
*/
void BookClusterIn(FILE * file, int positions, BOOK_POSITION * buffer) {
int i;
char file_buffer[BOOK_CLUSTER_SIZE * sizeof(BOOK_POSITION)];
i = fread(file_buffer, positions, sizeof(BOOK_POSITION), file);
if (i <= 0)
perror("BookClusterIn fread error: ");
for (i = 0; i < positions; i++) {
buffer[i].position =
BookIn64((unsigned char *) (file_buffer + i * sizeof(BOOK_POSITION)));
buffer[i].status_played =
BookIn32((unsigned char *) (file_buffer + i * sizeof(BOOK_POSITION) +
8));
buffer[i].learn =
BookIn32f((unsigned char *) (file_buffer + i * sizeof(BOOK_POSITION) +
12));
}
}
/*
*******************************************************************************
* *
* BookClusterOut() is used to write a cluster out as characters, after *
* converting the normal array of structures into character data that is *
* Endian-independent. *
* *
*******************************************************************************
*/
void BookClusterOut(FILE * file, int positions, BOOK_POSITION * buffer) {
int i;
char file_buffer[BOOK_CLUSTER_SIZE * sizeof(BOOK_POSITION)];
for (i = 0; i < positions; i++) {
memcpy(file_buffer + i * sizeof(BOOK_POSITION),
BookOut64(buffer[i].position), 8);
memcpy(file_buffer + i * sizeof(BOOK_POSITION) + 8,
BookOut32(buffer[i].status_played), 4);
memcpy(file_buffer + i * sizeof(BOOK_POSITION) + 12,
BookOut32f(buffer[i].learn), 4);
}
fwrite(file_buffer, positions, sizeof(BOOK_POSITION), file);
}
/*
*******************************************************************************
* *
* BookIn32f() is used to convert 4 bytes from the book file into a valid 32 *
* bit binary value. This eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
float BookIn32f(unsigned char *ch) {
union {
float fv;
int iv;
} temp;
temp.iv = ch[3] << 24 | ch[2] << 16 | ch[1] << 8 | ch[0];
return temp.fv;
}
/*
*******************************************************************************
* *
* BookIn32() is used to convert 4 bytes from the book file into a valid 32 *
* bit binary value. this eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
int BookIn32(unsigned char *ch) {
return ch[3] << 24 | ch[2] << 16 | ch[1] << 8 | ch[0];
}
/*
*******************************************************************************
* *
* BookIn64() is used to convert 8 bytes from the book file into a valid 64 *
* bit binary value. this eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
uint64_t BookIn64(unsigned char *ch) {
return (uint64_t) ch[7] << 56 | (uint64_t) ch[6] << 48 | (uint64_t)
ch[5] << 40 | (uint64_t) ch[4] << 32 | (uint64_t) ch[3]
<< 24 | (uint64_t) ch[2] << 16 | (uint64_t) ch[1] << 8 | (uint64_t)
ch[0];
}
/*
*******************************************************************************
* *
* BookOut32() is used to convert 4 bytes from a valid 32 bit binary value *
* to a book value. this eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
unsigned char *BookOut32(int val) {
convert_buff[3] = val >> 24 & 0xff;
convert_buff[2] = val >> 16 & 0xff;
convert_buff[1] = val >> 8 & 0xff;
convert_buff[0] = val & 0xff;
return convert_buff;
}
/*
*******************************************************************************
* *
* BookOut32f() is used to convert 4 bytes from a valid 32 bit binary value *
* to a book value. this eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
unsigned char *BookOut32f(float val) {
union {
float fv;
int iv;
} temp;
temp.fv = val;
convert_buff[3] = temp.iv >> 24 & 0xff;
convert_buff[2] = temp.iv >> 16 & 0xff;
convert_buff[1] = temp.iv >> 8 & 0xff;
convert_buff[0] = temp.iv & 0xff;
return convert_buff;
}
/*
*******************************************************************************
* *
* BookOut64() is used to convert 8 bytes from a valid 64 bit binary value *
* to a book value. this eliminates endian worries that make the binary *
* book non-portable across many architectures. *
* *
*******************************************************************************
*/
unsigned char *BookOut64(uint64_t val) {
convert_buff[7] = val >> 56 & 0xff;
convert_buff[6] = val >> 48 & 0xff;
convert_buff[5] = val >> 40 & 0xff;
convert_buff[4] = val >> 32 & 0xff;
convert_buff[3] = val >> 24 & 0xff;
convert_buff[2] = val >> 16 & 0xff;
convert_buff[1] = val >> 8 & 0xff;
convert_buff[0] = val & 0xff;
return convert_buff;
}
/*
*******************************************************************************
* *
* the following functions are used to determine if keyboard input is *
* present. there are several ways this is done depending on which *
* operating system is used. The primary function name is CheckInput() but *
* for simplicity there are several O/S-specific versions. *
* *
*******************************************************************************
*/
#if !defined(UNIX)
# include <windows.h>
# include <conio.h>
/* Windows NT using PeekNamedPipe() function */
int CheckInput(void) {
int i;
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (!xboard && !isatty(fileno(stdin)))
return 0;
if (batch_mode)
return 0;
if (strchr(cmd_buffer, '\n'))
return 1;
if (xboard) {
# if defined(FILE_CNT)
if (stdin->_cnt > 0)
return stdin->_cnt;
# endif
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) {
return 1;
}
return dw;
} else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
} else {
i = _kbhit();
}
return i;
}
#endif
#if defined(UNIX)
/* Simple UNIX approach using select with a zero timeout value */
int CheckInput(void) {
fd_set readfds;
struct timeval tv;
int data;
if (!xboard && !isatty(fileno(stdin)))
return 0;
if (batch_mode)
return 0;
if (strchr(cmd_buffer, '\n'))
return 1;
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
select(16, &readfds, 0, 0, &tv);
data = FD_ISSET(fileno(stdin), &readfds);
return data;
}
#endif
/*
*******************************************************************************
* *
* ClearHashTableScores() is used to clear hash table scores without *
* clearing the best move, so that move ordering information is preserved. *
* We clear the scorew as we approach a 50 move rule so that hash scores *
* won't give us false scores since the hash signature does not include any *
* search path information in it. *
* *
*******************************************************************************
*/
void ClearHashTableScores(void) {
int i;
if (hash_table)
for (i = 0; i < hash_table_size; i++) {
(hash_table + i)->word2 ^= (hash_table + i)->word1;
(hash_table + i)->word1 =
((hash_table + i)->word1 & mask_clear_entry) | (uint64_t) 65536;
(hash_table + i)->word2 ^= (hash_table + i)->word1;
}
}
/* last modified 02/28/14 */
/*
*******************************************************************************
* *
* ComputeDifficulty() is used to compute the difficulty rating for the *
* current position, which really is based on nothing more than how many *
* times we changed our mind in an iteration. No changes caused the *
* difficulty to drop (easier, use less time), while more changes ramps the *
* difficulty up (harder, use more time). It is called at the end of an *
* iteration as well as when displaying fail-high/fail-low moves, in an *
* effort to give the operator a heads-up on how long we are going to be *
* stuck in an active search. *
* *
*******************************************************************************
*/
int ComputeDifficulty(int difficulty, int direction) {
int searched = 0, i;
/*
************************************************************
* *
* Step 1. Handle fail-high-fail low conditions, which *
* occur in the middle of an iteration. The actions taken *
* are as follows: *
* *
* (1) Determine how many moves we have searched first, as *
* this is important. If we have not searched anything *
* (which means we failed high on the first move at the *
* root, at the beginning of a new iteration), a fail low *
* will immediately set difficult back to 100% (if it is *
* currently below 100%). A fail high on the first move *
* will not change difficulty at all. Successive fail *
* highs or fail lows will not change difficulty, we will *
* not even get into this code on the repeats. *
* *
* (2) If we are beyond the first move, then this must be *
* a fail high condition. Since we are changing our mind, *
* we need to increase the difficulty level to expend more *
* time on this iteration. If difficulty is currently *
* less than 100%, we set it to 120%. If it is currently *
* at 100% or more, we simply add 20% to the value and *
* continue searching, but with a longer time constraint. *
* Each time we fail high, we are changing our mind, and *
* we will increase difficulty by another 20%. *
* *
* (3) Direction = 0 means we are at the end of an the *
* iteration. Here we simply note if we changed our mind *
* during this iteration. If not, we reduce difficulty *
* to 90% of its previous value. *
* *
* After any of these changes, we enforce a lower bound of *
* 60% and an upperbound of 200% before we return. *
* *
* Note: direction = +1 means we failed high on the move, *
* direction = -1 means we failed low on the move, and *
* direction = 0 means we have completed the iteration and *
* all moves were searched successfully. *
* *
************************************************************
*/
if (direction) {
for (i = 0; i < n_root_moves; i++)
if (root_moves[i].status & 8)
searched++;
if (searched == 0) {
if (direction > 0)
return difficulty;
if (direction < 0)
difficulty = Max(100, difficulty);
} else {
if (difficulty < 100)
difficulty = 120;
else
difficulty = difficulty + 20;
}
}
/*
************************************************************
* *
* Step 2. We are at the end of an iteration. If we did *
* not change our mind and stuck with one move, we reduce *
* difficulty by 10% since the move looks to be a little *
* "easier" when we don't change our mind. *
* *
************************************************************
*/
else {
searched = 0;
for (i = 0; i < n_root_moves; i++)
if (root_moves[i].bm_age == 3)
searched++;
if (searched <= 1)
difficulty = 90 * difficulty / 100;
}
/*
************************************************************
* *
* Step 4. Apply limits. We don't let difficulty go *
* above 200% (take 2x the target time) nor do we let it *
* drop below 60 (take .6x target time) to avoid moving *
* too quickly and missing something tactically where the *
* move initially looks obvious but really is not. *
* *
************************************************************
*/
difficulty = Max(60, Min(difficulty, 200));
return difficulty;
}
/*
*******************************************************************************
* *
* CraftyExit() is used to terminate the program. the main functionality *
* is to make sure the "quit" flag is set so that any spinning threads will *
* also exit() rather than spinning forever which can cause GUIs to hang *
* since all processes have not terminated. *
* *
*******************************************************************************
*/
void CraftyExit(int exit_type) {
int proc;
for (proc = 1; proc < CPUS; proc++)
thread[proc].terminate = 1;
while (smp_threads);
exit(exit_type);
}
/*
*******************************************************************************
* *
* DisplayArray() prints array data either 8 or 16 values per line, and also *
* reverses the output for arrays that overlay the chess board so that the *
* 'white side" is at the bottom rather than the top. this is mainly used *
* from inside Option() to display the many evaluation terms. *
* *
*******************************************************************************
*/
void DisplayArray(int *array, int size) {
int i, j, len = 16;
if (Abs(size) % 10 == 0)
len = 10;
else if (Abs(size) % 8 == 0)
len = 8;
if (size > 0 && size % 16 == 0 && len == 8)
len = 16;
if (size > 0) {
printf(" ");
for (i = 0; i < size; i++) {
printf("%3d ", array[i]);
if ((i + 1) % len == 0) {
printf("\n");
if (i < size - 1)
printf(" ");
}
}
if (i % len != 0)
printf("\n");
}
if (size < 0) {
for (i = 0; i < 8; i++) {
printf(" ");
for (j = 0; j < 8; j++) {
printf("%3d ", array[(7 - i) * 8 + j]);
}
printf(" | %d\n", 8 - i);
}
printf(" ---------------------------------\n");
printf(" a b c d e f g h\n");
}
}
/*
*******************************************************************************
* *
* DisplayArray() prints array data either 8 or 16 values per line, and also *
* reverses the output for arrays that overlay the chess board so that the *
* 'white side" is at the bottom rather than the top. this is mainly used *
* from inside Option() to display the many evaluation terms. *
* *
*******************************************************************************
*/
void DisplayArrayX2(int *array, int *array2, int size) {
int i, j;
if (size == 256) {
printf(" ----------- Middlegame ----------- ");
printf(" ------------- Endgame -----------\n");
for (i = 0; i < 8; i++) {
printf(" ");
for (j = 0; j < 8; j++)
printf("%3d ", array[(7 - i) * 8 + j]);
printf(" | %d |", 8 - i);
printf(" ");
for (j = 0; j < 8; j++)
printf("%3d ", array2[(7 - i) * 8 + j]);
printf("\n");
}
printf
(" ---------------------------------- ---------------------------------\n");
printf(" a b c d e f g h ");
printf(" a b c d e f g h\n");
} else if (size == 32) {
printf(" ----------- Middlegame ----------- ");
printf(" ------------- Endgame -----------\n");
printf(" ");
for (i = 0; i < 8; i++)
printf("%3d ", array[i]);
printf(" | |");
printf(" ");
for (i = 0; i < 8; i++)
printf("%3d ", array2[i]);
printf("\n");
} else if (size <= 20) {
size = size / 2;
printf(" ");
for (i = 0; i < size; i++)
printf("%3d ", array[i]);
printf(" |<mg eg>|");
printf(" ");
for (i = 0; i < size; i++)
printf("%3d ", array2[i]);
printf("\n");
} else if (size > 128) {
printf(" ----------- Middlegame ----------- ");
printf(" ------------- Endgame -----------\n");
for (i = 0; i < size / 32; i++) {
printf(" ");
for (j = 0; j < 8; j++)
printf("%3d ", array[(7 - i) * 8 + j]);
printf(" | %d |", 8 - i);
printf(" ");
for (j = 0; j < 8; j++)
printf("%3d ", array2[(7 - i) * 8 + j]);
printf("\n");
}
} else
Print(4095, "ERROR, invalid size = -%d in packet\n", size);
}
/*
*******************************************************************************
* *
* DisplayBitBoard() is a debugging function used to display bitboards in a *
* more visual way. they are displayed as an 8x8 matrix oriented as the *
* normal chess board is, with a1 at the lower left corner. *
* *
*******************************************************************************
*/
void DisplayBitBoard(uint64_t board) {
int i, j, x;
for (i = 56; i >= 0; i -= 8) {
x = (board >> i) & 255;
for (j = 1; j < 256; j = j << 1)
if (x & j)
Print(4095, "X ");
else
Print(4095, "- ");
Print(4095, "\n");
}
}
/*
*******************************************************************************
* *
* Display2BitBoards() is a debugging function used to display bitboards in *
* a more visual way. they are displayed as an 8x8 matrix oriented as the *
* normal chess board is, with a1 at the lower left corner. this function *
* displays 2 boards side by side for comparison. *
* *
*******************************************************************************
*/
void Display2BitBoards(uint64_t board1, uint64_t board2) {
int i, j, x, y;
for (i = 56; i >= 0; i -= 8) {
x = (board1 >> i) & 255;
for (j = 1; j < 256; j = j << 1)
if (x & j)
printf("X ");
else
printf("- ");
printf(" ");
y = (board2 >> i) & 255;
for (j = 1; j < 256; j = j << 1)
if (y & j)
printf("X ");
else
printf("- ");
printf("\n");
}
}
/*
*******************************************************************************
* *
* DisplayChessBoard() is used to display the board since it is kept in *
* both the bit-board and array formats, here we use the array format which *
* is nearly ready for display as is. *
* *
*******************************************************************************
*/
void DisplayChessBoard(FILE * display_file, POSITION pos) {
int display_board[64], i, j;
static const char display_string[16][4] =
{ "<K>", "<Q>", "<R>", "<B>", "<N>", "<P>", " ",
"-P-", "-N-", "-B-", "-R-", "-Q-", "-K-", " . "
};
/*
************************************************************
* *
* First, convert square values to indices to the proper *
* text string. *
* *
************************************************************
*/
for (i = 0; i < 64; i++) {
display_board[i] = pos.board[i] + 6;
if (pos.board[i] == 0) {
if (((i / 8) & 1) == ((i % 8) & 1))
display_board[i] = 13;
}
}
/*
************************************************************
* *
* Now that that's done, simply display using 8 squares *
* per line. *
* *
************************************************************
*/
fprintf(display_file, "\n +---+---+---+---+---+---+---+---+\n");
for (i = 7; i >= 0; i--) {
fprintf(display_file, " %2d ", i + 1);
for (j = 0; j < 8; j++)
fprintf(display_file, "|%s", display_string[display_board[i * 8 + j]]);
fprintf(display_file, "|\n");
fprintf(display_file, " +---+---+---+---+---+---+---+---+\n");
}
fprintf(display_file, " a b c d e f g h\n\n");
}
/*
*******************************************************************************
* *
* DisplayChessMove() is a debugging function that displays a chess move in *
* a very simple (non-algebraic) form. *
* *
*******************************************************************************
*/
void DisplayChessMove(char *title, int move) {
Print(4095, "%s piece=%d, from=%d, to=%d, captured=%d, promote=%d\n",
title, Piece(move), From(move), To(move), Captured(move),
Promote(move));
}
/*
*******************************************************************************
* *
* DisplayEvaluation() is used to convert the evaluation to a string that *
* can be displayed. The length is fixed so that screen formatting will *
* look nice and aligned. *
* *
*******************************************************************************
*/
char *DisplayEvaluation(int value, int wtm) {
int tvalue;
static char out[10];
tvalue = (wtm) ? value : -value;
if (!MateScore(value))
sprintf(out, "%7.2f", ((float) tvalue) / 100.0);
else if (Abs(value) > MATE) {
if (tvalue < 0)
sprintf(out, " -infnty");
else
sprintf(out, " +infnty");
} else if (value == MATE - 2 && wtm)
sprintf(out, " Mate");
else if (value == MATE - 2 && !wtm)
sprintf(out, " -Mate");
else if (value == -(MATE - 1) && wtm)
sprintf(out, " -Mate");
else if (value == -(MATE - 1) && !wtm)
sprintf(out, " Mate");
else if (value > 0 && wtm)
sprintf(out, " Mat%.2d", (MATE - value) / 2);
else if (value > 0 && !wtm)
sprintf(out, " -Mat%.2d", (MATE - value) / 2);
else if (wtm)
sprintf(out, " -Mat%.2d", (MATE - Abs(value)) / 2);
else
sprintf(out, " Mat%.2d", (MATE - Abs(value)) / 2);
return out;
}
/*
*******************************************************************************
* *
* DisplayEvaluationKibitz() is used to convert the evaluation to a string *
* that can be displayed. The length is variable so that ICC kibitzes and *
* whispers will look nicer. *
* *
*******************************************************************************
*/
char *DisplayEvaluationKibitz(int value, int wtm) {
int tvalue;
static char out[10];
tvalue = (wtm) ? value : -value;
if (!MateScore(value))
sprintf(out, "%+.2f", ((float) tvalue) / 100.0);
else if (Abs(value) > MATE) {
if (tvalue < 0)
sprintf(out, "-infnty");
else
sprintf(out, "+infnty");
} else if (value == MATE - 2 && wtm)
sprintf(out, "Mate");
else if (value == MATE - 2 && !wtm)
sprintf(out, "-Mate");
else if (value == -(MATE - 1) && wtm)
sprintf(out, "-Mate");
else if (value == -(MATE - 1) && !wtm)
sprintf(out, "Mate");
else if (value > 0 && wtm)
sprintf(out, "Mat%.2d", (MATE - value) / 2);
else if (value > 0 && !wtm)
sprintf(out, "-Mat%.2d", (MATE - value) / 2);
else if (wtm)
sprintf(out, "-Mat%.2d", (MATE - Abs(value)) / 2);
else
sprintf(out, "Mat%.2d", (MATE - Abs(value)) / 2);
return out;
}
/*
*******************************************************************************
* *
* DisplayPath() is used to display a PV during the root move search. *
* *
*******************************************************************************
*/
char *DisplayPath(TREE * RESTRICT tree, int wtm, PATH * pv) {
static char buffer[4096];
int i, t_move_number;
/*
************************************************************
* *
* Initialize. *
* *
************************************************************
*/
t_move_number = move_number;
sprintf(buffer, " %d.", move_number);
if (!wtm)
sprintf(buffer + strlen(buffer), " ...");
for (i = 1; i < (int) pv->pathl; i++) {
if (i > 1 && wtm)
sprintf(buffer + strlen(buffer), " %d.", t_move_number);
sprintf(buffer + strlen(buffer), " %s", OutputMove(tree, i, wtm,
pv->path[i]));
MakeMove(tree, i, wtm, pv->path[i]);
wtm = Flip(wtm);
if (wtm)
t_move_number++;
}
if (pv->pathh == 1)
sprintf(buffer + strlen(buffer), " <HT> ");
else if (pv->pathh == 2)
sprintf(buffer + strlen(buffer), " <EGTB> ");
else if (pv->pathh == 3)
sprintf(buffer + strlen(buffer), " <3-fold> ");
else if (pv->pathh == 4)
sprintf(buffer + strlen(buffer), " <50-move> ");
if (strlen(buffer) < 30)
for (i = 0; i < 30 - strlen(buffer); i++)
strcat(buffer, " ");
strcpy(kibitz_text, buffer);
for (i = pv->pathl - 1; i > 0; i--) {
wtm = Flip(wtm);
UnmakeMove(tree, i, wtm, pv->path[i]);
}
return buffer;
}
/*
*******************************************************************************
* *
* DisplayFail() is used to display a PV (moves only) during the search. *
* *
*******************************************************************************
*/
void DisplayFail(TREE * RESTRICT tree, int type, int level, int wtm, int time,
int move, int value, int force) {
char buffer[4096], *fh_indicator;
/*
************************************************************
* *
* If we have not used "noise_level" units of time, we *
* return immediately. Otherwise we add the fail high/low *
* indicator (++/--) and then display the times. *
* *
************************************************************
*/
if (time < noise_level)
return;
if (type == 1)
fh_indicator = (wtm) ? "++" : "--";
else
fh_indicator = (wtm) ? "--" : "++";
Print(4, " %2i %s %2s ", iteration,
Display2Times(end_time - start_time), fh_indicator);
/*
************************************************************
* *
* If we are pondering, we need to add the (ponder-move) *
* to the front of the buffer, correcting the move number *
* if necessary. Then fill in the move number and the *
* fail high/low bound. *
* *
************************************************************
*/
if (!pondering) {
sprintf(buffer, "%d.", move_number);
if (!wtm)
sprintf(buffer + strlen(buffer), " ...");
} else {
if (wtm)
sprintf(buffer, "%d. ... (%s) %d.", move_number - 1, ponder_text,
move_number);
else
sprintf(buffer, "%d. (%s)", move_number, ponder_text);
}
sprintf(buffer + strlen(buffer), " %s%c", OutputMove(tree, 1, wtm, move),
(type == 1) ? '!' : '?');
strcpy(kibitz_text, buffer);
if (time >= noise_level || force) {
noise_block = 0;
Lock(lock_io);
Print(4, "%s", buffer);
Unlock(lock_io);
if (type == 1)
Print(4, " (%c%s) \n", (wtm) ? '>' : '<',
DisplayEvaluationKibitz(value, wtm));
else
Print(4, " (%c%s) \n", (wtm) ? '<' : '>',
DisplayEvaluationKibitz(value, wtm));
}
}
/*
*******************************************************************************
* *
* DisplayPV() is used to display a PV during the search. *
* *
*******************************************************************************
*/
void DisplayPV(TREE * RESTRICT tree, int level, int wtm, int time, PATH * pv,
int force) {
char buffer[4096], *buffp, *bufftemp;
char blanks[40] = { " " };
int i, len, t_move_number, nskip = 0, twtm = wtm, pv_depth = pv->pathd;;
unsigned int idle_time;
/*
************************************************************
* *
* Initialize. *
* *
************************************************************
*/
for (i = 0; i < n_root_moves; i++)
if (root_moves[i].status & 4)
nskip++;
for (i = 0; i < 4096; i++)
buffer[i] = ' ';
t_move_number = move_number;
if (!pondering || analyze_mode) {
sprintf(buffer, "%d.", move_number);
if (!wtm)
sprintf(buffer + strlen(buffer), " ...");
} else {
if (wtm)
sprintf(buffer, "%d. ... (%s) %d.", move_number - 1, ponder_text,
move_number);
else
sprintf(buffer, "%d. (%s)", move_number, ponder_text);
}
for (i = 1; i < (int) pv->pathl; i++) {
if (i > 1 && wtm)
sprintf(buffer + strlen(buffer), " %d.", t_move_number);
sprintf(buffer + strlen(buffer), " %s", OutputMove(tree, i, wtm,
pv->path[i]));
MakeMove(tree, i, wtm, pv->path[i]);
wtm = Flip(wtm);
if (wtm)
t_move_number++;
}
if (pv->pathh == 1)
sprintf(buffer + strlen(buffer), " <HT>");
else if (pv->pathh == 2)
sprintf(buffer + strlen(buffer), " <EGTB>");
else if (pv->pathh == 3)
sprintf(buffer + strlen(buffer), " <3-fold>");
else if (pv->pathh == 3)
sprintf(buffer + strlen(buffer), " <50-move>");
if (nskip > 1 && smp_max_threads > 1)
sprintf(buffer + strlen(buffer), " (s=%d)", nskip);
if (strlen(buffer) < 30) {
len = 30 - strlen(buffer);
for (i = 0; i < len; i++)
strcat(buffer, " ");
}
strcpy(kibitz_text, buffer);
if (time >= noise_level || force) {
noise_block = 0;
Lock(lock_io);
Print(2, " ");
if (level == 6)
Print(2, "%2i %s%s ", pv_depth, Display2Times(time),
DisplayEvaluation(pv->pathv, twtm));
else
Print(2, "%2i-> %s%s ", pv_depth, Display2Times(time)
, DisplayEvaluation(pv->pathv, twtm));
buffp = buffer;
do {
if ((int) strlen(buffp) > line_length - 38) {
bufftemp = buffp + line_length - 38;
while (*bufftemp != ' ')
bufftemp--;
if (*(bufftemp - 1) == '.')
while (*(--bufftemp) != ' ');
} else
bufftemp = 0;
if (bufftemp)
*bufftemp = 0;
Print(2, "%s\n", buffp);
buffp = bufftemp + 1;
if (bufftemp)
if (!strncmp(buffp, blanks, strlen(buffp)))
bufftemp = 0;
if (bufftemp)
Print(2, " ");