stellar

Stellar - Chess engine written in C
Log | Files | Refs

score.c (5028B)


      1 #include "score.h"
      2 #include "moves.h"
      3 #include "stats.h"
      4 
      5 // clang-format off
      6 struct Score_T {
      7   int value;
      8   int position[64];
      9   int capture[6];
     10 };
     11 
     12 static const struct Score_T Scores[] = {
     13 [PAWN] = {
     14 .value = 100,
     15 .position = {
     16              0,   0,   0,   0,   0,   0,   0,   0,
     17              0,   0,   0, -10, -10,   0,   0,   0,
     18              0,   0,   0,   5,   5,   0,   0,   0,
     19              5,   5,  10,  20,  20,   5,   5,   5,
     20             10,  10,  10,  20,  20,  10,  10,  10,
     21             20,  20,  20,  30,  30,  30,  20,  20,
     22             30,  30,  30,  40,  40,  30,  30,  30,
     23             90,  90,  90,  90,  90,  90,  90,  90, },
     24 .capture = {   [PAWN] = 105, [KNIGHT] = 205,
     25              [BISHOP] = 305,   [ROOK] = 405,
     26               [QUEEN] = 505,   [KING] = 605} },
     27 [KNIGHT] = {
     28 .value = 300,
     29 .position = {
     30             -5,  -10 , 0,   0,   0,   0, -10,  -5,
     31             -5,   0,   0,   0,   0,   0,   0,  -5,
     32             -5,   5,  20,  10,  10,  20,   5,  -5,
     33             -5,  10,  20,  30,  30,  20,  10,  -5,
     34             -5,  10,  20,  30,  30,  20,  10,  -5,
     35             -5,   5,  20,  20,  20,  20,   5,  -5,
     36             -5,   0,   0,  10,  10,   0,   0,  -5,
     37             -5,   0,   0,   0,   0,   0,   0,  -5, },
     38 .capture = {   [PAWN] = 104, [KNIGHT] = 204,
     39              [BISHOP] = 304,   [ROOK] = 404,
     40               [QUEEN] = 504,   [KING] = 604} },
     41 [BISHOP] = {
     42 .value = 350,
     43 .position = {
     44              0,   0, -10,   0,   0, -10,   0,   0,
     45              0,  30,   0,   0,   0,   0,  30,   0,
     46              0,  10,   0,   0,   0,   0,  10,   0,
     47              0,   0,  10,  20,  20,  10,   0,   0,
     48              0,   0,  10,  20,  20,  10,   0,   0,
     49              0,   0,   0,  10,  10,   0,   0,   0,
     50              0,   0,   0,   0,   0,   0,   0,   0,
     51              0,   0,   0,   0,   0,   0,   0,   0, },
     52 .capture = {   [PAWN] = 103, [KNIGHT] = 203,
     53              [BISHOP] = 303,   [ROOK] = 403,
     54               [QUEEN] = 503,   [KING] = 603} },
     55 [ROOK] = {
     56 .value = 500,
     57 .position = {
     58              0,   0,   0,  20,  20,   0,   0,   0,
     59              0,   0,  10,  20,  20,  10,   0,   0,
     60              0,   0,  10,  20,  20,  10,   0,   0,
     61              0,   0,  10,  20,  20,  10,   0,   0,
     62              0,   0,  10,  20,  20,  10,   0,   0,
     63              0,   0,  10,  20,  20,  10,   0,   0,
     64             50,  50,  50,  50,  50,  50,  50,  50,
     65             50,  50,  50,  50,  50,  50,  50,  50, },
     66 .capture = {   [PAWN] = 102, [KNIGHT] = 202,
     67              [BISHOP] = 302,   [ROOK] = 402,
     68               [QUEEN] = 502,   [KING] = 602} },
     69 [QUEEN] = {
     70 .value = 1000,
     71 .position = {
     72              0,   0,   0,   0,   0,   0,   0,   0,
     73              0,   0,   0,   0,   0,   0,   0,   0,
     74              0,   0,   0,   0,   0,   0,   0,   0,
     75              0,   0,   0,   0,   0,   0,   0,   0,
     76              0,   0,   0,   0,   0,   0,   0,   0,
     77              0,   0,   0,   0,   0,   0,   0,   0,
     78              0,   0,   0,   0,   0,   0,   0,   0,
     79              0,   0,   0,   0,   0,   0,   0,   0, },
     80 .capture = {   [PAWN] = 101, [KNIGHT] = 201,
     81              [BISHOP] = 301,   [ROOK] = 401,
     82               [QUEEN] = 501,   [KING] = 601} },
     83 [KING] = {
     84 .value = 10000,
     85 .position = {
     86              0,   0,   5,   0, -15,   0,  10,   0,
     87              0,   5,   5,  -5,  -5,   0,   5,   0,
     88              0,   0,   5,  10,  10,   5,   0,   0,
     89              0,   5,  10,  20,  20,  10,   5,   0,
     90              0,   5,  10,  20,  20,  10,   5,   0,
     91              0,   5,   5,  10,  10,   5,   5,   0,
     92              0,   0,   5,   5,   5,   5,   0,   0,
     93              0,   0,   0,   0,   0,   0,   0,   0, },
     94 .capture = {   [PAWN] = 100, [KNIGHT] = 200,
     95              [BISHOP] = 300,   [ROOK] = 400,
     96               [QUEEN] = 500,   [KING] = 600} },
     97 };
     98 
     99 const int mirror_score[128] =
    100 {
    101 	a8, b8, c8, d8, e8, f8, g8, h8,
    102 	a7, b7, c7, d7, e7, f7, g7, h7,
    103 	a6, b6, c6, d6, e6, f6, g6, h6,
    104 	a5, b5, c5, d5, e5, f5, g5, h5,
    105 	a4, b4, c4, d4, e4, f4, g4, h4,
    106 	a3, b3, c3, d3, e3, f3, g3, h3,
    107 	a2, b2, c2, d2, e2, f2, g2, h2,
    108 	a1, b1, c1, d1, e1, f1, g1, h1, no_sq,
    109 };
    110 // clang-format on
    111 
    112 int Score_value(ePiece piece) { return Scores[piece].value; }
    113 
    114 int Score_position(ePiece piece, eColor color, Square square) {
    115     if (color == BLACK) square = mirror_score[square];
    116     return Scores[piece].position[square];
    117 }
    118 
    119 int Score_capture(ePiece src, ePiece tgt) {
    120     return Scores[src].capture[tgt] + 10000;
    121 }
    122 
    123 int Score_move(const Stats *stats, Move move) {
    124     if (move_capture(move)) {
    125         return Score_capture(piece_piece(move_piece(move)),
    126                              piece_piece(move_piece_capture(move)));
    127     }
    128 
    129     if (move_cmp(stats->killer[0][stats->ply], move))
    130         return 9000;
    131     else if (move_cmp(stats->killer[1][stats->ply], move))
    132         return 8000;
    133 
    134     return stats->history[piece_index(move_piece(move))][move_target(move)];
    135 }
    136 
    137 void Score_move_list(const Stats *stats, MoveList *list) {
    138     for (int i = 0; i < move_list_size(list); i++) {
    139         list->moves[i].score = Score_move(stats, move_list_index_move(list, i));
    140     }
    141 }