perfect: Integrate Perfect AI to Qt (WIP)

This commit is contained in:
Calcitem 2021-01-23 12:02:11 +08:00
parent 920ad3f592
commit e58ad0f63a
5 changed files with 49 additions and 14 deletions

View File

@ -14,7 +14,7 @@ int perfect_init(void)
char databaseDirectory[] = ""; char databaseDirectory[] = "";
#endif #endif
if (mill != nullptr) { if (mill != nullptr || ai != nullptr) {
return 0; return 0;
} }
@ -26,6 +26,33 @@ int perfect_init(void)
return 0; return 0;
} }
int perfect_exit(void)
{
if (mill == nullptr) {
return 0;
}
delete mill;
mill = nullptr;
if (ai == nullptr) {
return 0;
}
delete ai;
ai = nullptr;
return 0;
}
int perfect_reset(void)
{
perfect_exit();
perfect_init();
return 0;
}
Square from_perfect_sq(unsigned int sq) Square from_perfect_sq(unsigned int sq)
{ {
Square map[] = { Square map[] = {
@ -72,11 +99,12 @@ void to_perfect_move(Move move, unsigned int &from, unsigned int &to)
{ {
Square f = from_sq(move); Square f = from_sq(move);
Square t = to_sq(move); Square t = to_sq(move);
MoveType type = type_of(move);
if (mill->mustStoneBeRemoved()) { if (type == MOVETYPE_REMOVE) {
from = fieldStruct::size; from = to_perfect_sq(t);
to = to_perfect_sq(t); to = fieldStruct::size;
} else if (mill->inSettingPhase()) { } else if (type == MOVETYPE_PLACE) {
from = fieldStruct::size; from = fieldStruct::size;
to = to_perfect_sq(t); to = to_perfect_sq(t);
} else { } else {
@ -99,6 +127,8 @@ Move perfect_search()
//sync_cout << "========================" << sync_endl; //sync_cout << "========================" << sync_endl;
mill->getComputersChoice(&from, &to); mill->getComputersChoice(&from, &to);
assert(!(from == 24 && to == 24));
ret = mill->doMove(from, to); ret = mill->doMove(from, to);
assert(ret == true); assert(ret == true);
@ -129,11 +159,6 @@ bool perfect_do_move(Move move)
return ret; return ret;
} }
bool perfect_reset()
{
return false;
}
bool perfect_command(const char *cmd) bool perfect_command(const char *cmd)
{ {
unsigned int ruleNo = 0; unsigned int ruleNo = 0;
@ -156,20 +181,22 @@ bool perfect_command(const char *cmd)
if (args >= 4) { if (args >= 4) {
move = make_move(make_square(file1, rank1), make_square(file2, rank2)); move = make_move(make_square(file1, rank1), make_square(file2, rank2));
return perfect_do_move(move);
} }
args = sscanf(cmd, "-(%1u,%1u)", (unsigned *)&file1, (unsigned *)&rank1); args = sscanf(cmd, "-(%1u,%1u)", (unsigned *)&file1, (unsigned *)&rank1);
if (args >= 2) { if (args >= 2) {
move = (Move)-make_move(SQ_0, make_square(file1, rank1)); move = (Move)-make_move(SQ_0, make_square(file1, rank1));
return perfect_do_move(move);
} }
args = sscanf(cmd, "(%1u,%1u)", (unsigned *)&file1, (unsigned *)&rank1); args = sscanf(cmd, "(%1u,%1u)", (unsigned *)&file1, (unsigned *)&rank1);
if (args >= 2) { if (args >= 2) {
move = make_move(SQ_0, make_square(file1, rank1)); move = make_move(SQ_0, make_square(file1, rank1));
return perfect_do_move(move);
} }
return perfect_do_move(move); return false;
args = sscanf(cmd, "Player%1u give up!", &t); args = sscanf(cmd, "Player%1u give up!", &t);

View File

@ -11,6 +11,8 @@ extern PerfectAI *ai;
// Perfect AI // Perfect AI
int perfect_init(void); int perfect_init(void);
int perfect_exit(void);
int perfect_reset(void);
Square from_perfect_sq(unsigned int sq); Square from_perfect_sq(unsigned int sq);
Move from_perfect_move(unsigned int from, unsigned int to); Move from_perfect_move(unsigned int from, unsigned int to);
unsigned to_perfect_sq(Square sq); unsigned to_perfect_sq(Square sq);

View File

@ -30,6 +30,8 @@
#include "misc.h" #include "misc.h"
#include "test.h" #include "test.h"
#include "perfect/perfect.h"
#ifdef TEST_MODE #ifdef TEST_MODE
#ifdef QT_GUI_LIB #ifdef QT_GUI_LIB
QString getAppFileName(); QString getAppFileName();
@ -216,6 +218,9 @@ void Test::readFromMemory()
memset(to, 0, SHARED_MEMORY_SIZE); memset(to, 0, SHARED_MEMORY_SIZE);
sharedMemory.unlock(); sharedMemory.unlock();
readStr = str; readStr = str;
#ifdef PERFECT_AI
perfect_command(str.toStdString().c_str());
#endif
emit command(str.toStdString()); emit command(str.toStdString());
} }
} }

View File

@ -60,8 +60,6 @@ Thread::Thread(size_t n
idx(n), stdThread(&Thread::idle_loop, this), idx(n), stdThread(&Thread::idle_loop, this),
timeLimit(3600) timeLimit(3600)
{ {
perfect_init();
wait_for_search_finished(); wait_for_search_finished();
} }

View File

@ -195,6 +195,9 @@ void Game::gameReset()
moveHistory.emplace_back(bak); moveHistory.emplace_back(bak);
} }
#ifdef PERFECT_AI
perfect_reset();
#endif
position.reset(); position.reset();
elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0; elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0;
sideToMove = position.side_to_move(); sideToMove = position.side_to_move();