Migrate from GitHub to SourceForge with this tool. Check out all of SourceForge's recent improvements.
Close

[9f60d7]: / src / alg1_search.c  Maximize  Restore  History

Download this file

1103 lines (938 with data), 39.4 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/***********************************************************************************
* Filename $RCSfile: alg1_search.c,v $
* Current CVS $Revision: 1.25 $
* Checked-in by $Author: yves $
*
* $Log: alg1_search.c,v $
*
***********************************************************************************/
#include "belofte.h"
#include "usercmd.h"
#include "alg1_search.h"
#include "alg1_moves.h"
#include "alg1_eval.h"
#include "alg1_book.h"
#include "alg1_board.h"
// --------------------------------------------------------------------
t_alg1_moves m1_movelist;
// --------------------------------------------------------------------
extern t_alg1_board m1_board;
extern t_alg1_board m1_boardmapper;
extern struct st_history m_boardhistory[MAX_GAME_LENGTH];
// --------------------------------------------------------------------
extern int curply;
extern int time_left;
extern int post_mode;
extern int level_mode;
extern int random_mode;
extern int maxdepth;
extern int engine_level;
// --------------------------------------------------------------------
static int iCurPly; // local search buffer to find the current ply while iterating
static int iMaxDepth;
static t_boolean bCountOnly;
static int iStoredMoves;
// --------------------------------------------------------------------
static void fill_movegenerationbuffers(void);
static int select_move(const t_score fScore, const int iDepth);
static t_score search_selectmove(t_depth fDepth, t_score fAlpha, t_score fBeta, t_boolean bQuieuscience);
static void add_singlemove(const posid iFrom, const posid iTo, const t_promotion cFlag);
static void fill_movesforpiecewhite(const posid iPos, const t_piece cPiece, const t_boolean bQuieuscience);
static void fill_movesforpieceblack(const posid iPos, const t_piece cPiece, const t_boolean bQuieuscience);
static void fill_movesforknightwhite(const posid iPos, const t_boolean bQuieuscience);
static void fill_movesforknightblack(const posid iPos, const t_boolean bQuieuscience);
static void fill_movesforpieceonewhite(const posid iPos, const posid iResult, const t_boolean bQuieuscience);
static void fill_movesforpieceoneblack(const posid iPos, const posid iResult, const t_boolean bQuieuscience);
static void fill_movesforpiecedir(const posid iPos, t_colour iCaptureAllowedColour,
const int iDir, const int iRep, const t_boolean bQuieuscience);
static void fill_movesforkingwhite(const posid iPos, const t_boolean bQuieuscience);
static void fill_movesforkingblack(const posid iPos, const t_boolean bQuieuscience);
static void fill_movesforpawnwhite(const posid iPos, const int iDir, const t_boolean bQuieuscience);
static void fill_movesforpawnblack(const posid iPos, const int iDir, const t_boolean bQuieuscience);
static int isInCheckAfterMove(const posid iFrom, const posid iTo, const t_promotion cFlag);
static t_boolean pieceseesmore(const posid iPiecePos, const t_piece cPieceToLookFor1,
const t_piece cPieceToLookFor2, const t_boolean bEvaluateForWhite,
const int iJump, const int iCount);
#define isEndPosition(f) (((f)<=-SCORE_ALMOST_WON)||((f)>=SCORE_ALMOST_WON))
/*----------------------------------------------------------------------+
| I/O functions |
+----------------------------------------------------------------------*/
/**
* function called once for engine construction
*/
void search_constructor(void) {
fill_movegenerationbuffers();
fill_moveevalbuffers();
}
/**
* function called once for new game, here the board position is copied from the board into the engine
* calling this function will cause ep, rock and repetition and 50 moves flags to be reset
*/
void search_init(void) {
search_initboard();
}
/**
* return 0 in case of some problem
* return 1 in case of ok
*/
int search_root(char *szMove, const int iLenMove) {
t_score fScore;
int iSelectedMove;
int iMovesLeft;
int iCalculatedDepth = maxdepth;
char szMessage[MAX_DATANAME_LENGTH];
iCurPly = curply;
bCountOnly = myFALSE;
if (iLenMove < 8) return 0; // some error, return right away
szMove[0] = '\0';
usercmd_delayedconstructor(); // in case we are not in xboard mode and the protover
// command is not called, make sure it gets called upon the first move
if (book_move(m_boardhistory[curply].szFEN, szMove)) {
// here perform opening book move and return
cmdresp_snd_info("book move");
return 1;
}
if (level_mode == 2) {
iMovesLeft = max((30 - curply) / 2, 10);
// increase depth if there is enough time left
// TODO: adjust parameters dynamically depening on game
if ((iCalculatedDepth < 2) && ((time_left / iMovesLeft) > 1)) iCalculatedDepth = 2;
if ((iCalculatedDepth < 3) && ((time_left / iMovesLeft) > 2)) iCalculatedDepth = 3;
if ((iCalculatedDepth < 4) && ((time_left / iMovesLeft) > 5)) iCalculatedDepth = 4;
if ((iCalculatedDepth < 5) && ((time_left / iMovesLeft) > 10)) iCalculatedDepth = 5;
if ((iMovesLeft > 25) && (iCalculatedDepth < 5) && (iCalculatedDepth > 0)) iCalculatedDepth++;
// decrease to avoid out of time
if ((time_left < 10) && (iCalculatedDepth > 2)) iCalculatedDepth = 2;
if ((time_left < 30) && (iCalculatedDepth > 3)) iCalculatedDepth = 3;
if ((time_left < 60) && (iCalculatedDepth > 4)) iCalculatedDepth = 4;
} else if (level_mode == 1) {
if (engine_level <= 2) {
iCalculatedDepth = 2;
} else {
iCalculatedDepth = 5;
}
}
iMaxDepth = iCalculatedDepth + MAX_DEPTH_EXTENTION; // inverted because we will go below zero
sprintf(szMessage, "Starting search depth: %d, maxdepth: %d, mode: %d",
iCalculatedDepth, iMaxDepth, level_mode);
cmdresp_snd_info(szMessage);
// we found a score of what is a good move
fScore = search_selectmove((t_depth) iCalculatedDepth, -SCORE_WON, SCORE_WON, myFALSE);
// when we return the moves, we need to use an 8x8 board, not the internal one
// we do it here, so we can use the moves generated to print out the evaluation results
normalize_movelist();
// now find in the list of moves the move with the corresponding score
iSelectedMove = select_move(fScore, iCalculatedDepth);
if (iSelectedMove >= 0) {
if (fScore < -14) {
if (iCurPly & 1) {
// black resigns
strcpy(szMove, "-4");
} else {
// white resigns
strcpy(szMove, "-5");
}
return 0;
} else {
search_printablemove(iSelectedMove, szMove);
return 1;
}
} else {
if (isInCheck(!(iCurPly & 1)) == 1) {
// in check, so mate
if (iCurPly & 1) {
// white wins
strcpy(szMove, "-3");
} else {
// black to move, and no move, so black wins
strcpy(szMove, "-2");
}
} else {
// not in check but still no move found: stalemate
strcpy(szMove, "-1");
}
}
return 0; // some problem, unidentified
}
// --------------------------------------------------------------------
void search_printablemove(const int iSelectedMove, char *szMove) {
char szPromotion[3];
// we found a move
if (m1_movelist.m1_moves[iSelectedMove].bFlag) {
// promotion flag is set, so write out = plus piece
sprintf(szPromotion, "%c", m1_movelist.m1_moves[iSelectedMove].bFlag);
} else {
szPromotion[0] = '\0';
}
// store it intermediately so we can use the move for internal housekeeping tasks
sprintf(szMove, "%c%c%c%c%s",
M1_LIJN(m1_movelist.m1_moves[iSelectedMove].ucFrom) + 'a',
M1_RIJ(m1_movelist.m1_moves[iSelectedMove].ucFrom) + '1',
M1_LIJN(m1_movelist.m1_moves[iSelectedMove].ucTo) + 'a',
M1_RIJ(m1_movelist.m1_moves[iSelectedMove].ucTo) + '1',
szPromotion);
}
/**
* when we enter, we know we can overwrite the board and the movelist variables
* the initial score is passed along to force cut-offs
* return the best score in the movelist
*
* three functions are available
* the first being the root search function, here all moves are always searched
* the second being an alpha-beta optimized function, for each sub-level
*/
static t_score search_selectmove(t_depth fDepth, t_score fAlpha, t_score fBeta, t_boolean bQuieuscience) {
int iMoves;
int i;
t_score fResult;
#if defined(_DEBUG)
char currentBest[20];
#endif
//byte cDestField;
//byte cSourceField;
t_alg1_board m1_boardbuffer;
t_alg1_moves m1_movesbuffer;
t_score fBestScore = -SCORE_WON;
// curply is even for white
iMoves = fill_movelist((iCurPly & 1) == COLOUR_BLACK, bQuieuscience, FILLMOVES);
if (iMoves > 0) {
order_moves(iMoves);
for (i = 0; i < iMoves; i++) {
// either we are looking for a non silent move or allow all possible moves
//cSourceField = m1_board.m_fields[m1_movelist.m1_moves[i].ucFrom];
//cDestField = m1_board.m_fields[m1_movelist.m1_moves[i].ucTo];
// store board position and movelist, for later restore
memcpy(&m1_boardbuffer, &m1_board, sizeof (m1_board));
memcpy(&m1_movesbuffer, &m1_movelist, sizeof (m1_movelist));
iCurPly++;
if (iCurPly & 1) {
// curply is odd for white
apply_movewhite(m1_movelist.m1_moves[i].ucFrom,
m1_movelist.m1_moves[i].ucTo,
m1_movelist.m1_moves[i].bFlag);
} else {
apply_moveblack(m1_movelist.m1_moves[i].ucFrom,
m1_movelist.m1_moves[i].ucTo,
m1_movelist.m1_moves[i].bFlag);
}
if (((int) fDepth > 1) && (m1_movelist.m1_moves[i].bNonSilent)) {
// search one level deeper
fResult = -search_selectmove(fDepth - CAPTURE_MOVE_EXTENTION, -fBeta, -fAlpha, myFALSE);
m1_movelist.m1_moves[i].iDepth = fDepth - CAPTURE_MOVE_EXTENTION;
} else if ((int) fDepth > 1) {
fResult = -search_selectmove(fDepth - SILENT_EXTENTION, -fBeta, -fAlpha, myFALSE);
m1_movelist.m1_moves[i].iDepth = fDepth - SILENT_EXTENTION;
} else if ((m1_movelist.m1_moves[i].bNonSilent)
&& (fDepth > (t_depth) iMaxDepth)
&& (bQuieuscience)) {
// quiescience search, only reaches this branch after going to depth zero in previous iteration
// so no additional test for depth greater than zero needed
// note: iDepth is negative here, iMaxDepth is even more negative
fResult = -search_selectmove(fDepth - NON_SILENT_EXTENTION, -fBeta, -fAlpha, myTRUE);
m1_movelist.m1_moves[i].iDepth = fDepth - NON_SILENT_EXTENTION;
} else {
// curply is odd for white
fResult = eval_pos(iCurPly & 1);
}
iCurPly--;
// restore old position and movelist
memcpy(&m1_movelist, &m1_movesbuffer, sizeof (m1_movelist));
memcpy(&m1_board, &m1_boardbuffer, sizeof (m1_board));
// keep the score of all moves for the higher level
m1_movelist.m1_moves[i].bValue = fResult;
if (fResult > fBestScore) fBestScore = fResult;
if (fResult >= fBeta) {
break;
}
if (fResult > fAlpha) fAlpha = fResult;
}
} else {
// there are no moves found, this can be in normal or in quieuscence
// when there are no more captures
// curply is even for white
fBestScore = eval_pos(!(iCurPly & 1));
}
// make sure the scores go to zero
if (fBestScore > DEPTH_PENALTY)
return fBestScore - DEPTH_PENALTY;
else if (fBestScore < -DEPTH_PENALTY)
return fBestScore + DEPTH_PENALTY;
return fBestScore;
}
/**
* select among one of the possible moves
* or minus one if no valid move could be found
*/
static int select_move(const t_score fScoreToBeFound, const int iDepth) {
int iIndex = 0;
char szBuffer[MAX_DATANAME_LENGTH];
int iIteration = 0;
t_score allowedDeviation = ALLOWED_DEVIATION;
// if non random, remove the width
if (random_mode == 0) allowedDeviation = 0;
qsort_moves();
if (post_mode) {
while ((iIndex < m1_movelist.m1_count) &&
((iIndex < LIST_LENGTH)
|| (m1_movelist.m1_moves[iIndex].bValue
>= (fScoreToBeFound - allowedDeviation)))) {
// print out all possible moves or best n moves
search_printablemove(iIndex, szBuffer);
cmdresp_snd_thinking(curply, m1_movelist.m1_moves[iIndex].bValue, szBuffer);
iIndex++;
}
sprintf(szBuffer, "== target at depth %d", iDepth);
cmdresp_snd_thinking(curply, fScoreToBeFound, szBuffer);
}
if (m1_movelist.m1_count) {
while (1) {
iIteration++; // make sure in case of serious trouble, to select a move
iIndex = rand() % m1_movelist.m1_count;
if ((m1_movelist.m1_moves[iIndex].bValue
>= (fScoreToBeFound - allowedDeviation))
|| (iIteration > 10000)) break;
}
return iIndex;
}
return -1;
}
/*----------------------------------------------------------------------+
| board interface functions |
+----------------------------------------------------------------------*/
/**
* generate internal buffers to speed up further working of the program
*/
static void fill_movegenerationbuffers(void) {
int iRow, iColumn;
// reset board to invalid
for (iRow = 0; iRow < 12; iRow++) {
for (iColumn = 0; iColumn < 12; iColumn++) {
m1_board.m_fields[iColumn + iRow * 12] = FIELD_BORDER;
}
}
// reset board to invalid
for (iRow = 0; iRow < 12; iRow++) {
for (iColumn = 0; iColumn < 12; iColumn++) {
m1_boardmapper.m_fields[iColumn + iRow * 12] = FIELD_BORDER;
}
}
for (iRow = 0; iRow < 8; iRow++) {
for (iColumn = 0; iColumn < 8; iColumn++) {
m1_boardmapper.m_fields[iColumn + 2 + (iRow + 2)*12] = iColumn + iRow * 8;
}
}
}
/**
* normalize the movelist to a 8x8 board
*/
void normalize_movelist(void) {
int i;
posid iFrom, iTo;
for (i = 0; i < m1_movelist.m1_count; i++) {
iFrom = m1_movelist.m1_moves[i].ucFrom;
iTo = m1_movelist.m1_moves[i].ucTo;
m1_movelist.m1_moves[i].ucFrom = MAP144TO64(iFrom);
m1_movelist.m1_moves[i].ucTo = MAP144TO64(iTo);
}
}
// --------------------------------------------------------------------
int search_apply_move(const char *szMove) {
posid iFrom;
posid iTo;
t_promotion cFlag = PROMOTION_NONE;
if (strlen(szMove) == 5)
cFlag = szMove [4];
iFrom = (2 + szMove[0] - 'a') + (2 + szMove[1] - '1')*12;
iTo = (2 + szMove[2] - 'a') + (2 + szMove[3] - '1')*12;
if (curply & 1) {
// curply is odd for white, because it is updated before this call
apply_movewhite(iFrom, iTo, cFlag);
} else {
apply_moveblack(iFrom, iTo, cFlag);
}
return 0;
}
/*----------------------------------------------------------------------+
| move generation |
+----------------------------------------------------------------------*/
/**
* fill all possible moves in the movelist
*/
int fill_movelist(const enum tColour nColour, const t_boolean bQuieuscience, const enum tFillMoves bFill) {
posid iPos;
t_piece cPiece;
int iMoves;
if (bFill) {
// if the bFill variable is set, moves are not added to the list
// but the global static variable iStoredMoves is updated
// beware, this operation is not recursive
memset(&m1_movelist, 0, sizeof (m1_movelist));
bCountOnly = myFALSE;
} else {
iStoredMoves = 0;
bCountOnly = myTRUE;
}
// we will take each pieace and generate a move for it
if (nColour == COLOUR_BLACK) {
// generate for black
for (iPos = STARTPOS; iPos < ENDPOS; iPos++) {
cPiece = m1_board.m_fields[iPos];
switch (cPiece) {
case BLACK_PAWN:
// black piece found
fill_movesforpawnblack(iPos, -12, bQuieuscience);
break;
case BLACK_KING:
fill_movesforkingblack(iPos, bQuieuscience);
break;
case BLACK_KNIGHT:
fill_movesforknightblack(iPos, bQuieuscience);
break;
case BLACK_ROOK:
case BLACK_BISHOP:
case BLACK_QUEEN:
fill_movesforpieceblack(iPos, cPiece, bQuieuscience);
break;
default:
break;
}
}
} else {
// generate for white
for (iPos = STARTPOS; iPos < ENDPOS; iPos++) {
cPiece = m1_board.m_fields[iPos];
switch (cPiece) {
case PIECE_PAWN:
// white piece found
fill_movesforpawnwhite(iPos, 12, bQuieuscience);
break;
case PIECE_KING:
fill_movesforkingwhite(iPos, bQuieuscience);
break;
case PIECE_KNIGHT:
fill_movesforknightwhite(iPos, bQuieuscience);
break;
case PIECE_ROOK:
case PIECE_BISHOP:
case PIECE_QUEEN:
fill_movesforpiecewhite(iPos, cPiece, bQuieuscience);
break;
default:
break;
}
}
}
if (bFill) {
iMoves = m1_movelist.m1_count;
} else {
iMoves = iStoredMoves;
}
return iMoves;
}
/**
* fill all possibe king moves
*/
static void fill_movesforkingwhite(const posid iPos, const t_boolean bQuieuscience) {
fill_movesforpieceonewhite(iPos, iPos + 11, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos + 12, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos + 13, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos + 1, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos - 11, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos - 12, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos - 13, bQuieuscience);
fill_movesforpieceonewhite(iPos, iPos - 1, bQuieuscience);
if (isUnderAttack(m1_board.m_whiteking, myTRUE) == 0) {
if (m1_board.m_whitecastle[CASTLE_LONG]) {
if ((m1_board.m_fields[27] == FIELD_EMPTY)
&& (m1_board.m_fields[28] == FIELD_EMPTY)
&& (m1_board.m_fields[29] == FIELD_EMPTY)
&& (isUnderAttack(29, myTRUE) == 0)) {
add_singlemove(30, 28, PROMOTION_NONE);
}
}
if (m1_board.m_whitecastle[CASTLE_SHORT]) {
if ((m1_board.m_fields[31] == FIELD_EMPTY)
&& (m1_board.m_fields[32] == FIELD_EMPTY)
&& (isUnderAttack(31, myTRUE) == 0)) {
add_singlemove(30, 32, PROMOTION_NONE);
}
}
}
}
// --------------------------------------------------------------------
static void fill_movesforkingblack(const posid iPos, const t_boolean bQuieuscience) {
fill_movesforpieceoneblack(iPos, (const posid) iPos + 11, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 12, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 13, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 1, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 11, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 12, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 13, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 1, bQuieuscience);
if (isUnderAttack(m1_board.m_blackking, myFALSE) == 0) {
if (m1_board.m_blackcastle[CASTLE_LONG]) {
if ((m1_board.m_fields[111] == FIELD_EMPTY)
&& (m1_board.m_fields[112] == FIELD_EMPTY)
&& (m1_board.m_fields[113] == FIELD_EMPTY)
&& (isUnderAttack(113, myFALSE) == 0)) {
add_singlemove(114, 112, PROMOTION_NONE);
}
}
if (m1_board.m_blackcastle[CASTLE_SHORT]) {
if ((m1_board.m_fields[115] == FIELD_EMPTY)
&& (m1_board.m_fields[116] == FIELD_EMPTY)
&& (isUnderAttack(115, myFALSE) == 0)) {
add_singlemove(114, 116, PROMOTION_NONE);
}
}
}
}
/**
* fill all possible moves for a piece on position x
*/
static void fill_movesforpiecewhite(const posid iPos, const t_piece cPiece, const t_boolean bQuieuscience) {
if ((cPiece == PIECE_ROOK) || (cPiece == PIECE_QUEEN)) {
// calculate all rook moves
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, 12, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, 1, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, -12, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, -1, 7, bQuieuscience);
}
if ((cPiece == PIECE_BISHOP) || (cPiece == PIECE_QUEEN)) {
// calculate all rook moves
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, 13, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, 11, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, -13, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_BLACK, -11, 7, bQuieuscience);
}
}
// --------------------------------------------------------------------
static void fill_movesforpieceblack(const posid iPos, const t_piece cPiece, const t_boolean bQuieuscience) {
if ((cPiece == BLACK_ROOK) || (cPiece == BLACK_QUEEN)) {
// calculate all rook moves
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, 12, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, 1, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, -12, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, -1, 7, bQuieuscience);
}
if ((cPiece == BLACK_BISHOP) || (cPiece == BLACK_QUEEN)) {
// calculate all rook moves
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, 13, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, 11, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, -13, 7, bQuieuscience);
fill_movesforpiecedir(iPos, CAPTURE_COLOUR_WHITE, -11, 7, bQuieuscience);
}
}
// --------------------------------------------------------------------
static void fill_movesforknightwhite(const posid iPos, const t_boolean bQuieuscience) {
// calculate all knights moves
fill_movesforpieceonewhite(iPos, (const posid) iPos + 25, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos + 23, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos + 14, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos + 10, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos - 25, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos - 23, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos - 14, bQuieuscience);
fill_movesforpieceonewhite(iPos, (const posid) iPos - 10, bQuieuscience);
}
// --------------------------------------------------------------------
static void fill_movesforknightblack(const posid iPos, const t_boolean bQuieuscience) {
// calculate all knights moves
fill_movesforpieceoneblack(iPos, (const posid) iPos + 25, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 23, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 14, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos + 10, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 25, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 23, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 14, bQuieuscience);
fill_movesforpieceoneblack(iPos, (const posid) iPos - 10, bQuieuscience);
}
/**
* add a single move
* fill cflag in case of promotion
* set cNonSilent in case of check or capture
* TODO add notion of king protector, only when moving kingprotector the incheck test needs
* to be performed
* Also, no test required for opponent check through pawn or knight
* TODO add single pawn move
*/
static void add_singlemove(const posid iFrom, const posid iTo, const t_promotion cFlag) {
int iWhoInCheck = isInCheckAfterMove(iFrom, iTo, cFlag);
if (bCountOnly) {
if (iWhoInCheck != 1)
iStoredMoves++;
} else {
auto int i = m1_movelist.m1_count;
if (iWhoInCheck != 1) {
// the player to play is not in check
m1_movelist.m1_moves[i].ucFrom = iFrom;
m1_movelist.m1_moves[i].ucTo = iTo;
m1_movelist.m1_moves[i].bFlag = cFlag;
m1_movelist.m1_moves[i].bValue = -SCORE_ALMOST_WON;
m1_movelist.m1_moves[i].iDepth = 0;
if ((m1_board.m_fields[iTo] != FIELD_EMPTY)) { // || (cFlag)) { // disable promotion non silent for debug
m1_movelist.m1_moves[i].bNonSilent = 1;
} else {
if (iWhoInCheck == 2) {
// but opponent is in check
m1_movelist.m1_moves[i].bNonSilent = 1;
}
}
m1_movelist.m1_count++;
} else {
iStoredMoves++; // for debug only, to set a breakpoint
}
}
}
/**
* add all moves in this direction for a piece
*/
static void fill_movesforpiecedir(const posid iPos, t_colour iCaptureAllowedColour,
const int iDir, const int iRep, const t_boolean bQuieuscience) {
int i;
char cPieceResult;
int isWhite;
posid iResult = iPos;
for (i = 0; i < iRep; i++) {
// repeat all moves in a direction
iResult += iDir;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
if (!bQuieuscience) add_singlemove(iPos, iResult, PROMOTION_NONE);
} else if (cPieceResult == FIELD_BORDER) {
// if we reach the border, we do not continue anymore
break;
} else {
// we will look if we can capture
isWhite = (!(cPieceResult & (LOWERCASEBIT)));
if ((isWhite && (iCaptureAllowedColour == COLOUR_WHITE)) || ((!isWhite) && (iCaptureAllowedColour == COLOUR_BLACK))) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, iResult, PROMOTION_NONE);
}
// we still break out as we cannot take 2 pieces in a row, and cannot jump over own pieces
break;
}
}
}
/**
* add single step move
*/
static void fill_movesforpieceonewhite(const posid iPos, const posid iResult, const t_boolean bQuieuscience) {
char cPieceResult;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
if (!bQuieuscience) add_singlemove(iPos, iResult, PROMOTION_NONE);
} else if (cPieceResult != FIELD_BORDER) {
// we will look if we can capture
if (cPieceResult & (LOWERCASEBIT)) {
// is it a black piece?
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, iResult, PROMOTION_NONE);
}
}
}
/**
* add single step move
*/
static void fill_movesforpieceoneblack(const posid iPos, const posid iResult, const t_boolean bQuieuscience) {
char cPieceResult;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
if (!bQuieuscience) add_singlemove(iPos, iResult, PROMOTION_NONE);
} else if (cPieceResult != FIELD_BORDER) {
// we will look if we can capture
// TODO: optimize by giving the bit as parameter
if (!(cPieceResult & (LOWERCASEBIT))) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, iResult, PROMOTION_NONE);
}
}
}
/**
* store all promotion moves, also adjust last move so it becomes a promotion
*/
static void fill_movesforpromotion(const posid iPos, const posid iDest) {
// correct last move, it does not have the flag set
m1_movelist.m1_count--;
add_singlemove(iPos, iDest, PROMOTION_QUEEN);
add_singlemove(iPos, iDest, PROMOTION_ROOK);
add_singlemove(iPos, iDest, PROMOTION_KNIGHT);
add_singlemove(iPos, iDest, PROMOTION_BISHOP);
}
/**
* fill all possible moves for a pawn
*/
static void fill_movesforpawnwhite(const posid iPos, const int iDir, const t_boolean bQuieuscience) {
char cPieceResult;
posid iResult = iPos + iDir;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
if (!bQuieuscience) add_singlemove(iPos, iResult, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 6) {
// always add promotion moves to the movelist
fill_movesforpromotion(iPos, iResult);
}
// check if we can do a double move
if (ROW144TO64(iPos) == 1) {
if (m1_board.m_fields[iResult + iDir] == FIELD_EMPTY) {
if (!bQuieuscience) add_singlemove(iPos, iResult + iDir, PROMOTION_NONE);
}
}
}
cPieceResult = m1_board.m_fields[iResult - 1];
if (cPieceResult != FIELD_BORDER) {
if (cPieceResult != FIELD_EMPTY) {
// we will look if we can capture
// TODO: optimize by giving the bit as parameter
if (cPieceResult & (LOWERCASEBIT)) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, iResult - 1, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 6) {
fill_movesforpromotion(iPos, iResult - 1);
}
}
} else if (iResult - 1 == m1_board.m_epfield) {
// ep move
add_singlemove(iPos, iResult - 1, PROMOTION_NONE);
}
}
cPieceResult = m1_board.m_fields[iResult + 1];
if (cPieceResult != FIELD_BORDER) {
if (cPieceResult != FIELD_EMPTY) {
// we will look if we can capture
// TODO: optimize by giving the bit as parameter
if (cPieceResult & (LOWERCASEBIT)) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, iResult + 1, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 6) {
fill_movesforpromotion(iPos, iResult + 1);
}
}
} else if (iResult + 1 == m1_board.m_epfield) {
// ep move
add_singlemove(iPos, iResult + 1, PROMOTION_NONE);
}
}
}
/**
* fill all possible moves for a pawn
*/
static void fill_movesforpawnblack(const posid iPos, const int iDir, const t_boolean bQuieuscience) {
char cPieceResult;
posid iResult = iPos + iDir;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
if (!bQuieuscience) add_singlemove(iPos, iResult, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 1) {
// always add promotion moves to the movelist
fill_movesforpromotion(iPos, iResult);
}
// check if we can do a double move
if (ROW144TO64(iPos) == 6) {
if (m1_board.m_fields[iResult + iDir] == FIELD_EMPTY) {
if (!bQuieuscience) add_singlemove(iPos, (const posid) iResult + iDir, PROMOTION_NONE);
}
}
}
cPieceResult = m1_board.m_fields[iResult - 1];
if (cPieceResult != FIELD_BORDER) {
if (cPieceResult != FIELD_EMPTY) {
// we will look if we can capture
// TODO: optimize by giving the bit as parameter
if (!(cPieceResult & (LOWERCASEBIT))) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, (const posid) iResult - 1, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 1) {
// always add promotion moves to the movelist
fill_movesforpromotion(iPos, (const posid) iResult - 1);
}
}
} else if (iResult - 1 == m1_board.m_epfield) {
// ep move
add_singlemove(iPos, (const posid) iResult - 1, PROMOTION_NONE);
}
}
cPieceResult = m1_board.m_fields[iResult + 1];
if (cPieceResult != FIELD_BORDER) {
if (cPieceResult != FIELD_EMPTY) {
// we will look if we can capture
// TODO: optimize by giving the bit as parameter
if (!(cPieceResult & (LOWERCASEBIT))) {
// we know that the allowed colour for capture is the colour of the destination piece
add_singlemove(iPos, (const posid) iResult + 1, PROMOTION_NONE);
// add check for promotion moves
if (ROW144TO64(iPos) == 1) {
// always add promotion moves to the movelist
fill_movesforpromotion(iPos, (const posid) iResult + 1);
}
}
} else if (iResult + 1 == m1_board.m_epfield) {
// ep move
add_singlemove(iPos, (const posid) iResult + 1, PROMOTION_NONE);
}
}
}
/**
* is the player in check after the move is executed (return 1)
* also do check if the current move causes a check (return 2)
*/
static int isInCheckAfterMove(const posid iFrom, const posid iTo, const t_promotion cFlag) {
int iCheck = 0;
// do we need to consider invalid moves?
struct st_alg1_board m1_boardbuffer;
t_boolean isWhite = (m1_board.m_fields [iFrom] & (LOWERCASEBIT)) ? myFALSE : myTRUE;
// store board position, for later restore
memcpy(&m1_boardbuffer, &m1_board, sizeof (m1_board));
if (isWhite) {
apply_movewhite(iFrom, iTo, cFlag);
} else {
apply_moveblack(iFrom, iTo, cFlag);
}
iCheck = isInCheck(isWhite);
if (!iCheck) {
if (isInCheck(!isWhite)) iCheck = 2;
}
// restore old position
memcpy(&m1_board, &m1_boardbuffer, sizeof (m1_board));
return (iCheck);
}
/**
* is the position in check
*/
t_boolean isInCheck(const t_boolean bEvaluateForWhite) {
posid iKingPos = bEvaluateForWhite ? m1_board.m_whiteking : m1_board.m_blackking;
return isUnderAttack(iKingPos, bEvaluateForWhite) > 0;
}
/**
* test if after executing the move, the king is in check
* the king to test will be of the same colour as the colour moving the piece
* we will first apply the move, then see if the king jumps to any of the opposing colours
*/
#define pieceseesone(x,y,z) if(m1_board.m_fields[(z)]==((y)?TO_BLACK(x):TO_WHITE(x))) { isAttack++; }
#define pieceseesdirm(x,y,z,a,b,c) if(pieceseesmore((x),(y),(z),(a),(b),(c))) { isAttack++; }
#define pieceopenline(x,y,z,a) if(pieceseesopen((x),(y),(z),(a))) { isAttack++; }
unsigned int isUnderAttack(const posid iPiecePos, const t_boolean bEvaluateForWhite) {
unsigned int isAttack = 0;
// calculate all knights moves
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 25);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 23);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 14);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 10);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 25);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 23);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 14);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 10);
// calculate all rook moves
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_QUEEN, bEvaluateForWhite, 12, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_QUEEN, bEvaluateForWhite, 1, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_QUEEN, bEvaluateForWhite, -12, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_QUEEN, bEvaluateForWhite, -1, 7);
// calculate all bishop moves
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_QUEEN, bEvaluateForWhite, 13, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_QUEEN, bEvaluateForWhite, 11, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_QUEEN, bEvaluateForWhite, -13, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_QUEEN, bEvaluateForWhite, -11, 7);
// calculate all king moves
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos + 12);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos + 1);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos - 12);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos - 1);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos + 13);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos + 11);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos - 13);
pieceseesone(PIECE_KING, bEvaluateForWhite, iPiecePos - 11);
// handle pawn moves
if (bEvaluateForWhite) {
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos + 11);
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos + 13);
} else {
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos - 11);
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos - 13);
}
return (isAttack);
}
/**
* see if the piece gets attached by a minor piece, needed for evaluation
*/
unsigned int isUnderMinorAttack(const posid iPiecePos, const t_piece cPiece, const t_boolean bEvaluateForWhite) {
int isAttack = 0;
if ((cPiece == PIECE_ROOK) || (cPiece == PIECE_QUEEN)) {
// calculate all knights moves
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 25);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 23);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 14);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos + 10);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 25);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 23);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 14);
pieceseesone(PIECE_KNIGHT, bEvaluateForWhite, iPiecePos - 10);
}
if (cPiece == PIECE_QUEEN) {
// calculate all rook moves
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_ROOK, bEvaluateForWhite, 12, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_ROOK, bEvaluateForWhite, 1, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_ROOK, bEvaluateForWhite, -12, 7);
pieceseesdirm(iPiecePos, PIECE_ROOK, PIECE_ROOK, bEvaluateForWhite, -1, 7);
}
if ((cPiece == PIECE_QUEEN) || (cPiece == PIECE_ROOK)) {
// calculate all bishop moves
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_BISHOP, bEvaluateForWhite, 13, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_BISHOP, bEvaluateForWhite, 11, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_BISHOP, bEvaluateForWhite, -13, 7);
pieceseesdirm(iPiecePos, PIECE_BISHOP, PIECE_BISHOP, bEvaluateForWhite, -11, 7);
}
if (cPiece != PIECE_PAWN) {
// handle pawn moves
if (bEvaluateForWhite) {
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos + 11);
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos + 13);
} else {
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos - 11);
pieceseesone(PIECE_PAWN, bEvaluateForWhite, iPiecePos - 13);
}
}
return (isAttack);
}
// --------------------------------------------------------------------
static t_boolean pieceseesmore(const posid iPiecePos, const t_piece cPieceToLookFor1, const t_piece cPieceToLookFor2,
const t_boolean bEvaluateForWhite, const int iJump, const int iCount) {
int i;
t_piece cPiece1;
t_piece cPiece2;
t_piece cPieceResult;
int iResult = iPiecePos;
if (bEvaluateForWhite) {
cPiece1 = cPieceToLookFor1 + LOWERCASEBIT;
cPiece2 = cPieceToLookFor2 + LOWERCASEBIT;
} else {
cPiece1 = cPieceToLookFor1;
cPiece2 = cPieceToLookFor2;
}
for (i = 0; i < iCount; i++) {
// repeat all moves in a direction
iResult += iJump;
cPieceResult = m1_board.m_fields[iResult];
if (cPieceResult == FIELD_EMPTY) {
// empty case, no capture move, so move possible
} else if (cPieceResult == cPiece1) {
return myTRUE;
} else if (cPieceResult == cPiece2) {
return myTRUE;
} else {
// if we reach the border or another piece, we do not continue anymore
break;
}
}
return myFALSE;
}
// eof