Skip to content

Commit

Permalink
v2.6, improve log system, fix a bug for over sending uci options
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenpham committed Jul 20, 2019
1 parent def1688 commit 4b907fb
Show file tree
Hide file tree
Showing 25 changed files with 593 additions and 204 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ Below are some features/functions we need someone helps too:
Working
---------
- Improve interface
- Support opening info ECO
- Support endgame adjudication, using tablebases
- Support some different tournament types: swiss
- Support other chess variants (not soon event it is designed for multi-variants)


History
--------
- 20 July 2019: v2.6, more info and more ways to control logs, improve engine managements
- 16 July 2019: v2.5, multiple ways to select openings: new for each match, same for a pair, one for all matches; override options for central controlling; match statistics LOS & error margin
- 14 July 2019: v2.2, knockout tournament and resumable the last tournament
- 12 July 2019: v2.0, auto generating JSON files
Expand All @@ -165,7 +168,7 @@ History
Terms of use
---------------

The project is released under the liberal [MIT license](http://en.wikipedia.org/wiki/MIT_License), so basically you can use it with almost no restrictions.
All codes and data in the folder 3rdparty are 3rd party libraries, copyrighted and released under their terms of use. Banksia code and data is released under the GPLv3+ license.


Credits
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
argument = "-u -c 16"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-v off"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-d /Users/nguyenpham/workspace/BanksiaMatch/engines"
isEnabled = "NO">
Expand Down

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ namespace banksia {
int8_t castleRights[2];
u64 hashKey;
int quietCnt;
double elapsed;
int score = 0, depth = 0;
double elapsed = 0;
std::string moveString, comment;

void set(const MoveFull& _move) {
Expand All @@ -270,7 +271,7 @@ namespace banksia {

int status;
Result result;

public:
void reset() {
for (auto && p : pieces) {
Expand Down
61 changes: 60 additions & 1 deletion src/base/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@
#include <direct.h>
#include <stdlib.h> // for full path

#include <tlhelp32.h> // for isRunning

#else

#include <glob.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

#include <sys/types.h>
#include <signal.h>

#endif


#include "comm.h"

namespace banksia {
bool banksiaVerbose = false;
bool banksiaVerbose = true;

extern const char* pieceTypeName;
extern const char* reasonStrings[11];
Expand All @@ -63,6 +68,13 @@ namespace banksia {
"*", "1-0", "1/2-1/2", "0-1", nullptr
};

const char* sideStrings[] = {
"black", "white", "none", nullptr
};
const char* shortSideStrings[] = {
"b", "w", "n", nullptr
};

static std::mutex consoleMutex;

std::string resultType2String(ResultType type) {
Expand Down Expand Up @@ -95,6 +107,25 @@ namespace banksia {
return ReasonType::noreason;
}

std::string side2String(Side side, bool shortFrom)
{
auto sd = static_cast<int>(side);
if (sd < 0 || sd > 1) sd = 2;
return shortFrom ? shortSideStrings[sd] : sideStrings[sd];
}

Side string2Side(std::string s)
{
toLower(s);
for(int i = 0; sideStrings[i]; i++) {
if (sideStrings[i] == s || shortSideStrings[i] == s) {
return static_cast<Side>(i);
}
}
return Side::none;

}

void printText(const std::string& str)
{
std::lock_guard<std::mutex> dolock(consoleMutex);
Expand Down Expand Up @@ -332,6 +363,29 @@ namespace banksia {
return path.find(".exe") != std::string::npos;
}

bool isRunning(int pid)
{
HANDLE pss = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);

PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(pe);

if (Process32First(pss, &pe))
{
do
{
// pe.szExeFile can also be useful
if (pe.th32ProcessID == pid)
return true;
}
while(Process32Next(pss, &pe));
}

CloseHandle(pss);

return false;
}

#else

std::vector<std::string> listdir(std::string dirname) {
Expand Down Expand Up @@ -383,6 +437,11 @@ namespace banksia {
return !access(path.c_str(), X_OK);
}

bool isRunning(int pid)
{
return 0 == kill(pid, 0);
}

#endif


Expand Down
6 changes: 5 additions & 1 deletion src/base/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

namespace banksia {

const int BANKSIA_VERSION = (2 << 8) + 52;
const int BANKSIA_VERSION = (2 << 8) + 6;

#ifdef _WIN32
const std::string folderSlash = "\\";
Expand Down Expand Up @@ -178,6 +178,7 @@ namespace banksia {
std::vector<std::string> listdir(std::string dirname);
i64 getFileSize(const std::string& path);
bool isExecutable(const std::string& path);
bool isRunning(int pid);

std::vector<std::string> splitString(const std::string& string, const std::string& regexString);
std::vector<std::string> splitString(const std::string &s, char delim);
Expand All @@ -187,6 +188,9 @@ namespace banksia {
std::string reasonType2String(ReasonType type);
ReasonType string2ReasonType(const std::string& s);

std::string side2String(Side side, bool shortFrom = true);
Side string2Side(const std::string& s);

} // namespace banksia

#endif /* Board_hpp */
Expand Down
22 changes: 19 additions & 3 deletions src/chess/chess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include <random>
#include <iomanip> // for setprecision

#include "chess.h"

Expand Down Expand Up @@ -1209,7 +1210,7 @@ bool ChessBoard::createStringForLastMove(const MoveList& moveList)
return true;
}

std::string ChessBoard::toMoveListString(MoveNotation notation, int itemPerLine, bool moveCounter) const
std::string ChessBoard::toMoveListString(MoveNotation notation, int itemPerLine, bool moveCounter, bool computingInfo) const
{
std::ostringstream stringStream;

Expand All @@ -1235,10 +1236,25 @@ std::string ChessBoard::toMoveListString(MoveNotation notation, int itemPerLine,
}

// Comment
auto haveComment = false;
if (computingInfo && hist.depth > 0) {
haveComment = true;
stringStream << " {" << (hist.score > 0 ? "+" : "")
<< std::setprecision(2) << ((float)hist.score / 100.0) << "/"
<< hist.depth
<< " " << std::setprecision(2) << hist.elapsed;
}
if (!hist.comment.empty() && moveCounter) {
stringStream << " {" << hist.comment << "} ";
stringStream << (haveComment ? "; " : " {");

haveComment = true;
stringStream << hist.comment ;
}


if (haveComment) {
stringStream << "} ";
}

c++;
if (itemPerLine > 0 && c >= itemPerLine) {
c = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/chess/chess.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace banksia {

bool checkMake(int from, int dest, PieceType promotion);

std::string toMoveListString(MoveNotation notation, int itemPerLine, bool moveCounter) const;
std::string toMoveListString(MoveNotation notation, int itemPerLine, bool moveCounter, bool computingInfo) const;

Move fromSanString(const std::string&);
bool fromSanMoveList(const std::string&);
Expand Down
3 changes: 0 additions & 3 deletions src/game/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ void BookPolyglot::load(const std::string& _path, int _maxPly, int _top100)
for(int i = 0; i < itemCnt; i++) {
items[i].convertToLittleEndian();
}

std::cout << "BookPolyglot::load " << path << ", itemCnt: " << itemCnt << std::endl;

}

bool BookPolyglot::isValid() const
Expand Down
2 changes: 1 addition & 1 deletion src/game/configmng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void Option::setDefaultValue(const std::string& val, const std::vector<std::stri

bool Option::isDefaultValue() const
{
if (!overrideType)
if (overrideType)
return false;

switch (type) {
Expand Down
30 changes: 17 additions & 13 deletions src/game/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ using namespace banksia;
////////////////////////////////////
Engine::~Engine()
{
deleteThread();
// deleteThread();
if (processId && isRunning(processId)) {
std::cout << "Warning 1: a chess engine/program refuses to stop, it may be still running, " << name << std::endl;
TinyProcessLib::Process::kill(processId, true);
}

if (pThread && pThread->joinable()) {
std::cout << "Warning 2: a chess engine/program refuses to stop, it may be still running, " << name << std::endl;
pThread->join();
}

}

void Engine::tickWork()
Expand All @@ -60,7 +70,7 @@ void Engine::tickWork()
if (tick_being_kill == 0) {
TinyProcessLib::Process::kill(processId, true);
process = nullptr;
deleteThread();
// deleteThread();
setState(PlayerState::stopped);
finished();
}
Expand Down Expand Up @@ -186,17 +196,6 @@ void Engine::parseLine(const std::string& line)
parseLine(it->second, cmdString, line);
}

void Engine::deleteThread()
{
if (pThread && pThread->joinable()) {
#ifndef _WIN32
pthread_cancel(pThread->native_handle());
#endif
pThread = nullptr;
std::cout << "Warning: thread is still resident " << name << std::endl;
}
}

bool Engine::kickStart()
{
resetPing();
Expand Down Expand Up @@ -244,6 +243,7 @@ bool Engine::kickStart()
pThread = nullptr;
});

native_handle = processThread.native_handle();
pThread = &processThread;
processThread.detach();
return true;
Expand All @@ -258,6 +258,10 @@ void Engine::attach(ChessBoard* board, const GameTimeController* timeController,
Player::attach(board, timeController, moveFunc, resignFunc);
tick_deattach = -1;
tick_idle = 0;

if (board == nullptr) {
messageLogger = nullptr;
}
}

bool Engine::isSafeToDeattach() const
Expand Down
5 changes: 4 additions & 1 deletion src/game/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ namespace banksia {
int tick_ping, tick_idle, tick_being_kill = -1; //, tick_stopping = 0;
std::function<void(const std::string&, const std::string&, LogType)> messageLogger = nullptr;

void deleteThread();
int correctCmdCnt = 0;

private:
Expand All @@ -122,6 +121,10 @@ namespace banksia {
TinyProcessLib::Process::id_type processId = 0;
TinyProcessLib::Process* process = nullptr;
std::thread* pThread = nullptr;

#ifndef _WIN32
std::thread::native_handle_type native_handle = 0;
#endif
};


Expand Down
Loading

0 comments on commit 4b907fb

Please sign in to comment.