refactor: Rename to potential_mills_count()

This commit is contained in:
Calcitem 2020-12-31 02:54:18 +08:00
parent 90ca5f0e49
commit 410435278e
5 changed files with 42 additions and 41 deletions

View File

@ -107,7 +107,7 @@ ExtMove *generate<REMOVE>(Position &pos, ExtMove *moveList)
for (auto i = EFFECTIVE_SQUARE_NB - 1; i >= 0; i--) {
s = MoveList<LEGAL>::movePriorityList[i];
if (pos.get_board()[s] & make_piece(them)) {
if (rule.mayTakeFromMillsAlways || !pos.in_how_many_mills(s, NOBODY)) {
if (rule.mayTakeFromMillsAlways || !pos.potential_mills_count(s, NOBODY)) {
*cur++ = (Move)-s;
}
}

View File

@ -57,11 +57,11 @@ void MovePicker::score()
while (cur++->move != MOVE_NONE) {
Move m = cur->move;
Square sq = to_sq(m);
Square sqsrc = from_sq(m);
Square to = to_sq(m);
Square from = from_sq(m);
// if stat before moving, moving phrase maybe from @-0-@ to 0-@-@, but no mill, so need sqsrc to judge
int nOurMills = pos.in_how_many_mills(sq, pos.side_to_move(), sqsrc);
int nOurMills = pos.potential_mills_count(to, pos.side_to_move(), from);
int nTheirMills = 0;
#ifndef SORT_MOVE_WITHOUT_HUMAN_KNOWLEDGES
@ -72,13 +72,13 @@ void MovePicker::score()
cur->value += RATING_ONE_MILL * nOurMills;
} else if (pos.get_phase() == Phase::placing) {
// placing phrase, check if place sq can block their close mill
nTheirMills = pos.in_how_many_mills(sq, ~pos.side_to_move());
nTheirMills = pos.potential_mills_count(to, ~pos.side_to_move());
cur->value += RATING_BLOCK_ONE_MILL * nTheirMills;
}
#if 1
else if (pos.get_phase() == Phase::moving) {
// moving phrase, check if place sq can block their close mill
nTheirMills = pos.in_how_many_mills(sq, ~pos.side_to_move());
nTheirMills = pos.potential_mills_count(to, ~pos.side_to_move());
if (nTheirMills) {
int nOurPieces = 0;
@ -86,11 +86,11 @@ void MovePicker::score()
int nBanned = 0;
int nEmpty = 0;
pos.surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty);
pos.surrounded_pieces_count(to, nOurPieces, nTheirPieces, nBanned, nEmpty);
if (sq % 2 == 0 && nTheirPieces == 3) {
if (to % 2 == 0 && nTheirPieces == 3) {
cur->value += RATING_BLOCK_ONE_MILL * nTheirMills;
} else if (sq % 2 == 1 && nTheirPieces == 2 && rule.piecesCount == 12) {
} else if (to % 2 == 1 && nTheirPieces == 2 && rule.piecesCount == 12) {
cur->value += RATING_BLOCK_ONE_MILL * nTheirMills;
}
}
@ -111,7 +111,7 @@ void MovePicker::score()
int nBanned = 0;
int nEmpty = 0;
pos.surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty);
pos.surrounded_pieces_count(to, nOurPieces, nTheirPieces, nBanned, nEmpty);
if (nOurMills > 0) {
// remove point is in our mill
@ -128,7 +128,7 @@ void MovePicker::score()
}
// remove point is in their mill
nTheirMills = pos.in_how_many_mills(sq, ~pos.side_to_move());
nTheirMills = pos.potential_mills_count(to, ~pos.side_to_move());
if (nTheirMills) {
if (nTheirPieces >= 2) {
// if nearby their piece, prefer do not remove

View File

@ -917,7 +917,7 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
return false;
if (!rule.mayTakeFromMillsAlways &&
in_how_many_mills(s, NOBODY) &&
potential_mills_count(s, NOBODY) &&
!is_all_in_mills(~sideToMove)) {
return false;
}
@ -1410,28 +1410,28 @@ bool Position::bitboard_is_ok()
return true;
}
int Position::in_how_many_mills(Square s, Color c, Square squareSelected)
int Position::potential_mills_count(Square to, Color c, Square from)
{
int n = 0;
Piece locbak = NO_PIECE;
assert(SQ_0 <= squareSelected && squareSelected < SQUARE_NB);
assert(SQ_0 <= from && from < SQUARE_NB);
if (c == NOBODY) {
c = color_on(s);
c = color_on(to);
}
if (squareSelected != SQ_0) {
locbak = board[squareSelected];
board[squareSelected] = NO_PIECE;
if (from != SQ_0) {
locbak = board[from];
board[from] = NO_PIECE;
CLEAR_BIT(byTypeBB[ALL_PIECES], squareSelected);
CLEAR_BIT(byTypeBB[type_of(locbak)], squareSelected);
CLEAR_BIT(byColorBB[color_of(locbak)], squareSelected);
CLEAR_BIT(byTypeBB[ALL_PIECES], from);
CLEAR_BIT(byTypeBB[type_of(locbak)], from);
CLEAR_BIT(byColorBB[color_of(locbak)], from);
}
Bitboard bc = byColorBB[c];
Bitboard *mt = millTableBB[s];
Bitboard *mt = millTableBB[to];
for (int l = 0; l < LD_NB; l++) {
if ((bc & mt[l]) == mt[l]) {
@ -1439,12 +1439,12 @@ int Position::in_how_many_mills(Square s, Color c, Square squareSelected)
}
}
if (squareSelected != SQ_0) {
board[squareSelected] = locbak;
if (from != SQ_0) {
board[from] = locbak;
SET_BIT(byTypeBB[ALL_PIECES], squareSelected);
SET_BIT(byTypeBB[type_of(locbak)], squareSelected);
SET_BIT(byColorBB[color_of(locbak)], squareSelected);
SET_BIT(byTypeBB[ALL_PIECES], from);
SET_BIT(byTypeBB[type_of(locbak)], from);
SET_BIT(byColorBB[color_of(locbak)], from);
}
return n;
@ -1470,7 +1470,7 @@ bool Position::is_all_in_mills(Color c)
{
for (Square i = SQ_BEGIN; i < SQ_END; ++i) {
if (board[i] & ((uint8_t)make_piece(c))) {
if (!in_how_many_mills(i, NOBODY)) {
if (!potential_mills_count(i, NOBODY)) {
return false;
}
}

View File

@ -133,7 +133,9 @@ public:
void create_mill_table();
int add_mills(Square s);
int in_how_many_mills(Square s, Color c, Square squareSelected = SQ_0);
// The number of mills that would be closed by the given move.
int potential_mills_count(Square to, Color c, Square from = SQ_0);
bool is_all_in_mills(Color c);
int surrounded_empty_squares_count(Square s, bool includeFobidden);

View File

@ -626,7 +626,7 @@ class Position {
if (!(Color.opponent(sideToMove()) == board[s])) return false;
if (!rule.mayTakeFromMillsAlways &&
inHowManyMills(s, Color.nobody) > 0 &&
potentialMillsCount(s, Color.nobody) > 0 &&
!isAllInMills(Color.opponent(sideToMove()))) {
return false;
}
@ -1323,30 +1323,29 @@ class Position {
return board[sq];
}
int inHowManyMills(int s, String c, {int squareSelected = 0}) {
int potentialMillsCount(int to, String c, {int from = 0}) {
int n = 0;
String ptBak = Piece.noPiece;
assert(0 <= squareSelected && squareSelected < sqNumber);
assert(0 <= from && from < sqNumber);
if (c == Color.nobody) {
c = colorOn(s);
c = colorOn(to);
}
if (squareSelected != 0) {
ptBak = board[squareSelected];
board[squareSelected] =
_grid[squareToIndex[squareSelected]] = Piece.noPiece;
if (from != 0) {
ptBak = board[from];
board[from] = _grid[squareToIndex[from]] = Piece.noPiece;
}
for (int l = 0; l < lineDirectionNumber; l++) {
if (c == board[millTable[s][l][0]] && c == board[millTable[s][l][1]]) {
if (c == board[millTable[to][l][0]] && c == board[millTable[to][l][1]]) {
n++;
}
}
if (squareSelected != 0) {
board[squareSelected] = _grid[squareToIndex[squareSelected]] = ptBak;
if (from != 0) {
board[from] = _grid[squareToIndex[from]] = ptBak;
}
return n;
@ -1397,7 +1396,7 @@ class Position {
bool isAllInMills(String c) {
for (int i = sqBegin; i < sqEnd; i++) {
if (board[i] == c) {
if (inHowManyMills(i, Color.nobody) == 0) {
if (potentialMillsCount(i, Color.nobody) == 0) {
return false;
}
}