新增 index_t 类型并设置为8位 (原来用32位表示 index)
This commit is contained in:
parent
0d6f4edd52
commit
be82dbf1b5
|
@ -31,7 +31,7 @@ void MoveList::generate(AIAlgorithm &ai, Game &tempGame,
|
||||||
move_t bestMove)
|
move_t bestMove)
|
||||||
{
|
{
|
||||||
const int MOVE_PRIORITY_TABLE_SIZE = Board::N_RINGS * Board::N_SEATS;
|
const int MOVE_PRIORITY_TABLE_SIZE = Board::N_RINGS * Board::N_SEATS;
|
||||||
int index = 0;
|
index_t index = 0;
|
||||||
size_t newCapacity = 24;
|
size_t newCapacity = 24;
|
||||||
|
|
||||||
// 留足余量空间避免多次重新分配,此动作本身也占用 CPU/内存 开销
|
// 留足余量空间避免多次重新分配,此动作本身也占用 CPU/内存 开销
|
||||||
|
@ -96,7 +96,7 @@ void MoveList::generate(AIAlgorithm &ai, Game &tempGame,
|
||||||
|
|
||||||
// 对于移子阶段
|
// 对于移子阶段
|
||||||
if (tempGame.position.phase & PHASE_MOVING) {
|
if (tempGame.position.phase & PHASE_MOVING) {
|
||||||
int newIndex, oldIndex;
|
index_t newIndex, oldIndex;
|
||||||
|
|
||||||
// 尽量走理论上较差的位置的棋子
|
// 尽量走理论上较差的位置的棋子
|
||||||
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
|
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
|
||||||
|
|
|
@ -192,7 +192,7 @@ void Board::createMillTable()
|
||||||
#endif /* DEBUG_MODE */
|
#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;
|
//r = index / N_SEATS;
|
||||||
//s = index % N_SEATS + 1;
|
//s = index % N_SEATS + 1;
|
||||||
|
@ -200,7 +200,7 @@ void Board::indexToPolar(const int index, int &r, int &s)
|
||||||
s = (index & 0x07) + 1;
|
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) {
|
if (r < 1 || r > N_RINGS || s < 1 || s > N_SEATS) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -209,7 +209,7 @@ int Board::polarToIndex(int r, int s)
|
||||||
return r * N_SEATS + s - 1;
|
return r * N_SEATS + s - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Board::inHowManyMills(int index)
|
int Board::inHowManyMills(index_t index)
|
||||||
{
|
{
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int index_1, index_2;
|
int index_1, index_2;
|
||||||
|
@ -225,7 +225,7 @@ int Board::inHowManyMills(int index)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Board::addMills(int index)
|
int Board::addMills(index_t index)
|
||||||
{
|
{
|
||||||
// 成三用一个64位整数了,规则如下
|
// 成三用一个64位整数了,规则如下
|
||||||
// 0x 00 00 00 00 00 00 00 00
|
// 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 Board::getSurroundedEmptyLocationCount(int sideId, int nPiecesOnBoard[],
|
||||||
int index, bool includeFobidden)
|
index_t index, bool includeFobidden)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast ||
|
if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast ||
|
||||||
!rule.allowFlyWhenRemainThreePieces) {
|
!rule.allowFlyWhenRemainThreePieces) {
|
||||||
int moveIndex;
|
index_t moveIndex;
|
||||||
for (direction_t d = DIRECTION_BEGIN; d < DIRECTIONS_COUNT; d = (direction_t)(d + 1)) {
|
for (direction_t d = DIRECTION_BEGIN; d < DIRECTIONS_COUNT; d = (direction_t)(d + 1)) {
|
||||||
moveIndex = MoveList::moveTable[index][d];
|
moveIndex = MoveList::moveTable[index][d];
|
||||||
if (moveIndex) {
|
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处的棋子是否被“闷”
|
// 判断index处的棋子是否被“闷”
|
||||||
if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast ||
|
if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast ||
|
||||||
!rule.allowFlyWhenRemainThreePieces) {
|
!rule.allowFlyWhenRemainThreePieces) {
|
||||||
int i, moveIndex;
|
int i;
|
||||||
|
index_t moveIndex;
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
moveIndex = MoveList::moveTable[index][i];
|
moveIndex = MoveList::moveTable[index][i];
|
||||||
if (moveIndex && !locations[moveIndex])
|
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++) {
|
for (int i = 1; i < N_SEATS * (N_RINGS + 1); i++) {
|
||||||
if (!(ch & locations[i])) {
|
if (!(ch & locations[i])) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -397,7 +398,7 @@ bool Board::isAllSurrounded(int sideId, int nPiecesOnBoard[], player_t player)
|
||||||
#if 0
|
#if 0
|
||||||
player_t Board::getWhosPiece(int r, int s)
|
player_t Board::getWhosPiece(int r, int s)
|
||||||
{
|
{
|
||||||
int index = polarToIndex(r, s);
|
index_t index = polarToIndex(r, s);
|
||||||
|
|
||||||
if (locations[index] & PLAYER_1)
|
if (locations[index] & PLAYER_1)
|
||||||
return 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])
|
if (!onBoard[index])
|
||||||
return false;
|
return false;
|
||||||
|
@ -457,7 +458,7 @@ bool Board::getCurrentPiece(player_t &player, int &number, int index)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool Board::isStar(int index)
|
bool Board::isStar(index_t index)
|
||||||
{
|
{
|
||||||
return (index == 17 ||
|
return (index == 17 ||
|
||||||
index == 19 ||
|
index == 19 ||
|
||||||
|
@ -465,7 +466,7 @@ bool Board::isStar(int index)
|
||||||
index == 23);
|
index == 23);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::mirror(list <string> &cmdlist, char* cmdline, int32_t move_, int index, bool cmdChange /*= true*/)
|
void Board::mirror(list <string> &cmdlist, char* cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/)
|
||||||
{
|
{
|
||||||
int ch;
|
int ch;
|
||||||
int r, s;
|
int r, s;
|
||||||
|
@ -577,7 +578,7 @@ void Board::mirror(list <string> &cmdlist, char* cmdline, int32_t move_, int ind
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::turn(list <string> &cmdlist, char *cmdline, int32_t move_, int index, bool cmdChange /*= true*/)
|
void Board::turn(list <string> &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/)
|
||||||
{
|
{
|
||||||
int ch;
|
int ch;
|
||||||
int r, s;
|
int r, s;
|
||||||
|
@ -742,7 +743,7 @@ void Board::turn(list <string> &cmdlist, char *cmdline, int32_t move_, int index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::rotate(int degrees, list <string> &cmdlist, char *cmdline, int32_t move_, int index, bool cmdChange /*= true*/)
|
void Board::rotate(int degrees, list <string> &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange /*= true*/)
|
||||||
{
|
{
|
||||||
// 将degrees转化为0~359之间的数
|
// 将degrees转化为0~359之间的数
|
||||||
degrees = degrees % 360;
|
degrees = degrees % 360;
|
||||||
|
|
|
@ -57,7 +57,7 @@ public:
|
||||||
static const int onBoard[EXPANDED_BOARD_SIZE];
|
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 createMillTable();
|
||||||
|
|
||||||
// 局面左右镜像
|
// 局面左右镜像
|
||||||
void mirror(list <string> &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true);
|
void mirror(list <string> &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true);
|
||||||
|
|
||||||
// 局面内外翻转
|
// 局面内外翻转
|
||||||
void turn(list <string> &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true);
|
void turn(list <string> &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true);
|
||||||
|
|
||||||
// 局面逆时针旋转
|
// 局面逆时针旋转
|
||||||
void rotate(int degrees, list <string> &cmdlist, char *cmdline, int32_t move_, int currentPos, bool cmdChange = true);
|
void rotate(int degrees, list <string> &cmdlist, char *cmdline, int32_t move_, index_t index, bool cmdChange = true);
|
||||||
|
|
||||||
// 判断棋盘 index 处的棋子处于几个“三连”中
|
// 判断棋盘 index 处的棋子处于几个“三连”中
|
||||||
int inHowManyMills(int index);
|
int inHowManyMills(index_t index);
|
||||||
|
|
||||||
// 判断玩家的所有棋子是否都处于“三连”状态
|
// 判断玩家的所有棋子是否都处于“三连”状态
|
||||||
bool isAllInMills(player_t);
|
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);
|
bool isAllSurrounded(int sideId, int nPiecesOnBoard[], char ch);
|
||||||
|
@ -93,7 +93,7 @@ public:
|
||||||
bool isAllSurrounded(int sideId, int nPiecesOnBoard[], player_t ply);
|
bool isAllSurrounded(int sideId, int nPiecesOnBoard[], player_t ply);
|
||||||
|
|
||||||
// 三连加入列表
|
// 三连加入列表
|
||||||
int addMills(int index);
|
int addMills(index_t index);
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// 获取位置点棋子的归属人
|
// 获取位置点棋子的归属人
|
||||||
|
@ -106,10 +106,10 @@ public:
|
||||||
bool getCurrentPiece(player_t &player, int &number, int currentPos);
|
bool getCurrentPiece(player_t &player, int &number, int currentPos);
|
||||||
|
|
||||||
// 将棋盘下标形式转化为第r圈,第s位,r和s下标都从1开始
|
// 将棋盘下标形式转化为第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开始
|
// 将第c圈,第p位转化为棋盘下标形式,r和s下标都从1开始
|
||||||
static int polarToIndex(int r, int s);
|
static index_t polarToIndex(int r, int s);
|
||||||
|
|
||||||
static void printBoard();
|
static void printBoard();
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ int Game::countPiecesOnBoard()
|
||||||
|
|
||||||
for (int r = 1; r < Board::N_RINGS + 2; r++) {
|
for (int r = 1; r < Board::N_RINGS + 2; r++) {
|
||||||
for (int s = 0; s < Board::N_SEATS; s++) {
|
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) {
|
if (boardLocations[index] & 0x10) {
|
||||||
position.nPiecesOnBoard[1]++;
|
position.nPiecesOnBoard[1]++;
|
||||||
} else if (boardLocations[index] & 0x20) {
|
} 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
|
// 如果局面为“结局”,返回false
|
||||||
if (position.phase == PHASE_GAMEOVER)
|
if (position.phase == PHASE_GAMEOVER)
|
||||||
|
@ -454,7 +454,7 @@ out:
|
||||||
bool Game::_place(int r, int s)
|
bool Game::_place(int r, int s)
|
||||||
{
|
{
|
||||||
// 转换为 index
|
// 转换为 index
|
||||||
int index = Board::polarToIndex(r, s);
|
index_t index = Board::polarToIndex(r, s);
|
||||||
|
|
||||||
return place(index, true);
|
return place(index, true);
|
||||||
}
|
}
|
||||||
|
@ -462,12 +462,12 @@ bool Game::_place(int r, int s)
|
||||||
bool Game::_capture(int r, int s)
|
bool Game::_capture(int r, int s)
|
||||||
{
|
{
|
||||||
// 转换为 index
|
// 转换为 index
|
||||||
int index = Board::polarToIndex(r, s);
|
index_t index = Board::polarToIndex(r, s);
|
||||||
|
|
||||||
return capture(index, 1);
|
return capture(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::capture(int index, int8_t updateCmdlist)
|
bool Game::capture(index_t index, int8_t updateCmdlist)
|
||||||
{
|
{
|
||||||
// 如果局面为"未开局"或“结局”,返回false
|
// 如果局面为"未开局"或“结局”,返回false
|
||||||
if (position.phase & PHASE_NOTPLAYING)
|
if (position.phase & PHASE_NOTPLAYING)
|
||||||
|
@ -605,7 +605,7 @@ out:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::choose(int index)
|
bool Game::choose(index_t index)
|
||||||
{
|
{
|
||||||
// 如果局面不是"中局”,返回false
|
// 如果局面不是"中局”,返回false
|
||||||
if (position.phase != PHASE_MOVING)
|
if (position.phase != PHASE_MOVING)
|
||||||
|
@ -927,7 +927,7 @@ int Game::getMobilityDiff(player_t turn, int nPiecesOnBoard[], bool includeFobid
|
||||||
|
|
||||||
void Game::cleanForbiddenLocations()
|
void Game::cleanForbiddenLocations()
|
||||||
{
|
{
|
||||||
int index = 0;
|
index_t index = 0;
|
||||||
|
|
||||||
for (int r = 1; r <= Board::N_RINGS; r++) {
|
for (int r = 1; r <= Board::N_RINGS; r++) {
|
||||||
for (int s = 0; s < Board::N_SEATS; s++) {
|
for (int s = 0; s < Board::N_SEATS; s++) {
|
||||||
|
@ -1043,7 +1043,7 @@ hash_t Game::getHash()
|
||||||
return position.hash;
|
return position.hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
hash_t Game::updateHash(int index)
|
hash_t Game::updateHash(index_t index)
|
||||||
{
|
{
|
||||||
// PieceType is boardLocations[index]
|
// PieceType is boardLocations[index]
|
||||||
|
|
||||||
|
@ -1056,7 +1056,7 @@ hash_t Game::updateHash(int index)
|
||||||
return position.hash;
|
return position.hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
hash_t Game::revertHash(int index)
|
hash_t Game::revertHash(index_t index)
|
||||||
{
|
{
|
||||||
return updateHash(index);
|
return updateHash(index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,7 +111,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前棋子位置点
|
// 获取当前棋子位置点
|
||||||
int getCurrentIndex() const
|
index_t getCurrentIndex() const
|
||||||
{
|
{
|
||||||
return currentIndex;
|
return currentIndex;
|
||||||
}
|
}
|
||||||
|
@ -242,14 +242,14 @@ public:
|
||||||
|
|
||||||
// 下面几个函数没有算法无关判断和无关操作,节约算法时间
|
// 下面几个函数没有算法无关判断和无关操作,节约算法时间
|
||||||
bool command(int move);
|
bool command(int move);
|
||||||
bool choose(int index);
|
bool choose(index_t index);
|
||||||
bool place(int index, int8_t cp = 0);
|
bool place(index_t index, int8_t cp = 0);
|
||||||
bool capture(int index, int8_t cp = 0);
|
bool capture(index_t index, int8_t cp = 0);
|
||||||
|
|
||||||
// hash 相关
|
// hash 相关
|
||||||
hash_t getHash();
|
hash_t getHash();
|
||||||
hash_t revertHash(int index);
|
hash_t revertHash(index_t index);
|
||||||
hash_t updateHash(int index);
|
hash_t updateHash(index_t index);
|
||||||
hash_t updateHashMisc();
|
hash_t updateHashMisc();
|
||||||
|
|
||||||
public: /* TODO: move to private */
|
public: /* TODO: move to private */
|
||||||
|
@ -288,7 +288,7 @@ public: /* TODO: move to private */
|
||||||
move_t move{};
|
move_t move{};
|
||||||
|
|
||||||
// 选中的棋子在board中的位置
|
// 选中的棋子在board中的位置
|
||||||
int currentIndex{};
|
index_t currentIndex {};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// 棋局哈希值
|
// 棋局哈希值
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
using step_t = uint16_t;
|
using step_t = uint16_t;
|
||||||
using depth_t = uint8_t;
|
using depth_t = uint8_t;
|
||||||
using location_t = uint8_t;
|
using location_t = uint8_t;
|
||||||
|
using index_t = uint8_t;
|
||||||
|
|
||||||
#ifdef TRANSPOSITION_TABLE_CUTDOWN
|
#ifdef TRANSPOSITION_TABLE_CUTDOWN
|
||||||
using hash_t = uint32_t;
|
using hash_t = uint32_t;
|
||||||
|
|
Loading…
Reference in New Issue