perfect: Integrate Perfect AI to Qt (WIP)

This commit is contained in:
Calcitem 2021-01-23 02:03:51 +08:00
parent 702e0eaae0
commit 726c8a4223
5 changed files with 117 additions and 95 deletions

View File

@ -754,7 +754,6 @@
<ClCompile Include="src\perfect\miniMax_statistics.cpp" />
<ClCompile Include="src\perfect\miniMax_test.cpp" />
<ClCompile Include="src\perfect\minMaxAI.cpp" />
<ClCompile Include="src\perfect\perfect.cpp" />
<ClCompile Include="src\perfect\perfectAI.cpp" />
<ClCompile Include="src\perfect\randomAI.cpp" />
<ClCompile Include="src\perfect\strLib.cpp" />

View File

@ -446,9 +446,6 @@
<ClCompile Include="src\perfect\threadManager.cpp">
<Filter>Perfect AI Files</Filter>
</ClCompile>
<ClCompile Include="src\perfect\perfect.cpp">
<Filter>Perfect AI Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="millgame.rc">

View File

@ -1,91 +0,0 @@
#include <cstdio>
#include <iostream>
#include <windows.h>
#include "mill.h"
#include "miniMaxAI.h"
#include "randomAI.h"
#include "perfectAI.h"
using namespace std;
unsigned int startTestFromLayer = 0;
unsigned int endTestAtLayer = NUM_LAYERS - 1;
#ifdef _DEBUG
char databaseDirectory[] = "D:\\database";
#elif _RELEASE_X64
char databaseDirectory[] = "";
#endif
Mill *mill;
PerfectAI *ai;
int perfect_init(void)
{
mill = new Mill();
ai = new PerfectAI(databaseDirectory);
ai->setDatabasePath(databaseDirectory);
return 0;
}
int perfect_main(void)
{
// locals
bool playerOneHuman = false;
bool playerTwoHuman = true;
char ch[100];
unsigned int from, to;
mill->beginNewGame(ai, ai, fieldStruct::playerOne);
// play
do {
mill->getComputersChoice(&from, &to);
cout << "\nlast move was from " << (char)(mill->getLastMoveFrom() + 'a') << " to " << (char)(mill->getLastMoveTo() + 'a') << "\n\n";
mill->printBoard();
// Human
if ((mill->getCurrentPlayer() == fieldStruct::playerOne && playerOneHuman) ||
(mill->getCurrentPlayer() == fieldStruct::playerTwo && playerTwoHuman)) {
do {
// Show text
if (mill->mustStoneBeRemoved())
cout << "\n Which stone do you want to remove? [a-x]: \n\n\n";
else if (mill->inSettingPhase())
cout << "\n Where are you going? [a-x]: \n\n\n";
else
cout << "\n Your train? [a-x][a-x]: \n\n\n";
// get input
cin >> ch;
if ((ch[0] >= 'a') && (ch[0] <= 'x'))
from = ch[0] - 'a';
else
from = fieldStruct::size;
if (mill->inSettingPhase()) {
if ((ch[0] >= 'a') && (ch[0] <= 'x'))
to = ch[0] - 'a';
else
to = fieldStruct::size;
} else {
if ((ch[1] >= 'a') && (ch[1] <= 'x'))
to = ch[1] - 'a';
else
to = fieldStruct::size;
}
} while (mill->doMove(from, to) == false);
// Computer
}
} while (mill->getWinner() == 0);
return 0;
}

View File

