Esempio n. 1
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent) const
{
	QString path(QDir::currentPath());
	EngineProcess* process = new EngineProcess();

	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
	{
		// Make sure the path to the executable is resolved
		// in the engine's working directory
		if (!QDir::setCurrent(workDir))
		{
			qWarning() << "Invalid working directory:" << workDir;
			delete process;
			return 0;
		}
		process->setWorkingDirectory(QDir::currentPath());
	}

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);
	bool ok = process->waitForStarted();

	if (!workDir.isEmpty())
		QDir::setCurrent(path);
	if (!ok)
	{
		qWarning() << "Cannot start engine" << m_config.command();
		delete process;
		return 0;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != 0);
	engine->setParent(parent);
	if (receiver != 0 && method != 0)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}
Esempio n. 2
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent,
				   QString* error) const
{
	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command().trimmed();
	QString stderrFile = m_config.stderrFile();

	if (cmd.isEmpty())
	{
		setError(error, tr("Empty engine command"));
		return nullptr;
	}

	if (!EngineFactory::protocols().contains(m_config.protocol()))
	{
		setError(error, tr("Unknown chess protocol: %1")
			 .arg(m_config.protocol()));
		return nullptr;
	}

	EngineProcess* process = new EngineProcess();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
		process->setWorkingDirectory(workDir);

	if (!stderrFile.isEmpty())
		process->setStandardErrorFile(stderrFile, QIODevice::Append);

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);

	bool ok = process->waitForStarted();
	if (!ok)
	{
		setError(error, tr("Cannot execute command: %1")
			 .arg(m_config.command()));
		delete process;
		return nullptr;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != nullptr);

	engine->setParent(parent);
	if (receiver != nullptr && method != nullptr)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}
Esempio n. 3
0
int main2() {
	char res[50];
	board b;
	Evaluator e;
	ChessEngine ce;
	int xforce=1;

	int engineplay = 0;
	bitmove m;

	for (;;) {
		if (!xforce && ((b.moveof == white && engineplay == PLAYWHITE)
				|| (b.moveof == black && engineplay == PLAYBLACK))) {
			ce.think(&b, m);
			b.domove(m);
			cout << "move " << m << endl;
		}
		cin.getline(res, 500);
		if (!strcmp(res, "q") || !strcmp(res, "quit"))
			break;
		else if (!strcmp(res, "xboard")) {
			// started by WinBoard
		}else if (!strncmp(res, "protover", 8)) {
			cout << "feature myname=\""
					<< ENGINEFULLNAME<<"\" time=0 reuse=0 analyze=0 done=1\n";
			cout.flush();
		} else if (!strcmp(res, "random")) {
			// ignore random command
		} else if (!strncmp(res, "level", 5)) {
			//Set time controls. need to parse.
		} else if (!strcmp(res, "hard")) {
			//Turn on pondering (thinking on the opponent's time,
			//also known as "permanent brain").
		} else if (!strncmp(res, "time", 4)) {
			//Set a clock that always belongs to the engine.
		} else if (!strncmp(res, "otim", 4)) {
			//Set a clock that always belongs to the opponent.
		} else if (!strcmp(res, "post")) {
			//Turn on thinking/pondering output.
		} else if (!strcmp(res, "black")) {
			engineplay = PLAYBLACK;
            xforce = 1;
		} else if (!strcmp(res, "white")) {
			engineplay = PLAYWHITE;
            xforce = 1;
		} else if (!strcmp(res, "force")) {
			/* Set the engine to play neither color ("force mode").
			 * Stop clocks.
			 * The engine should check that moves received in force mode are
			 * legal and made in the proper turn, but should not think,
			 * ponder, or make moves of its own.
			 */
			engineplay = 0;
            xforce = 1;
		} else if (!strcmp(res, "go")) {
			/*
			 * Leave force mode and set the engine to play the color that is on move.
			 * Associate the engine's clock with the color that is on move,
			 * the opponent's clock with the color that is not on move.
			 * Start the engine's clock.
			 * Start thinking and eventually make a move.
			 */
			engineplay = (b.moveof == white ? PLAYWHITE : PLAYBLACK);
            xforce = 0;
		} else if (!strcmp(res, "new")) {
			/*
			 * Reset the board to the standard chess starting position.
			 * Set White on move.
			 * Leave force mode and set the engine to play Black.
			 */
			b.newgame();
            engineplay = PLAYBLACK;
            xforce = 0;
		} else if (!strncmp(res, "accepted", 8)) {
			// features accepted
		} else if (!strcmp(res, "u") || !strcmp(res, "undo"))
			b.undolastmove();
		else if (!strcmp(res, "l") || !strcmp(res, "list"))
			generateAndListMoves(b);
		else if (!strcmp(res, "e") || !strcmp(res, "evaluate"))
			cout << "Score: " << e.score(b) << endl;
		else if (!strcmp(res, "t") || !strcmp(res, "think")) {
			ce.think(&b, m);
			b.domove(m);
		} else if (m.set(res)) {
			b.domove(m);
		} else
			cout << "Illigal move: "<<res<<endl;;
	}
	return 1;
}