// KFACE heuristic: bonus (or penalty) for King facing toward the other King ev_score_t kface(position_t *p, rnk_t r, fil_t f, full_board_t* board) { piece_t x = get_piece(p, r, f, board); color_t c = color_of(x); square_t opp_sq = board->pieces[opp_color(c)][0]; int delta_fil = fil_of(opp_sq) - f; int delta_rnk = rnk_of(opp_sq) - r; int bonus; switch (ori_of(x)) { case NN: bonus = delta_rnk; break; case EE: bonus = delta_fil; break; case SS: bonus = -delta_rnk; break; case WW: bonus = -delta_fil; break; default: bonus = 0; tbassert(false, "Illegal King orientation.\n"); } return (bonus * KFACE) / (abs(delta_rnk) + abs(delta_fil)); }
// KAGGRESSIVE heuristic: bonus for King with more space to back ev_score_t kaggressive(position_t *p, rnk_t r, fil_t f, full_board_t* board) { piece_t x = get_piece(p, r, f, board); color_t c = color_of(x); tbassert(ptype_of(x) == KING, "ptype_of(x) = %d\n", ptype_of(x)); square_t opp_sq = board->pieces[opp_color(c)][0]; fil_t of = fil_of(opp_sq); rnk_t _or = (rnk_t) rnk_of(opp_sq); int delta_fil = of - f; int delta_rnk = _or - r; int bonus = 0; if (delta_fil >= 0 && delta_rnk >= 0) { bonus = (f + 1) * (r + 1); } else if (delta_fil <= 0 && delta_rnk >= 0) { bonus = (BOARD_WIDTH - f) * (r + 1); } else if (delta_fil <= 0 && delta_rnk <= 0) { bonus = (BOARD_WIDTH - f) * (BOARD_WIDTH - r); } else if (delta_fil >= 0 && delta_rnk <= 0) { bonus = (f + 1) * (BOARD_WIDTH - r); } return (KAGGRESSIVE * bonus) / (BOARD_WIDTH * BOARD_WIDTH); }
// KFACE heuristic: bonus (or penalty) for King facing toward the other King ev_score_t kface(position_t *p, fil_t f, rnk_t r) { square_t sq = square_of(f, r); piece_t x = p->board[sq]; color_t c = color_of(x); square_t opp_sq = p->kloc[opp_color(c)]; int delta_fil = fil_of(opp_sq) - f; int delta_rnk = rnk_of(opp_sq) - r; int bonus; switch (ori_of(x)) { case NN: bonus = delta_rnk; break; case EE: bonus = delta_fil; break; case SS: bonus = -delta_rnk; break; case WW: bonus = -delta_fil; break; default: bonus = 0; tbassert(false, "Illegal King orientation.\n"); } return (bonus * KFACE) / (abs(delta_rnk) + abs(delta_fil)); }
// KAGGRESSIVE heuristic: bonus for King with more space to back ev_score_t kaggressive_old(position_t *p, fil_t f, rnk_t r) { square_t sq = square_of(f, r); piece_t x = p->board[sq]; color_t c = color_of(x); tbassert(ptype_of(x) == KING, "ptype_of(x) = %d\n", ptype_of(x)); square_t opp_sq = p->kloc[opp_color(c)]; fil_t of = fil_of(opp_sq); rnk_t _or = (rnk_t) rnk_of(opp_sq); int delta_fil = of - f; int delta_rnk = _or - r; int bonus = 0; if (delta_fil >= 0 && delta_rnk >= 0) { bonus = (f + 1) * (r + 1); } else if (delta_fil <= 0 && delta_rnk >= 0) { bonus = (BOARD_WIDTH - f) * (r + 1); } else if (delta_fil <= 0 && delta_rnk <= 0) { bonus = (BOARD_WIDTH - f) * (BOARD_WIDTH - r); } else if (delta_fil >= 0 && delta_rnk <= 0) { bonus = (f + 1) * (BOARD_WIDTH - r); } return (KAGGRESSIVE * bonus) / (BOARD_WIDTH * BOARD_WIDTH); }
// Marks the path of the laser until it hits a piece or goes off the board. // Returns the number of unpinned pawns. // // p : current board state // laser_map : end result will be stored here. Every square on the // path of the laser is marked with mark_mask // c : color of king shooting laser // mark_mask: what each square is marked with int mark_laser_path(position_t *p, char *laser_map, color_t c, char mark_mask) { int pinned_pawns = 0; uint8_t total_pawns; color_t color = opp_color(c); square_t o_king_sq = p->kloc[color]; if (c == WHITE) { // opposing king pins our pawns total_pawns = p->pawn_count[BLACK]; } else { total_pawns = p->pawn_count[WHITE]; } // Fire laser, recording in laser_map square_t sq = p->kloc[c]; int bdir = ori_of(p->board[sq]); int beam = beam_of(bdir); tbassert(ptype_of(p->board[sq]) == KING, "ptype: %d\n", ptype_of(p->board[sq])); laser_map[sq] |= mark_mask; // we update h_attackable here h_attackable = h_dist(sq, o_king_sq); while (true) { sq += beam; laser_map[sq] |= mark_mask; tbassert(sq < ARR_SIZE && sq >= 0, "sq: %d\n", sq); switch (ptype_of(p->board[sq])) { case EMPTY: // empty square h_attackable += h_dist(sq, o_king_sq); break; case PAWN: // Pawn h_attackable += h_dist(sq, o_king_sq); if (color_of(p->board[sq]) == color) { pinned_pawns += 1; } bdir = reflect_of(bdir, ori_of(p->board[sq])); if (bdir < 0) { // Hit back of Pawn return total_pawns - pinned_pawns; } beam = beam_of(bdir); break; case KING: // King h_attackable += h_dist(sq, o_king_sq); return total_pawns - pinned_pawns; break; case INVALID: // Ran off edge of board return total_pawns - pinned_pawns; break; default: // Shouldna happen, man! tbassert(false, "Not cool, man. Not cool.\n"); break; } } }
// H_SQUARES_ATTACKABLE heuristic: for shooting the enemy king float h_squares_attackable(position_t *p, color_t c, square_t* laser_list, int laser_list_length, full_board_t* board) { square_t o_king_sq = board->pieces[opp_color(c)][0]; rnk_t k_rnk = rnk_of(o_king_sq); fil_t k_fil = fil_of(o_king_sq); float h_attackable = 0; for (int i = 0; i < laser_list_length - 1; i++) { h_attackable += fast_h_dist(laser_list[i], laser_list[i+1], k_rnk, k_fil); } if (in_bounds(laser_list[laser_list_length - 1])) { h_attackable += fast_h_dist(laser_list[laser_list_length - 1], laser_list[laser_list_length - 1] + 1, k_rnk, k_fil); } return h_attackable; }
// H_SQUARES_ATTACKABLE heuristic: for shooting the enemy king int h_squares_attackable(position_t *p, color_t c) { char* laser_map; if (c == WHITE) { laser_map = laser_map_white; } else { laser_map = laser_map_black; } square_t o_king_sq = p->kloc[opp_color(c)]; tbassert(ptype_of(p->board[o_king_sq]) == KING, "ptype: %d\n", ptype_of(p->board[o_king_sq])); tbassert(color_of(p->board[o_king_sq]) != c, "color: %d\n", color_of(p->board[o_king_sq])); float h_attackable_temp = 0; for (fil_t f = 0; f < BOARD_WIDTH; f++) { for (rnk_t r = 0; r < BOARD_WIDTH; r++) { square_t sq = square_of(f, r); if (laser_map[sq] != 0) { h_attackable_temp += h_dist(sq, o_king_sq); } } } return h_attackable_temp; }
// Static evaluation. Returns score score_t eval(position_t *p, bool verbose) { // seed rand_r with a value of 1, as per // http://linux.die.net/man/3/rand_r static __thread unsigned int seed = 1; // verbose = true: print out components of score ev_score_t score[2] = { 0, 0 }; // int corner[2][2] = { {INF, INF}, {INF, INF} }; ev_score_t bonus; //char buf[MAX_CHARS_IN_MOVE]; color_t c; for (fil_t f = 0; f < BOARD_WIDTH; f++) { for (rnk_t r = 0; r < BOARD_WIDTH; r++) { square_t sq = square_of(f, r); piece_t x = p->board[sq]; //if (verbose) { // square_to_str(sq, buf, MAX_CHARS_IN_MOVE); //} switch (ptype_of(x)) { case EMPTY: break; case PAWN: c = color_of(x); // MATERIAL heuristic: Bonus for each Pawn bonus = PAWN_EV_VALUE; // if (verbose) { // printf("MATERIAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf); // } score[c] += bonus; // PBETWEEN heuristic bonus = pbetween(p, f, r); // if (verbose) { // printf("PBETWEEN bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf); // } score[c] += bonus; // PCENTRAL heuristic bonus = pcentral(f, r); // if (verbose) { // printf("PCENTRAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf); // } score[c] += bonus; break; case KING: c = color_of(x); // KFACE heuristic bonus = kface(p, f, r); // if (verbose) { // printf("KFACE bonus %d for %s King on %s\n", bonus, // color_to_str(c), buf); // } score[c] += bonus; // KAGGRESSIVE heuristic color_t othercolor = opp_color(c); square_t otherking = p->kloc[othercolor]; fil_t otherf = fil_of(otherking); rnk_t otherr = rnk_of(otherking); bonus = kaggressive(f, r, otherf, otherr); assert(bonus == kaggressive_old(p, f, r)); // if (verbose) { // printf("KAGGRESSIVE bonus %d for %s King on %s\n", bonus, color_to_str(c), buf); // } score[c] += bonus; break; case INVALID: break; default: tbassert(false, "Jose says: no way!\n"); // No way, Jose! } laser_map_black[sq] = 0; laser_map_white[sq] = 0; } } int black_pawns_unpinned = mark_laser_path(p, laser_map_white, WHITE, 1); // 1 = path of laser with no moves ev_score_t w_hattackable = HATTACK * (int) h_attackable; score[WHITE] += w_hattackable; // if (verbose) { // printf("HATTACK bonus %d for White\n", w_hattackable); // } // PAWNPIN Heuristic --- is a pawn immobilized by the enemy laser. int b_pawnpin = PAWNPIN * black_pawns_unpinned; score[BLACK] += b_pawnpin; int b_mobility = MOBILITY * mobility(p, BLACK); score[BLACK] += b_mobility; // if (verbose) { // printf("MOBILITY bonus %d for Black\n", b_mobility); // } int white_pawns_unpinned = mark_laser_path(p, laser_map_black, BLACK, 1); // 1 = path of laser with no moves ev_score_t b_hattackable = HATTACK * (int) h_attackable; score[BLACK] += b_hattackable; // if (verbose) { // printf("HATTACK bonus %d for Black\n", b_hattackable); // } int w_mobility = MOBILITY * mobility(p, WHITE); score[WHITE] += w_mobility; // if (verbose) { // printf("MOBILITY bonus %d for White\n", w_mobility); // } int w_pawnpin = PAWNPIN * white_pawns_unpinned; score[WHITE] += w_pawnpin; // score from WHITE point of view ev_score_t tot = score[WHITE] - score[BLACK]; if (RANDOMIZE) { ev_score_t z = rand_r(&seed) % (RANDOMIZE*2+1); tot = tot + z - RANDOMIZE; } if (color_to_move_of(p) == BLACK) { tot = -tot; } return tot / EV_SCORE_RATIO; }