![]() |
Cinnamon
1.0
chess engine
|
00001 #include "Uci.h" 00002 00003 Uci::Uci() { 00004 it = new IterativeDeeping(); 00005 listner(); 00006 } 00007 00008 Uci::~Uci() { 00009 it->stop(); 00010 delete it; 00011 } 00012 00013 void Uci::getToken(istringstream& uip,string& token) { 00014 token.clear(); 00015 uip >> token; 00016 for(unsigned i = 0; i < token.size(); i++) 00017 token[i] = tolower(token[i]); 00018 } 00019 00020 void Uci::listner() { 00021 string command; 00022 bool knowCommand; 00023 string token; 00024 bool stop=false; 00025 int lastTime=0; 00026 while (!stop) { 00027 getline(cin, command); 00028 istringstream uip(command, ios::in); 00029 getToken(uip,token); 00030 knowCommand = false; 00031 if (token == "make_book") { 00032 cout <<"create open book:"<<endl; 00033 cout <<"\tpolyglot make-book -pgn book.pgn"<<endl; 00034 cout <<"\tpolyglot dump-book -bin book.bin -color white -out white"<<endl; 00035 cout <<"\tpolyglot dump-book -bin book.bin -color black -out black"<<endl; 00036 bool useBook=it->getUseBook(); 00037 it->setUseBook(false); 00038 OpenBook* b= new OpenBook(); 00039 cout <<"create book..."<<flush; 00040 b->create("white","black"); 00041 cout <<"ok"<<endl; 00042 delete b; 00043 it->setUseBook(useBook); 00044 knowCommand = true; 00045 } 00046 if (token == "perft") { 00047 int perftDepth=-1; 00048 int nCpu=1; 00049 int PERFT_HASH_SIZE=0; 00050 string fen; 00051 getToken(uip,token); 00052 while ( !uip.eof()) { 00053 if(token=="depth") { 00054 getToken(uip,token); 00055 perftDepth=atoi(token.c_str()); 00056 if (perftDepth > GenMoves::MAX_PLY || perftDepth <= 0)perftDepth=2; 00057 getToken(uip,token); 00058 } else if(token=="ncpu") { 00059 getToken(uip,token); 00060 nCpu=atoi(token.c_str()); 00061 if (nCpu > 32 || nCpu <= 0)nCpu=1; 00062 getToken(uip,token); 00063 } else if(token=="hash_size") { 00064 getToken(uip,token); 00065 PERFT_HASH_SIZE=atoi(token.c_str()); 00066 if (PERFT_HASH_SIZE > 32768 || PERFT_HASH_SIZE < 0) PERFT_HASH_SIZE=64; 00067 getToken(uip,token); 00068 } else if(token=="fen") { 00069 uip >> token; 00070 do { 00071 fen += token; 00072 fen += ' '; 00073 uip >> token; 00074 } while (token != "ncpu" && token != "hash_size" &&token != "depth" &&!uip.eof()); 00075 } else break; 00076 } 00077 if(perftDepth!=-1) { 00078 int hashSize=it->getHashSize(); 00079 it->setHashSize(0); 00080 if(fen.empty()) 00081 fen=it->START_FEN; 00082 cout << "perft depth "<<perftDepth <<" nCpu "<<nCpu<<" hash_size "<<PERFT_HASH_SIZE<<" fen "<<fen<<endl; 00083 Perft* p = new Perft(fen, perftDepth, nCpu, PERFT_HASH_SIZE); 00084 delete p; 00085 it->setHashSize(hashSize); 00086 } else 00087 cout <<"use: perft depth d [nCpu n] [hash_size mb] [fen fen_string]"<<endl; 00088 knowCommand = true; 00089 } else if (token == "quit") { 00090 knowCommand = true; 00091 it->setRunning(false); 00092 stop=true; 00093 } else if (token == "ponderhit") { 00094 knowCommand = true; 00095 it->startClock(); 00096 it->setMaxTimeMillsec(lastTime-lastTime/3); 00097 it->setPonder(false); 00098 } else if (token == "display") { 00099 knowCommand = true; 00100 it->display(); 00101 } else if (token == "isready") { 00102 knowCommand = true; 00103 it->setRunning(0); 00104 cout << "readyok"<<endl; 00105 } else if (token == "uci") { 00106 knowCommand = true; 00107 it->setUci(true); 00108 cout << "id name "<<NAME<<endl; 00109 cout << "id author Giuseppe Cannella"<<endl; 00110 #ifndef NO_HASH_MODE 00111 cout << "option name Hash type spin default 64 min 1 max 16384"<<endl; 00112 #endif 00113 cout << "option name Nullmove type check default true"<<endl; 00114 cout << "option name Clear Hash type button"<<endl; 00115 if(it->getPonderEnabled()) 00116 cout << "option name Ponder type check default true"<<endl; 00117 else 00118 cout << "option name Ponder type check default false"<<endl; 00119 cout << "uciok"<<endl; 00120 } else if (token == "score") { 00121 int t; 00122 t = it->getScore(it->getSide() , -_INFINITE, _INFINITE ); 00123 if (!it->getSide()) 00124 t = -t; 00125 cout << "Score: " << t << endl; 00126 knowCommand = true; 00127 } else if (token == "stop") { 00128 knowCommand = true; 00129 it->setPonder(false); 00130 it->setRunning(0); 00131 } else if (token == "ucinewgame") { 00132 it->loadFen(); 00133 it->clearMovesPath(); 00134 it->clearHash(); 00135 knowCommand = true; 00136 } else if (token == "setoption") { 00137 getToken(uip,token); 00138 if (token == "name") { 00139 getToken(uip,token); 00140 if (token == "hash") { 00141 getToken(uip,token); 00142 if (token == "value") { 00143 getToken(uip,token); 00144 knowCommand = true; 00145 it->setHashSize(atoi(token.c_str())); 00146 } 00147 } else if (token == "nullmove") { 00148 getToken(uip,token); 00149 if (token == "value") { 00150 getToken(uip,token); 00151 knowCommand = true; 00152 it->setNullMove(token=="true"?true:false); 00153 } 00154 } else if (token == "ownbook") { 00155 getToken(uip,token); 00156 if (token == "value") { 00157 getToken(uip,token); 00158 if (token == "true") 00159 it->setUseBook(true); 00160 else 00161 it->setUseBook(false); 00162 knowCommand = true; 00163 } 00164 } else if (token == "ponder") { 00165 getToken(uip,token); 00166 if (token == "value") { 00167 getToken(uip,token); 00168 if (token == "true") 00169 it->enablePonder(true); 00170 else 00171 it->enablePonder(false); 00172 knowCommand = true; 00173 } 00174 } else if (token == "clear") { 00175 getToken(uip,token); 00176 if (token == "hash") { 00177 knowCommand = true; 00178 it->clearHash(); 00179 } 00180 } 00181 } 00182 } else if (token == "position") { 00183 it->lockMutex(true); 00184 knowCommand = true; 00185 it->setRepetitionMapCount(0); 00186 getToken(uip,token); 00187 _Tmove move; 00188 it->setFollowBook(false); 00189 if (token == "startpos") { 00190 it->clearMovesPath(); 00191 it->setUseBook(it->getUseBook()); 00192 it->loadFen(); 00193 getToken(uip,token); 00194 } 00195 if (token == "fen") { 00196 string fen; 00197 while (token != "moves" && !uip.eof()) { 00198 uip >> token; 00199 fen += token; 00200 fen += ' '; 00201 } 00202 it->init(); 00203 it->setSide(it->loadFen(fen)); 00204 it->pushStackMove(); 00205 } 00206 if (token == "moves") { 00207 while (!uip.eof()) { 00208 uip >> token; 00209 it->setSide(!it->getMoveFromSan(token,&move) ); 00210 it->pushMovesPath(it->decodeBoard(token.substr(0,2))); 00211 it->pushMovesPath(it->decodeBoard(token.substr(2,2))); 00212 it->makemove(&move); 00213 } 00214 } 00215 it->lockMutex(false); 00216 } else if (token == "go") { 00217 int wtime = 200000; //5 min 00218 int btime = 200000; 00219 int winc = 0; 00220 int binc = 0; 00221 bool forceTime=false; 00222 while (!uip.eof()) { 00223 getToken(uip,token); 00224 if (token == "wtime") 00225 uip >> wtime; 00226 else if (token == "btime") 00227 uip >> btime; 00228 else if (token == "winc") 00229 uip >> winc; 00230 else if (token == "binc") 00231 uip >> binc; 00232 else if (token == "movetime") { 00233 int tim; 00234 uip >> tim; 00235 it->setMaxTimeMillsec(tim); 00236 forceTime=true; 00237 } else if (token == "infinite") { 00238 it->setMaxTimeMillsec(0x7FFFFFFF); 00239 forceTime=true; 00240 } else if (token == "ponder") { 00241 it->setPonder(true); 00242 } 00243 } 00244 if (!forceTime) { 00245 if (it->getSide() == WHITE) { 00246 winc -= (int) (winc * 0.1); 00247 it->setMaxTimeMillsec(winc + wtime / 40); 00248 if (btime > wtime) { 00249 it->setMaxTimeMillsec( 00250 it->getMaxTimeMillsec() - (int) (it->getMaxTimeMillsec() * ((135.0 - wtime * 100.0 / btime) / 100.0))); 00251 } 00252 } else { 00253 binc -= (int) (binc * 0.1); 00254 it->setMaxTimeMillsec(binc + btime / 40); 00255 if (wtime > btime) { 00256 it->setMaxTimeMillsec( 00257 it->getMaxTimeMillsec() - (int) (it->getMaxTimeMillsec() * ((135.0 - btime * 100.0 / wtime) / 100.0))); 00258 } 00259 } 00260 lastTime=it->getMaxTimeMillsec() ; 00261 } 00262 if (!it->getUci()) 00263 it->display(); 00264 it->stop(); 00265 it->start(); 00266 knowCommand = true; 00267 } 00268 if (!knowCommand) { 00269 cout << "Unknown command: " << command << endl; 00270 }; 00271 cout << flush; 00272 } 00273 }