eval.c (6376B)
1 /* eval.c - evaluacion: tablas pieza-casilla PeSTO (Ronald Friederich) con 2 * interpolacion medio juego / final, mas pareja de alfiles y peones pasados. 3 * Tablas con a8 en el indice 0 (como las publico el autor). */ 4 #include "chess.h" 5 6 static const int MG_VAL[7] = { 0, 82, 337, 365, 477, 1025, 0 }; 7 static const int EG_VAL[7] = { 0, 94, 281, 297, 512, 936, 0 }; 8 static const int PHASE_INC[7] = { 0, 0, 1, 1, 2, 4, 0 }; 9 10 static const int16_t MG_PST[7][64] = { 11 { 0 }, 12 { 0, 0, 0, 0, 0, 0, 0, 0, 13 98, 134, 61, 95, 68, 126, 34, -11, 14 -6, 7, 26, 31, 65, 56, 25, -20, 15 -14, 13, 6, 21, 23, 12, 17, -23, 16 -27, -2, -5, 12, 17, 6, 10, -25, 17 -26, -4, -4, -10, 3, 3, 33, -12, 18 -35, -1, -20, -23, -15, 24, 38, -22, 19 0, 0, 0, 0, 0, 0, 0, 0 }, 20 { -167, -89, -34, -49, 61, -97, -15, -107, 21 -73, -41, 72, 36, 23, 62, 7, -17, 22 -47, 60, 37, 65, 84, 129, 73, 44, 23 -9, 17, 19, 53, 37, 69, 18, 22, 24 -13, 4, 16, 13, 28, 19, 21, -8, 25 -23, -9, 12, 10, 19, 17, 25, -16, 26 -29, -53, -12, -3, -1, 18, -14, -19, 27 -105, -21, -58, -33, -17, -28, -19, -23 }, 28 { -29, 4, -82, -37, -25, -42, 7, -8, 29 -26, 16, -18, -13, 30, 59, 18, -47, 30 -16, 37, 43, 40, 35, 50, 37, -2, 31 -4, 5, 19, 50, 37, 37, 7, -2, 32 -6, 13, 13, 26, 34, 12, 10, 4, 33 0, 15, 15, 15, 14, 27, 18, 10, 34 4, 15, 16, 0, 7, 21, 33, 1, 35 -33, -3, -14, -21, -13, -12, -39, -21 }, 36 { 32, 42, 32, 51, 63, 9, 31, 43, 37 27, 32, 58, 62, 80, 67, 26, 44, 38 -5, 19, 26, 36, 17, 45, 61, 16, 39 -24, -11, 7, 26, 24, 35, -8, -20, 40 -36, -26, -12, -1, 9, -7, 6, -23, 41 -45, -25, -16, -17, 3, 0, -5, -33, 42 -44, -16, -20, -9, -1, 11, -6, -71, 43 -19, -13, 1, 17, 16, 7, -37, -26 }, 44 { -28, 0, 29, 12, 59, 44, 43, 45, 45 -24, -39, -5, 1, -16, 57, 28, 54, 46 -13, -17, 7, 8, 29, 56, 47, 57, 47 -27, -27, -16, -16, -1, 17, -2, 1, 48 -9, -26, -9, -10, -2, -4, 3, -3, 49 -14, 2, -11, -2, -5, 2, 14, 5, 50 -35, -8, 11, 2, 8, 15, -3, 1, 51 -1, -18, -9, 10, -15, -25, -31, -50 }, 52 { -65, 23, 16, -15, -56, -34, 2, 13, 53 29, -1, -20, -7, -8, -4, -38, -29, 54 -9, 24, 2, -16, -20, 6, 22, -22, 55 -17, -20, -12, -27, -30, -25, -14, -36, 56 -49, -1, -27, -39, -46, -44, -33, -51, 57 -14, -14, -22, -46, -44, -30, -15, -27, 58 1, 7, -8, -64, -43, -16, 9, 8, 59 -15, 36, 12, -54, 8, -28, 24, 14 }, 60 }; 61 62 static const int16_t EG_PST[7][64] = { 63 { 0 }, 64 { 0, 0, 0, 0, 0, 0, 0, 0, 65 178, 173, 158, 134, 147, 132, 165, 187, 66 94, 100, 85, 67, 56, 53, 82, 84, 67 32, 24, 13, 5, -2, 4, 17, 17, 68 13, 9, -3, -7, -7, -8, 3, -1, 69 4, 7, -6, 1, 0, -5, -1, -8, 70 13, 8, 8, 10, 13, 0, 2, -7, 71 0, 0, 0, 0, 0, 0, 0, 0 }, 72 { -58, -38, -13, -28, -31, -27, -63, -99, 73 -25, -8, -25, -2, -9, -25, -24, -52, 74 -24, -20, 10, 9, -1, -9, -19, -41, 75 -17, 3, 22, 22, 22, 11, 8, -18, 76 -18, -6, 16, 25, 16, 17, 4, -18, 77 -23, -3, -1, 15, 10, -3, -20, -22, 78 -42, -20, -10, -5, -2, -20, -23, -44, 79 -29, -51, -23, -15, -22, -18, -50, -64 }, 80 { -14, -21, -11, -8, -7, -9, -17, -24, 81 -8, -4, 7, -12, -3, -13, -4, -14, 82 2, -8, 0, -1, -2, 6, 0, 4, 83 -3, 9, 12, 9, 14, 10, 3, 2, 84 -6, 3, 13, 19, 7, 10, -3, -9, 85 -12, -3, 8, 10, 13, 3, -7, -15, 86 -14, -18, -7, -1, 4, -9, -15, -27, 87 -23, -9, -23, -5, -9, -16, -5, -17 }, 88 { 13, 10, 18, 15, 12, 12, 8, 5, 89 11, 13, 13, 11, -3, 3, 8, 3, 90 7, 7, 7, 5, 4, -3, -5, -3, 91 4, 3, 13, 1, 2, 1, -1, 2, 92 3, 5, 8, 4, -5, -6, -8, -11, 93 -4, 0, -5, -1, -7, -12, -8, -16, 94 -6, -6, 0, 2, -9, -9, -11, -3, 95 -9, 2, 3, -1, -5, -13, 4, -20 }, 96 { -9, 22, 22, 27, 27, 19, 10, 20, 97 -17, 20, 32, 41, 58, 25, 30, 0, 98 -20, 6, 9, 49, 47, 35, 19, 9, 99 3, 22, 24, 45, 57, 40, 57, 36, 100 -18, 28, 19, 47, 31, 34, 39, 23, 101 -16, -27, 15, 6, 9, 17, 10, 5, 102 -22, -23, -30, -16, -16, -23, -36, -32, 103 -33, -28, -22, -43, -5, -32, -20, -41 }, 104 { -74, -35, -18, -18, -11, 15, 4, -17, 105 -12, 17, 14, 17, 17, 38, 23, 11, 106 10, 17, 23, 15, 20, 45, 44, 13, 107 -8, 22, 24, 27, 26, 33, 26, 3, 108 -18, -4, 21, 24, 27, 23, 9, -11, 109 -19, -3, 11, 21, 23, 16, 7, -9, 110 -27, -11, 4, 13, 14, 4, -5, -17, 111 -53, -34, -21, -11, -28, -14, -24, -43 }, 112 }; 113 114 /* bonus de final por peon pasado segun cuanto avanzo (0 = fila de salida) */ 115 static const int PASSED_EG[8] = { 0, 10, 15, 25, 45, 75, 120, 0 }; 116 117 static bool passed(const Pos *p, int sq, int c) 118 { 119 int f = FILE_OF(sq), r = RANK_OF(sq); 120 uint8_t enemy = PIECE(c ^ 1, PAWN); 121 for (int ff = f - 1; ff <= f + 1; ff++) { 122 if (ff < 0 || ff > 7) continue; 123 if (c == WHITE) { for (int rr = r + 1; rr < 7; rr++) if (p->sq[SQ(ff, rr)] == enemy) return false; } 124 else { for (int rr = r - 1; rr > 0; rr--) if (p->sq[SQ(ff, rr)] == enemy) return false; } 125 } 126 return true; 127 } 128 129 int evaluate(const Pos *p) 130 { 131 int mg[2] = { 0, 0 }, eg[2] = { 0, 0 }, phase = 0, bishops[2] = { 0, 0 }; 132 for (int s = 0; s < 64; s++) { 133 uint8_t pc = p->sq[s]; 134 if (!pc) continue; 135 int t = PTYPE(pc), c = PCOLOR(pc); 136 int idx = c == WHITE ? s ^ 56 : s; 137 mg[c] += MG_VAL[t] + MG_PST[t][idx]; 138 eg[c] += EG_VAL[t] + EG_PST[t][idx]; 139 phase += PHASE_INC[t]; 140 if (t == BISHOP) bishops[c]++; 141 else if (t == PAWN && passed(p, s, c)) eg[c] += PASSED_EG[c == WHITE ? RANK_OF(s) : 7 - RANK_OF(s)]; 142 } 143 for (int c = 0; c < 2; c++) 144 if (bishops[c] >= 2) { mg[c] += 30; eg[c] += 50; } 145 if (phase > 24) phase = 24; 146 int us = p->side, them = us ^ 1; 147 int mgs = mg[us] - mg[them], egs = eg[us] - eg[them]; 148 return (mgs * phase + egs * (24 - phase)) / 24 + 10; /* +10: tempo */ 149 }