movegen/movepick: Position * 改为 Position &

This commit is contained in:
Calcitem 2020-09-06 12:32:35 +08:00
parent 490983195d
commit 31d9bd8294
7 changed files with 34 additions and 34 deletions

View File

@ -284,30 +284,30 @@ void MoveList::shuffle()
/// generate generates all the legal moves in the given position /// generate generates all the legal moves in the given position
ExtMove *generate(Position *position, ExtMove *moveList) ExtMove *generate(Position &position, ExtMove *moveList)
{ {
Square s; Square s;
Color us = position->side_to_move(); Color us = position.side_to_move();
Color them = ~us; Color them = ~us;
const int MOVE_PRIORITY_TABLE_SIZE = FILE_NB * RANK_NB; const int MOVE_PRIORITY_TABLE_SIZE = FILE_NB * RANK_NB;
ExtMove *cur = moveList; ExtMove *cur = moveList;
switch (position->action) { switch (position.action) {
case ACTION_SELECT: case ACTION_SELECT:
case ACTION_PLACE: case ACTION_PLACE:
if (position->phase & (PHASE_PLACING | PHASE_READY)) { if (position.phase & (PHASE_PLACING | PHASE_READY)) {
for (auto i : MoveList::movePriorityTable) { for (auto i : MoveList::movePriorityTable) {
if (position->board[i]) { if (position.board[i]) {
continue; continue;
} }
#ifdef MCTS_AI #ifdef MCTS_AI
moves.push_back((Move)i); moves.push_back((Move)i);
#else // MCTS_AI #else // MCTS_AI
if (position->phase != PHASE_READY) { if (position.phase != PHASE_READY) {
*cur++ = (Move)i; *cur++ = (Move)i;
} else { } else {
#ifdef FIRST_MOVE_STAR_PREFERRED #ifdef FIRST_MOVE_STAR_PREFERRED
@ -323,22 +323,22 @@ ExtMove *generate(Position *position, ExtMove *moveList)
break; break;
} }
if (position->phase & PHASE_MOVING) { if (position.phase & PHASE_MOVING) {
Square newSquare, oldSquare; Square newSquare, oldSquare;
// move piece that location weak first // move piece that location weak first
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
oldSquare = MoveList::movePriorityTable[i]; oldSquare = MoveList::movePriorityTable[i];
if (!position->select_piece(oldSquare)) { if (!position.select_piece(oldSquare)) {
continue; continue;
} }
if (position->pieceCountOnBoard[position->sideToMove] > rule.nPiecesAtLeast || if (position.pieceCountOnBoard[position.sideToMove] > rule.nPiecesAtLeast ||
!rule.allowFlyWhenRemainThreePieces) { !rule.allowFlyWhenRemainThreePieces) {
for (int direction = MD_BEGIN; direction < MD_NB; direction++) { for (int direction = MD_BEGIN; direction < MD_NB; direction++) {
newSquare = static_cast<Square>(MoveList::moveTable[oldSquare][direction]); newSquare = static_cast<Square>(MoveList::moveTable[oldSquare][direction]);
if (newSquare && !position->board[newSquare]) { if (newSquare && !position.board[newSquare]) {
Move m = make_move(oldSquare, newSquare); Move m = make_move(oldSquare, newSquare);
*cur++ = (Move)m; *cur++ = (Move)m;
} }
@ -346,7 +346,7 @@ ExtMove *generate(Position *position, ExtMove *moveList)
} else { } else {
// piece count < 3and allow fly, if is empty point, that's ok, do not need in move list // piece count < 3and allow fly, if is empty point, that's ok, do not need in move list
for (newSquare = SQ_BEGIN; newSquare < SQ_END; newSquare = static_cast<Square>(newSquare + 1)) { for (newSquare = SQ_BEGIN; newSquare < SQ_END; newSquare = static_cast<Square>(newSquare + 1)) {
if (!position->board[newSquare]) { if (!position.board[newSquare]) {
Move m = make_move(oldSquare, newSquare); Move m = make_move(oldSquare, newSquare);
*cur++ = (Move)m; *cur++ = (Move)m;
} }
@ -357,10 +357,10 @@ ExtMove *generate(Position *position, ExtMove *moveList)
break; break;
case ACTION_REMOVE: case ACTION_REMOVE:
if (position->is_all_in_mills(them)) { if (position.is_all_in_mills(them)) {
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
s = MoveList::movePriorityTable[i]; s = MoveList::movePriorityTable[i];
if (position->board[s]& (them << PLAYER_SHIFT)) { if (position.board[s]& (them << PLAYER_SHIFT)) {
*cur++ = (Move)-s; *cur++ = (Move)-s;
} }
} }
@ -370,8 +370,8 @@ ExtMove *generate(Position *position, ExtMove *moveList)
// not is all in mills // not is all in mills
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
s = MoveList::movePriorityTable[i]; s = MoveList::movePriorityTable[i];
if (position->board[s] & (them << PLAYER_SHIFT)) { if (position.board[s] & (them << PLAYER_SHIFT)) {
if (rule.allowRemovePieceInMill || !position->in_how_many_mills(s, NOBODY)) { if (rule.allowRemovePieceInMill || !position.in_how_many_mills(s, NOBODY)) {
*cur++ = (Move)-s; *cur++ = (Move)-s;
} }
} }

View File

@ -51,13 +51,13 @@ inline bool operator<(const ExtMove &f, const ExtMove &s)
return f.value < s.value; return f.value < s.value;
} }
ExtMove *generate(Position *pos, ExtMove *moveList); ExtMove *generate(Position &pos, ExtMove *moveList);
/// The MoveList struct is a simple wrapper around generate(). It sometimes comes /// The MoveList struct is a simple wrapper around generate(). It sometimes comes
/// in handy to use this class instead of the low level generate() function. /// in handy to use this class instead of the low level generate() function.
struct MoveList struct MoveList
{ {
explicit MoveList(Position *pos) : last(generate(pos, moveList)) {} explicit MoveList(Position &pos) : last(generate(pos, moveList)) {}
const ExtMove *begin() const const ExtMove *begin() const
{ {

View File

@ -46,7 +46,7 @@ void partial_insertion_sort(ExtMove *begin, ExtMove *end, int limit)
/// ordering is at the current node. /// ordering is at the current node.
/// MovePicker constructor for the main search /// MovePicker constructor for the main search
MovePicker::MovePicker(Position *p) MovePicker::MovePicker(Position &p)
: pos(p) : pos(p)
{ {
#ifdef HOSTORY_HEURISTIC #ifdef HOSTORY_HEURISTIC
@ -67,7 +67,7 @@ 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 nOurMills = pos->in_how_many_mills(sq, pos->sideToMove, sqsrc); int nOurMills = pos.in_how_many_mills(sq, pos.sideToMove, sqsrc);
int nTheirMills = 0; int nTheirMills = 0;
#ifdef SORT_MOVE_WITH_HUMAN_KNOWLEDGES #ifdef SORT_MOVE_WITH_HUMAN_KNOWLEDGES
@ -78,17 +78,17 @@ void MovePicker::score()
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
cur->value += RATING_ONE_MILL * nOurMills; cur->value += RATING_ONE_MILL * nOurMills;
#endif #endif
} else if (pos->get_phase() == PHASE_PLACING) { } else if (pos.get_phase() == PHASE_PLACING) {
// placing phrase, check if place sq can block their close mill // placing phrase, check if place sq can block their close mill
nTheirMills = pos->in_how_many_mills(sq, pos->them); nTheirMills = pos.in_how_many_mills(sq, pos.them);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
cur->value += RATING_BLOCK_ONE_MILL * nTheirMills; cur->value += RATING_BLOCK_ONE_MILL * nTheirMills;
#endif #endif
} }
#if 1 #if 1
else if (pos->get_phase() == PHASE_MOVING) { else if (pos.get_phase() == PHASE_MOVING) {
// moving phrase, check if place sq can block their close mill // moving phrase, check if place sq can block their close mill
nTheirMills = pos->in_how_many_mills(sq, pos->them); nTheirMills = pos.in_how_many_mills(sq, pos.them);
if (nTheirMills) { if (nTheirMills) {
int nOurPieces = 0; int nOurPieces = 0;
@ -96,7 +96,7 @@ void MovePicker::score()
int nBanned = 0; int nBanned = 0;
int nEmpty = 0; int nEmpty = 0;
pos->surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty); pos.surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
if (sq % 2 == 0 && nTheirPieces == 3) { if (sq % 2 == 0 && nTheirPieces == 3) {
@ -114,7 +114,7 @@ void MovePicker::score()
// for 12 men, white 's 2nd move place star point is as important as close mill (TODO) // for 12 men, white 's 2nd move place star point is as important as close mill (TODO)
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
if (rule.nTotalPiecesEachSide == 12 && if (rule.nTotalPiecesEachSide == 12 &&
pos->count<ON_BOARD>(WHITE) < 2 && // patch: only when white's 2nd move pos.count<ON_BOARD>(WHITE) < 2 && // patch: only when white's 2nd move
Position::is_star_square(static_cast<Square>(m))) { Position::is_star_square(static_cast<Square>(m))) {
cur->value += RATING_STAR_SQUARE; cur->value += RATING_STAR_SQUARE;
} }
@ -125,7 +125,7 @@ void MovePicker::score()
int nBanned = 0; int nBanned = 0;
int nEmpty = 0; int nEmpty = 0;
pos->surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty); pos.surrounded_pieces_count(sq, nOurPieces, nTheirPieces, nBanned, nEmpty);
#ifdef ALPHABETA_AI #ifdef ALPHABETA_AI
if (nOurMills > 0) { if (nOurMills > 0) {
@ -143,7 +143,7 @@ void MovePicker::score()
} }
// remove point is in their mill // remove point is in their mill
nTheirMills = pos->in_how_many_mills(sq, pos->them); nTheirMills = pos.in_how_many_mills(sq, pos.them);
if (nTheirMills) { if (nTheirMills) {
if (nTheirPieces >= 2) { if (nTheirPieces >= 2) {
// if nearby their piece, prefer do not remove // if nearby their piece, prefer do not remove

View File

@ -50,7 +50,7 @@ class MovePicker
public: public:
MovePicker(const MovePicker &) = delete; MovePicker(const MovePicker &) = delete;
MovePicker &operator=(const MovePicker &) = delete; MovePicker &operator=(const MovePicker &) = delete;
MovePicker(Position *position); MovePicker(Position &position);
Move next_move(); Move next_move();
@ -68,7 +68,7 @@ public:
return endMoves; return endMoves;
} }
Position *pos; Position &pos;
Move ttMove { MOVE_NONE }; Move ttMove { MOVE_NONE };
ExtMove *cur, *endMoves; ExtMove *cur, *endMoves;
ExtMove moves[MAX_MOVES] { MOVE_NONE }; ExtMove moves[MAX_MOVES] { MOVE_NONE };

View File

@ -809,7 +809,7 @@ Value search(Position *pos, Stack<Position> &ss, Depth depth, Depth originDepth,
return bestValue; return bestValue;
} }
MovePicker mp(pos); MovePicker mp(*pos);
Move nextMove = mp.next_move(); Move nextMove = mp.next_move();
int moveCount = mp.move_count(); int moveCount = mp.move_count();

View File

@ -168,7 +168,7 @@ void ThreadPool::clear()
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and /// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
/// returns immediately. Main thread will wake up other threads and start the search. /// returns immediately. Main thread will wake up other threads and start the search.
void ThreadPool::start_thinking(Position *pos, StateListPtr &states, void ThreadPool::start_thinking(Position &pos, StateListPtr &states,
const Search::LimitsType &limits, bool ponderMode) const Search::LimitsType &limits, bool ponderMode)
{ {
@ -180,7 +180,7 @@ void ThreadPool::start_thinking(Position *pos, StateListPtr &states,
Search::Limits = limits; Search::Limits = limits;
Search::RootMoves rootMoves; Search::RootMoves rootMoves;
for (const auto &m : MoveList(pos)) for (auto &m : MoveList(pos))
if (limits.searchmoves.empty() if (limits.searchmoves.empty()
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
rootMoves.emplace_back(m); rootMoves.emplace_back(m);
@ -208,7 +208,7 @@ void ThreadPool::start_thinking(Position *pos, StateListPtr &states,
th->nodes = th->tbHits = th->nmpMinPly = 0; th->nodes = th->tbHits = th->nmpMinPly = 0;
th->rootDepth = th->completedDepth = 0; th->rootDepth = th->completedDepth = 0;
th->rootMoves = rootMoves; th->rootMoves = rootMoves;
th->rootPos.set(pos->fen(), &setupStates->back(), th); th->rootPos.set(pos.fen(), &setupStates->back(), th);
} }
setupStates->back() = tmp; setupStates->back() = tmp;

View File

@ -96,7 +96,7 @@ struct MainThread : public Thread
struct ThreadPool : public std::vector<Thread *> struct ThreadPool : public std::vector<Thread *>
{ {
void start_thinking(Position *, StateListPtr &, const Search::LimitsType &, bool = false); void start_thinking(Position &, StateListPtr &, const Search::LimitsType &, bool = false);
void clear(); void clear();
void set(size_t); void set(size_t);