Evaluation::value() 直接调用 pos 的成员变量改为调用函数

This commit is contained in:
Calcitem 2020-09-29 17:39:21 +08:00
parent 5eaff47045
commit c68c81ac40
3 changed files with 43 additions and 22 deletions

View File

@ -46,16 +46,17 @@ class Evaluation
{ {
public: public:
Evaluation() = delete; Evaluation() = delete;
explicit Evaluation(const Position &p) : pos(p) explicit Evaluation(Position &p) : pos(p)
{ {
} }
Evaluation &operator=(const Evaluation &) = delete; Evaluation &operator=(const Evaluation &) = delete;
Value value(); Value value();
private: private:
const Position &pos; Position &pos;
}; };
// Evaluation::value() is the main function of the class. It computes the various // Evaluation::value() is the main function of the class. It computes the various
// parts of the evaluation and returns the value of the position from the point // parts of the evaluation and returns the value of the position from the point
// of view of the side to move. // of view of the side to move.
@ -68,25 +69,25 @@ Value Evaluation::value()
int nPiecesOnBoardDiff; int nPiecesOnBoardDiff;
int pieceCountNeedRemove; int pieceCountNeedRemove;
switch (pos.phase) { switch (pos.get_phase()) {
case PHASE_READY: case PHASE_READY:
break; break;
case PHASE_PLACING: case PHASE_PLACING:
nPiecesInHandDiff = pos.pieceCountInHand[BLACK] - pos.pieceCountInHand[WHITE]; nPiecesInHandDiff = pos.pieces_count_in_hand(BLACK) - pos.pieces_count_in_hand(WHITE);
value += nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND; value += nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND;
nPiecesOnBoardDiff = pos.pieceCountOnBoard[BLACK] - pos.pieceCountOnBoard[WHITE]; nPiecesOnBoardDiff = pos.pieces_count_on_board(BLACK) - pos.pieces_count_on_board(WHITE);
value += nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD; value += nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD;
switch (pos.action) { switch (pos.get_action()) {
case ACTION_SELECT: case ACTION_SELECT:
case ACTION_PLACE: case ACTION_PLACE:
break; break;
case ACTION_REMOVE: case ACTION_REMOVE:
pieceCountNeedRemove = (pos.sideToMove == BLACK) ? pieceCountNeedRemove = (pos.side_to_move() == BLACK) ?
pos.pieceCountNeedRemove : -(pos.pieceCountNeedRemove); pos.piece_count_need_remove() : -(pos.piece_count_need_remove());
value += pieceCountNeedRemove * VALUE_EACH_PIECE_PLACING_NEEDREMOVE; value += pieceCountNeedRemove * VALUE_EACH_PIECE_PLACING_NEEDREMOVE;
break; break;
default: default:
@ -96,21 +97,21 @@ Value Evaluation::value()
break; break;
case PHASE_MOVING: case PHASE_MOVING:
value = pos.pieceCountOnBoard[BLACK] * VALUE_EACH_PIECE_ONBOARD - value = pos.pieces_count_on_board(BLACK) * VALUE_EACH_PIECE_ONBOARD -
pos.pieceCountOnBoard[WHITE] * VALUE_EACH_PIECE_ONBOARD; pos.pieces_count_on_board(WHITE) * VALUE_EACH_PIECE_ONBOARD;
#ifdef EVALUATE_MOBILITY #ifdef EVALUATE_MOBILITY
value += pos.get_mobility_diff(position->turn, position->pieceCountInHand[BLACK], position->pieceCountInHand[WHITE], false) * 10; value += pos.get_mobility_diff(position->turn, position->pieceCountInHand[BLACK], position->pieceCountInHand[WHITE], false) * 10;
#endif /* EVALUATE_MOBILITY */ #endif /* EVALUATE_MOBILITY */
switch (pos.action) { switch (pos.get_action()) {
case ACTION_SELECT: case ACTION_SELECT:
case ACTION_PLACE: case ACTION_PLACE:
break; break;
case ACTION_REMOVE: case ACTION_REMOVE:
pieceCountNeedRemove = (pos.sideToMove == BLACK) ? pieceCountNeedRemove = (pos.side_to_move() == BLACK) ?
pos.pieceCountNeedRemove : -(pos.pieceCountNeedRemove); pos.piece_count_need_remove() : -(pos.piece_count_need_remove());
value += pieceCountNeedRemove * VALUE_EACH_PIECE_MOVING_NEEDREMOVE; value += pieceCountNeedRemove * VALUE_EACH_PIECE_MOVING_NEEDREMOVE;
break; break;
default: default:
@ -120,23 +121,23 @@ Value Evaluation::value()
break; break;
case PHASE_GAMEOVER: case PHASE_GAMEOVER:
if (pos.pieceCountOnBoard[BLACK] + pos.pieceCountOnBoard[WHITE] >= if (pos.pieces_count_on_board(BLACK) + pos.pieces_count_on_board(WHITE) >=
RANK_NB * FILE_NB) { RANK_NB * FILE_NB) {
if (rule.isBlackLosebutNotDrawWhenBoardFull) { if (rule.isBlackLosebutNotDrawWhenBoardFull) {
value -= VALUE_MATE; value -= VALUE_MATE;
} else { } else {
value = VALUE_DRAW; value = VALUE_DRAW;
} }
} else if (pos.action == ACTION_SELECT && } else if (pos.get_action() == ACTION_SELECT &&
pos.is_all_surrounded() && pos.is_all_surrounded() &&
rule.isLoseButNotChangeSideWhenNoWay) { rule.isLoseButNotChangeSideWhenNoWay) {
Value delta = pos.sideToMove == BLACK ? -VALUE_MATE : VALUE_MATE; Value delta = pos.side_to_move() == BLACK ? -VALUE_MATE : VALUE_MATE;
value += delta; value += delta;
} }
else if (pos.pieceCountOnBoard[BLACK] < rule.nPiecesAtLeast) { else if (pos.pieces_count_on_board(BLACK) < rule.nPiecesAtLeast) {
value -= VALUE_MATE; value -= VALUE_MATE;
} else if (pos.pieceCountOnBoard[WHITE] < rule.nPiecesAtLeast) { } else if (pos.pieces_count_on_board(WHITE) < rule.nPiecesAtLeast) {
value += VALUE_MATE; value += VALUE_MATE;
} }
@ -146,7 +147,7 @@ Value Evaluation::value()
break; break;
} }
if (pos.sideToMove == WHITE) { if (pos.side_to_move() == WHITE) {
value = -value; value = -value;
} }
@ -159,7 +160,7 @@ Value Evaluation::value()
/// evaluate() is the evaluator for the outer world. It returns a static /// evaluate() is the evaluator for the outer world. It returns a static
/// evaluation of the position from the point of view of the side to move. /// evaluation of the position from the point of view of the side to move.
Value Eval::evaluate(const Position &pos) Value Eval::evaluate(Position &pos)
{ {
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
return Evaluation(pos).value(); return Evaluation(pos).value();

View File

@ -30,7 +30,7 @@ namespace Eval {
std::string trace(const Position &pos); std::string trace(const Position &pos);
Value evaluate(const Position &pos); Value evaluate(Position &pos);
}; };
#endif // #ifndef EVALUATE_H_INCLUDED #endif // #ifndef EVALUATE_H_INCLUDED

View File

@ -161,9 +161,14 @@ public:
static void print_board(); static void print_board();
int pieces_on_board_count(); int pieces_on_board_count();
int pieces_in_hand_count(); int pieces_in_hand_count();
int pieces_count_on_board(Color c);
int pieces_count_in_hand(Color c);
int piece_count_need_remove();
static bool is_star_square(Square s); static bool is_star_square(Square s);
// private: // private:
@ -396,4 +401,19 @@ inline void Position::set_start_time(int stimeb)
startTime = stimeb; startTime = stimeb;
} }
inline int Position::pieces_count_on_board(Color c)
{
return pieceCountOnBoard[c];
}
inline int Position::pieces_count_in_hand(Color c)
{
return pieceCountInHand[c];
}
inline int Position::piece_count_need_remove()
{
return pieceCountNeedRemove;
}
#endif // #ifndef POSITION_H_INCLUDED #endif // #ifndef POSITION_H_INCLUDED