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)
{
Square square;
Color opponent;
Color us = position->sideToMove;
Color them = ~us;
//moves.clear();
ExtMove *cur = moveList;
@ -363,12 +365,10 @@ ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
break;
case ACTION_REMOVE:
opponent = ~position->sideToMove;
if (position->board.isAllInMills(opponent)) {
if (position->board.isAllInMills(them)) {
for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; 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);
}
}
@ -378,7 +378,7 @@ ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
// not is all in mills
for (int i = Board::MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; 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)) {
*cur++ = ((Move)-square);
}

View File

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

View File

@ -336,7 +336,7 @@ int Board::getSurroundedEmptyLocationCount(Color sideToMove, int nPiecesOnBoard[
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;
@ -360,7 +360,7 @@ void Board::getSurroundedPieceCount(Square square, Color sideToMove, int &nPlaye
if (sideToMove == pieceType >> PLAYER_SHIFT) {
nPlayerPiece++;
} else {
nOpponentPiece++;
nTheirPieces++;
}
break;
}

View File

@ -58,7 +58,7 @@ public:
bool isAllInMills(Color c);
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[]);
int addMills(Square square);

View File

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

View File

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

View File

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