refactor: position: 去掉 flag

去掉之后对性能几无影响.
This commit is contained in:
CalciteM Team 2019-09-13 19:51:34 +08:00
parent 37fa763508
commit 8e70b82d4f
2 changed files with 17 additions and 34 deletions

View File

@ -116,7 +116,9 @@ bool Position::configure(bool giveUpIfMostLose, bool randomMove)
// 设置棋局状态和棋盘数据,用于初始化
bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int maxTimeLedToLose,
step_t initialStep, int flags, const char *locations,
step_t initialStep,
phase_t phase, player_t turn, action_t action,
const char *locations,
int nPiecesInHand_1, int nPiecesInHand_2, int nPiecesNeedRemove)
{
// 有效性判断
@ -136,37 +138,13 @@ bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int
this->moveStep = initialStep;
// 局面阶段标识
if (flags & PHASE_NOTSTARTED) {
context.phase = PHASE_NOTSTARTED;
} else if (flags & PHASE_PLACING) {
context.phase = PHASE_PLACING;
} else if (flags & PHASE_MOVING) {
context.phase = PHASE_MOVING;
} else if (flags & PHASE_GAMEOVER) {
context.phase = PHASE_GAMEOVER;
} else {
return false;
}
context.phase = phase;
// 轮流状态标识
if (flags & PLAYER_1) {
context.turn = PLAYER_1;
} else if (flags & PLAYER_2) {
context.turn = PLAYER_2;
} else {
return false;
}
context.turn = turn;
// 动作状态标识
if (flags & ACTION_CHOOSE) {
context.action = ACTION_CHOOSE;
} else if (flags & ACTION_PLACE) {
context.action = ACTION_PLACE;
} else if (flags & ACTION_CAPTURE) {
context.action = ACTION_CAPTURE;
} else {
return false;
}
context.action = action;
// 当前棋局3×8
if (locations == nullptr) {
@ -217,7 +195,7 @@ bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int
context.nPiecesInHand_2 = std::min(nPiecesInHand_2, context.nPiecesInHand_2);
// 设置去子状态时的剩余尚待去除子数
if (flags & ACTION_CAPTURE) {
if (action == ACTION_CAPTURE) {
if (0 <= nPiecesNeedRemove && nPiecesNeedRemove < 3) {
context.nPiecesNeedRemove = nPiecesNeedRemove;
}
@ -266,12 +244,15 @@ bool Position::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int
return false;
}
void Position::getContext(struct Rule &rule, step_t &step, int &flags,
int *&locations, int &nPiecesInHand_1, int &nPiecesInHand_2, int &nPiecesNeedRemove)
void Position::getContext(struct Rule &rule, step_t &step,
phase_t &phase, player_t &turn, action_t &action,
int *&locations, int &nPiecesInHand_1, int &nPiecesInHand_2, int &nPiecesNeedRemove)
{
rule = this->currentRule;
step = this->currentStep;
flags = context.phase | context.turn | context.action;
phase = context.phase;
turn = context.turn;
action = context.action;
boardLocations = locations;
nPiecesInHand_1 = context.nPiecesInHand_1;
nPiecesInHand_2 = context.nPiecesInHand_2;

View File

@ -114,7 +114,7 @@ public:
step_t maxStepsLedToDraw = 0, // 限制步数
int maxTimeLedToLose = 0, // 限制时间
step_t initialStep = 0, // 默认起始步数为0
int flags = PHASE_NOTSTARTED | PLAYER_1 | ACTION_PLACE, // 默认状态
phase_t phase = PHASE_NOTSTARTED, player_t turn = PLAYER_1, action_t action = ACTION_PLACE,
const char *locations = nullptr, // 默认空棋盘
int nPiecesInHand_1 = 12, // 玩家1剩余未放置子数
int nPiecesInHand_2 = 12, // 玩家2剩余未放置子数
@ -122,7 +122,9 @@ public:
);
// 获取棋局状态和棋盘上下文
void getContext(struct Rule &rule, step_t &step, int &flags, int *&board,
void getContext(struct Rule &rule, step_t &step,
phase_t &phase, player_t &turn, action_t &action,
int *&board,
int &nPiecesInHand_1, int &nPiecesInHand_2, int &nPiecesNeedRemove);
// 获取当前规则