⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qgtp.cpp

📁 qgo-1.5.4-r3.tar.gz linux下一个很好玩的游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************qgtp.cpp  -  description-------------------begin                : Thu Nov 29 2001copyright            : (C) 2001 by PALM Thomas , DINTILHAC Florian, HIVERT Anthony, PIOC Sebastienemail                : ***************************************************************************//****************************************************************************                                                                         **   This program is free software; you can redistribute it and/or modify  **   it under the terms of the GNU General Public License as published by  **   the Free Software Foundation; either version 2 of the License, or     **   (at your option) any later version.                                   **                                                                         ****************************************************************************///#include <unistd.h>#include <stdlib.h>#include <qprocess.h>#include "qgtp.h"#ifdef Q_WS_WIN#include <Windows.h>#endif/* Function:  open a session* Arguments: name and path of go program* Fails:     never* Returns:   nothing*/QGtp::QGtp(){	/*openGtpSession(filename);*/	programProcess = NULL; // This to permit proper ending}QGtp::~QGtp(){	if (programProcess)		programProcess->kill();     // not very clean (should use tryTerminate)}/* Code *//* Function:  get the last message from gnugo* Arguments: none* Fails:     never* Returns:   last message from GO*/QString QGtp::getLastMessage(){	return _response;}int QGtp::openGtpSession(QString filename, int size, float komi, int handicap, int level){	_cpt = 1;	    programProcess = new QProcess();        programProcess->clearArguments();    programProcess->addArgument(filename);    programProcess->addArgument("--mode");    programProcess->addArgument("gtp");    programProcess->addArgument("--quiet");  	    connect(programProcess, SIGNAL(readyReadStdout()),		this, SLOT(slot_readFromStdout()) );    connect(programProcess, SIGNAL(processExited()),		this, SLOT(slot_processExited()) );		if (!programProcess->start())	{		  _response="Could not start "+filename;		  return FAIL ;	}	    //?? never know ... otherwise, I get a segfault with sprint ...	if ((outFile = (char *)malloc (100)) == NULL)	{		  _response="Yuck ! Could not allocate 100 bytes !!!"  ;		  return FAIL ;	}   		if (protocolVersion()==OK)	{		if(getLastMessage().toInt() !=2)		{			qDebug("Protocol version problem???");			//				quit();			_response="Protocol version not supported";			//				return FAIL;		}		if(setBoardsize(size)==FAIL)		{			return FAIL;		}		if(clearBoard()==FAIL)		{			// removed by frosla -> protocol changes...			// return FAIL;		}		if(knownCommand("level")==FAIL)    		{			  return FAIL;		}			else if (getLastMessage().contains("true"))        	{        		if (setLevel(level)==FAIL)			{				return FAIL;			}        	}       		if(setKomi(komi)==FAIL)		{			return FAIL;		}		if(fixedHandicap(handicap)==FAIL)		{			return FAIL;		}	}	else	{		quit();		_response="Protocol version error";		return FAIL;	}	//}	return OK;}// Read from stdoutvoid QGtp::slot_readFromStdout(){	QString buff;		while (programProcess->canReadLineStdout())	{		buff=programProcess->readLineStdout();		buff=buff.stripWhiteSpace();		if (buff.length() != 0)		{			_response = buff;//			qDebug(QString("** QGtp::slot_readFromStdout(): [%1]").arg(_response));		}	}}// exitvoid QGtp::slot_processExited(){	qDebug(QString("%1 quit").arg(_cpt));	sprintf (outFile, "%d quit\n", _cpt);	fflush(outFile);	//	return waitResponse();}/* Function:  wait for a go response* Arguments: none* Fails:     never* Returns:   nothing*/intQGtp::waitResponse(){	QString buff = _response;	//	QTextStream * inFile;	//	char symbole;	int number;	int pos;		do	{		qApp->processEvents();/*#ifdef Q_WS_WIN		Sleep(100000);#else		usleep(100000);#endif*/	} while (_response.length() == 0 || _response == buff);		/*	inFile=new QTextStream(programProcess->readStdout(),IO_ReadOnly);	do	{	* inFile>>symbole;	* inFile>>number;	buff=inFile->readLine();	} while(number !=_cpt);	*/		//	_response=buff.stripWhiteSpace();//	qDebug(QString("** QGtp::waitResponse(): [%1]").arg(_response));	/*		buff=programProcess->readLineStdout();	while(!buff.isEmpty())	{	_response=_response+"\n"+buff;	buff=programProcess->readLineStdout();	}	*/	buff = _response[0];	if ((pos = _response.find(' ')) < 1)		pos = 1;	number = _response.mid(1,pos).toInt();	_response = _response.right(_response.length() - pos - 1);	qDebug(QString("** QGtp::waitResponse(): [%1]").arg(_response));	if (buff == '?') //symbole=='?')	{		return FAIL;	}	else	{		return OK;	}}/***************************** Program identity.        *****************************//* Function:  Report the name of the program.* Arguments: none* Fails:     never* Returns:   program name*/intQGtp::name (){	qDebug(QString("%1 name").arg(_cpt));	sprintf (outFile, "%d name\n", _cpt);	fflush(outFile);	return waitResponse();}/* Function:  Report protocol version.* Arguments: none* Fails:     never* Returns:   protocol version number*/intQGtp::protocolVersion (){	qDebug(QString("%1 protocol_version").arg(_cpt));	sprintf (outFile, "%d protocol_version\n", _cpt);	fflush(outFile);	return waitResponse();}void QGtp::fflush(char * s){/*int msglen = strlen(s);  QByteArray dat(msglen);  for (int j = 0; j < msglen; j++) {  dat[j] = s[j];  }  programProcess->writeToStdin(dat);	*/	_cpt++;	qDebug(QString("flush -> %1").arg(s));    programProcess->writeToStdin(QString::QString(s));		}/***************************** Administrative commands. *****************************//* Function:  Quit* Arguments: none* Fails:     never* Returns:   nothing*/intQGtp::quit (){	sprintf (outFile, "%d quit\n", _cpt);	fflush(outFile);	return waitResponse();}/* Function:  Report the version number of the program.* Arguments: none* Fails:     never* Returns:   version number*/intQGtp::version (){	sprintf (outFile, "%d version\n", _cpt);	fflush(outFile);	return waitResponse();}/**************************** Setting the board size. ****************************//* Function:  Set the board size to NxN.* Arguments: integer* Fails:     board size outside engine's limits* Returns:   nothing*/intQGtp::setBoardsize (int size){	sprintf (outFile, "%d boardsize %d\n", _cpt, size);	fflush(outFile);	return waitResponse();}/* Function:  Find the current boardsize* Arguments: none* Fails:     never* Returns:   board_size*/intQGtp::queryBoardsize(){	sprintf (outFile, "%d query_boardsize\n", _cpt);	fflush(outFile);	return waitResponse();}/************************ Clearing the board. ************************//* Function:  Clear the board.* Arguments: none* Fails:     never* Returns:   nothing*/intQGtp::clearBoard (){    sprintf (outFile, "%d clear_board\n", _cpt);    fflush(outFile);    return waitResponse();}/**************************** Setting.           ****************************//* Function:  Set the komi.* Arguments: float* Fails:     incorrect argument* Returns:   nothing*/intQGtp::setKomi(float f){	sprintf (outFile, "%d komi %.2f\n", _cpt,f);	fflush(outFile);	return waitResponse();}/* Function:  Set the playing level.* Arguments: int* Fails:     incorrect argument* Returns:   nothing*/intQGtp::setLevel (int level){	sprintf (outFile, "%d level %d\n", _cpt,level);	fflush(outFile);	return waitResponse();}/******************* Playing moves. *******************//* Function:  Play a black stone at the given vertex.* Arguments: vertex* Fails:     invalid vertex, illegal move* Returns:   nothing*/intQGtp::playblack (char c , int i){	//  sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);	sprintf (outFile, "%d play black %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Black pass.* Arguments: None* Fails:     never* Returns:   nothing*/intQGtp::playblackPass (){	//  sprintf (outFile, "%d play black pass\n", _cpt);	sprintf (outFile, "%d play black pass\n", _cpt);	fflush(outFile);	return waitResponse();}/* Function:  Play a white stone at the given vertex.* Arguments: vertex* Fails:     invalid vertex, illegal move* Returns:   nothing*/intQGtp::playwhite (char c, int i){	//  sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);	sprintf (outFile, "%d play white %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  White pass.* Arguments: none* Fails:     never* Returns:   nothing*/intQGtp::playwhitePass (){	//  sprintf (outFile, "%d play white pass\n", _cpt);	sprintf (outFile, "%d play white pass\n", _cpt);	fflush(outFile);	return waitResponse();}/* Function:  Set up fixed placement handicap stones.* Arguments: number of handicap stones* Fails:     invalid number of stones for the current boardsize* Returns:   list of vertices with handicap stones*/intQGtp::fixedHandicap (int handicap){	if (handicap < 2) 		return OK;		sprintf (outFile, "%d fixed_handicap %d\n", _cpt,handicap);	fflush(outFile);	return waitResponse();}/* Function:  Load an sgf file, possibly up to a move number or the first*            occurence of a move.* Arguments: filename + move number, vertex, or nothing* Fails:     missing filename or failure to open or parse file* Returns:   color to play*/intQGtp::loadsgf (QString filename,int movNumber,char c,int i){	//sprintf (outFile, "%d loadsgf %s %d %c%d\n", _cpt,(const char *) filename,movNumber,c,i);	sprintf (outFile, "%d loadsgf %s\n", _cpt,(const char *) filename);	fflush(outFile);	return waitResponse();}/****************** Board status. ******************//* Function:  Return the color at a vertex.* Arguments: vertex* Fails:     invalid vertex* Returns:   "black", "white", or "empty"*/intQGtp::whatColor (char c, int i){	sprintf (outFile, "%d color %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Count number of liberties for the string at a vertex.* Arguments: vertex* Fails:     invalid vertex, empty vertex* Returns:   Number of liberties.*/intQGtp::countlib (char c, int i){	sprintf (outFile, "%d countlib %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Return the positions of the liberties for the string at a vertex.* Arguments: vertex* Fails:     invalid vertex, empty vertex* Returns:   Sorted space separated list of vertices.*/intQGtp::findlib (char c, int i){	sprintf (outFile, "%d findlib %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Tell whether a move is legal.* Arguments: move* Fails:     invalid move* Returns:   1 if the move is legal, 0 if it is not.*/intQGtp::isLegal (QString color, char c, int i){	sprintf (outFile, "%d is_legal %s %c%d\n", _cpt,(const char *)color,c,i);	fflush(outFile);	return waitResponse();}/* Function:  List all legal moves for either color.* Arguments: color* Fails:     invalid color* Returns:   Sorted space separated list of vertices.*/intQGtp::allLegal (QString color){	sprintf (outFile, "%d all_legal %s\n", _cpt,(const char *)color);	fflush(outFile);	return waitResponse();}/* Function:  List the number of captures taken by either color.* Arguments: color* Fails:     invalid color* Returns:   Number of captures.*/intQGtp::captures (QString color){	sprintf (outFile, "%d captures %s\n", _cpt,(const char *)color);	fflush(outFile);	return waitResponse();}/*********************** Retractable moves. ***********************//* Function:  Play a stone of the given color at the given vertex.* Arguments: move (color + vertex)* Fails:     invalid color, invalid vertex, illegal move* Returns:   nothing*/intQGtp::trymove (QString color, char c, int i){	sprintf (outFile, "%d trymove %s %c%d\n", _cpt,(const char *)color,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Undo a trymove.* Arguments: none* Fails:     stack empty* Returns:   nothing*/intQGtp::popgo (){	sprintf (outFile, "%d popgo\n", _cpt);	fflush(outFile);	return waitResponse();}/********************** Tactical reading. **********************//* Function:  Try to attack a string.* Arguments: vertex* Fails:     invalid vertex, empty vertex* Returns:   attack code followed by attack point if attack code nonzero.*/intQGtp::attack (char c, int i){	sprintf (outFile, "%d attack %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Try to defend a string.* Arguments: vertex* Fails:     invalid vertex, empty vertex* Returns:   defense code followed by defense point if defense code nonzero.*/intQGtp::defend (char c, int i){	sprintf (outFile, "%d defend %c%d\n", _cpt,c,i);	fflush(outFile);	return waitResponse();}/* Function:  Increase depth values by one.* Arguments: none* Fails:     never* Returns:   nothing

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -