analyze: 控制台输出棋局分析结果

This commit is contained in:
Calcitem 2020-05-11 01:13:35 +08:00
parent 80bbec96a7
commit 585cdb7b36
5 changed files with 90 additions and 4 deletions

View File

@ -295,6 +295,9 @@ int AIAlgorithm::search(depth_t depth)
// 生成了 Alpha-Beta 树 // 生成了 Alpha-Beta 树
lastvalue = bestvalue;
bestvalue = value;
return 0; return 0;
} }
@ -477,7 +480,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta)
if (nchild == 1 && depth == originDepth) { if (nchild == 1 && depth == originDepth) {
bestMove = extMoves[0].move; bestMove = extMoves[0].move;
bestValue = VALUE_STRONG; bestValue = VALUE_UNIQUE;
return bestValue; return bestValue;
} }

View File

@ -166,6 +166,11 @@ public:
MovePicker *movePicker { nullptr }; MovePicker *movePicker { nullptr };
value_t bestvalue { VALUE_ZERO };
value_t lastvalue { VALUE_ZERO };
depth_t originDepth{ 0 };
private: private:
// 演算用的模型 // 演算用的模型
@ -180,9 +185,6 @@ private:
bool requiredQuit {false}; bool requiredQuit {false};
move_t bestMove { MOVE_NONE }; move_t bestMove { MOVE_NONE };
//value_t bestvalue { VALUE_ZERO };
depth_t originDepth { 0 };
private: private:
// 命令行 // 命令行

View File

@ -27,6 +27,10 @@
using namespace std; using namespace std;
#endif #endif
#if _MSC_VER >= 1600
#pragma execution_character_set("GB2312")
#endif
AiThread::AiThread(int id, QObject *parent) : AiThread::AiThread(int id, QObject *parent) :
QThread(parent), QThread(parent),
state(nullptr), state(nullptr),
@ -135,6 +139,77 @@ void sq2str(char *str)
} }
#endif // OPENING_BOOK #endif // OPENING_BOOK
void AiThread::analyze()
{
int d = (int)ai.originDepth;
int v = (int)ai.bestvalue;
int lv = (int)ai.lastvalue;
bool win = v >= VALUE_WIN;
bool lose = v <= -VALUE_WIN;
int p = v / VALUE_EACH_PIECE;
if (v == VALUE_UNIQUE) {
cout << "唯一着法" << endl << endl << endl;
return;
}
if (lv < -VALUE_EACH_PIECE && v == 0) {
cout << "坏棋, 被拉回均势!" << endl;
}
if (lv < 0 && v > 0) {
cout << "坏棋, 被翻转了局势!" << endl;
}
if (lv == 0 && v > VALUE_EACH_PIECE) {
cout << "败着!" << endl;
}
if (lv > VALUE_EACH_PIECE && v == 0) {
cout << "好棋, 拉回均势!" << endl;
}
if (lv > 0 && v < 0) {
cout << "好棋, 翻转了局势!" << endl;
}
if (lv == 0 && v < -VALUE_EACH_PIECE) {
cout << "秒棋!" << endl;
}
if (lv != v) {
if (lv < 0 && v < 0) {
if (abs(lv) < abs(v)) {
cout << "领先幅度扩大" << endl;
} else if (abs(lv) > abs(v)) {
cout << "领先幅度缩小" << endl;
}
}
if (lv > 0 && v > 0) {
if (abs(lv) < abs(v)) {
cout << "落后幅度扩大" << endl;
} else if (abs(lv) > abs(v)) {
cout << "落后幅度缩小" << endl;
}
}
}
if (win) {
cout << "将在 " << d << " 步后输棋!" << endl;
} else if (lose) {
cout << "将在 " << d << " 步后赢棋!" << endl;
} else if (p == 0) {
cout << "将在 " << d << " 步后双方保持均势" << endl;
} else if (p > 0) {
cout << "将在 " << d << " 步后落后 " << p << "" << endl;
} else if (p < 0) {
cout << "将在 " << d << " 步后领先 " << -p << "" << endl;
}
cout << endl << endl;
}
void AiThread::run() void AiThread::run()
{ {
// 测试用数据 // 测试用数据
@ -147,6 +222,8 @@ void AiThread::run()
loggerDebug("Thread %d start\n", playerId); loggerDebug("Thread %d start\n", playerId);
ai.bestvalue = ai.lastvalue = VALUE_ZERO;
while (!isInterruptionRequested()) { while (!isInterruptionRequested()) {
mutex.lock(); mutex.lock();
@ -189,6 +266,7 @@ void AiThread::run()
strCommand = ai.nextMove(); strCommand = ai.nextMove();
if (strCommand && strcmp(strCommand, "error!") != 0) { if (strCommand && strcmp(strCommand, "error!") != 0) {
loggerDebug("Computer: %s\n\n", strCommand); loggerDebug("Computer: %s\n\n", strCommand);
analyze();
emitCommand(); emitCommand();
} }
} }

View File

@ -78,6 +78,8 @@ public:
return timeLimit; return timeLimit;
} }
void analyze();
public slots: public slots:
// 强制出招,不退出线程 // 强制出招,不退出线程
void act(); void act();

View File

@ -144,6 +144,7 @@ enum value_t : int8_t
VALUE_ZERO = 0, VALUE_ZERO = 0,
VALUE_DRAW = 0, VALUE_DRAW = 0,
VALUE_STRONG = 20, VALUE_STRONG = 20,
VALUE_UNIQUE = 60,
VALUE_WIN = 80, VALUE_WIN = 80,
VALUE_INFINITE = 125, VALUE_INFINITE = 125,
VALUE_UNKNOWN = std::numeric_limits<int8_t>::min(), VALUE_UNKNOWN = std::numeric_limits<int8_t>::min(),