refactor: 使用 f/r 表示 FILE 和 RANK 迭代下标

This commit is contained in:
Calcitem 2020-07-12 10:30:31 +08:00
parent 89b371af93
commit 6532622dd4
3 changed files with 104 additions and 106 deletions

View File

@ -104,12 +104,10 @@ template<class T> constexpr const T &clamp(const T &v, const T &lo, const T &hi)
class PRNG class PRNG
{ {
uint64_t s; uint64_t s;
uint64_t rand64() uint64_t rand64()
{ {
s ^= s >> 12, s ^= s << 25, s ^= s >> 27; s ^= s >> 12, s ^= s << 25, s ^= s >> 27;
return s * 2685821657736338717LL; return s * 2685821657736338717LL;
} }

View File

@ -361,9 +361,9 @@ int Position::pieces_on_board_count()
{ {
nPiecesOnBoard[BLACK] = nPiecesOnBoard[WHITE] = 0; nPiecesOnBoard[BLACK] = nPiecesOnBoard[WHITE] = 0;
for (int r = 1; r < FILE_NB + 2; r++) { for (int f = 1; f < FILE_NB + 2; f++) {
for (int s = 0; s < RANK_NB; s++) { for (int r = 0; r < RANK_NB; r++) {
Square square = static_cast<Square>(r * RANK_NB + s); Square square = static_cast<Square>(f * RANK_NB + r);
if (board[square] & B_STONE) { if (board[square] & B_STONE) {
nPiecesOnBoard[BLACK]++; nPiecesOnBoard[BLACK]++;
} else if (board[square] & W_STONE) { } else if (board[square] & W_STONE) {
@ -1134,7 +1134,7 @@ int Position::get_mobility_diff(bool includeFobidden)
int n = 0; int n = 0;
for (Square i = SQ_BEGIN; i < SQ_END; i = static_cast<Square>(i + 1)) { for (Square i = SQ_BEGIN; i < SQ_END; i = static_cast<Square>(i + 1)) {
n = surrounded_empty_squres_count(i, includeFobidden); n = surrounded_empty_squares_count(i, includeFobidden);
if (board[i] & B_STONE) { if (board[i] & B_STONE) {
mobilityBlack += n; mobilityBlack += n;
@ -1156,9 +1156,9 @@ void Position::clean_banned()
Square square = SQ_0; Square square = SQ_0;
for (int r = 1; r <= FILE_NB; r++) { for (int f = 1; f <= FILE_NB; f++) {
for (int s = 0; s < RANK_NB; s++) { for (int r = 0; r < RANK_NB; r++) {
square = static_cast<Square>(r * RANK_NB + s); square = static_cast<Square>(f * RANK_NB + r);
if (board[square] == BAN_STONE) { if (board[square] == BAN_STONE) {
revert_key(square); revert_key(square);
@ -1638,7 +1638,7 @@ bool Position::is_all_in_mills(Color c)
} }
// Stat include ban // Stat include ban
int Position::surrounded_empty_squres_count(Square square, bool includeFobidden) int Position::surrounded_empty_squares_count(Square square, bool includeFobidden)
{ {
//assert(rule.hasBannedLocations == includeFobidden); //assert(rule.hasBannedLocations == includeFobidden);
@ -1740,43 +1740,43 @@ bool Position::is_star_square(Square square)
void Position::mirror(int32_t move_, Square square, bool cmdChange /*= true*/) void Position::mirror(int32_t move_, Square square, bool cmdChange /*= true*/)
{ {
Piece ch; Piece ch;
int r, s; int f, r;
int i; int i;
for (r = 1; r <= FILE_NB; r++) { for (f = 1; f <= FILE_NB; f++) {
for (s = 1; s < RANK_NB / 2; s++) { for (r = 1; r < RANK_NB / 2; r++) {
ch = board[r * RANK_NB + s]; ch = board[f * RANK_NB + r];
board[r * RANK_NB + s] = board[(r + 1) * RANK_NB - s]; board[f * RANK_NB + r] = board[(f + 1) * RANK_NB - r];
board[(r + 1) * RANK_NB - s] = ch; board[(f + 1) * RANK_NB - r] = ch;
} }
} }
uint64_t llp[3] = { 0 }; uint64_t llp[3] = { 0 };
if (move_ < 0) { if (move_ < 0) {
r = (-move_) / RANK_NB; f = (-move_) / RANK_NB;
s = (-move_) % RANK_NB; r = (-move_) % RANK_NB;
s = (RANK_NB - s) % RANK_NB; r = (RANK_NB - r) % RANK_NB;
move_ = -(r * RANK_NB + s); move_ = -(f * RANK_NB + r);
} else { } else {
llp[0] = static_cast<uint64_t>(from_sq((Move)move_)); llp[0] = static_cast<uint64_t>(from_sq((Move)move_));
llp[1] = to_sq((Move)move_); llp[1] = to_sq((Move)move_);
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
r = static_cast<int>(llp[i]) / RANK_NB; f = static_cast<int>(llp[i]) / RANK_NB;
s = static_cast<int>(llp[i]) % RANK_NB; r = static_cast<int>(llp[i]) % RANK_NB;
s = (RANK_NB - s) % RANK_NB; r = (RANK_NB - r) % RANK_NB;
llp[i] = (static_cast<uint64_t>(r) * RANK_NB + s); llp[i] = (static_cast<uint64_t>(f) * RANK_NB + r);
} }
move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1])); move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1]));
} }
if (square != 0) { if (square != 0) {
r = square / RANK_NB; f = square / RANK_NB;
s = square % RANK_NB; r = square % RANK_NB;
s = (RANK_NB - s) % RANK_NB; r = (RANK_NB - r) % RANK_NB;
square = static_cast<Square>(r * RANK_NB + s); square = static_cast<Square>(f * RANK_NB + r);
} }
if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) { if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) {
@ -1786,10 +1786,10 @@ void Position::mirror(int32_t move_, Square square, bool cmdChange /*= true*/)
llp[2] = (mill & 0x00000000000000ff); llp[2] = (mill & 0x00000000000000ff);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
r = static_cast<int>(llp[i]) / RANK_NB; f = static_cast<int>(llp[i]) / RANK_NB;
s = static_cast<int>(llp[i]) % RANK_NB; r = static_cast<int>(llp[i]) % RANK_NB;
s = (RANK_NB - s) % RANK_NB; r = (RANK_NB - r) % RANK_NB;
llp[i] = static_cast<uint64_t>(r * RANK_NB + s); llp[i] = static_cast<uint64_t>(f * RANK_NB + r);
} }
mill &= 0xffffff00ff00ff00; mill &= 0xffffff00ff00ff00;
@ -1849,56 +1849,56 @@ void Position::mirror(int32_t move_, Square square, bool cmdChange /*= true*/)
void Position::turn(int32_t move_, Square square, bool cmdChange /*= true*/) void Position::turn(int32_t move_, Square square, bool cmdChange /*= true*/)
{ {
Piece ch; Piece ch;
int r, s; int f, r;
int i; int i;
for (s = 0; s < RANK_NB; s++) { for (r = 0; r < RANK_NB; r++) {
ch = board[RANK_NB + s]; ch = board[RANK_NB + r];
board[RANK_NB + s] = board[RANK_NB * FILE_NB + s]; board[RANK_NB + r] = board[RANK_NB * FILE_NB + r];
board[RANK_NB * FILE_NB + s] = ch; board[RANK_NB * FILE_NB + r] = ch;
} }
uint64_t llp[3] = { 0 }; uint64_t llp[3] = { 0 };
if (move_ < 0) { if (move_ < 0) {
r = (-move_) / RANK_NB; f = (-move_) / RANK_NB;
s = (-move_) % RANK_NB; r = (-move_) % RANK_NB;
if (r == 1) if (f == 1)
r = FILE_NB; f = FILE_NB;
else if (r == FILE_NB) else if (f == FILE_NB)
r = 1; f = 1;
move_ = -(r * RANK_NB + s); move_ = -(f * RANK_NB + r);
} else { } else {
llp[0] = static_cast<uint64_t>(from_sq((Move)move_)); llp[0] = static_cast<uint64_t>(from_sq((Move)move_));
llp[1] = to_sq((Move)move_); llp[1] = to_sq((Move)move_);
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
r = static_cast<int>(llp[i]) / RANK_NB; f = static_cast<int>(llp[i]) / RANK_NB;
s = static_cast<int>(llp[i]) % RANK_NB; r = static_cast<int>(llp[i]) % RANK_NB;
if (r == 1) if (f == 1)
r = FILE_NB; f = FILE_NB;
else if (r == FILE_NB) else if (f == FILE_NB)
r = 1; f = 1;
llp[i] = static_cast<uint64_t>(r * RANK_NB + s); llp[i] = static_cast<uint64_t>(f * RANK_NB + r);
} }
move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1])); move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1]));
} }
if (square != 0) { if (square != 0) {
r = square / RANK_NB; f = square / RANK_NB;
s = square % RANK_NB; r = square % RANK_NB;
if (r == 1) if (f == 1)
r = FILE_NB; f = FILE_NB;
else if (r == FILE_NB) else if (f == FILE_NB)
r = 1; f = 1;
square = static_cast<Square>(r * RANK_NB + s); square = static_cast<Square>(f * RANK_NB + r);
} }
if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) { if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) {
@ -1908,15 +1908,15 @@ void Position::turn(int32_t move_, Square square, bool cmdChange /*= true*/)
llp[2] = (mill & 0x00000000000000ff); llp[2] = (mill & 0x00000000000000ff);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
r = static_cast<int>(llp[i]) / RANK_NB; f = static_cast<int>(llp[i]) / RANK_NB;
s = static_cast<int>(llp[i]) % RANK_NB; r = static_cast<int>(llp[i]) % RANK_NB;
if (r == 1) if (f == 1)
r = FILE_NB; f = FILE_NB;
else if (r == FILE_NB) else if (f == FILE_NB)
r = 1; f = 1;
llp[i] = static_cast<uint64_t>(r * RANK_NB + s); llp[i] = static_cast<uint64_t>(f * RANK_NB + r);
} }
mill &= 0xffffff00ff00ff00; mill &= 0xffffff00ff00ff00;
@ -2022,39 +2022,39 @@ void Position::rotate(int degrees, int32_t move_, Square square, bool cmdChange
degrees /= 45; degrees /= 45;
Piece ch1, ch2; Piece ch1, ch2;
int r, s; int f, r;
int i; int i;
if (degrees == 2) { if (degrees == 2) {
for (r = 1; r <= FILE_NB; r++) { for (f = 1; f <= FILE_NB; f++) {
ch1 = board[r * RANK_NB]; ch1 = board[f * RANK_NB];
ch2 = board[r * RANK_NB + 1]; ch2 = board[f * RANK_NB + 1];
for (s = 0; s < RANK_NB - 2; s++) { for (r = 0; r < RANK_NB - 2; r++) {
board[r * RANK_NB + s] = board[r * RANK_NB + s + 2]; board[f * RANK_NB + r] = board[f * RANK_NB + r + 2];
} }
board[r * RANK_NB + 6] = ch1; board[f * RANK_NB + 6] = ch1;
board[r * RANK_NB + 7] = ch2; board[f * RANK_NB + 7] = ch2;
} }
} else if (degrees == 6) { } else if (degrees == 6) {
for (r = 1; r <= FILE_NB; r++) { for (f = 1; f <= FILE_NB; f++) {
ch1 = board[r * RANK_NB + 7]; ch1 = board[f * RANK_NB + 7];
ch2 = board[r * RANK_NB + 6]; ch2 = board[f * RANK_NB + 6];
for (s = RANK_NB - 1; s >= 2; s--) { for (r = RANK_NB - 1; r >= 2; r--) {
board[r * RANK_NB + s] = board[r * RANK_NB + s - 2]; board[f * RANK_NB + r] = board[f * RANK_NB + r - 2];
} }
board[r * RANK_NB + 1] = ch1; board[f * RANK_NB + 1] = ch1;
board[r * RANK_NB] = ch2; board[f * RANK_NB] = ch2;
} }
} else if (degrees == 4) { } else if (degrees == 4) {
for (r = 1; r <= FILE_NB; r++) { for (f = 1; f <= FILE_NB; f++) {
for (s = 0; s < RANK_NB / 2; s++) { for (r = 0; r < RANK_NB / 2; r++) {
ch1 = board[r * RANK_NB + s]; ch1 = board[f * RANK_NB + r];
board[r * RANK_NB + s] = board[r * RANK_NB + s + 4]; board[f * RANK_NB + r] = board[f * RANK_NB + r + 4];
board[r * RANK_NB + s + 4] = ch1; board[f * RANK_NB + r + 4] = ch1;
} }
} }
} else { } else {
@ -2064,29 +2064,29 @@ void Position::rotate(int degrees, int32_t move_, Square square, bool cmdChange
uint64_t llp[3] = { 0 }; uint64_t llp[3] = { 0 };
if (move_ < 0) { if (move_ < 0) {
r = (-move_) / RANK_NB; f = (-move_) / RANK_NB;
s = (-move_) % RANK_NB; r = (-move_) % RANK_NB;
s = (s + RANK_NB - degrees) % RANK_NB; r = (r + RANK_NB - degrees) % RANK_NB;
move_ = -(r * RANK_NB + s); move_ = -(f * RANK_NB + r);
} else { } else {
llp[0] = static_cast<uint64_t>(from_sq((Move)move_)); llp[0] = static_cast<uint64_t>(from_sq((Move)move_));
llp[1] = to_sq((Move)move_); llp[1] = to_sq((Move)move_);
r = static_cast<int>(llp[0]) / RANK_NB; f = static_cast<int>(llp[0]) / RANK_NB;
s = static_cast<int>(llp[0]) % RANK_NB; r = static_cast<int>(llp[0]) % RANK_NB;
s = (s + RANK_NB - degrees) % RANK_NB; r = (r + RANK_NB - degrees) % RANK_NB;
llp[0] = static_cast<uint64_t>(r * RANK_NB + s); llp[0] = static_cast<uint64_t>(f * RANK_NB + r);
r = static_cast<int>(llp[1]) / RANK_NB; f = static_cast<int>(llp[1]) / RANK_NB;
s = static_cast<int>(llp[1]) % RANK_NB; r = static_cast<int>(llp[1]) % RANK_NB;
s = (s + RANK_NB - degrees) % RANK_NB; r = (r + RANK_NB - degrees) % RANK_NB;
llp[1] = static_cast<uint64_t>(r * RANK_NB + s); llp[1] = static_cast<uint64_t>(f * RANK_NB + r);
move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1])); move_ = static_cast<int16_t>(((llp[0] << 8) | llp[1]));
} }
if (square != 0) { if (square != 0) {
r = square / RANK_NB; f = square / RANK_NB;
s = square % RANK_NB; r = square % RANK_NB;
s = (s + RANK_NB - degrees) % RANK_NB; r = (r + RANK_NB - degrees) % RANK_NB;
square = static_cast<Square>(r * RANK_NB + s); square = static_cast<Square>(f * RANK_NB + r);
} }
if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) { if (rule.allowRemovePiecesRepeatedlyWhenCloseSameMill) {
@ -2096,10 +2096,10 @@ void Position::rotate(int degrees, int32_t move_, Square square, bool cmdChange
llp[2] = (mill & 0x00000000000000ff); llp[2] = (mill & 0x00000000000000ff);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
r = static_cast<int>(llp[i]) / RANK_NB; f = static_cast<int>(llp[i]) / RANK_NB;
s = static_cast<int>(llp[i]) % RANK_NB; r = static_cast<int>(llp[i]) % RANK_NB;
s = (s + RANK_NB - degrees) % RANK_NB; r = (r + RANK_NB - degrees) % RANK_NB;
llp[i] = static_cast<uint64_t>(r * RANK_NB + s); llp[i] = static_cast<uint64_t>(f * RANK_NB + r);
} }
mill &= 0xffffff00ff00ff00; mill &= 0xffffff00ff00ff00;

View File

@ -157,7 +157,7 @@ public:
int in_how_many_mills(Square square, Color c, Square squareSelected = SQ_0); int in_how_many_mills(Square square, Color c, Square squareSelected = SQ_0);
bool is_all_in_mills(Color c); bool is_all_in_mills(Color c);
int surrounded_empty_squres_count(Square square, bool includeFobidden); int surrounded_empty_squares_count(Square square, bool includeFobidden);
void surrounded_pieces_count(Square square, int &nOurPieces, int &nTheirPieces, int &nBanned, int &nEmpty); void surrounded_pieces_count(Square square, int &nOurPieces, int &nTheirPieces, int &nBanned, int &nEmpty);
bool is_all_surrounded(); bool is_all_surrounded();