Shortened version:
Code: Select all
/*********************************************************************************
*** Poor Man Table Base - End Game Classification ***
*** ***
*** Step-1 : Read classification file "class.mat" into the ***
*** end_class table ***
*** ***
*** Step-2 : In EVAL fill the 10 material parameters with the ***
*** numbers matching the position and call the ***
*** end_class table. ***
*** ***
*** The example KBN_K should return 15 ***
*********************************************************************************/
static char end_class [9][9] [3][3] [3][3] [3][3] [2][2];
static int wp,wn,wb,wr,wq,bp,bn,bb,br,bq;
static int category;
FILE *fp;
void main()
/*********************************************************************************
*** ***
*** Initialization at program start ***
*** ***
*********************************************************************************/
{ fp = fopen("class.mat","rb");
if (fp==NULL) { fclose(fp); printf("File CLASS.MAT not present"); exit (0); }
for (wp=0; wp<=8; wp++) // read predefined classifications
for (bp=0; bp<=8; bp++) // into the "end_class" table
for (wn=0; wn<=2; wn++)
for (bn=0; bn<=2; bn++)
for (wb=0; wb<=2; wb++)
for (bb=0; bb<=2; bb++)
for (wr=0; wr<=2; wr++)
for (br=0; br<=2; br++)
for (wq=0; wq<=1; wq++)
for (bq=0; bq<=1; bq++)
fread(&end_class [wp][bp] [wn][bn] [wb][bb] [wr][br] [wq][bq],1,1,fp);
fclose(fp);
/*********************************************************************************
*** ***
*** In EVAL ***
*** ***
*** Example Bishop + Knight vs lonely black King ***
*** ***
*********************************************************************************/
wp=0; bp=0; wn=1; bn=0; wb=1; bb=0; wr=0; br=0; wq=0; bq=0;
category = end_class [wp][bp] [wn][bn] [wb][bb] [wr][br] [wq][bq];
switch (category)
{ case 0 : goto normal; // no endgame class for this position
case 1 : goto Kp_Kp; // pawn ending multiple (>=2) pawns (W/B)
case 2 : goto Kp_K; // Kp vs K
case 3 : goto K_Kp; // K vs Kp
case 4 : goto KN_Kpp; // lone WN vs multiple (>=2) black pawns
case 5 : goto Kpp_KN; // lone BN vs multiple (>=2) white pawns
case 6 : goto KB_Kpp; // lone WB vs multiple (>=2) black pawns
case 7 : goto Kpp_KB; // lone BN vs multiple (>=2) white pawns
............
case 61 : goto KBx_KBx; // bishop ending with pawns (check for unequal bishops)
case 62 : goto K_K; // K vs K (draw) almost forgot this one :-)
break; }
Kp_Kp: goto normal; // case 1 : pawn ending multiple (>=2) pawns (W/B)
Kp_K: goto normal; // case 2 : Kp vs K
K_Kp: goto normal; // case 3 : K vs Kp
KN_Kpp: goto normal; // case 4 : lone WN vs multiple (>=2) black pawns
Kpp_KN: goto normal; // case 5 : lone BN vs multiple (>=2) white pawns
KB_Kpp: goto normal; // case 6 : lone WB vs multiple (>=2) black pawns
Kpp_KB: goto normal; // case 7 : lone BB vs multiple (>=2) white pawns
...............
normal: // case 0 : No endgame class for this position
return;
}
/*********************************************************************************
*** Poor Man Table Base - End Game Classification ***
*** ***
*** Remarks ***
*** ***
*** 1. Make sure the table parameters (wp,wn....bq) don't cross ***
*** their limits, a false result or a crash will the result. ***
*** ***
*** 2. The chosen numerology together with the use of SWITCH - CASE ***
*** has the advantage the compiler will lump all the (current) 62 ***
*** classifications into a so called jump-table and thus not 62 ***
*** instructions but only one is executed via an indirect jump. ***
*** ***
*** This makes the code real fast and justifies a call to a 10 ***
*** dimensional table. ***
*** ***
*** Its ASM beauty: ***
*** ***
*** mov ESI,dword ptr category // load the found table entry ***
*** jmp DATA[038h][ESI*4] // and jump to right place ***
*** ***
*** ***
*** 3. The file CLASS.MAT is included in the download and is generated ***
*** by an utility which will be released later when the full ***
*** classification process is finished. You then can organize, ***
*** extend the endgame classifications to your own liking. ***
*** ***
*** For now I would like to hear your comments first before ***
*** proceeding. ***
*** ***
*********************************************************************************/