From be82dbf1b5474ab315bb13e84353cf6a1af27f60 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 29 Sep 2019 23:19:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20index=5Ft=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=B9=B6=E8=AE=BE=E7=BD=AE=E4=B8=BA8=E4=BD=8D=20(?= =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E7=94=A832=E4=BD=8D=E8=A1=A8=E7=A4=BA=20inde?= =?UTF-8?q?x)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ai/movegen.cpp | 4 ++-- src/game/board.cpp | 31 ++++++++++++++++--------------- src/game/board.h | 20 ++++++++++---------- src/game/position.cpp | 18 +++++++++--------- src/game/position.h | 14 +++++++------- src/game/types.h | 1 + 6 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/ai/movegen.cpp b/src/ai/movegen.cpp index 85a7cc53..f907b981 100644 --- a/src/ai/movegen.cpp +++ b/src/ai/movegen.cpp @@ -31,7 +31,7 @@ void MoveList::generate(AIAlgorithm &ai, Game &tempGame, move_t bestMove) { const int MOVE_PRIORITY_TABLE_SIZE = Board::N_RINGS * Board::N_SEATS; - int index = 0; + index_t index = 0; size_t newCapacity = 24; // 留足余量空间避免多次重新分配,此动作本身也占用 CPU/内存 开销 @@ -96,7 +96,7 @@ void MoveList::generate(AIAlgorithm &ai, Game &tempGame, // 对于移子阶段 if (tempGame.position.phase & PHASE_MOVING) { - int newIndex, oldIndex; + index_t newIndex, oldIndex; // 尽量走理论上较差的位置的棋子 for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) { diff --git a/src/game/board.cpp b/src/game/board.cpp index 8f13c33e..9c8c93c9 100644 --- a/src/game/board.cpp +++ b/src/game/board.cpp @@ -192,7 +192,7 @@ void Board::createMillTable() #endif /* DEBUG_MODE */ } -void Board::indexToPolar(const int index, int &r, int &s) +void Board::indexToPolar(const index_t index, int &r, int &s) { //r = index / N_SEATS; //s = index % N_SEATS + 1; @@ -200,7 +200,7 @@ void Board::indexToPolar(const int index, int &r, int &s) s = (index & 0x07) + 1; } -int Board::polarToIndex(int r, int s) +index_t Board::polarToIndex(int r, int s) { if (r < 1 || r > N_RINGS || s < 1 || s > N_SEATS) { return 0; @@ -209,7 +209,7 @@ int Board::polarToIndex(int r, int s) return r * N_SEATS + s - 1; } -int Board::inHowManyMills(int index) +int Board::inHowManyMills(index_t index) { int n = 0; int index_1, index_2; @@ -225,7 +225,7 @@ int Board::inHowManyMills(int index) return n; } -int Board::addMills(int index) +int Board::addMills(index_t index) { // 成三用一个64位整数了,规则如下 // 0x 00 00 00 00 00 00 00 00 @@ -316,13 +316,13 @@ bool Board::isAllInMills(player_t player) // 判断玩家的棋子周围有几个空位 int Board::getSurroundedEmptyLocationCount(int sideId, int nPiecesOnBoard[], - int index, bool includeFobidden) + index_t index, bool includeFobidden) { int count = 0; if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast || !rule.allowFlyWhenRemainThreePieces) { - int moveIndex; + index_t moveIndex; for (direction_t d = DIRECTION_BEGIN; d < DIRECTIONS_COUNT; d = (direction_t)(d + 1)) { moveIndex = MoveList::moveTable[index][d]; if (moveIndex) { @@ -338,12 +338,13 @@ int Board::getSurroundedEmptyLocationCount(int sideId, int nPiecesOnBoard[], } // 判断玩家的棋子是否被围 -bool Board::isSurrounded(int sideId, int nPiecesOnBoard[], int index) +bool Board::isSurrounded(int sideId, int nPiecesOnBoard[], index_t index) { // 判断index处的棋子是否被“闷” if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast || !rule.allowFlyWhenRemainThreePieces) { - int i, moveIndex; + int i; + index_t moveIndex; for (i = 0; i < 4; i++) { moveIndex = MoveList::moveTable[index][i]; if (moveIndex && !locations[moveIndex]) @@ -370,7 +371,7 @@ bool Board::isAllSurrounded(int sideId, int nPiecesOnBoard[], char ch) } // 查询整个棋盘 - int moveIndex; + index_t moveIndex; for (int i = 1; i < N_SEATS * (N_RINGS + 1); i++) { if (!(ch & locations[i])) { continue; @@ -397,7 +398,7 @@ bool Board::isAllSurrounded(int sideId, int nPiecesOnBoard[], player_t player) #if 0 player_t Board::getWhosPiece(int r, int s) { - int index = polarToIndex(r, s); + index_t index = polarToIndex(r, s); if (locations[index] & PLAYER_1) return PLAYER_1; @@ -436,7 +437,7 @@ bool Board::getPieceRS(const player_t &player, const int &number, int &r, int &s } // 获取当前棋子 -bool Board::getCurrentPiece(player_t &player, int &number, int index) +bool Board::getCurrentPiece(player_t &player, int &number, index_t index) { if (!onBoard[index]) return false; @@ -457,7 +458,7 @@ bool Board::getCurrentPiece(player_t &player, int &number, int index) } #endif -bool Board::isStar(int index) +bool Board::isStar(index_t index) { return (index == 17 || index == 19 || @@ -465,7 +466,7 @@ bool Board::isStar(int index) index == 23); } -void Board::mirror(list &cmdlist, char* cmdline, int32_t move_, int index, bool cmdChange /*= true*/) +void Board::mirror(list &cmdlist, char* cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/) { int ch; int r, s; @@ -577,7 +578,7 @@ void Board::mirror(list &cmdlist, char* cmdline, int32_t move_, int ind } } -void Board::turn(list &cmdlist, char *cmdline, int32_t move_, int index, bool cmdChange /*= true*/) +void Board::turn(list &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/) { int ch; int r, s; @@ -742,7 +743,7 @@ void Board::turn(list &cmdlist, char *cmdline, int32_t move_, int index } } -void Board::rotate(int degrees, list &cmdlist, char *cmdline, int32_t move_, int index, bool cmdChange /*= true*/) +void Board::rotate(int degrees, list &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/) { // 将degrees转化为0~359之间的数 degrees = degrees % 360; diff --git a/src/game/board.h b/src/game/board.h index ae48b6ea..1c9a1332 100644 --- a/src/game/board.h +++ b/src/game/board.h @@ -57,7 +57,7 @@ public: static const int onBoard[EXPANDED_BOARD_SIZE]; // 判断位置点是否为星位 (星位是经常会先占的位置) - static bool isStar(int index); + static bool isStar(index_t index); // 成三表,表示棋盘上各个位置有成三关系的对应位置表 // 这个表跟规则有关,一旦规则改变需要重新修改 @@ -67,25 +67,25 @@ public: void createMillTable(); // 局面左右镜像 - void mirror(list &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true); + void mirror(list &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true); // 局面内外翻转 - void turn(list &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true); + void turn(list &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true); // 局面逆时针旋转 - void rotate(int degrees, list &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true); + void rotate(int degrees, list &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true); // 判断棋盘 index 处的棋子处于几个“三连”中 - int inHowManyMills(int index); + int inHowManyMills(index_t index); // 判断玩家的所有棋子是否都处于“三连”状态 bool isAllInMills(player_t); // 判断玩家的棋子周围有几个空位 - int getSurroundedEmptyLocationCount(int sideId, int nPiecesOnBoard[], int index, bool includeFobidden); + int getSurroundedEmptyLocationCount(int sideId, int nPiecesOnBoard[], index_t index, bool includeFobidden); // 判断玩家的棋子是否被围 - bool isSurrounded(int sideId, int nPiecesOnBoard[], int index); + bool isSurrounded(int sideId, int nPiecesOnBoard[], index_t index); // 判断玩家的棋子是否全部被围 bool isAllSurrounded(int sideId, int nPiecesOnBoard[], char ch); @@ -93,7 +93,7 @@ public: bool isAllSurrounded(int sideId, int nPiecesOnBoard[], player_t ply); // 三连加入列表 - int addMills(int index); + int addMills(index_t index); #if 0 // 获取位置点棋子的归属人 @@ -106,10 +106,10 @@ public: bool getCurrentPiece(player_t &player, int &number, int currentPos); // 将棋盘下标形式转化为第r圈,第s位,r和s下标都从1开始 - static void indexToPolar(int index, int &r, int &s); + static void indexToPolar(index_t index, int &r, int &s); // 将第c圈,第p位转化为棋盘下标形式,r和s下标都从1开始 - static int polarToIndex(int r, int s); + static index_t polarToIndex(int r, int s); static void printBoard(); diff --git a/src/game/position.cpp b/src/game/position.cpp index b35e5252..32f53801 100644 --- a/src/game/position.cpp +++ b/src/game/position.cpp @@ -85,7 +85,7 @@ int Game::countPiecesOnBoard() for (int r = 1; r < Board::N_RINGS + 2; r++) { for (int s = 0; s < Board::N_SEATS; s++) { - int index = r * Board::N_SEATS + s; + index_t index = r * Board::N_SEATS + s; if (boardLocations[index] & 0x10) { position.nPiecesOnBoard[1]++; } else if (boardLocations[index] & 0x20) { @@ -279,7 +279,7 @@ bool Game::start() } } -bool Game::place(int index, int8_t updateCmdlist) +bool Game::place(index_t index, int8_t updateCmdlist) { // 如果局面为“结局”,返回false if (position.phase == PHASE_GAMEOVER) @@ -454,7 +454,7 @@ out: bool Game::_place(int r, int s) { // 转换为 index - int index = Board::polarToIndex(r, s); + index_t index = Board::polarToIndex(r, s); return place(index, true); } @@ -462,12 +462,12 @@ bool Game::_place(int r, int s) bool Game::_capture(int r, int s) { // 转换为 index - int index = Board::polarToIndex(r, s); + index_t index = Board::polarToIndex(r, s); return capture(index, 1); } -bool Game::capture(int index, int8_t updateCmdlist) +bool Game::capture(index_t index, int8_t updateCmdlist) { // 如果局面为"未开局"或“结局”,返回false if (position.phase & PHASE_NOTPLAYING) @@ -605,7 +605,7 @@ out: return true; } -bool Game::choose(int index) +bool Game::choose(index_t index) { // 如果局面不是"中局”,返回false if (position.phase != PHASE_MOVING) @@ -927,7 +927,7 @@ int Game::getMobilityDiff(player_t turn, int nPiecesOnBoard[], bool includeFobid void Game::cleanForbiddenLocations() { - int index = 0; + index_t index = 0; for (int r = 1; r <= Board::N_RINGS; r++) { for (int s = 0; s < Board::N_SEATS; s++) { @@ -1043,7 +1043,7 @@ hash_t Game::getHash() return position.hash; } -hash_t Game::updateHash(int index) +hash_t Game::updateHash(index_t index) { // PieceType is boardLocations[index] @@ -1056,7 +1056,7 @@ hash_t Game::updateHash(int index) return position.hash; } -hash_t Game::revertHash(int index) +hash_t Game::revertHash(index_t index) { return updateHash(index); } diff --git a/src/game/position.h b/src/game/position.h index 5426ff0a..e33499e2 100644 --- a/src/game/position.h +++ b/src/game/position.h @@ -111,7 +111,7 @@ public: } // 获取当前棋子位置点 - int getCurrentIndex() const + index_t getCurrentIndex() const { return currentIndex; } @@ -242,14 +242,14 @@ public: // 下面几个函数没有算法无关判断和无关操作,节约算法时间 bool command(int move); - bool choose(int index); - bool place(int index, int8_t cp = 0); - bool capture(int index, int8_t cp = 0); + bool choose(index_t index); + bool place(index_t index, int8_t cp = 0); + bool capture(index_t index, int8_t cp = 0); // hash 相关 hash_t getHash(); - hash_t revertHash(int index); - hash_t updateHash(int index); + hash_t revertHash(index_t index); + hash_t updateHash(index_t index); hash_t updateHashMisc(); public: /* TODO: move to private */ @@ -288,7 +288,7 @@ public: /* TODO: move to private */ move_t move{}; // 选中的棋子在board中的位置 - int currentIndex{}; + index_t currentIndex {}; private: // 棋局哈希值 diff --git a/src/game/types.h b/src/game/types.h index 989b8a1d..ab032908 100644 --- a/src/game/types.h +++ b/src/game/types.h @@ -27,6 +27,7 @@ using step_t = uint16_t; using depth_t = uint8_t; using location_t = uint8_t; +using index_t = uint8_t; #ifdef TRANSPOSITION_TABLE_CUTDOWN using hash_t = uint32_t;