Node结构体增加成员表征局面

This commit is contained in:
CalciteM 2019-06-30 11:59:06 +08:00
parent 810f639f29
commit 7273f29974
3 changed files with 52 additions and 9 deletions

View File

@ -1,10 +1,10 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
//#define DEBUG #define DEBUG
#ifdef DEBUG #ifdef DEBUG
//#define DONOT_PLAY_SOUND #define DONOT_PLAY_SOUND
#define DEBUG_AB_TREE #define DEBUG_AB_TREE
#endif #endif

View File

@ -41,6 +41,10 @@ void NineChessAi_ab::addNode(Node *parent, int value, int move)
newNode->root = rootNode; newNode->root = rootNode;
newNode->stage = chessTemp.context.stage; newNode->stage = chessTemp.context.stage;
newNode->action = chessTemp.context.action; newNode->action = chessTemp.context.action;
newNode->nPiecesInHandDiff = INT_MAX;
newNode->nPiecesOnBoardDiff = INT_MAX;
newNode->nPiecesNeedRemove = INT_MAX;
newNode->result = 0;
#endif #endif
int c, p; int c, p;
char cmd[32] = { 0 }; char cmd[32] = { 0 };
@ -221,6 +225,10 @@ int NineChessAi_ab::evaluate(Node *node)
// 初始评估值为0对先手有利则增大对后手有利则减小 // 初始评估值为0对先手有利则增大对后手有利则减小
int value = 0; int value = 0;
int nPiecesInHandDiff = INT_MAX;
int nPiecesOnBoardDiff = INT_MAX;
int nPiecesNeedRemove = 0;
#if 0 #if 0
int loc_value = 0; int loc_value = 0;
@ -270,10 +278,18 @@ int NineChessAi_ab::evaluate(Node *node)
case NineChess::GAME_PLACING: case NineChess::GAME_PLACING:
// 按手中的棋子计分不要break; // 按手中的棋子计分不要break;
value += (chessContext->nPiecesInHand_1 - chessContext->nPiecesInHand_2) * 50; nPiecesInHandDiff = chessContext->nPiecesInHand_1 - chessContext->nPiecesInHand_2;
value += nPiecesInHandDiff * 50;
#ifdef DEBUG_AB_TREE
node->nPiecesInHandDiff = nPiecesInHandDiff;
#endif
// 按场上棋子计分 // 按场上棋子计分
value += (chessContext->nPiecesOnBoard_1 - chessContext->nPiecesOnBoard_2) * 100; nPiecesOnBoardDiff = chessContext->nPiecesOnBoard_1 - chessContext->nPiecesOnBoard_2;
value += nPiecesOnBoardDiff * 100;
#ifdef DEBUG_AB_TREE
node->nPiecesOnBoardDiff = nPiecesOnBoardDiff;
#endif
switch (chessContext->action) { switch (chessContext->action) {
// 选子和落子使用相同的评价方法 // 选子和落子使用相同的评价方法
@ -282,7 +298,8 @@ int NineChessAi_ab::evaluate(Node *node)
break; break;
// 如果形成去子状态每有一个可去的子算100分 // 如果形成去子状态每有一个可去的子算100分
case NineChess::ACTION_CAPTURE: case NineChess::ACTION_CAPTURE:
value += (chessContext->turn == NineChess::PLAYER1) ? (chessContext->nPiecesNeedRemove) * 100 : -(chessContext->nPiecesNeedRemove) * 100; nPiecesNeedRemove = (chessContext->turn == NineChess::PLAYER1) ? chessContext->nPiecesNeedRemove : -(chessContext->nPiecesNeedRemove);
value += nPiecesNeedRemove * 100;
break; break;
default: default:
break; break;
@ -302,7 +319,11 @@ int NineChessAi_ab::evaluate(Node *node)
// 如果形成去子状态每有一个可去的子算128分 // 如果形成去子状态每有一个可去的子算128分
case NineChess::ACTION_CAPTURE: case NineChess::ACTION_CAPTURE:
value += (chessContext->turn == NineChess::PLAYER1) ? (chessContext->nPiecesNeedRemove) * 128 : -(chessContext->nPiecesNeedRemove) * 128; nPiecesNeedRemove = (chessContext->turn == NineChess::PLAYER1) ? chessContext->nPiecesNeedRemove : -(chessContext->nPiecesNeedRemove);
value += nPiecesNeedRemove * 128;
#ifdef DEBUG_AB_TREE
node->nPiecesNeedRemove = nPiecesNeedRemove;
#endif
break; break;
default: default:
break; break;
@ -318,6 +339,9 @@ int NineChessAi_ab::evaluate(Node *node)
if (chessTemp.currentRule.isStartingPlayerLoseWhenBoardFull) { if (chessTemp.currentRule.isStartingPlayerLoseWhenBoardFull) {
// winner = PLAYER2; // winner = PLAYER2;
value -= 10000; value -= 10000;
#ifdef DEBUG_AB_TREE
node->result = -3;
#endif
} }
else { else {
value = 0; value = 0;
@ -330,18 +354,33 @@ int NineChessAi_ab::evaluate(Node *node)
if (chessTemp.currentRule.isLoseWhenNoWay) { if (chessTemp.currentRule.isLoseWhenNoWay) {
if (chessContext->turn == NineChess::PLAYER1) { if (chessContext->turn == NineChess::PLAYER1) {
value -= 10000; value -= 10000;
#ifdef DEBUG_AB_TREE
node->result = -2;
#endif
} }
else { else {
value += 10000; value += 10000;
#ifdef DEBUG_AB_TREE
node->result = 2;
#endif
} }
} }
} }
// 剩余棋子个数判断 // 剩余棋子个数判断
if (chessContext->nPiecesOnBoard_1 < chessTemp.currentRule.nPiecesAtLeast) if (chessContext->nPiecesOnBoard_1 < chessTemp.currentRule.nPiecesAtLeast) {
value -= 10000; value -= 10000;
else if (chessContext->nPiecesOnBoard_2 < chessTemp.currentRule.nPiecesAtLeast) #ifdef DEBUG_AB_TREE
node->result = -1;
#endif
}
else if (chessContext->nPiecesOnBoard_2 < chessTemp.currentRule.nPiecesAtLeast) {
value += 10000; value += 10000;
#ifdef DEBUG_AB_TREE
node->result = 1;
#endif
}
break; break;
default: default:

View File

@ -51,7 +51,11 @@ public:
bool isTimeout; // 是否遍历到此结点时因为超时而被迫退出 bool isTimeout; // 是否遍历到此结点时因为超时而被迫退出
bool isLeaf; // 是否为叶子结点, 叶子结点是决胜局面 bool isLeaf; // 是否为叶子结点, 叶子结点是决胜局面
NineChess::GameStage stage; // 摆棋阶段还是走棋阶段 NineChess::GameStage stage; // 摆棋阶段还是走棋阶段
NineChess::Action action; NineChess::Action action; // 动作状态
int nPiecesOnBoardDiff; // 场上棋子个数和对手的差值
int nPiecesInHandDiff; // 手中的棋子个数和对手的差值
int nPiecesNeedRemove; // 手中有多少可去的子,如对手有可去的子则为负数
int result; // 终局结果,-1为负0为未到终局1为胜走棋阶段被闷棋则为 -2/2布局阶段闷棋为 -3
struct Node* root; // 根节点 struct Node* root; // 根节点
#endif /* DEBUG_AB_TREE */ #endif /* DEBUG_AB_TREE */
}; };