-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
2266 lines (898 loc) · 23.3 KB
/
search.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
/*
This File is part of Boo's Chess Engine
Copyright 2001 by Christopher Bowron
*/
#include <pspkernel.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <sys/timeb.h>
/* #include <setjmp.h> */
//#include <signal.h>
//#include <string.h>
#include "pg.h"
#include "bce.h"
#include "colors.h"
/* #define NULL_MOVE */
/* #define POLL_FREQ ((1<<13)-1) */
//#define POLL_FREQ 256
#define POLL_FREQ 1024
#define NO_TIMEOUT INF
#define ITERATIVE_SEARCH 1
#define ASPIRATION_WINDOW 2000
/* #define poll_check() if (((++search_info.nodes)&(POLL_FREQ))==0) poll(); */
#define poll_check() if (((++search_info.nodes)%(POLL_FREQ))==0) poll();
#define stored_inc(d) (d*4)
#define cutoff_inc(d) (d)
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
void sceKernelRotateThreadReadyQueue(int priority);
int gamemode;
int pondering = 0;
int g_current_searchdepth = 0;
int g_start_time = 0;
int history[64*64]; /* history heuristic for move ordering */
int g_show_thinking = 0;
move g_best_line[32];
struct
{
move m;
int depth;
} counter[64*64];
search_info_type search_info;
branch_info_type branch_info;
int (*search)(int,int,int,int) = negascout_tables;
//int (*search)(int,int,int,int) = qsearch;
/* int (*search)(int,int,int,int) = negascout_search; */
//int (*search)(int,int,int,int) = horrible_search;
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
long get_ms()
{
//struct timeb timebuffer;
//ftime(&timebuffer);
//return (timebuffer.time * 1000) + timebuffer.millitm;
return sceKernelLibcTime((void *) 0) * 1000;
}
void print_square(int x, int y, square s)
{
char str[3] = { FL(F(s)), RK(R(s)), 0 };
pgFancyPrint(x,y,lightblue, str,0,1,1);
}
void print_move(int x, int y, move m)
{
msxPutString(x,y,cyan,movestring(m));
}
void print_best_line(int x, int y, move *line, int depth)
{
int i;
int localx = x;
pgFancyPrint(x,y,red,"BEST LINE",1,0,0);
y++;
for(i=0;i<depth;i++)
{
if (line[i] == 0) break;
print_move(localx,y,line[i]);
localx+=5;
if (localx>50)
{
y++;
localx=x;
}
}
}
void display_killers(int x, int y)
{
int i;
int j;
pgFancyPrint(x,y,red,"Killers",1,0,0);
for (i=0;i<g_current_searchdepth;i++)
{
for (j=0;j<2;j++)
{
print_move(x + 5*j, y+1+i, killers[i][j].m);
}
}
}
void pgPrintCenter(int y, int color, char *str)
{
int x = 30 - strlen(str)/2;
if (color==green)
{
pgFancyPrint(x,y,color,str,0,1,0);
}
else if (color==red)
{
pgFancyPrint(x,y,color,str,1,0,0);
}
else
{
pgFancyPrint(x,y,color,str,0,1,1);
}
}
int actualmode()
{
int i, pieces = 0;
for (i=0;i<=BKING;i++) pieces += board->pieces[i];
if (pieces<=10)
return ENDGAME;
else
return MIDGAME;
}
void change_gamemode(int mode, int clear)
{
switch(mode)
{
case OPENING:
gamemode = OPENING;
break;
case MIDGAME:
gamemode = MIDGAME;
break;
case ENDGAME:
gamemode = ENDGAME;
break;
}
if (clear)
{
clear_hash();
}
}
/*
return a quick evaluation of a move for use in move ordering
*/
int quickeval(move m, int p, move hashed)
{
int result;
int piece;
piece = getpiece__(TO(m));
if (piece)
{
int n = weights[chesspiecevalue(piece)];
int d = weights[chesspiecevalue(getpiece__(FR(m)))];
if (d==0) d = 100000; /* king */
result = 10000 + 3000 * n / d;
}
else
result = 0;
if (m == counter[lastmove()].m)
result += 20000;
else if (m == hashed)
result += 15000;
else if (m == killers[p][0].m)
result += 10000;
else if (m == killers[p][1].m)
result += 5000;
else
result += history[m];
return result;
}
/*
compare two moves for initial move ordering
*/
/* static int movecmp(const void *foo, const void *bar) */
/* { */
/* return ((move_and_score*)foo)->score-((move_and_score*)bar)->score; */
/* } */
/*
** The h-constants for this version of Shell-sort come from:
** Handbook of Algorithms and Data Structures in Pascal and C
** By Gaston Gonnet, Ricardo Baeza-Yates
** Addison Wesley; ISBN: 0201416077
** These h-increments work better for me than Sedgewick's under
** full optimization. Your mileage may vary, so I suggest you
** try Sedgewick's suggestions as well. The h-increment arena
** is a rich environment for exploration. I suggest attempting
** all possible permutations below 20, since that is where a
** good shell-sort is crucuial. If you find something wonderul
** you may get your name up in lights.
** -- D. Corbit
*/
int ShellSort(move_and_score start[], move_and_score end[])
{
int i, j, h;
size_t n = (end - start);
move_and_score key;
for (h = n; h > 0;)
{
for (i = h; i < n; i++)
{
j = i;
key = start[i];
while ((j >= h) & (key.score < start[j - h].score))
{
start[j] = start[j - h];
j -= h;
}
start[j] = key;
}
h = (size_t) ((h > 1) & (h < 5)) ? 1 : 5 * h / 11;
}
return 0;
}
void sortmoves(move_and_score *start, move_and_score *end, move hashed)
{
move_and_score *t;
int p = ply - search_info.startply;
for (t=start;t<end;t++)
t->score = quickeval(t->m, p, hashed);
ShellSort(start, end);
}
void clearthoughts()
{
memset(history, 0, sizeof(history));
memset(search_info.pv.moves, 0, sizeof(search_info.pv.moves));
memset(search_info.pv.length, 0, sizeof(search_info.pv.length));
memset(killers,0,sizeof(killers));
memset(counter,0,sizeof(counter));
memset(g_best_line,0,sizeof(g_best_line));
}
/*
void signal_handler(int sig)
{
search_info.stop = sig;
signal(SIGINT, signal_handler);
}
*/
/*
c is the player who is pondering what they are going to do when
there opponent makes a move.
this will set up some values in history that are hopefully cutoffs
for the real search and put stuff in the trasposition tables
*/
void ponder()
{
int saveddepth;
int savedtime;
int j;
clearthoughts();
if (gamemode == OPENING) return;
if (ply<=0) return;
//savedhandler = signal(SIGINT, signal_handler);
search_info.stop = 0;
savedtime = searchtime;
saveddepth = searchdepth;
searchdepth = 100;
searchtime = NO_TIMEOUT;
pondering = 1;
//flash_display("ponder");
think();
//signal(SIGINT, savedhandler);
searchtime = savedtime;
searchdepth = saveddepth;
for (j=1;j<PV_LENGTH;j++)
{
search_info.pv.moves[j-1] = search_info.pv.moves[j];
killers[j-1][0].m = killers[j][0].m;
killers[j-1][1].m = killers[j][1].m;
killers[j-1][0].score = killers[j][0].score;
killers[j-1][1].score = killers[j][1].score;
}
pondering = 0;
}
move bookmove()
{
if (gamemode == OPENING)
{
move m = bookopening();
if (validmove(m)==1)
{
return m;
}
else
{
clear_screen();
pgPrintCenter(10,lightblue,"No Book Move Available");
pgPrintCenter(12,lightblue,"Switching to Search Mode");
pgPrintCenter(16,lightblue,"Clearing hash tables");
pgScreenFlipV();
change_gamemode(MIDGAME, 1);
pgScreenFlipV();
}
}
return dummymove; /* not in book mode or invalid book move */
}
move think()
{
long starttime;
//int duration;
//int sig;
int c = tomove();
//move best_line[PV_LENGTH];
//int i;
static int best;
//static int best_depth;
static int x;
static int depth_reached;
static move best_move;
static int branches;
//double bf;
move_and_score *restore_sp;
#ifdef ASPIRATION_WINDOW
best = evaluate();
#endif
/*
if (actualmode()!=gamemode)
change_gamemode(actualmode(), 1);
*/
if (!ponder_mode)
clearthoughts();
starttime = get_ms();
search_info.startply = ply;
restore_sp = move_sp;
long timeout = 0;
/*
search for time/30
*/
if (!pondering)
{
int clock_time = chessclock[c];
if (clock_time<0)
clock_time = 500;
timeout = max(1,clock_time / 30);
search_info.stoptime = starttime + timeout;
}
search_info.nodes = 0;
search_info.branches = 0;
tablehits[0] = tablehits[1] = tablehits[2] = 0;
search_info.pv.hits = 0;
memset(search_info.move_depth,0,sizeof(search_info.move_depth));
search_info.polling = 0;
#if ITERATIVE_SEARCH
g_current_searchdepth = 1;
#else
g_current_searchdepth = searchdepth;
#endif
search_info.stop = 0;
g_start_time = get_ms()/1000;
while (g_current_searchdepth <= searchdepth && !search_info.stop)
{
#ifdef ASPIRATION_WINDOW
int alpha = best - ASPIRATION_WINDOW;
int beta = best + ASPIRATION_WINDOW;
best = search(alpha, beta, g_current_searchdepth, 1);
if (best<=alpha)
{
best = search (-INF, beta, g_current_searchdepth, 1);
}
else if (best>=beta)
{
best = search (alpha, INF, g_current_searchdepth, 1);
}
#else
best = negascout_tables (-INF, INF, g_current_searchdepth,0);
#endif
best_move = g_best_line[0];
branches = search_info.branches;
g_current_searchdepth++;
if (best>WIN-50)
break;
}
depth_reached = x;
if ((best>WIN-50)||(best<LOSE+50))
output("mate in %d\n", WIN-abs(best));
return best_move;
}
move bce()
{
move temp;
if ((temp=bookmove())!=dummymove)
return temp;
else
return think();
}
int bioskey()
{
static SceCtrlData control_data;
sceCtrlPeekBufferPositive(&control_data, 1);
return control_data.Buttons;
}
/* check for time up or pondering interrupt... */
void poll()
{
if (search_info.stop)
{
}
else if (pondering)
{
if (bioskey())
{
pgWaitV();
//sceKernelRotateThreadReadyQueue(0x11);
}
}
else if (bioskey()&PSP_CTRL_TRIANGLE)
{
search_info.stop = 1;
}
}
void storepv(move m, int p)
{
search_info.pv.moves[p] = m;
search_info.pv.length[p] = search_info.pv.length[p+1];
}
void store_retrieved(move m, int p)
{
search_info.pv.moves[p] = m;
search_info.pv.moves[p+1] = dummymove;
search_info.pv.length[p] = p+2;
search_info.pv.length[p+1] = p+2;
}
int extend_search_p()
{
if (board->flags&promo_flag)
return 1;
return 0;
}
void store_counter(move m, int depth)
{
if (-depth<=counter[lastmove()].depth)
{
counter[lastmove()].m = m;
counter[lastmove()].depth = depth;
}
}
#ifdef NULL_MOVE
#define AVOID_NULL_PIECES (7)
#define AVOID_NULL_MAT (10*STATIC_PAWN_VALUE)
int NullOk(int depth, int side_to_move)
{
int cwp, cbp,
base_reduction = (depth > 3) ? 0 : 1;
/* If there is a risk of Zugzwang then return base_reduction */
if (side_to_move == WHITE && board->material[WHITE] < AVOID_NULL_MAT)
return base_reduction;
if (side_to_move == BLACK && board->material[BLACK] < AVOID_NULL_MAT)
return base_reduction;
cwp = board->piececount[WHITE];
if (side_to_move == WHITE && cwp < AVOID_NULL_PIECES)
return base_reduction;
cbp = board->piececount[BLACK];
if (side_to_move == BLACK && cbp < AVOID_NULL_PIECES)
return base_reduction;
return 2 + ((depth) > ((6) + (((cwp < 4 && cbp < 4) ? 2 : 0))) ? 1 : 0);
}
#endif
int qsearch(int a, int b, int d)
{
move_and_score *restore_sp;
int incheck = 0;
int searched = 0;
int b2;
int p;
int c = tomove();
int current_eval;
move bestmove = 0;
int a_original;
p = ply-search_info.startply;
search_info.pv.length[p]=p;
if (position_seen_before())
return 0;
current_eval = evaluate();
if (p>=PV_LENGTH)
return current_eval;
else if (current_eval>=b)
return current_eval;
else if (current_eval>a)
a = current_eval;
poll_check();
a_original = a;
b2 = b;
incheck = incheckp(c);
restore_sp = move_sp;
genattacks();
sortmoves(restore_sp, move_sp, 0);
while (move_sp>restore_sp)
{
move m;
int intocheck;
m = popmove();
domove(m);
intocheck = (incheck) ? fullincheckp(c) : intocheckp(m,c);
if (!intocheck)
{
int t = -qsearch(-b2, -a, d+1);
searched++;
/* re-search */
if ((t>a)&&(t<b))
{
if ((searched>1))
a = -qsearch(-b,-t, d+1);
storepv(m, p);
}
a = max(a,t);
if (a >= b)
{
move_sp = restore_sp;
undomove();
store_counter(m, p);
return a;
}
b2 = a + 1;
}
undomove();
}
move_sp=restore_sp;
if (searched)
{
store_counter(bestmove, p);
return a; /* had at least one valid move/attack */
}
else
{
return current_eval; /* we found no valid attacks */
}
}
int negascout_tables(int a, int beta, int d, int DoNull)
{
int searched = 0;
int incheck;
move_and_score *restore_sp;
int b;
int p;
int c = tomove();
#ifdef NULL_MOVE
int nd;
#endif
int hashscore;
move bestmove = 0;
int oldalpha = a;
int retrieve_type;
p = ply-search_info.startply;
/* draw? */
if (p&&position_seen_before())
return 0;
switch (retrieve_type = retrieve_hash(d, &hashscore, &bestmove))
{
case EXACT:
if ((hashscore>a)&&(hashscore<beta))
if (validmove(bestmove) == 1)
{
//store_retrieved(bestmove, p);
g_best_line[p] = bestmove;
return hashscore;
}
break;
case L_BOUND:
if (hashscore>=beta)
return hashscore;
break;
case U_BOUND:
if (hashscore<=a)
return hashscore;
break;
}
if (d<=0)
return qsearch(a,beta,0);
search_info.branches ++;
poll_check();
search_info.pv.length[p] = p;
b = beta;
#define CHECKTEST_ALL
#ifdef CHECKTEST_ALL
incheck = incheckp(c);
#endif
restore_sp = move_sp;
genmoves();
sortmoves(restore_sp, move_sp, bestmove);
while (!search_info.stop && move_sp>restore_sp)
{
move m;
int intocheck;
m = popmove();
domove(m);
#ifdef CHECKTEST_ALL
// TODO:
// try moving into check test so that it tests only
// prior to storing a position
intocheck = (incheck) ? fullincheckp(c) : intocheckp(m, c);
if (!intocheck)
#endif
{
int t;
searched++;
if (d == g_current_searchdepth)
{
if (pondering)
{
char *message = "Pondering";
int x = 35;
int y = 14;
int message_len = strlen(message);
pgScreenCopy();
// black out text region
int aa,bb;
for(aa=x*8;aa<(x+message_len+1)*8;aa++)
{
for(bb=y*8;bb<(y+3)*8;bb++)
{
set_pixel(aa,bb,0);
}
}
pgFancyPrint(35,y,red,message,0,1,1);
print_move(35,y+1,m);
pgPrintInt(35,y+2,red,g_current_searchdepth);
pgScreenFlipV();
}
else if (g_show_thinking || g_hardcore_debug)
{
static int bit = 0;
bit = (bit+1) % 2;
draw_board();
hilite_move(m,green,green);
if (bit)
pgFancyPrint(35,1,red,"Considering: ",1,0,0);
else
pgFancyPrint(35,1,magenta,"Considering: ",1,0,1);
print_move(48,1,m);
pgPrintInt(35,2,lightgrey,a);
pgPrintInt(45,2,lightblue,beta);
if (bestmove)
{
hilite_move(bestmove,cyan,blue);
print_best_line(35,4,
//search_info.pv.moves,
g_best_line,
//search_info.pv.length[p]
100
);
}
int py = 19;
pgFancyPrint(35,py,red,"Table Hits",1,0,0);
pgFancyPrint(35,++py,lightblue,">",0,1,1);
pgPrintInt(37,py,lawngreen,tablehits[0]);
pgFancyPrint(35,++py,lightblue,"=",0,1,1);
pgPrintInt(37,py,lawngreen,tablehits[1]);
pgFancyPrint(35,++py,lightblue,"<",0,1,1);
pgPrintInt(37,py,lawngreen,tablehits[2]);
py+=2;
int current_time = get_ms()/1000;
pgFancyPrint(35,py,lightblue,"Elapsed Time",1,0,0);
int elapsed = current_time-g_start_time;
display_time(35,++py,lawngreen,elapsed*1000);
py+=2;
pgFancyPrint(35,py,lightblue,"Current Search Depth",1,0,0);
pgPrintInt(35,++py,lawngreen,g_current_searchdepth);
py+=2;
pgFancyPrint(35,py,lightblue,"Branches",1,0,0);
pgPrintInt(35,++py,lawngreen,search_info.branches);
pgFancyPrint(50,--py,lightblue,"Nodes",1,0,0);
pgPrintInt(50,++py,lawngreen,search_info.nodes);
display_killers(35,9);
pgScreenFlipV();
if (g_hardcore_debug)
{
while (Read_Key () == 0) ;
}
}
}
t = -negascout_tables(-b, -a, d-1, 1);
int tested_check = 0;
if ((t>a)&&(t<beta))
{
#ifndef CHECKTEST_ALL
if (!fullincheckp(c))
#endif
{
tested_check = 1;
/* re-search */
if (searched>1)
a = -negascout_tables(-beta,-t, d-1, 1);
history[m] += stored_inc(d);
search_info.move_depth[p] = searched;
g_best_line[p] = m;
bestmove = m;
}
}
if (t>a)
{
#ifndef CHECKTEST_ALL
if (tested_check || !fullincheckp(c))
#endif
{
a=t;
tested_check = 1;
}
}
if (a>=beta)
{
#ifndef CHECKTEST_ALL
if (tested_check || !fullincheckp(c))
#endif
{
undomove();
move_sp = restore_sp;
history[m] += cutoff_inc(d);
store_killer(m,p,a,beta);
store_hash(d, L_BOUND, a, m);
store_counter(m, p);
return a;
}
}
b = a + 1;
}
undomove();
}
move_sp = restore_sp;
if (searched)
{
if (a>oldalpha)
{
store_hash(d, EXACT, a, bestmove);
store_counter(bestmove, p);
}
else
{
store_hash(d, U_BOUND, a, bestmove);
}
return a; /* at least one valid move */
}
#ifdef CHECKTEST_ALL
else if (incheck)
#else
else if (incheckp(c))
#endif
return LOSE+p; /* farther we are from losing the better */
else
return STALEMATE; /* no valid moves and not incheck */
}
int negascout_search(int a, int beta, int d)
{
int searched = 0;
int incheck;
move_and_score *restore_sp;
int b;
int p;
move bestmove = 0;
int c = tomove();
p = ply-search_info.startply;
search_info.pv.length[p] = p;
if (p&&position_seen_before())
return 0;
if (d<=0)
return qsearch(a,beta,0);
poll_check();
search_info.branches++;
b = beta;
incheck = incheckp(c);
restore_sp = move_sp;
genmoves();
//sortmoves(restore_sp, move_sp, 0);
while (move_sp>restore_sp)
{
move m;
int intocheck;
m = popmove();
domove(m);
intocheck = (incheck) ? fullincheckp(c) : intocheckp(m, c);
if (!intocheck)
{
int t;
searched++;
if (d == g_current_searchdepth)
{
static int bit = 0;
bit = (bit+1) % 2;
draw_board();
pgPrint(35,1,bit?red:magenta,"Considering...");
hilite_move(bestmove,red,blue);
pgScreenFlipV();
}
t = -negascout_search(-b, -a, d-1);
if ((t>a)&&(t<beta))
{
if (searched>1)
a = -negascout_search(-beta,-t, d-1);
history[m] += stored_inc(d);
storepv(m, p);
bestmove = m;
search_info.move_depth[p] = searched;
if (d == g_current_searchdepth)
{
draw_board();
pgScreenFlipV();
}
}
if (t>a) a = t;
if (a>=beta)
{
history[m]+=cutoff_inc(d);
undomove();