position: refactor

This commit is contained in:
Calcitem 2020-09-01 00:11:31 +08:00
parent c501597a9f
commit 6ea2126713
5 changed files with 74 additions and 75 deletions

View File

@ -72,17 +72,17 @@ Value Evaluation::value()
int nPiecesInHandDiff;
int nPiecesOnBoardDiff;
int nPiecesNeedRemove;
int pieceCountNeedRemove;
switch (pos->phase) {
case PHASE_READY:
break;
case PHASE_PLACING:
nPiecesInHandDiff = pos->nPiecesInHand[BLACK] - pos->nPiecesInHand[WHITE];
nPiecesInHandDiff = pos->pieceCountInHand[BLACK] - pos->pieceCountInHand[WHITE];
value += nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND;
nPiecesOnBoardDiff = pos->nPiecesOnBoard[BLACK] - pos->nPiecesOnBoard[WHITE];
nPiecesOnBoardDiff = pos->pieceCountOnBoard[BLACK] - pos->pieceCountOnBoard[WHITE];
value += nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD;
switch (pos->action) {
@ -91,9 +91,9 @@ Value Evaluation::value()
break;
case ACTION_REMOVE:
nPiecesNeedRemove = (pos->sideToMove == BLACK) ?
pos->nPiecesNeedRemove : -(pos->nPiecesNeedRemove);
value += nPiecesNeedRemove * VALUE_EACH_PIECE_PLACING_NEEDREMOVE;
pieceCountNeedRemove = (pos->sideToMove == BLACK) ?
pos->pieceCountNeedRemove : -(pos->pieceCountNeedRemove);
value += pieceCountNeedRemove * VALUE_EACH_PIECE_PLACING_NEEDREMOVE;
break;
default:
break;
@ -102,11 +102,11 @@ Value Evaluation::value()
break;
case PHASE_MOVING:
value = pos->nPiecesOnBoard[BLACK] * VALUE_EACH_PIECE_ONBOARD -
pos->nPiecesOnBoard[WHITE] * VALUE_EACH_PIECE_ONBOARD;
value = pos->pieceCountOnBoard[BLACK] * VALUE_EACH_PIECE_ONBOARD -
pos->pieceCountOnBoard[WHITE] * VALUE_EACH_PIECE_ONBOARD;
#ifdef EVALUATE_MOBILITY
value += pos->get_mobility_diff(position->turn, position->nPiecesInHand[BLACK], position->nPiecesInHand[WHITE], false) * 10;
value += pos->get_mobility_diff(position->turn, position->pieceCountInHand[BLACK], position->pieceCountInHand[WHITE], false) * 10;
#endif /* EVALUATE_MOBILITY */
switch (pos->action) {
@ -115,9 +115,9 @@ Value Evaluation::value()
break;
case ACTION_REMOVE:
nPiecesNeedRemove = (pos->sideToMove == BLACK) ?
pos->nPiecesNeedRemove : -(pos->nPiecesNeedRemove);
value += nPiecesNeedRemove * VALUE_EACH_PIECE_MOVING_NEEDREMOVE;
pieceCountNeedRemove = (pos->sideToMove == BLACK) ?
pos->pieceCountNeedRemove : -(pos->pieceCountNeedRemove);
value += pieceCountNeedRemove * VALUE_EACH_PIECE_MOVING_NEEDREMOVE;
break;
default:
break;
@ -126,7 +126,7 @@ Value Evaluation::value()
break;
case PHASE_GAMEOVER:
if (pos->nPiecesOnBoard[BLACK] + pos->nPiecesOnBoard[WHITE] >=
if (pos->pieceCountOnBoard[BLACK] + pos->pieceCountOnBoard[WHITE] >=
RANK_NB * FILE_NB) {
if (rule.isBlackLosebutNotDrawWhenBoardFull) {
value -= VALUE_MATE;
@ -140,9 +140,9 @@ Value Evaluation::value()
value += delta;
}
else if (pos->nPiecesOnBoard[BLACK] < rule.nPiecesAtLeast) {
else if (pos->pieceCountOnBoard[BLACK] < rule.nPiecesAtLeast) {
value -= VALUE_MATE;
} else if (pos->nPiecesOnBoard[WHITE] < rule.nPiecesAtLeast) {
} else if (pos->pieceCountOnBoard[WHITE] < rule.nPiecesAtLeast) {
value += VALUE_MATE;
}

View File

@ -334,7 +334,7 @@ ExtMove *generate(Position *position, ExtMove *moveList)
continue;
}
if (position->nPiecesOnBoard[position->sideToMove] > rule.nPiecesAtLeast ||
if (position->pieceCountOnBoard[position->sideToMove] > rule.nPiecesAtLeast ||
!rule.allowFlyWhenRemainThreePieces) {
for (int direction = MD_BEGIN; direction < MD_NB; direction++) {
newSquare = static_cast<Square>(MoveList::moveTable[oldSquare][direction]);

View File

@ -359,15 +359,15 @@ const string Position::fen() const
int Position::pieces_on_board_count()
{
nPiecesOnBoard[BLACK] = nPiecesOnBoard[WHITE] = 0;
pieceCountOnBoard[BLACK] = pieceCountOnBoard[WHITE] = 0;
for (int f = 1; f < FILE_NB + 2; f++) {
for (int r = 0; r < RANK_NB; r++) {
Square s = static_cast<Square>(f * RANK_NB + r);
if (board[s] & B_STONE) {
nPiecesOnBoard[BLACK]++;
pieceCountOnBoard[BLACK]++;
} else if (board[s]& W_STONE) {
nPiecesOnBoard[WHITE]++;
pieceCountOnBoard[WHITE]++;
}
#if 0
else if (board[s]& BAN_STONE) {
@ -376,20 +376,20 @@ int Position::pieces_on_board_count()
}
}
if (nPiecesOnBoard[BLACK] > rule.nTotalPiecesEachSide ||
nPiecesOnBoard[WHITE] > rule.nTotalPiecesEachSide) {
if (pieceCountOnBoard[BLACK] > rule.nTotalPiecesEachSide ||
pieceCountOnBoard[WHITE] > rule.nTotalPiecesEachSide) {
return -1;
}
return nPiecesOnBoard[BLACK] + nPiecesOnBoard[WHITE];
return pieceCountOnBoard[BLACK] + pieceCountOnBoard[WHITE];
}
int Position::pieces_in_hand_count()
{
nPiecesInHand[BLACK] = rule.nTotalPiecesEachSide - nPiecesOnBoard[BLACK];
nPiecesInHand[WHITE] = rule.nTotalPiecesEachSide - nPiecesOnBoard[WHITE];
pieceCountInHand[BLACK] = rule.nTotalPiecesEachSide - pieceCountOnBoard[BLACK];
pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide - pieceCountOnBoard[WHITE];
return nPiecesInHand[BLACK] + nPiecesInHand[WHITE];
return pieceCountInHand[BLACK] + pieceCountInHand[WHITE];
}
bool Position::set_position(const struct Rule *newRule)
@ -412,7 +412,7 @@ bool Position::set_position(const struct Rule *newRule)
}
pieces_in_hand_count();
nPiecesNeedRemove = 0;
pieceCountNeedRemove = 0;
millListSize = 0;
winner = NOBODY;
MoveList::create();
@ -457,9 +457,9 @@ bool Position::reset()
st.key = 0;
memset(byTypeBB, 0, sizeof(byTypeBB));
nPiecesOnBoard[BLACK] = nPiecesOnBoard[WHITE] = 0;
nPiecesInHand[BLACK] = nPiecesInHand[WHITE] = rule.nTotalPiecesEachSide;
nPiecesNeedRemove = 0;
pieceCountOnBoard[BLACK] = pieceCountOnBoard[WHITE] = 0;
pieceCountInHand[BLACK] = pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide;
pieceCountNeedRemove = 0;
millListSize = 0;
currentSquare = SQ_0;
elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0;
@ -537,9 +537,9 @@ bool Position::put_piece(Square s, bool updateCmdlist)
Position::square_to_polar(s, file, rank);
if (phase == PHASE_PLACING) {
piece = (Piece)((0x01 | (sideToMove << PLAYER_SHIFT)) + rule.nTotalPiecesEachSide - nPiecesInHand[us]);
nPiecesInHand[us]--;
nPiecesOnBoard[us]++;
piece = (Piece)((0x01 | (sideToMove << PLAYER_SHIFT)) + rule.nTotalPiecesEachSide - pieceCountInHand[us]);
pieceCountInHand[us]--;
pieceCountOnBoard[us]++;
board[s]= piece;
@ -563,9 +563,9 @@ bool Position::put_piece(Square s, bool updateCmdlist)
n = add_mills(currentSquare);
if (n == 0) {
assert(nPiecesInHand[BLACK] >= 0 && nPiecesInHand[WHITE] >= 0);
assert(pieceCountInHand[BLACK] >= 0 && pieceCountInHand[WHITE] >= 0);
if (nPiecesInHand[BLACK] == 0 && nPiecesInHand[WHITE] == 0) {
if (pieceCountInHand[BLACK] == 0 && pieceCountInHand[WHITE] == 0) {
if (check_gameover_condition(updateCmdlist)) {
goto out;
}
@ -587,7 +587,7 @@ bool Position::put_piece(Square s, bool updateCmdlist)
change_side_to_move();
}
} else {
nPiecesNeedRemove = rule.allowRemoveMultiPiecesWhenCloseMultiMill ? n : 1;
pieceCountNeedRemove = rule.allowRemoveMultiPiecesWhenCloseMultiMill ? n : 1;
action = ACTION_REMOVE;
}
@ -601,7 +601,7 @@ bool Position::put_piece(Square s, bool updateCmdlist)
// When hase == GAME_MOVING
// if illegal
if (nPiecesOnBoard[sideToMove] > rule.nPiecesAtLeast ||
if (pieceCountOnBoard[sideToMove] > rule.nPiecesAtLeast ||
!rule.allowFlyWhenRemainThreePieces) {
for (i = 0; i < 4; i++) {
if (s == MoveList::moveTable[currentSquare][i])
@ -648,7 +648,7 @@ bool Position::put_piece(Square s, bool updateCmdlist)
goto out;
}
} else {
nPiecesNeedRemove = rule.allowRemoveMultiPiecesWhenCloseMultiMill ? n : 1;
pieceCountNeedRemove = rule.allowRemoveMultiPiecesWhenCloseMultiMill ? n : 1;
action = ACTION_REMOVE;
}
@ -682,7 +682,7 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
if (action != ACTION_REMOVE)
return false;
if (nPiecesNeedRemove <= 0)
if (pieceCountNeedRemove <= 0)
return false;
File file;
@ -718,7 +718,7 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
byTypeBB[them] ^= s;
}
nPiecesOnBoard[them]--;
pieceCountOnBoard[them]--;
move = static_cast<Move>(-s);
@ -731,7 +731,7 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
}
currentSquare = SQ_0;
nPiecesNeedRemove--;
pieceCountNeedRemove--;
// Remove piece completed
@ -739,12 +739,12 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
goto out;
}
if (nPiecesNeedRemove > 0) {
if (pieceCountNeedRemove > 0) {
return true;
}
if (phase == PHASE_PLACING) {
if (nPiecesInHand[BLACK] == 0 && nPiecesInHand[WHITE] == 0) {
if (pieceCountInHand[BLACK] == 0 && pieceCountInHand[WHITE] == 0) {
phase = PHASE_MOVING;
action = ACTION_SELECT;
@ -965,9 +965,9 @@ bool Position::undo_move(Move m)
#endif
// TODO: Adjust
//int nPiecesInHand[COLOR_NB]{ 0 };
//int nPiecesOnBoard[COLOR_NB]{ 0 };
//int nPiecesNeedRemove{ 0 };
//int pieceCountInHand[COLOR_NB]{ 0 };
//int pieceCountOnBoard[COLOR_NB]{ 0 };
//int pieceCountNeedRemove{ 0 };
m = m;
return ret;
@ -1050,7 +1050,7 @@ bool Position::check_gameover_condition(int8_t updateCmdlist)
for (int i = 1; i <= 2; i++)
{
if (nPiecesOnBoard[i] + nPiecesInHand[i] < rule.nPiecesAtLeast) {
if (pieceCountOnBoard[i] + pieceCountInHand[i] < rule.nPiecesAtLeast) {
winner = ~Color(i);
phase = PHASE_GAMEOVER;
@ -1065,7 +1065,7 @@ bool Position::check_gameover_condition(int8_t updateCmdlist)
#ifdef MCTS_AI
#if 0
int diff = nPiecesOnBoard[BLACK] - nPiecesOnBoard[WHITE];
int diff = pieceCountOnBoard[BLACK] - pieceCountOnBoard[WHITE];
if (diff > 4) {
winner = BLACK;
phase = PHASE_GAMEOVER;
@ -1086,7 +1086,7 @@ bool Position::check_gameover_condition(int8_t updateCmdlist)
#endif
#endif
if (nPiecesOnBoard[BLACK] + nPiecesOnBoard[WHITE] >= RANK_NB * FILE_NB) {
if (pieceCountOnBoard[BLACK] + pieceCountOnBoard[WHITE] >= RANK_NB * FILE_NB) {
phase = PHASE_GAMEOVER;
if (rule.isBlackLosebutNotDrawWhenBoardFull) {
@ -1205,15 +1205,15 @@ void Position::set_tips()
switch (phase) {
case PHASE_READY:
tips = "轮到玩家1落子剩余" + std::to_string(nPiecesInHand[BLACK]) + "" +
tips = "轮到玩家1落子剩余" + std::to_string(pieceCountInHand[BLACK]) + "" +
" 比分 " + to_string(score[BLACK]) + ":" + to_string(score[WHITE]) + ", 和棋 " + to_string(score_draw);
break;
case PHASE_PLACING:
if (action == ACTION_PLACE) {
tips = "轮到玩家" + turnStr + "落子,剩余" + std::to_string(nPiecesInHand[sideToMove]) + "";
tips = "轮到玩家" + turnStr + "落子,剩余" + std::to_string(pieceCountInHand[sideToMove]) + "";
} else if (action == ACTION_REMOVE) {
tips = "成三!轮到玩家" + turnStr + "去子,需去" + std::to_string(nPiecesNeedRemove) + "";
tips = "成三!轮到玩家" + turnStr + "去子,需去" + std::to_string(pieceCountNeedRemove) + "";
}
break;
@ -1221,7 +1221,7 @@ void Position::set_tips()
if (action == ACTION_PLACE || action == ACTION_SELECT) {
tips = "轮到玩家" + turnStr + "选子移动";
} else if (action == ACTION_REMOVE) {
tips = "成三!轮到玩家" + turnStr + "去子,需去" + std::to_string(nPiecesNeedRemove) + "";
tips = "成三!轮到玩家" + turnStr + "去子,需去" + std::to_string(pieceCountNeedRemove) + "";
}
break;
@ -1302,8 +1302,8 @@ Key Position::update_key_misc()
hi |= 1U << 1;
}
hi |= static_cast<Key>(nPiecesNeedRemove) << 2;
hi |= static_cast<Key>(nPiecesInHand[BLACK]) << 4; // TODO: may use phase is also OK?
hi |= static_cast<Key>(pieceCountNeedRemove) << 2;
hi |= static_cast<Key>(pieceCountInHand[BLACK]) << 4; // TODO: may use phase is also OK?
st.key = st.key | (hi << (CHAR_BIT * sizeof(Key) - KEY_MISC_BIT));
@ -1650,7 +1650,7 @@ int Position::surrounded_empty_squares_count(Square s, bool includeFobidden)
int count = 0;
if (nPiecesOnBoard[sideToMove] > rule.nPiecesAtLeast ||
if (pieceCountOnBoard[sideToMove] > rule.nPiecesAtLeast ||
!rule.allowFlyWhenRemainThreePieces) {
Square moveSquare;
for (MoveDirection d = MD_BEGIN; d < MD_NB; d = (MoveDirection)(d + 1)) {
@ -1701,11 +1701,11 @@ void Position::surrounded_pieces_count(Square s, int &nOurPieces, int &nTheirPie
bool Position::is_all_surrounded()
{
// Full
if (nPiecesOnBoard[BLACK] + nPiecesOnBoard[WHITE] >= RANK_NB * FILE_NB)
if (pieceCountOnBoard[BLACK] + pieceCountOnBoard[WHITE] >= RANK_NB * FILE_NB)
return true;
// Can fly
if (nPiecesOnBoard[sideToMove] <= rule.nPiecesAtLeast &&
if (pieceCountOnBoard[sideToMove] <= rule.nPiecesAtLeast &&
rule.allowFlyWhenRemainThreePieces) {
return false;
}

View File

@ -113,7 +113,7 @@ public:
bool pos_is_ok() const;
void flip();
//////////////////////////////////////
/// Mill Game
bool set_position(const struct Rule *rule);
@ -185,8 +185,18 @@ public:
bool move_piece(Square from, Square to);
// Data members
Piece board[SQUARE_NB];
Bitboard byTypeBB[PIECE_TYPE_NB];
// TODO: [0] is sum of Black and White
int pieceCountInHand[COLOR_NB]{ 0 };
int pieceCountOnBoard[COLOR_NB]{ 0 };
int pieceCountNeedRemove{ 0 };
int gamePly;
Color sideToMove { NOCOLOR };
Thread *thisThread;
StateInfo st;
/// Mill Game
Color them { NOCOLOR };
Color winner;
@ -205,24 +215,15 @@ public:
static int millTable[SQUARE_NB][LD_NB][FILE_NB - 1];
Square currentSquare;
int nPlayed{ 0 };
int nPlayed { 0 };
std::vector <std::string> cmdlist;
char cmdline[64]{ '\0' };
// Note: [0] is sum of Black and White
int nPiecesInHand[COLOR_NB] { 0 };
int nPiecesOnBoard[COLOR_NB] { 0 };
int nPiecesNeedRemove { 0 };
char cmdline[64] { '\0' };
int tm { -1 };
time_t startTime;
time_t currentTime;
time_t elapsedSeconds[COLOR_NB];
Piece board[SQUARE_NB];
Bitboard byTypeBB[PIECE_TYPE_NB];
time_t elapsedSeconds[COLOR_NB];
/*
0x 00 00 00 00 00 00 00 00
@ -252,8 +253,6 @@ public:
29 ----- 28 ----- 27
*/
Move move { MOVE_NONE };
StateInfo st;
};
inline bool Position::empty(Square s) const
@ -290,9 +289,9 @@ inline std::string Position::char_to_string(char ch)
template<PieceType Pt> inline int Position::count(Color c) const
{
if (Pt == ON_BOARD) {
return nPiecesOnBoard[c];
return pieceCountOnBoard[c];
} else if (Pt == IN_HAND) {
return nPiecesInHand[c];
return pieceCountInHand[c];
}
return 0;

View File

@ -699,7 +699,7 @@ Value search(Position *pos, Stack<Position> &ss, Depth depth, Depth originDepth,
#endif /* TRANSPOSITION_TABLE_ENABLE */
#if 0
if (position->phase == PHASE_PLACING && depth == 1 && pos->nPiecesNeedRemove > 0) {
if (position->phase == PHASE_PLACING && depth == 1 && pos->pieceCountNeedRemove > 0) {
depth--;
}
#endif