refactor: opponent 替换为 them

This commit is contained in:
Calcitem 2020-07-05 20:06:19 +08:00
parent 7e75211c18
commit cb34e7f6be
7 changed files with 56 additions and 60 deletions

View File

@ -293,7 +293,9 @@ void MoveList::shuffle()
ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList) ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
{ {
Square square; Square square;
Color opponent;
Color us = position->sideToMove;
Color them = ~us;
//moves.clear(); //moves.clear();
ExtMove *cur = moveList; ExtMove *cur = moveList;
@ -363,12 +365,10 @@ ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
break; break;
case ACTION_REMOVE: case ACTION_REMOVE:
opponent = ~position->sideToMove; if (position->board.isAllInMills(them)) {
if (position->board.isAllInMills(opponent)) {
for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
square = static_cast<Square>(MoveList::movePriorityTable[i]); square = static_cast<Square>(MoveList::movePriorityTable[i]);
if (position->board.locations[square] & (opponent << PLAYER_SHIFT)) { if (position->board.locations[square] & (them << PLAYER_SHIFT)) {
*cur++ = ((Move)-square); *cur++ = ((Move)-square);
} }
} }
@ -378,7 +378,7 @@ ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
// not is all in mills // not is all in mills
for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
square = static_cast<Square>(MoveList::movePriorityTable[i]); square = static_cast<Square>(MoveList::movePriorityTable[i]);
if (position->board.locations[square] & (opponent << PLAYER_SHIFT)) { if (position->board.locations[square] & (them << PLAYER_SHIFT)) {
if (rule.allowRemovePieceInMill || !position->board.inHowManyMills(square, NOBODY)) { if (rule.allowRemovePieceInMill || !position->board.inHowManyMills(square, NOBODY)) {
*cur++ = ((Move)-square); *cur++ = ((Move)-square);
} }

View File

@ -55,43 +55,43 @@ void MovePicker::score()
Square sqsrc = from_sq(m); Square sqsrc = from_sq(m);
// if stat before moving, moving phrase maybe from @-0-@ to 0-@-@, but no mill, so need sqsrc to judge // if stat before moving, moving phrase maybe from @-0-@ to 0-@-@, but no mill, so need sqsrc to judge
int nMills = position->board.inHowManyMills(sq, position->sideToMove, sqsrc); int nOurMills = position->board.inHowManyMills(sq, position->sideToMove, sqsrc);
int nopponentMills = 0; int nTheirMills = 0;
#ifdef SORT_MOVE_WITH_HUMAN_KNOWLEDGES #ifdef SORT_MOVE_WITH_HUMAN_KNOWLEDGES
// TODO: rule.allowRemoveMultiPiecesWhenCloseMultiMill adapt other rules // TODO: rule.allowRemoveMultiPiecesWhenCloseMultiMill adapt other rules
if (type_of(m) != MOVETYPE_REMOVE) { if (type_of(m) != MOVETYPE_REMOVE) {
// all phrase, check if place sq can close mill // all phrase, check if place sq can close mill
if (nMills > 0) { if (nOurMills > 0) {
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
cur->rating += static_cast<Rating>(RATING_ONE_MILL * nMills); cur->rating += static_cast<Rating>(RATING_ONE_MILL * nOurMills);
#endif #endif
} else if (position->getPhase() == PHASE_PLACING) { } else if (position->getPhase() == PHASE_PLACING) {
// placing phrase, check if place sq can block opponent close mill // placing phrase, check if place sq can block their close mill
nopponentMills = position->board.inHowManyMills(sq, position->opponent); nTheirMills = position->board.inHowManyMills(sq, position->them);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nopponentMills); cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nTheirMills);
#endif #endif
} }
#if 1 #if 1
else if (position->getPhase() == PHASE_MOVING) { else if (position->getPhase() == PHASE_MOVING) {
// moving phrase, check if place sq can block opponent close mill // moving phrase, check if place sq can block their close mill
nopponentMills = position->board.inHowManyMills(sq, position->opponent); nTheirMills = position->board.inHowManyMills(sq, position->them);
if (nopponentMills) { if (nTheirMills) {
int nPlayerPiece = 0; int nOurPieces = 0;
int nOpponentPiece = 0; int nTheirPieces = 0;
int nBanned = 0; int nBanned = 0;
int nEmpty = 0; int nEmpty = 0;
position->board.getSurroundedPieceCount(sq, position->sideToMove, position->board.getSurroundedPieceCount(sq, position->sideToMove,
nPlayerPiece, nOpponentPiece, nBanned, nEmpty); nOurPieces, nTheirPieces, nBanned, nEmpty);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
if (sq % 2 == 0 && nOpponentPiece == 3) { if (sq % 2 == 0 && nTheirPieces == 3) {
cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nopponentMills); cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nTheirMills);
} else if (sq % 2 == 1 && nOpponentPiece == 2 && rule.nTotalPiecesEachSide == 12) { } else if (sq % 2 == 1 && nTheirPieces == 2 && rule.nTotalPiecesEachSide == 12) {
cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nopponentMills); cur->rating += static_cast<Rating>(RATING_BLOCK_ONE_MILL * nTheirMills);
} }
#endif #endif
} }
@ -109,37 +109,37 @@ void MovePicker::score()
} }
#endif #endif
} else { // Remove } else { // Remove
int nPlayerPiece = 0; int nOurPieces = 0;
int nOpponentPiece = 0; int nTheirPieces = 0;
int nBanned = 0; int nBanned = 0;
int nEmpty = 0; int nEmpty = 0;
position->board.getSurroundedPieceCount(sq, position->sideToMove, position->board.getSurroundedPieceCount(sq, position->sideToMove,
nPlayerPiece, nOpponentPiece, nBanned, nEmpty); nOurPieces, nTheirPieces, nBanned, nEmpty);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
if (nMills > 0) { if (nOurMills > 0) {
// remove point is in our mill // remove point is in our mill
//newNode->rating += static_cast<Rating>(RATING_REMOVE_ONE_MILL * nMills); //newNode->rating += static_cast<Rating>(RATING_REMOVE_ONE_MILL * nOurMills);
if (nOpponentPiece == 0) { if (nTheirPieces == 0) {
// if remove point nearby has no opponent's stone, preferred. // if remove point nearby has no their stone, preferred.
cur->rating += static_cast<Rating>(1); cur->rating += static_cast<Rating>(1);
if (nPlayerPiece > 0) { if (nOurPieces > 0) {
// if remove point nearby our stone, preferred // if remove point nearby our stone, preferred
cur->rating += static_cast<Rating>(nPlayerPiece); cur->rating += static_cast<Rating>(nOurPieces);
} }
} }
} }
// remove point is in their mill // remove point is in their mill
nopponentMills = position->board.inHowManyMills(sq, position->opponent); nTheirMills = position->board.inHowManyMills(sq, position->them);
if (nopponentMills) { if (nTheirMills) {
if (nOpponentPiece >= 2) { if (nTheirPieces >= 2) {
// if nearby opponent's piece, prefer do not remove // if nearby their piece, prefer do not remove
cur->rating -= static_cast<Rating>(nOpponentPiece); cur->rating -= static_cast<Rating>(nTheirPieces);
if (nPlayerPiece == 0) { if (nOurPieces == 0) {
// if nearby has no our piece, more prefer do not remove // if nearby has no our piece, more prefer do not remove
cur->rating -= static_cast<Rating>(1); cur->rating -= static_cast<Rating>(1);
} }

View File

@ -336,7 +336,7 @@ int Board::getSurroundedEmptyLocationCount(Color sideToMove, int nPiecesOnBoard[
return count; return count;
} }
void Board::getSurroundedPieceCount(Square square, Color sideToMove, int &nPlayerPiece, int &nOpponentPiece, int &nBanned, int &nEmpty) void Board::getSurroundedPieceCount(Square square, Color sideToMove, int &nPlayerPiece, int &nTheirPieces, int &nBanned, int &nEmpty)
{ {
Square moveSquare; Square moveSquare;
@ -360,7 +360,7 @@ void Board::getSurroundedPieceCount(Square square, Color sideToMove, int &nPlaye
if (sideToMove == pieceType >> PLAYER_SHIFT) { if (sideToMove == pieceType >> PLAYER_SHIFT) {
nPlayerPiece++; nPlayerPiece++;
} else { } else {
nOpponentPiece++; nTheirPieces++;
} }
break; break;
} }

View File

@ -58,7 +58,7 @@ public:
bool isAllInMills(Color c); bool isAllInMills(Color c);
int getSurroundedEmptyLocationCount(Color sideToMove, int nPiecesOnBoard[], Square square, bool includeFobidden); int getSurroundedEmptyLocationCount(Color sideToMove, int nPiecesOnBoard[], Square square, bool includeFobidden);
void getSurroundedPieceCount(Square square, Color sideToMove, int &nPlayerPiece, int &nOpponentPiece, int &nBanned, int &nEmpty); void getSurroundedPieceCount(Square square, Color sideToMove, int &nPlayerPiece, int &nTheirPieces, int &nBanned, int &nEmpty);
bool isAllSurrounded(Color sideToMove, int nPiecesOnBoard[]); bool isAllSurrounded(Color sideToMove, int nPiecesOnBoard[]);
int addMills(Square square); int addMills(Square square);

View File

@ -380,10 +380,10 @@ bool Position::removePiece(Square square, bool updateCmdlist)
int seconds = -1; int seconds = -1;
int oppId = opponent; int oppId = them;
// if piece is not opponent's // if piece is not their
if (!((opponent << PLAYER_SHIFT) & board.locations[square])) if (!((them << PLAYER_SHIFT) & board.locations[square]))
return false; return false;
if (!rule.allowRemovePieceInMill && if (!rule.allowRemovePieceInMill &&
@ -404,10 +404,10 @@ bool Position::removePiece(Square square, bool updateCmdlist)
board.locations[square] = '\x00'; board.locations[square] = '\x00';
board.byTypeBB[ALL_PIECES] ^= square; board.byTypeBB[ALL_PIECES] ^= square;
board.byTypeBB[opponent] ^= square; board.byTypeBB[them] ^= square;
} }
nPiecesOnBoard[opponent]--; nPiecesOnBoard[them]--;
move = static_cast<Move>(-square); move = static_cast<Move>(-square);
@ -625,8 +625,8 @@ int Position::update()
{ {
int ret = -1; int ret = -1;
int timePoint = -1; int timePoint = -1;
time_t *seconds = &elapsedSeconds[sideToMove]; time_t *ourSeconds = &elapsedSeconds[sideToMove];
time_t opponentSeconds = elapsedSeconds[opponent]; time_t theirSeconds = elapsedSeconds[them];
if (!(phase & PHASE_PLAYING)) { if (!(phase & PHASE_PLAYING)) {
return -1; return -1;
@ -634,11 +634,11 @@ int Position::update()
currentTime = time(NULL); currentTime = time(NULL);
if (timePoint >= *seconds) { if (timePoint >= *ourSeconds) {
*seconds = ret = timePoint; *ourSeconds = ret = timePoint;
startTime = currentTime - (elapsedSeconds[BLACK] + elapsedSeconds[WHITE]); startTime = currentTime - (elapsedSeconds[BLACK] + elapsedSeconds[WHITE]);
} else { } else {
*seconds = ret = currentTime - startTime - opponentSeconds; *ourSeconds = ret = currentTime - startTime - theirSeconds;
} }
if (rule.maxTimeLedToLose > 0) { if (rule.maxTimeLedToLose > 0) {
@ -819,8 +819,7 @@ void Position::setSideToMove(Color c)
chSide = Player::colorToCh(sideToMove); chSide = Player::colorToCh(sideToMove);
opponent = ~sideToMove; them = ~sideToMove;
chOpponent = Player::colorToCh(opponent);
} }
Color Position::getSideToMove() Color Position::getSideToMove()

View File

@ -53,10 +53,7 @@ public:
Color sideToMove {NOCOLOR}; Color sideToMove {NOCOLOR};
char chSide {'0'}; char chSide {'0'};
//string turnStr; Color them { NOCOLOR };
Color opponent { NOCOLOR };
char chOpponent {'0'};
//string opponentStr;
enum Action action { }; enum Action action { };

View File

@ -131,9 +131,9 @@ enum Rating : int8_t
RATING_REMOVE_TWO_MILLS = RATING_TWO_MILLS, RATING_REMOVE_TWO_MILLS = RATING_TWO_MILLS,
RATING_REMOVE_THREE_MILLS = RATING_THREE_MILLS, RATING_REMOVE_THREE_MILLS = RATING_THREE_MILLS,
RATING_REMOVE_OPPONENT_ONE_MILL = -RATING_REMOVE_ONE_MILL, RATING_REMOVE_THEIR_ONE_MILL = -RATING_REMOVE_ONE_MILL,
RATING_REMOVE_OPPONENT_TWO_MILLS = -RATING_REMOVE_TWO_MILLS, RATING_REMOVE_THEIR_TWO_MILLS = -RATING_REMOVE_TWO_MILLS,
RATING_REMOVE_OPPONENT_THREE_MILLS = -RATING_REMOVE_THREE_MILLS, RATING_REMOVE_THEIR_THREE_MILLS = -RATING_REMOVE_THREE_MILLS,
RATING_TT = 100, RATING_TT = 100,
RATING_MAX = std::numeric_limits<int8_t>::max(), RATING_MAX = std::numeric_limits<int8_t>::max(),