Skip to content

Commit

Permalink
生成着法时子结点vector预留的空间计算出来而非写死
Browse files Browse the repository at this point in the history
内存占用未看到明显差异。修改前后都是 521MB 左右。
  • Loading branch information
calcitem committed Aug 4, 2019
1 parent 2365d71 commit a86edd6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion NineChess/src/ninechessai_ab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,39 @@ void NineChessAi_ab::generateLegalMoves(Node *node, move_t bestMove)
{
const int MOVE_PRIORITY_TABLE_SIZE = NineChess::N_RINGS * NineChess::N_SEATS;
int pos = 0;
size_t newCapacity = 24;

// 留足余量空间避免多次重新分配,此动作本身也占用 CPU/内存 开销
node->children.reserve(24);
switch (chessTemp.getStage()) {
case NineChess::GAME_PLACING:
if (chessTemp.getAction() == NineChess::ACTION_CAPTURE) {
if (chessTemp.whosTurn() == NineChess::PLAYER1)
newCapacity = chessTemp.getPiecesOnBoardCount_2();
else
newCapacity = chessTemp.getPiecesOnBoardCount_1();
} else {
newCapacity = chessTemp.getPiecesInHandCount_1() + chessTemp.getPiecesInHandCount_2();
}
break;
case NineChess::GAME_MOVING:
if (chessTemp.getAction() == NineChess::ACTION_CAPTURE) {
if (chessTemp.whosTurn() == NineChess::PLAYER1)
newCapacity = chessTemp.getPiecesOnBoardCount_2();
else
newCapacity = chessTemp.getPiecesOnBoardCount_1();
} else {
newCapacity = 6;
}
break;
case NineChess::GAME_NOTSTARTED:
newCapacity = 24;
break;
default:
newCapacity = 24;
break;
};

node->children.reserve(newCapacity + 2 /* TODO: 未细调故再多留余量2 */);

#ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE
Expand Down

0 comments on commit a86edd6

Please sign in to comment.