diff --git a/NineChess/src/ninechess.cpp b/NineChess/src/ninechess.cpp index 070c9ce2..829c7f03 100644 --- a/NineChess/src/ninechess.cpp +++ b/NineChess/src/ninechess.cpp @@ -169,6 +169,24 @@ NineChess::~NineChess() { } +NineChess::Player NineChess::getOpponent(NineChess::Player player) +{ + switch (player) + { + case PLAYER1: + return PLAYER2; + break; + case PLAYER2: + return PLAYER1; + break; + default: + return NOBODY; + break; + } + + return NOBODY; +} + void NineChess::createMoveTable() { for (int r = 1; r <= N_RINGS; r++) { @@ -509,7 +527,7 @@ bool NineChess::start() } } -bool NineChess::getPieceCP(const Players &player, const int &number, int &c, int &p) +bool NineChess::getPieceCP(const Player &player, const int &number, int &c, int &p) { int piece; @@ -536,7 +554,7 @@ bool NineChess::getPieceCP(const Players &player, const int &number, int &c, int } // 获取当前棋子 -bool NineChess::getCurrentPiece(Players &player, int &number) +bool NineChess::getCurrentPiece(Player &player, int &number) { if (!onBoard[currentPos]) return false; @@ -1219,7 +1237,7 @@ uint64_t NineChess::chessHash() return hash; } -bool NineChess::giveup(Players loser) +bool NineChess::giveup(Player loser) { if (context.stage == GAME_MOVING || context.stage == GAME_PLACING) { if (loser == PLAYER1) { @@ -1554,7 +1572,7 @@ bool NineChess::isAllInMills(char ch) return true; } -bool NineChess::isAllInMills(enum Players player) +bool NineChess::isAllInMills(enum Player player) { char ch = '\x00'; @@ -1618,7 +1636,7 @@ bool NineChess::isAllSurrounded(char ch) } // 判断玩家的棋子是否全部被围 -bool NineChess::isAllSurrounded(enum Players ply) +bool NineChess::isAllSurrounded(enum Player ply) { char t = '\x30'; if (ply == PLAYER1) @@ -1637,7 +1655,7 @@ void NineChess::cleanForbiddenPoints() } } -enum NineChess::Players NineChess::changeTurn() +enum NineChess::Player NineChess::changeTurn() { // 设置轮到谁走 context.turn = (context.turn == PLAYER1) ? PLAYER2 : PLAYER1; @@ -1700,7 +1718,7 @@ void NineChess::setTips() } } -enum NineChess::Players NineChess::getWhosPiece(int c, int p) +enum NineChess::Player NineChess::getWhosPiece(int c, int p) { int pos = cp2pos(c, p); if (board_[pos] & '\x10') diff --git a/NineChess/src/ninechess.h b/NineChess/src/ninechess.h index 050ad8dd..01c96970 100644 --- a/NineChess/src/ninechess.h +++ b/NineChess/src/ninechess.h @@ -110,7 +110,7 @@ public: }; // 玩家标识, 轮流状态, 胜负标识 - enum Players : uint16_t + enum Player : uint16_t { PLAYER1 = 0x0010, // 玩家1 PLAYER2 = 0x0020, // 玩家2 @@ -118,6 +118,8 @@ public: NOBODY = 0x0080 // 胜负未分 }; + static Player getOpponent(enum Player player); + // 动作状态标识 enum Action : uint16_t { @@ -145,7 +147,7 @@ public: enum NineChess::GameStage stage; // 轮流状态标识 - enum NineChess::Players turn; + enum NineChess::Player turn; // 动作状态标识 enum NineChess::Action action; @@ -239,10 +241,10 @@ public: return context.board; } // 获取棋子位置(c, p) - bool getPieceCP(const Players &player, const int &number, int &c, int &p); + bool getPieceCP(const Player &player, const int &number, int &c, int &p); // 获取当前棋子 - bool getCurrentPiece(Players &player, int &number); + bool getCurrentPiece(Player &player, int &number); // 获取当前棋子位置点 int getCurrentPos() const @@ -263,7 +265,7 @@ public: } // 获取轮流状态标识 - enum Players whosTurn() const + enum Player whosTurn() const { return context.turn; } @@ -275,7 +277,7 @@ public: } // 判断胜负 - enum Players whoWin() const + enum Player whoWin() const { return winner; } @@ -289,7 +291,7 @@ public: } // 获取位置点棋子的归属人 - enum Players getWhosPiece(int c, int p); + enum Player getWhosPiece(int c, int p); // 获取当前招法 const char *getCmdLine() const @@ -361,7 +363,7 @@ public: bool capture(int c, int p, long time_p = -1); // 认输 - bool giveup(Players loser); + bool giveup(Player loser); // 命令行解析函数 bool command(const char *cmd); @@ -381,7 +383,7 @@ protected: // 判断玩家的所有棋子是否都处于“三连”状态 bool isAllInMills(char ch); - bool isAllInMills(enum Players); + bool isAllInMills(enum Player); // 判断玩家的棋子是否被围 bool isSurrounded(int pos); @@ -389,7 +391,7 @@ protected: // 判断玩家的棋子是否全部被围 bool isAllSurrounded(char ch); - bool isAllSurrounded(enum Players); + bool isAllSurrounded(enum Player); // 三连加入列表 int addMills(int pos); @@ -410,7 +412,7 @@ protected: void cleanForbiddenPoints(); // 改变轮流 - enum NineChess::Players changeTurn(); + enum NineChess::Player changeTurn(); // 设置提示 void setTips(); @@ -438,7 +440,7 @@ private: int currentPos; // 胜负标识 - enum Players winner; + enum Player winner; // 当前步数 int currentStep; diff --git a/NineChess/src/ninechessai_ab.cpp b/NineChess/src/ninechessai_ab.cpp index cc1f8f19..3c45fa7c 100644 --- a/NineChess/src/ninechessai_ab.cpp +++ b/NineChess/src/ninechessai_ab.cpp @@ -13,10 +13,7 @@ NineChessAi_ab::NineChessAi_ab() : rootNode(nullptr), requiredQuit(false) { - rootNode = new Node; - rootNode->value = 0; - rootNode->move = 0; - rootNode->parent = nullptr; + buildRoot(); } NineChessAi_ab::~NineChessAi_ab() @@ -25,6 +22,14 @@ NineChessAi_ab::~NineChessAi_ab() rootNode = nullptr; } +void NineChessAi_ab::buildRoot() +{ + rootNode = new Node; + rootNode->value = 0; + rootNode->move = 0; + rootNode->parent = nullptr; +} + void NineChessAi_ab::addNode(Node *parent, int value, int move) { Node *newNode = new Node; @@ -41,11 +46,12 @@ unordered_map NineChessAi_ab::hashmap; void NineChessAi_ab::buildChildren(Node *node) { // 如果有子节点,则返回,避免重复建立 - if (node->children.size()) + if (node->children.size()) { return; + } // 临时变量 - char opponent = chessTemp.context.turn == NineChess::PLAYER1 ? 0x20 : 0x10; + NineChess::Player opponent = NineChess::getOpponent(chessTemp.context.turn); // 列出所有合法的下一招 switch (chessTemp.context.action) { diff --git a/NineChess/src/ninechessai_ab.h b/NineChess/src/ninechessai_ab.h index 4e5b8490..12e26655 100644 --- a/NineChess/src/ninechessai_ab.h +++ b/NineChess/src/ninechessai_ab.h @@ -66,6 +66,9 @@ protected: // 清空节点树 void deleteTree(Node *node); + // 构造根节点 + void buildRoot(); + // 增加新节点 void addNode(Node* parent, int value, NineChess::move_t move);