From 156770281b27606a145808fef0179dfaa5345f3f Mon Sep 17 00:00:00 2001 From: Breakwa11 Date: Mon, 29 Oct 2018 17:40:02 +0800 Subject: [PATCH] so many problems fixed --- gtp4zen/gtp.h | 2 + gtp4zen/gtp4zen.cpp | 98 +++++++++++++++++-------- gtp4zen/gtp4zen.vcxproj.user | 12 ---- gtp4zen/stdafx.h | 6 +- gtp4zen/zen6gtp.cpp | 134 +++++++++++++++++++++++++---------- gtp4zen/zen6gtp.h | 3 +- gtp4zen/zen7gtp.cpp | 134 ++++++++++++++++++++++++++--------- gtp4zen/zen7gtp.h | 3 +- 8 files changed, 275 insertions(+), 117 deletions(-) delete mode 100644 gtp4zen/gtp4zen.vcxproj.user diff --git a/gtp4zen/gtp.h b/gtp4zen/gtp.h index a3d0a47..22cd203 100644 --- a/gtp4zen/gtp.h +++ b/gtp4zen/gtp.h @@ -23,6 +23,8 @@ class CGtp virtual std::string genmove(std::string color) = 0; virtual std::string time_settings(int main_time, int byo_yomi_time, int byo_yomi_stones) = 0; virtual std::string time_left(std::string color, int time, int stones) = 0; + virtual std::string score() = 0; + virtual float get_winrate() = 0; }; #endif diff --git a/gtp4zen/gtp4zen.cpp b/gtp4zen/gtp4zen.cpp index ad920b9..e70d04b 100644 --- a/gtp4zen/gtp4zen.cpp +++ b/gtp4zen/gtp4zen.cpp @@ -14,12 +14,17 @@ using namespace boost::program_options; static void _play_zen(CGtp *pgtp); int g_zenver = 7; -int g_threads = 4; -int g_maxtime = 10; // Ã룬ĬÈÏ10Ãë +int g_threads = 1; +int g_maxtime = 60; // Ã룬ĬÈÏ60Ãë int g_strength = 10000; // ²½Êý£¬Ä¬ÈÏ10000²½ bool g_logfilenametime = false; bool g_debug = false; -int g_think_interval = 1000; +int g_think_interval = 200; +float g_think_level_1 = 1.0; +float g_think_level_2 = 1.0; +int g_think_level_0 = 1; +int g_resign = 10; + #ifdef _DEBUG string g_logfile = "gtp4zen_log.txt"; #else @@ -44,11 +49,6 @@ inline std::string GetModuleFilePath() int _tmain(int argc, _TCHAR* argv[]) { - fprintf(stderr, "gtp4zen by yongjian(QQ group:14501533, mail:80101277@qq.com)\n"\ - "www.weiqiba.com(%s, compile: %s)\n" - , GTP4ZEN_VERSION, get_compile_strtime().c_str()); - fprintf(stderr, "Usage: gtp4zen -z 7 -t 4 -T 10 -s 10000 -l mylog.txt -L -d -i 1000\n"); - SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); g_threads = sysInfo.dwNumberOfProcessors; @@ -62,7 +62,11 @@ int _tmain(int argc, _TCHAR* argv[]) ("threads,t", value(), "Set the number of threads to use. (default CPU_CORES)") ("maxtime,T", value(), "Set the max time for one move. (default 10 seconds)") ("strength,s", value(), "Set the playing strength. (default 10000)") - ("ithink,i", value(), "thinking interval, only set 100 when play cgos. (default 1000)") + ("ithink,i", value(), "thinking interval, only set 100 when play cgos. (default 100)") + ("ilevel0,n", value(), "factor0. (default 1)") + ("ilevel1,o", value(), "factor1. (default 1)") + ("ilevel2,p", value(), "factor2. (default 1)") + ("resign,r", value(), "resign. (default 10)") ("logfile,l", value(), "Enable logging and set the log file. (default none)") ("logfilenametime,L", "Add timestamp after log filename. (default off)") ("debug,d", "Enable debug output to gtp shell. (default off)") @@ -119,9 +123,21 @@ int _tmain(int argc, _TCHAR* argv[]) if (vm.count("ithink")) { g_think_interval = vm["ithink"].as(); if (g_think_interval <= 0) { - g_think_interval = 1000; + g_think_interval = 200; } } + if (vm.count("ilevel0")) { + g_think_level_0 = vm["ilevel0"].as(); + } + if (vm.count("ilevel1")) { + g_think_level_1 = vm["ilevel1"].as(); + } + if (vm.count("ilevel2")) { + g_think_level_2 = vm["ilevel2"].as(); + } + if (vm.count("resign")) { + g_resign = vm["resign"].as(); + } if (vm.count("logfile")) { g_logfile = vm["logfile"].as(); } @@ -194,7 +210,7 @@ int _tmain(int argc, _TCHAR* argv[]) if (!boost::filesystem::is_regular_file(zen_dll_path.c_str())) { fprintf(stderr, "ERROR: zen.dll not exist?\n"); logprintf(L"%s", L"ERROR: zen.dll not exist?"); - system("pause"); + //system("pause"); return 0; } @@ -218,6 +234,7 @@ int _tmain(int argc, _TCHAR* argv[]) static void _play_zen(CGtp *pgtp) { std::string result; + std::string info_result; while (true) { std::string line; std::getline(cin, line); @@ -225,11 +242,10 @@ static void _play_zen(CGtp *pgtp) boost::trim_if(line, boost::is_any_of("\r\n\t ")); std::vector list; boost::split(list, line, boost::is_any_of("\r\n\t "), boost::token_compress_on); - //for (auto &item : list) { - // boost::trim_if(item, boost::is_any_of("\r\n\t ")); - //} - + if (line.size() == 0) + continue; result = ""; + info_result = ""; if (0 == list.size()) { continue; } else if ("list_commands" == list[0]) { @@ -243,6 +259,8 @@ static void _play_zen(CGtp *pgtp) } else if ("boardsize" == list[0] && list.size() >= 2) { result = pgtp->boardsize(atoi(list[1].c_str())); } else if ("quit" == list[0]) { + fprintf(stdout, "=\n\n"); + fflush(stdout); break; } else if ("clear_board" == list[0]) { result = pgtp->clear_board(); @@ -252,6 +270,14 @@ static void _play_zen(CGtp *pgtp) result = pgtp->play(list[1].c_str(), list[2].c_str()); } else if ("genmove" == list[0] && list.size() >= 2) { // genmove b result = pgtp->genmove(list[1].c_str()); + float winrate = pgtp->get_winrate(); + if (winrate > -1 && winrate < 2 && result != "pass") { + char buff[1024]; + if (list[1] == "W" || list[1] == "w") + winrate = 1 - winrate; + sprintf_s(buff, "Black winrate: %.2f", winrate * 100); + info_result = buff; + } } else if ("place_free_handicap" == list[0] && list.size() >= 2) { std::vector posarray; std::copy(list.begin() + 1, list.end(), posarray.begin()); @@ -276,13 +302,21 @@ static void _play_zen(CGtp *pgtp) , atoi(list[2].c_str()) , atoi(list[3].c_str()) ); + } else if ("final_score" == list[0]) { + result = pgtp->score(); } else { - result = "? unknown command\n"; + result = "? unknown command [" + line + "]\n"; } fprintf(stdout, "%s\n", result.c_str()); //logprintf(L"command: %s", line.c_str()); //logprintf(L"result: %s", result.c_str()); fflush(stdout); + if (info_result.size() > 0) + { + int ret = fprintf(stderr, "%s\n", info_result.c_str()); + if (ret > 0) + fflush(stderr); + } } } @@ -318,21 +352,23 @@ void logprintf(const TCHAR *_Format, ...) // дÎļþ ofstream out; - out.open(g_logfile.c_str(), ios::app | ios::out | ios::binary); - if (out.is_open()) { - SYSTEMTIME sys; - GetLocalTime(&sys); - TCHAR timestr[128] = L""; - wsprintf(timestr - , L"%d-%02d-%02d %02d:%02d:%02d.%04d | " - , sys.wYear, sys.wMonth, sys.wDay - , sys.wHour, sys.wMinute, sys.wSecond - , sys.wMilliseconds - ); - out << CWtoA(timestr); - out << CWtoA(pszBuffer); - out << "\r\n"; - out.close(); + if (g_logfile.size() > 0) { + out.open(g_logfile.c_str(), ios::app | ios::out | ios::binary); + if (out.is_open()) { + SYSTEMTIME sys; + GetLocalTime(&sys); + TCHAR timestr[128] = L""; + wsprintf(timestr + , L"%d-%02d-%02d %02d:%02d:%02d.%04d | " + , sys.wYear, sys.wMonth, sys.wDay + , sys.wHour, sys.wMinute, sys.wSecond + , sys.wMilliseconds + ); + out << CWtoA(timestr); + out << CWtoA(pszBuffer); + out << "\r\n"; + out.close(); + } } } diff --git a/gtp4zen/gtp4zen.vcxproj.user b/gtp4zen/gtp4zen.vcxproj.user deleted file mode 100644 index 0596056..0000000 --- a/gtp4zen/gtp4zen.vcxproj.user +++ /dev/null @@ -1,12 +0,0 @@ - - - - $(SolutionDir)/bin/debug - WindowsLocalDebugger - -l log.txt -L -d - - - $(SolutionDir)/bin/release - WindowsLocalDebugger - - \ No newline at end of file diff --git a/gtp4zen/stdafx.h b/gtp4zen/stdafx.h index b46dbb5..ac2455c 100644 --- a/gtp4zen/stdafx.h +++ b/gtp4zen/stdafx.h @@ -9,7 +9,6 @@ #include #include -#include // TODO: reference additional headers your program requires here @@ -25,7 +24,6 @@ using namespace boost::gregorian; using namespace boost::posix_time; #include "win32xx.h" -#include "xtrace.h" #include "lua/lua.hpp" extern int g_zenver; @@ -35,6 +33,10 @@ extern int g_strength; extern string g_logfile; extern bool g_debug; extern int g_think_interval; +extern float g_think_level_1; +extern float g_think_level_2; +extern int g_think_level_0; +extern int g_resign; extern int __ansi2num(char ch); extern std::string __num2ansi(int x, int y, int boardsize); diff --git a/gtp4zen/zen6gtp.cpp b/gtp4zen/zen6gtp.cpp index 3bef314..4ea945a 100644 --- a/gtp4zen/zen6gtp.cpp +++ b/gtp4zen/zen6gtp.cpp @@ -8,8 +8,12 @@ #endif typedef void(LIBZENAPI *_ZenClearBoard)(void); -typedef int(LIBZENAPI *_ZenGetNextColor)(void); +typedef int(LIBZENAPI *_ZenGetBoardColor)(int, int); +typedef void(LIBZENAPI *_ZenGetTerritoryStatictics)(int(*const)[19]); typedef void(LIBZENAPI *_ZenGetTopMoveInfo)(int, int &, int &, int &, float &, char *, int); +typedef int(LIBZENAPI *_ZenGetNextColor)(void); +typedef int(LIBZENAPI *_ZenGetNumBlackPrisoners)(void); +typedef int(LIBZENAPI *_ZenGetNumWhitePrisoners)(void); typedef void(LIBZENAPI *_ZenInitialize)(char const *); typedef bool(LIBZENAPI *_ZenIsInitialized)(void); typedef bool(LIBZENAPI *_ZenIsThinking)(void); @@ -31,25 +35,27 @@ struct _zen_dll_proxy { // ÄÚ²¿±äÁ¿ HMODULE m_hModule; std::string m_dllpath; - int m_boardsize; + int m_boardsize; + float m_komi; ptime m_maintime; ptime m_lefttime; lua_State *m_L; - int m_curmovenum; + int m_curmovenum; std::string winrate; + float m_winrate; // µ¼³öº¯Êý - //ZenAddStone ZenAddStone1; + //ZenAddStone ZenAddStone; _ZenClearBoard ZenClearBoard; //void (*ZenFixedHandicap)(int); //void (*ZenGetBestMoveRate)(void); - //void (*ZenGetBoardColor)(int, int); + _ZenGetBoardColor ZenGetBoardColor; //void (*ZenGetHistorySize)(void); _ZenGetNextColor ZenGetNextColor; - //void (*ZenGetNumBlackPrisoners)(void); - //void (*ZenGetNumWhitePrisoners)(void); + _ZenGetNumBlackPrisoners ZenGetNumBlackPrisoners; + _ZenGetNumWhitePrisoners ZenGetNumWhitePrisoners; //void (*ZenGetPriorKnowledge)(int(*const)[19]); - //void (*ZenGetTerritoryStatictics)(int(*const)[19]); + _ZenGetTerritoryStatictics ZenGetTerritoryStatictics; _ZenGetTopMoveInfo ZenGetTopMoveInfo; _ZenInitialize ZenInitialize; _ZenIsInitialized ZenIsInitialized; @@ -87,6 +93,56 @@ CZen6Gtp::~CZen6Gtp() unload(); } +float CZen6Gtp::get_winrate() +{ + return ((_zen_dll_proxy*)m_proxy)->m_winrate;; +} + +std::string CZen6Gtp::score() +{ + int territory[19][19]; + ((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics(territory); + float moku_w = ((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners(), moku_b = ((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners(); + for (int j = 0; j < ((_zen_dll_proxy*)m_proxy)->m_boardsize; ++j) + { + for (int i = 0; i < ((_zen_dll_proxy*)m_proxy)->m_boardsize; ++i) + { + int color = ((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor(i, j); + if (territory[j][i] <= -500) + { + if (color == 2) + moku_w += 2; + else if (color == 0) + moku_w += 1; + } + else if (territory[j][i] >= 500) + { + if (color == 1) + moku_b += 2; + else if (color == 0) + moku_b += 1; + } + } + } + moku_w += ((_zen_dll_proxy*)m_proxy)->m_komi; + if (moku_b > moku_w) + { + char result[100]; + sprintf_s(result, "= B+%g\n", moku_b - moku_w); + return result; + } + else if (moku_b < moku_w) + { + char result[100]; + sprintf_s(result, "= W+%g\n", moku_w - moku_b); + return result; + } + else + { + return "= Draw\n"; + } +} + // cur_move_num£ºµ±Ç°ÊÖÊý£¬time_left£ºÊ£Óàʱ¼ä int CZen6Gtp::lua_genmove_calctime(int cur_move_num, int time_left) { @@ -105,22 +161,6 @@ int CZen6Gtp::lua_genmove_calctime(int cur_move_num, int time_left) return result; } -// -float CZen6Gtp::lua_komi_get() -{ - // Ö´Ðгõʼ»¯º¯Êý£¬×¢Òâƽºâ¶ÑÕ»£¬lua_gettop(m_L)¿ÉÒÔ»ñÈ¡µ±Ç°Õ»Ê¹Óà - int ret = lua_getglobal(((_zen_dll_proxy*)m_proxy)->m_L, "komi"); - float result = -1; - if (ret > 0) { - lua_pcall(((_zen_dll_proxy*)m_proxy)->m_L, 2, 1, 0); - result = (float)luaL_checknumber(((_zen_dll_proxy*)m_proxy)->m_L, 1); - } else { - lua_pop(((_zen_dll_proxy*)m_proxy)->m_L, 1); - } - lua_settop(((_zen_dll_proxy*)m_proxy)->m_L, 0); - return result; -} - bool CZen6Gtp::load(std::string zen_dll_path, std::string lua_engine_path) { #if 0 @@ -137,6 +177,7 @@ bool CZen6Gtp::load(std::string zen_dll_path, std::string lua_engine_path) unload(); m_proxy = new _zen_dll_proxy(); + ((_zen_dll_proxy*)m_proxy)->m_komi = 6.5f; ((_zen_dll_proxy*)m_proxy)->m_L = luaL_newstate(); luaL_openlibs(((_zen_dll_proxy*)m_proxy)->m_L); luaL_dostring(((_zen_dll_proxy*)m_proxy)->m_L, "package.path = package.path .. ';./?.lua'"); @@ -155,10 +196,20 @@ bool CZen6Gtp::load(std::string zen_dll_path, std::string lua_engine_path) HMODULE hModule = ((_zen_dll_proxy*)m_proxy)->m_hModule; ((_zen_dll_proxy*)m_proxy)->ZenClearBoard = (_ZenClearBoard)GetProcAddress(hModule, "?ZenClearBoard@@YAXXZ"); assert(((_zen_dll_proxy*)m_proxy)->ZenClearBoard); + ((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor = (_ZenGetBoardColor)GetProcAddress(hModule, "?ZenGetBoardColor@@YAHHH@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor); ((_zen_dll_proxy*)m_proxy)->ZenGetNextColor = (_ZenGetNextColor)GetProcAddress(hModule, "?ZenGetNextColor@@YAHXZ"); assert(((_zen_dll_proxy*)m_proxy)->ZenGetNextColor); + ((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics = (_ZenGetTerritoryStatictics)GetProcAddress(hModule, "?ZenGetTerritoryStatictics@@YAXQAY0BD@H@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics); + ((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo = (_ZenGetTopMoveInfo)GetProcAddress(hModule, "?ZenGetTopMoveInfo@@YAXHAAH00AAMPADH@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo); ((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo = (_ZenGetTopMoveInfo)GetProcAddress(hModule, "?ZenGetTopMoveInfo@@YAXHAAH00AAMPADH@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo); + ((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners = (_ZenGetNumBlackPrisoners)GetProcAddress(hModule, "?ZenGetNumBlackPrisoners@@YAHXZ"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners); + ((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners = (_ZenGetNumWhitePrisoners)GetProcAddress(hModule, "?ZenGetNumWhitePrisoners@@YAHXZ"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners); ((_zen_dll_proxy*)m_proxy)->ZenInitialize = (_ZenInitialize)GetProcAddress(hModule, "?ZenInitialize@@YAXPBD@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenInitialize); ((_zen_dll_proxy*)m_proxy)->ZenIsInitialized = (_ZenIsInitialized)GetProcAddress(hModule, "?ZenIsInitialized@@YA_NXZ"); @@ -217,8 +268,6 @@ bool CZen6Gtp::load(std::string zen_dll_path, std::string lua_engine_path) fprintf(stderr, "zen.dll(zen6) initialize %s.\n", is_init ? "success" : "failed"); logprintf(L"zen.dll(zen7) initialize %s.", is_init ? L"success" : L"failed"); fflush(stderr); - if (!g_debug) - fclose(stderr); return is_init; } @@ -260,7 +309,7 @@ std::string CZen6Gtp::list_commands() std::string CZen6Gtp::name() { - return "= Gtp4Zen(zen7)\n"; + return "= Gtp4Zen(zen6)\n"; } std::string CZen6Gtp::version() @@ -298,13 +347,9 @@ std::string CZen6Gtp::boardsize(int size) std::string CZen6Gtp::komi(float k) { logprintf(L"komi(%.2f)", k); - if (lua_komi_get() >= 0) { - logprintf(L"\tlua set komi: %f", lua_komi_get()); - ((_zen_dll_proxy*)m_proxy)->ZenSetKomi(lua_komi_get()); - return "= \n"; - } if (k >= 0 && k <= 300) { + ((_zen_dll_proxy*)m_proxy)->m_komi = k; ((_zen_dll_proxy*)m_proxy)->ZenSetKomi(k); return "= \n"; } else { @@ -378,6 +423,7 @@ std::string CZen6Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati int N = -1; char S[256]; // È«²¿ÊÖÊý£¨º¬µ±Ç°ÊÖ£© #if 1 + std::string result = "pass"; for (int i = 4; i >= 0; i--) { // zenÒ»°ã¸ø³ö5¸öÑ¡µã ((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo(i, x, y, simulation, W, S, 99); //XTRACE("%d: %d-%d, P:%d, W:%f, %s\n", i, x, y, simulation, W, S); @@ -399,17 +445,28 @@ std::string CZen6Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati fflush(stderr); } if (0 == i) { - if (strstr(S, "pass")) + if (strstr(S, "pass") == S) { return "pass"; + } + + if (W > -1 && W * 100 < g_resign) { + result = "resign"; + return result; + } - std::string result; if (x >= 0 && y >= 0 && simulation >= 0) { result = __num2ansi(x, y, ((_zen_dll_proxy*)m_proxy)->m_boardsize); } else { - result = "resign"; + //result = "resign"; result = "pass"; } return result; + } else { + if (strstr(S, "pass") != S) { + if (x >= 0 && y >= 0 && simulation >= 0) { + result = __num2ansi(x, y, ((_zen_dll_proxy*)m_proxy)->m_boardsize); + } + } } } #else @@ -423,7 +480,7 @@ std::string CZen6Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati //} //return result; #endif - return ""; + return "pass"; } // Ö¸¶¨×î´óʱ¼ä£¨ºÁÃ룩£¬×î¶à²½Êý @@ -436,6 +493,10 @@ std::string CZen6Gtp::__genmove(std::string _color, int _maxtime, int _strength) int color = ("w" == _color ? GTP4ZEN_COLOR_WHITE : GTP4ZEN_COLOR_BLACK); // Âä×Ó + ((_zen_dll_proxy*)m_proxy)->ZenSetNumberOfSimulations(_strength); + ((_zen_dll_proxy*)m_proxy)->ZenSetAmafWeightFactor(g_think_level_1); + ((_zen_dll_proxy*)m_proxy)->ZenSetPriorWeightFactor(g_think_level_2); + ((_zen_dll_proxy*)m_proxy)->ZenSetDCNN(g_think_level_0 != 0); ((_zen_dll_proxy*)m_proxy)->ZenStartThinking(color); ptime time_start, time_now; millisec_posix_time_system_config::time_duration_type time_elapse; @@ -488,6 +549,7 @@ std::string CZen6Gtp::__genmove(std::string _color, int _maxtime, int _strength) play(_color, result); char winrate_str[32]; sprintf_s(winrate_str, 32, "%.2f", W * 100); + ((_zen_dll_proxy*)m_proxy)->m_winrate = W; ((_zen_dll_proxy*)m_proxy)->winrate = winrate_str; return "= " + result + "\n"; } diff --git a/gtp4zen/zen6gtp.h b/gtp4zen/zen6gtp.h index aa1939c..13344a6 100644 --- a/gtp4zen/zen6gtp.h +++ b/gtp4zen/zen6gtp.h @@ -28,12 +28,13 @@ class CZen6Gtp : public CGtp std::string genmove(std::string color); std::string time_settings(int main_time, int byo_yomi_time, int byo_yomi_stones); std::string time_left(std::string color, int time, int stones); + std::string score(); + float get_winrate(); private: std::string __genmove(std::string _color, int _maxtime, int _strength); std::string __find_best_move(bool debug, int &x, int &y, int &simulation, float &W); int lua_genmove_calctime(int cur_move_num, int time_left); - float lua_komi_get(); private: void *m_proxy; diff --git a/gtp4zen/zen7gtp.cpp b/gtp4zen/zen7gtp.cpp index 5b67b46..8910492 100644 --- a/gtp4zen/zen7gtp.cpp +++ b/gtp4zen/zen7gtp.cpp @@ -8,8 +8,12 @@ #endif typedef void(LIBZENAPI *_ZenClearBoard)(void); -typedef int(LIBZENAPI *_ZenGetNextColor)(void); +typedef int(LIBZENAPI *_ZenGetBoardColor)(int, int); +typedef void(LIBZENAPI *_ZenGetTerritoryStatictics)(int(*const)[19]); typedef void(LIBZENAPI *_ZenGetTopMoveInfo)(int, int &, int &, int &, float &, char *, int); +typedef int(LIBZENAPI *_ZenGetNextColor)(void); +typedef int(LIBZENAPI *_ZenGetNumBlackPrisoners)(void); +typedef int(LIBZENAPI *_ZenGetNumWhitePrisoners)(void); typedef void(LIBZENAPI *_ZenInitialize)(char const *); typedef bool(LIBZENAPI *_ZenIsInitialized)(void); typedef bool(LIBZENAPI *_ZenIsThinking)(void); @@ -22,6 +26,7 @@ typedef void(LIBZENAPI *_ZenSetNumberOfSimulations)(int); typedef void(LIBZENAPI *_ZenSetNumberOfThreads)(int); typedef void(LIBZENAPI *_ZenSetPnLevel)(int); typedef void(LIBZENAPI *_ZenSetPnWeight)(float); +typedef void(LIBZENAPI *_ZenSetVnMixRate)(float); typedef void(LIBZENAPI *_ZenStartThinking)(int); typedef void(LIBZENAPI *_ZenStopThinking)(void); typedef bool(LIBZENAPI *_ZenUndo)(int); @@ -30,25 +35,28 @@ struct _zen_dll_proxy { // ÄÚ²¿±äÁ¿ HMODULE m_hModule; std::string m_dllpath; - int m_boardsize; + int m_boardsize; + float m_komi; ptime m_maintime; ptime m_lefttime; lua_State *m_L; - int m_curmovenum; + int m_curmovenum; std::string winrate; + float m_winrate; // µ¼³öº¯Êý //ZenAddStone ZenAddStone1; _ZenClearBoard ZenClearBoard; //void (*ZenFixedHandicap)(int); //void (*ZenGetBestMoveRate)(void); - //void (*ZenGetBoardColor)(int, int); + _ZenGetBoardColor ZenGetBoardColor; //void (*ZenGetHistorySize)(void); _ZenGetNextColor ZenGetNextColor; - //void (*ZenGetNumBlackPrisoners)(void); - //void (*ZenGetNumWhitePrisoners)(void); + _ZenGetNumBlackPrisoners ZenGetNumBlackPrisoners; + _ZenGetNumWhitePrisoners ZenGetNumWhitePrisoners; //void (*ZenGetPriorKnowledge)(int(*const)[19]); //void (*ZenGetTerritoryStatictics)(int(*const)[19]); + _ZenGetTerritoryStatictics ZenGetTerritoryStatictics; _ZenGetTopMoveInfo ZenGetTopMoveInfo; _ZenInitialize ZenInitialize; _ZenIsInitialized ZenIsInitialized; @@ -68,6 +76,7 @@ struct _zen_dll_proxy { _ZenSetNumberOfThreads ZenSetNumberOfThreads; _ZenSetPnLevel ZenSetPnLevel; _ZenSetPnWeight ZenSetPnWeight; + _ZenSetVnMixRate ZenSetVnMixRate; _ZenStartThinking ZenStartThinking; _ZenStopThinking ZenStopThinking; _ZenUndo ZenUndo; @@ -85,6 +94,56 @@ CZen7Gtp::~CZen7Gtp() unload(); } +float CZen7Gtp::get_winrate() +{ + return ((_zen_dll_proxy*)m_proxy)->m_winrate;; +} + +std::string CZen7Gtp::score() +{ + int territory[19][19]; + ((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics(territory); + float moku_w = ((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners(), moku_b = ((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners(); + for (int j = 0; j < ((_zen_dll_proxy*)m_proxy)->m_boardsize; ++j) + { + for (int i = 0; i < ((_zen_dll_proxy*)m_proxy)->m_boardsize; ++i) + { + int color = ((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor(i, j); + if (territory[j][i] <= -500) + { + if (color == 2) + moku_w += 2; + else if (color == 0) + moku_w += 1; + } + else if (territory[j][i] >= 500) + { + if (color == 1) + moku_b += 2; + else if (color == 0) + moku_b += 1; + } + } + } + moku_w += ((_zen_dll_proxy*)m_proxy)->m_komi; + if (moku_b > moku_w) + { + char result[100]; + sprintf_s(result, "= B+%g\n", moku_b - moku_w); + return result; + } + else if (moku_b < moku_w) + { + char result[100]; + sprintf_s(result, "= W+%g\n", moku_w - moku_b); + return result; + } + else + { + return "= Draw\n"; + } +} + // cur_move_num£ºµ±Ç°ÊÖÊý£¬time_left£ºÊ£Óàʱ¼ä int CZen7Gtp::lua_genmove_calctime(int cur_move_num, int time_left) { @@ -103,22 +162,6 @@ int CZen7Gtp::lua_genmove_calctime(int cur_move_num, int time_left) return result; } -// -float CZen7Gtp::lua_komi_get() -{ - // Ö´Ðгõʼ»¯º¯Êý£¬×¢Òâƽºâ¶ÑÕ»£¬lua_gettop(m_L)¿ÉÒÔ»ñÈ¡µ±Ç°Õ»Ê¹Óà - int ret = lua_getglobal(((_zen_dll_proxy*)m_proxy)->m_L, "komi"); - float result = -1; - if (ret > 0) { - lua_pcall(((_zen_dll_proxy*)m_proxy)->m_L, 0, 1, 0); - result = (float)luaL_checknumber(((_zen_dll_proxy*)m_proxy)->m_L, 1); - } else { - lua_pop(((_zen_dll_proxy*)m_proxy)->m_L, 1); - } - lua_settop(((_zen_dll_proxy*)m_proxy)->m_L, 0); - return result; -} - bool CZen7Gtp::load(std::string zen_dll_path, std::string lua_engine_path) { #if 0 @@ -135,6 +178,7 @@ bool CZen7Gtp::load(std::string zen_dll_path, std::string lua_engine_path) unload(); m_proxy = new _zen_dll_proxy(); + ((_zen_dll_proxy*)m_proxy)->m_komi = 6.5f; ((_zen_dll_proxy*)m_proxy)->m_L = luaL_newstate(); luaL_openlibs(((_zen_dll_proxy*)m_proxy)->m_L); luaL_dostring(((_zen_dll_proxy*)m_proxy)->m_L, "package.path = package.path .. ';./?.lua'"); @@ -156,10 +200,18 @@ bool CZen7Gtp::load(std::string zen_dll_path, std::string lua_engine_path) HMODULE hModule = ((_zen_dll_proxy*)m_proxy)->m_hModule; ((_zen_dll_proxy*)m_proxy)->ZenClearBoard = (_ZenClearBoard)GetProcAddress(hModule, "?ZenClearBoard@@YAXXZ"); assert(((_zen_dll_proxy*)m_proxy)->ZenClearBoard); + ((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor = (_ZenGetBoardColor)GetProcAddress(hModule, "?ZenGetBoardColor@@YAHHH@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetBoardColor); ((_zen_dll_proxy*)m_proxy)->ZenGetNextColor = (_ZenGetNextColor)GetProcAddress(hModule, "?ZenGetNextColor@@YAHXZ"); assert(((_zen_dll_proxy*)m_proxy)->ZenGetNextColor); + ((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics = (_ZenGetTerritoryStatictics)GetProcAddress(hModule, "?ZenGetTerritoryStatictics@@YAXQAY0BD@H@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetTerritoryStatictics); ((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo = (_ZenGetTopMoveInfo)GetProcAddress(hModule, "?ZenGetTopMoveInfo@@YAXHAAH00AAMPADH@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo); + ((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners = (_ZenGetNumBlackPrisoners)GetProcAddress(hModule, "?ZenGetNumBlackPrisoners@@YAHXZ"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetNumBlackPrisoners); + ((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners = (_ZenGetNumWhitePrisoners)GetProcAddress(hModule, "?ZenGetNumWhitePrisoners@@YAHXZ"); + assert(((_zen_dll_proxy*)m_proxy)->ZenGetNumWhitePrisoners); ((_zen_dll_proxy*)m_proxy)->ZenInitialize = (_ZenInitialize)GetProcAddress(hModule, "?ZenInitialize@@YAXPBD@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenInitialize); ((_zen_dll_proxy*)m_proxy)->ZenIsInitialized = (_ZenIsInitialized)GetProcAddress(hModule, "?ZenIsInitialized@@YA_NXZ"); @@ -184,6 +236,8 @@ bool CZen7Gtp::load(std::string zen_dll_path, std::string lua_engine_path) assert(((_zen_dll_proxy*)m_proxy)->ZenSetPnLevel); ((_zen_dll_proxy*)m_proxy)->ZenSetPnWeight = (_ZenSetPnWeight)GetProcAddress(hModule, "?ZenSetPnWeight@@YAXM@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenSetPnWeight); + ((_zen_dll_proxy*)m_proxy)->ZenSetVnMixRate = (_ZenSetVnMixRate)GetProcAddress(hModule, "?ZenSetVnMixRate@@YAXM@Z"); + assert(((_zen_dll_proxy*)m_proxy)->ZenSetVnMixRate); ((_zen_dll_proxy*)m_proxy)->ZenStartThinking = (_ZenStartThinking)GetProcAddress(hModule, "?ZenStartThinking@@YAXH@Z"); assert(((_zen_dll_proxy*)m_proxy)->ZenStartThinking); ((_zen_dll_proxy*)m_proxy)->ZenStopThinking = (_ZenStopThinking)GetProcAddress(hModule, "?ZenStopThinking@@YAXXZ"); @@ -213,8 +267,6 @@ bool CZen7Gtp::load(std::string zen_dll_path, std::string lua_engine_path) fprintf(stderr, "zen.dll(zen7) initialize %s.\n", is_init ? "success" : "failed"); logprintf(L"zen.dll(zen7) initialize %s.", is_init ? L"success" : L"failed"); fflush(stderr); - if (!g_debug) - fclose(stderr); return is_init; } @@ -294,13 +346,9 @@ std::string CZen7Gtp::boardsize(int size) std::string CZen7Gtp::komi(float k) { logprintf(L"komi(%.2f)", k); - if (lua_komi_get() >= 0) { - logprintf(L"\tlua set komi: %f", lua_komi_get()); - ((_zen_dll_proxy*)m_proxy)->ZenSetKomi(lua_komi_get()); - return "= \n"; - } if (k >= 0 && k <= 300) { + ((_zen_dll_proxy*)m_proxy)->m_komi = k; ((_zen_dll_proxy*)m_proxy)->ZenSetKomi(k); return "= \n"; } else { @@ -374,6 +422,7 @@ std::string CZen7Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati int N = -1; char S[256]; // È«²¿ÊÖÊý£¨º¬µ±Ç°ÊÖ£© #if 1 + std::string result = "pass"; for (int i = 4; i >= 0; i--) { // zenÒ»°ã¸ø³ö5¸öÑ¡µã ((_zen_dll_proxy*)m_proxy)->ZenGetTopMoveInfo(i, x, y, simulation, W, S, 99); //XTRACE("%d: %d-%d, P:%d, W:%f, %s\n", i, x, y, simulation, W, S); @@ -395,17 +444,29 @@ std::string CZen7Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati fflush(stderr); } if (0 == i) { - if (strstr(S, "pass")) + if (strstr(S, "pass") == S) { return "pass"; + } + + if (W > -1 && W * 100 < g_resign) { + result = "resign"; + return result; + } - std::string result; if (x >= 0 && y >= 0 && simulation >= 0) { result = __num2ansi(x, y, ((_zen_dll_proxy*)m_proxy)->m_boardsize); - } else { - result = "resign"; + } + else { + //result = "resign"; result = "pass"; } return result; + } else { + if (strstr(S, "pass") != S) { + if (x >= 0 && y >= 0 && simulation >= 0) { + result = __num2ansi(x, y, ((_zen_dll_proxy*)m_proxy)->m_boardsize); + } + } } } #else @@ -419,7 +480,7 @@ std::string CZen7Gtp::__find_best_move(bool debug, int &x, int &y, int &simulati //} //return result; #endif - return ""; + return "pass"; } // Ö¸¶¨×î´óʱ¼ä£¨ºÁÃ룩£¬×î¶à²½Êý @@ -432,6 +493,10 @@ std::string CZen7Gtp::__genmove(std::string _color, int _maxtime, int _strength) int color = ("w" == _color ? GTP4ZEN_COLOR_WHITE : GTP4ZEN_COLOR_BLACK); // Âä×Ó + ((_zen_dll_proxy*)m_proxy)->ZenSetNumberOfSimulations(_strength); + ((_zen_dll_proxy*)m_proxy)->ZenSetPnLevel(g_think_level_0); + ((_zen_dll_proxy*)m_proxy)->ZenSetPnWeight(g_think_level_1); + ((_zen_dll_proxy*)m_proxy)->ZenSetVnMixRate(g_think_level_2); ((_zen_dll_proxy*)m_proxy)->ZenStartThinking(color); ptime time_start, time_now; millisec_posix_time_system_config::time_duration_type time_elapse; @@ -484,6 +549,7 @@ std::string CZen7Gtp::__genmove(std::string _color, int _maxtime, int _strength) play(_color, result); char winrate_str[32]; sprintf_s(winrate_str, 32, "%.2f", W * 100); + ((_zen_dll_proxy*)m_proxy)->m_winrate = W; ((_zen_dll_proxy*)m_proxy)->winrate = winrate_str; return "= " + result + "\n"; } diff --git a/gtp4zen/zen7gtp.h b/gtp4zen/zen7gtp.h index fe06e5b..d3d483a 100644 --- a/gtp4zen/zen7gtp.h +++ b/gtp4zen/zen7gtp.h @@ -28,12 +28,13 @@ class CZen7Gtp : public CGtp std::string genmove(std::string color); std::string time_settings(int main_time, int byo_yomi_time, int byo_yomi_stones); std::string time_left(std::string color, int time, int stones); + std::string score(); + float get_winrate(); private: std::string __genmove(std::string _color, int _maxtime, int _strength); std::string __find_best_move(bool debug, int &x, int &y, int &simulation, float &W); int lua_genmove_calctime(int cur_move_num, int time_left); - float lua_komi_get(); private: void *m_proxy;