@ -58,6 +58,8 @@ Thread::Thread(size_t n
idx(n), stdThread(&Thread::idle_loop, this),
timeLimit(3600)
{
perfect_init();
wait_for_search_finished();
}
@ -693,3 +695,101 @@ void ThreadPool::start_thinking(Position *pos, bool ponderMode)
main()->start_searching();
}
////////////////////////////////////////////////////////////////////////////
int Thread::perfect_init(void)
{
#ifdef _DEBUG
char databaseDirectory[] = "D:\\database";
#elif _RELEASE_X64
char databaseDirectory[] = "";
#endif
mill = new Mill();
ai = new PerfectAI(databaseDirectory);
ai->setDatabasePath(databaseDirectory);
mill->beginNewGame(ai, ai, fieldStruct::playerOne);
return 0;
}
Square Thread::perfect_sq_to_sq(unsigned int sq)
{
Square map[] = {
SQ_31, SQ_24, SQ_25, SQ_23, SQ_16, SQ_17, SQ_15, SQ_8,
SQ_9, SQ_30, SQ_22, SQ_14, SQ_10, SQ_18, SQ_26, SQ_13,
SQ_12, SQ_11, SQ_21, SQ_20, SQ_19, SQ_29, SQ_28, SQ_27,
SQ_0 };
return map[sq];
}
Move Thread::perfect_move_to_move(unsigned int from, unsigned int to)
{
if (mill->mustStoneBeRemoved())
return (Move)-perfect_sq_to_sq(to);
else if (mill->inSettingPhase())
return (Move)perfect_sq_to_sq(to);
else
return (Move)(make_move(perfect_sq_to_sq(from), perfect_sq_to_sq(to)));
}
unsigned Thread::sq_to_perfect_sq(Square sq)
{
int map[] = {
-1, -1, -1, -1, -1, -1, -1, -1,
7, 8, 12, 17, 16, 15, 11, 6, /* 8 - 15 */
4, 5, 13, 20, 19, 18, 10, 3, /* 16 - 23 */
1, 2, 14, 23, 22, 21, 9, 0, /* 24 - 31 */
-1, -1, -1, -1, -1, -1, -1, -1,
};
return map[sq];
}
void Thread::move_to_perfect_move(Move move, unsigned int &from, unsigned int &to)
{
Square f = from_sq(move);
Square t = to_sq(move);
if (mill->mustStoneBeRemoved()) {
from = fieldStruct::size;
to = sq_to_perfect_sq(t);
} else if (mill->inSettingPhase()) {
from = fieldStruct::size;
to = sq_to_perfect_sq(t);
} else {
from = sq_to_perfect_sq(f);
to = sq_to_perfect_sq(t);
}
}
int Thread::perfect_search()
{
unsigned int from, to;
mill->getComputersChoice(&from, &to);
cout << "\nlast move was from " << (char)(mill->getLastMoveFrom() + 'a') << " to " << (char)(mill->getLastMoveTo() + 'a') << "\n\n";
mill->printBoard();
bestMove = perfect_move_to_move(mill->getLastMoveFrom(), mill->getLastMoveTo());
return 0;
}
bool Thread::perfect_do_move(Move move)
{
bool ret;
unsigned int from, to;
move_to_perfect_move(move, from, to);
ret = mill->doMove(from, to);
return ret;
}
// mill->getWinner() == 0
// mill->getCurrentPlayer() == fieldStruct::playerTwo

View File

@ -29,7 +29,11 @@
#include "search.h"
#include "thread_win32_osx.h"
#include "perfect/mill.h"
#include "perfect/perfectAI.h"
#include "config.h"
#ifdef QT_GUI_LIB
#include <QObject>
#endif // QT_GUI_LIB
@ -50,6 +54,10 @@ public:
bool exit = false, searching = true; // Set before starting std::thread
NativeThread stdThread;
// Perfect AI
Mill *mill;
PerfectAI *ai;
explicit Thread(size_t n
#ifdef QT_GUI_LIB
, QObject *parent = nullptr
@ -84,6 +92,15 @@ public:
void analyze(Color c);
// Perfect AI
int perfect_init(void);
Square perfect_sq_to_sq(unsigned int sq);
Move perfect_move_to_move(unsigned int from, unsigned int to);
unsigned sq_to_perfect_sq(Square sq);
void move_to_perfect_move(Move move, unsigned int &from, unsigned int &to);
int perfect_search();
bool perfect_do_move(Move move);
#ifdef TIME_STAT
TimePoint sortTime{ 0 };
#endif