📄 evaluate.cpp
字号:
else if(mat_info->recog == MAT_KKNP)
{
// KNPK (black)
draw_init_list(list, board, 1);
if(draw_knpk(list, COLOUR_OPP(board->turn)))
{
mul[0] = 1; // 1/16;
mul[1] = 1; // 1/16;
}
}
else if(mat_info->recog == MAT_KRPKR)
{
// KRPKR (white)
draw_init_list(list, board, 0);
if(draw_krpkr(list, board->turn))
{
mul[0] = 1; // 1/16;
mul[1] = 1; // 1/16;
}
}
else if(mat_info->recog == MAT_KRKRP)
{
// KRPKR (black)
draw_init_list(list, board, 1);
if(draw_krpkr(list, COLOUR_OPP(board->turn)))
{
mul[0] = 1; // 1/16;
mul[1] = 1; // 1/16;
}
}
else if(mat_info->recog == MAT_KBPKB)
{
// KBPKB (white)
draw_init_list(list, board, 0);
if(draw_kbpkb(list, board->turn))
{
mul[0] = 1; // 1/16;
mul[1] = 1; // 1/16;
}
}
else if(mat_info->recog == MAT_KBKBP)
{
// KBPKB (black)
draw_init_list(list, board, 1);
if(draw_kbpkb(list, COLOUR_OPP(board->turn)))
{
mul[0] = 1; // 1/16;
mul[1] = 1; // 1/16;
}
}
}
// eval_piece()
static void eval_piece(const board_t *board, const material_info_t *mat_info, const pawn_info_t *pawn_info,
int *opening, int *endgame)
{
int color;
int op[2], eg[2];
int me, opp;
int opp_flag;
const sq_t *ptr;
int from, to;
int piece;
int mob;
int capture;
const int *unit;
int rook_file, king_file;
int king;
int delta;
int piece_nb, attackvalue;
int king_rank, piece_rank, new_mob, piece_file;
// init
for ( color = 0; color < 2; color++ )
{
op[color] = 0;
eg[color] = 0;
}
// eval
for ( color = 0; color < 2; color++ )
{
me = color;
opp = COLOUR_OPP(me);
opp_flag = COLOUR_FLAG(opp);
unit = mob_unit[me];
piece_nb = 0;
attackvalue = 0;
// piece loop
for ( ptr = &board->piece[me][1]; (from = *ptr) != 0; ptr++ )
{
piece = board->square[from];
switch(PIECE_TYPE(piece))
{
case (1 << 4):
// mobility
mob = -4;
mob += unit[board->square[from - 33]];
mob += unit[board->square[from - 31]];
mob += unit[board->square[from - 18]];
mob += unit[board->square[from - 14]];
mob += unit[board->square[from + 14]];
mob += unit[board->square[from + 18]];
mob += unit[board->square[from + 31]];
mob += unit[board->square[from + 33]];
op[me] += mob * 4;
eg[me] += mob * 4;
// mob = KnightPosMatrix[me][square_to_64[from]];
// outpost
mob = 0;
if(me == 0)
{
if(board->square[from - 17] == ((1 << 2) | (1 << 0)))
mob += knight_outpost[me][square_to_64[from]];
if(board->square[from - 15] == ((1 << 2) | (1 << 0)))
mob += knight_outpost[me][square_to_64[from]];
}
else
{
if(board->square[from + 17] == ((1 << 3) | (1 << 1)))
mob += knight_outpost[me][square_to_64[from]];
if(board->square[from + 15] == ((1 << 3) | (1 << 1)))
mob += knight_outpost[me][square_to_64[from]];
}
op[me] += mob;
// eg[me] += mob;
if(PAWN_RANK(from, me) >= (0x8))
{
piece_nb++;
attackvalue += 1;
}
break;
case (1 << 5):
// mobility
mob = -6;
for ( to = from - 17; capture=board->square[to], THROUGH(capture); to -= 17 )
mob += 1;
mob += unit[capture];
for ( to = from - 15; capture=board->square[to], THROUGH(capture); to -= 15 )
mob += 1;
mob += unit[capture];
for ( to = from + 15; capture=board->square[to], THROUGH(capture); to += 15 )
mob += 1;
mob += unit[capture];
for ( to = from + 17; capture=board->square[to], THROUGH(capture); to += 17 )
mob += 1;
mob += unit[capture];
op[me] += mob * 5;
eg[me] += mob * 5;
if(PAWN_RANK(from, me) >= (0x8))
{
piece_nb++;
attackvalue += 1;
}
break;
case (1 << 6):
// mobility
mob = -7;
for ( to = from - 16; capture=board->square[to], THROUGH(capture); to -= 16 )
mob += 1;
mob += unit[capture];
for ( to = from - 1; capture=board->square[to], THROUGH(capture); to -= 1 )
mob += 1;
mob += unit[capture];
for ( to = from + 1; capture=board->square[to], THROUGH(capture); to += 1 )
mob += 1;
mob += unit[capture];
for ( to = from + 16; capture=board->square[to], THROUGH(capture); to += 16 )
mob += 1;
mob += unit[capture];
op[me] += mob * 2;
eg[me] += mob * 4;
// open file
op[me] -= 20 / 2;
eg[me] -= 20 / 2;
rook_file = SQUARE_FILE(from);
if(board->pawn_file[me][rook_file] == 0)
{ // no friendly pawn
op[me] += 10;
eg[me] += 10;
if(board->pawn_file[opp][rook_file] == 0)
{ // no enemy pawn
op[me] += 20 - 10;
eg[me] += 20 - 10;
}
if((mat_info->cflags[opp]&(1 << 3)) != 0)
{
king = KING_POS(board, opp);
king_file = SQUARE_FILE(king);
delta = abs(rook_file - king_file); // file distance
if(delta <= 1)
{
op[me] += 10;
if(delta == 0)
op[me] += 20 - 10;
}
}
}
// 7th rank
if(PAWN_RANK(from, me) == (0xA))
{
if((pawn_info->flags[opp]&(1 << 0)) != 0 // opponent pawn on 7th rank
|| PAWN_RANK(KING_POS(board, opp), me) == (0xB))
{
op[me] += 20;
eg[me] += 40;
}
}
if(PAWN_RANK(from, me) >= (0x8))
{
piece_nb++;
attackvalue += 2;
}
break;
case ((1 << 5) | (1 << 6)):
// mobility
king = KING_POS(board, opp);
king_file = SQUARE_FILE(king);
king_rank = SQUARE_RANK(king);
piece_file = SQUARE_FILE(from);
piece_rank = SQUARE_RANK(from);
new_mob = 10 - ((abs(king_file - piece_file) + abs(king_rank - piece_rank)));
op[me] += new_mob;
eg[me] += new_mob;
// 7th rank
if(PAWN_RANK(from, me) == (0xA))
{
if((pawn_info->flags[opp]&(1 << 0)) != 0 // opponent pawn on 7th rank
|| PAWN_RANK(KING_POS(board, opp), me) == (0xB))
{
op[me] += 10;
eg[me] += 20;
}
}
if(PAWN_RANK(from, me) >= (0x8))
{
piece_nb++;
attackvalue += 4;
}
break;
}
}
op[me] += s_space_weight[piece_nb] * attackvalue;
}
// update
*opening += op[0] - op[1];
*endgame += eg[0] - eg[1];
}
// eval_king()
static void eval_king(const board_t *board, const material_info_t *mat_info, int *opening, int *endgame)
{
int color;
int op[2], eg[2];
int me, opp;
int from;
int penalty_1, penalty_2;
int tmp;
int penalty;
int king;
const sq_t *ptr;
int piece;
int attack_tot;
int piece_nb;
int king_file, king_rank, file;
// init
for ( color = 0; color < 2; color++ )
{
op[color] = 0;
eg[color] = 0;
}
// white pawn shelter
if((mat_info->cflags[0]&(1 << 3)) != 0)
{
king_is_safe[0] = false;
penalty = 0;
me = 0;
if(board->square[(0x4A)] == ((1 << 7) | (1 << 0)) || board->square[(0x4B)] == ((1 << 7) | (1 << 0)))
{
if(board->square[(0x5A)] == ((1 << 2) | (1 << 0))
&& (board->square[(0x5B)] == ((1 << 2) | (1 << 0)) || board->square[(0x6B)] == ((1 << 2) | (1 << 0))))
{
king_is_safe[0] = true;
}
else if(board->square[(0x59)] == ((1 << 2) | (1 << 0)) && board->square[(0x6A)] == ((1 << 2) | (1 << 0))
&& board->square[(0x5B)] == ((1 << 2) | (1 << 0)))
{
king_is_safe[0] = true;
}
}
else if(board->square[(0x45)] == ((1 << 7) | (1 << 0)) || board->square[(0x44)] == ((1 << 7) | (1 << 0)))
{
if(board->square[(0x55)] == ((1 << 2) | (1 << 0))
&& (board->square[(0x54)] == ((1 << 2) | (1 << 0)) || board->square[(0x64)] == ((1 << 2) | (1 << 0))))
{
king_is_safe[0] = true;
}
else if(board->square[(0x56)] == ((1 << 2) | (1 << 0)) && board->square[(0x65)] == ((1 << 2) | (1 << 0))
&& board->square[(0x54)] == ((1 << 2) | (1 << 0)))
{
king_is_safe[0] = true;
}
}
if(king_is_safe[0] == false && board->number[9] > 0)
{
// king
penalty_1 = shelter_square(board, KING_POS(board, me), me);
// castling
penalty_2 = penalty_1;
if((board->flags &(1 << 0)) != 0)
{
tmp = shelter_square(board, (0x4A), me);
if(tmp < penalty_2)
penalty_2 = tmp;
}
if((board->flags &(1 << 1)) != 0)
{
tmp = shelter_square(board, (0x45), me);
if(tmp < penalty_2)
penalty_2 = tmp;
}
// penalty
penalty = (penalty_1 + penalty_2) / 2;
}
file = SQUARE_FILE(KING_POS(board, 0));
penalty += storm_file(board, file, 0);
if(file != (0x4))
penalty += storm_file(board, file - 1, 0);
if(file != (0xB))
penalty += storm_file(board, file + 1, 0);
op[me] -= (penalty * 256) / 256;
}
// black pawn shelter
if((mat_info->cflags[1]&(1 << 3)) != 0)
{
king_is_safe[1] = false;
penalty = 0;
me = 1;
if(board->square[(0xBA)] == ((1 << 7) | (1 << 1)) || board->square[(0xBB)] == ((1 << 7) | (1 << 1)))
{
if(board->square[(0xAA)] == ((1 << 3) | (1 << 1))
&& (board->square[(0xAB)] == ((1 << 3) | (1 << 1)) || board->square[(0x9B)] == ((1 << 3) | (1 << 1))))
{
king_is_safe[1] = true;
}
else if(board->square[(0xA9)] == ((1 << 3) | (1 << 1)) && board->square[(0x9A)] == ((1 << 3) | (1 << 1))
&& board->square[(0xAB)] == ((1 << 3) | (1 << 1)))
{
king_is_safe[1] = true;
}
}
else if(board->square[(0xB5)] == ((1 << 7) | (1 << 1)) || board->square[(0xB4)] == ((1 << 7) | (1 << 1)))
{
if(board->square[(0xA5)] == ((1 << 3) | (1 << 1))
&& (board->square[(0xA4)] == ((1 << 3) | (1 << 1)) || board->square[(0x94)] == ((1 << 3) | (1 << 1))))
{
king_is_safe[1] = true;
}
else if(board->square[(0xA6)] == ((1 << 3) | (1 << 1)) && board->square[(0x95)] == ((1 << 3) | (1 << 1))
&& board->square[(0xA4)] == ((1 << 3) | (1 << 1)))
{
king_is_safe[1] = true;
}
}
if(king_is_safe[1] == false && board->number[8] > 0)
{
// king
penalty_1 = shelter_square(board, KING_POS(board, me), me);
// castling
penalty_2 = penalty_1;
if((board->flags &(1 << 2)) != 0)
{
tmp = shelter_square(board, (0xBA), me);
if(tmp < penalty_2)
penalty_2 = tmp;
}
if((board->flags &(1 << 3)) != 0)
{
tmp = shelter_square(board, (0xB5), me);
if(tmp < penalty_2)
penalty_2 = tmp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -