Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef GO_GTPENGINE_H
00007 #define GO_GTPENGINE_H
00008
00009 #include <sstream>
00010 #include <boost/filesystem/path.hpp>
00011 #include <boost/scoped_ptr.hpp>
00012 #include "GoBoard.h"
00013 #include "GoBook.h"
00014 #include "GoAutoBook.h"
00015 #include "GoGame.h"
00016 #include "GoPlayer.h"
00017 #include "GoStaticLadder.h"
00018 #include "GoTimeSettings.h"
00019 #include "GtpEngine.h"
00020 #include "SgGtpCommands.h"
00021 #include "SgPointArray.h"
00022 #include "SgMpiSynchronizer.h"
00023
00024
00025
00026
00027
00028 class GoGtpEngine
00029 : public GtpEngine
00030 {
00031 public:
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 virtual void CmdAllLegal(GtpCommand&);
00081 virtual void CmdAllMoveValues(GtpCommand&);
00082 virtual void CmdAnalyzeCommands(GtpCommand&);
00083 virtual void CmdBoard(GtpCommand&);
00084 virtual void CmdBoardSize(GtpCommand&);
00085 virtual void CmdClearBoard(GtpCommand&);
00086 virtual void CmdClock(GtpCommand&);
00087 virtual void CmdFinalScore(GtpCommand&);
00088 virtual void CmdFixedHandicap(GtpCommand&);
00089 virtual void CmdGameOver(GtpCommand&);
00090 virtual void CmdGenMove(GtpCommand&);
00091 virtual void CmdGenMoveCleanup(GtpCommand&);
00092 virtual void CmdGetKomi(GtpCommand&);
00093 virtual void CmdGGUndo(GtpCommand&);
00094 virtual void CmdInterrupt(GtpCommand&);
00095 virtual void CmdIsLegal(GtpCommand&);
00096 virtual void CmdKgsTimeSettings(GtpCommand& cmd);
00097 virtual void CmdKomi(GtpCommand&);
00098 virtual void CmdListStones(GtpCommand&);
00099 virtual void CmdLoadSgf(GtpCommand&);
00100 virtual void CmdName(GtpCommand&);
00101 virtual void CmdParam(GtpCommand&);
00102 virtual void CmdParamRules(GtpCommand&);
00103 virtual void CmdParamTimecontrol(GtpCommand&);
00104 virtual void CmdPlaceFreeHandicap(GtpCommand&);
00105 virtual void CmdPlay(GtpCommand&);
00106 virtual void CmdPlayerBoard(GtpCommand&);
00107 virtual void CmdPlaySequence(GtpCommand&);
00108 virtual void CmdPointNumbers(GtpCommand&);
00109 virtual void CmdPointInfo(GtpCommand&);
00110 virtual void CmdQuit(GtpCommand& cmd);
00111 virtual void CmdRegGenMove(GtpCommand&);
00112 virtual void CmdRegGenMoveToPlay(GtpCommand&);
00113 virtual void CmdRules(GtpCommand&);
00114 virtual void CmdSaveSgf(GtpCommand&);
00115 virtual void CmdSentinelFile(GtpCommand&);
00116 virtual void CmdSetFreeHandicap(GtpCommand&);
00117 virtual void CmdSetInfo(GtpCommand&);
00118 virtual void CmdSetup(GtpCommand&);
00119 virtual void CmdSetupPlayer(GtpCommand&);
00120 virtual void CmdShowBoard(GtpCommand&);
00121 virtual void CmdTimeLastMove(GtpCommand&);
00122 virtual void CmdTimeLeft(GtpCommand&);
00123 virtual void CmdTimeSettings(GtpCommand&);
00124 virtual void CmdUndo(GtpCommand&);
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 GoGtpEngine(int fixedBoardSize = 0, const char* programPath = 0,
00139 bool noPlayer = false, bool noHandicap = false);
00140
00141 ~GoGtpEngine();
00142
00143 GoBook& Book();
00144
00145 const GoGame& Game() const;
00146
00147 const GoBoard& Board() const;
00148
00149
00150
00151 void SetPlayer(GoPlayer* player);
00152
00153
00154 GoPlayer& Player() const;
00155
00156
00157 void DumpState(std::ostream& out) const;
00158
00159
00160
00161
00162 void SetAutoSave(const std::string& prefix);
00163
00164
00165
00166
00167
00168 void SetStatisticsFile(const std::string& fileName);
00169
00170
00171
00172 void SetAutoShowBoard(bool showBoard);
00173
00174
00175
00176
00177
00178
00179
00180
00181 void SetDebugToComment(bool debugToComment);
00182
00183
00184
00185
00186
00187
00188 void SetMaxClearBoard(int n);
00189
00190
00191
00192
00193
00194 void SetNamedRules(const std::string& namedRules);
00195
00196
00197 void SetTimeLimit(double timeLimit);
00198
00199
00200 double TimeLimit();
00201
00202 #if GTPENGINE_PONDER
00203
00204
00205 void Ponder();
00206
00207
00208
00209 void StopPonder();
00210
00211
00212
00213 void InitPonder();
00214 #endif // GTPENGINE_PONDER
00215
00216 #if GTPENGINE_INTERRUPT
00217
00218
00219 void Interrupt();
00220 #endif // GTPENGINE_INTERRUPT
00221
00222 void SetMpiSynchronizer(const SgMpiSynchronizerHandle &m_handle);
00223
00224 SgMpiSynchronizerHandle MpiSynchronizer();
00225
00226 const SgMpiSynchronizerHandle MpiSynchronizer() const;
00227
00228 protected:
00229
00230
00231
00232
00233
00234
00235
00236 GoPlayer* m_player;
00237
00238 boost::scoped_ptr<GoAutoBook> m_autoBook;
00239
00240
00241
00242
00243
00244 void BeforeHandleCommand();
00245
00246
00247
00248
00249 void BeforeWritingResponse();
00250
00251 void BoardChanged();
00252
00253 void CheckLegal(std::string message, SgBlackWhite color, SgPoint move,
00254 bool checkOnlyOccupied);
00255
00256 void CheckMaxClearBoard();
00257
00258 void CheckMoveStackOverflow() const;
00259
00260 SgPoint GenMove(SgBlackWhite color, bool ignoreClock);
00261
00262
00263
00264
00265
00266 void RespondNumberArray(GtpCommand& cmd, const SgPointArray<int>& array,
00267 int scale);
00268
00269 void Init(int size);
00270
00271
00272
00273 void Play(SgBlackWhite color, SgPoint move);
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 virtual std::vector<std::string> CreateStatisticsSlots();
00290
00291
00292
00293 virtual void AddPlayStatistics();
00294
00295
00296 void AddStatistics(const std::string& key, const std::string& value);
00297
00298
00299 template<typename T>
00300 void AddStatistics(const std::string& key, const T& value);
00301
00302
00303
00304
00305 SgBlackWhite BlackWhiteArg(const GtpCommand& cmd,
00306 std::size_t number) const;
00307
00308 SgEmptyBlackWhite EmptyBlackWhiteArg(const GtpCommand& cmd,
00309 std::size_t number) const;
00310
00311 SgPoint EmptyPointArg(const GtpCommand& cmd, std::size_t number) const;
00312
00313 SgPoint MoveArg(const GtpCommand& cmd, std::size_t number) const;
00314
00315
00316 SgPoint PointArg(const GtpCommand& cmd) const;
00317
00318 SgPoint PointArg(const GtpCommand& cmd, std::size_t number) const;
00319
00320 SgVector<SgPoint> PointListArg(const GtpCommand& cmd,
00321 std::size_t number) const;
00322
00323 SgVector<SgPoint> PointListArg(const GtpCommand& cmd) const;
00324
00325 SgPoint StoneArg(const GtpCommand& cmd, std::size_t number) const;
00326
00327 void RulesChanged();
00328
00329 private:
00330 bool m_noPlayer;
00331
00332
00333 bool m_acceptIllegal;
00334
00335
00336 bool m_autoSave;
00337
00338
00339 bool m_autoShowBoard;
00340
00341
00342 bool m_debugToComment;
00343
00344 bool m_useBook;
00345
00346
00347
00348
00349
00350
00351
00352 bool m_isPonderPosition;
00353
00354 int m_fixedBoardSize;
00355
00356
00357
00358
00359 int m_maxClearBoard;
00360
00361
00362
00363 int m_numberClearBoard;
00364
00365 double m_timeLastMove;
00366
00367
00368 double m_timeLimit;
00369
00370
00371 double m_overhead;
00372
00373
00374 GoTimeSettings m_timeSettings;
00375
00376
00377
00378 GoRules m_defaultRules;
00379
00380
00381 GoGame m_game;
00382
00383 SgGtpCommands m_sgCommands;
00384
00385 GoBook m_book;
00386
00387 GoBookCommands m_bookCommands;
00388
00389 std::string m_autoSaveFileName;
00390
00391 std::string m_autoSavePrefix;
00392
00393
00394 boost::filesystem::path m_sentinelFile;
00395
00396 std::string m_statisticsFile;
00397
00398
00399 std::vector<std::string> m_statisticsSlots;
00400
00401
00402 std::vector<std::string> m_statisticsValues;
00403
00404 SgMpiSynchronizerHandle m_mpiSynchronizer;
00405
00406 void ApplyTimeSettings();
00407
00408 void AutoSave() const;
00409
00410 void CheckBoardEmpty() const;
00411
00412 void CreateAutoSaveFileName();
00413
00414 void GameFinished();
00415
00416 SgPoint GenBookMove(SgBlackWhite toPlay);
00417
00418 void InitStatistics();
00419
00420 void PlaceHandicap(const SgVector<SgPoint>& stones);
00421
00422 void SaveGame(const std::string& fileName) const;
00423
00424 void SaveStatistics();
00425
00426 void StartStatistics();
00427
00428 void Undo(int n);
00429
00430 static void WriteBoardInfo(GtpCommand& cmd, const GoBoard& bd);
00431 };
00432
00433 template<typename T>
00434 void GoGtpEngine::AddStatistics(const std::string& key, const T& value)
00435 {
00436 std::ostringstream s;
00437 s << value;
00438 AddStatistics(key, s.str());
00439 }
00440
00441 inline const GoBoard& GoGtpEngine::Board() const
00442 {
00443 return m_game.Board();
00444 }
00445
00446 inline GoBook& GoGtpEngine::Book()
00447 {
00448 return m_book;
00449 }
00450
00451 inline const GoGame& GoGtpEngine::Game() const
00452 {
00453 return m_game;
00454 }
00455
00456 inline void GoGtpEngine::SetDebugToComment(bool debugToComment)
00457 {
00458 m_debugToComment = debugToComment;
00459 }
00460
00461 inline void GoGtpEngine::SetMaxClearBoard(int n)
00462 {
00463 m_maxClearBoard = n;
00464 }
00465
00466 inline void GoGtpEngine::SetTimeLimit(double timeLimit)
00467 {
00468 m_timeLimit = timeLimit;
00469 }
00470
00471 inline double GoGtpEngine::TimeLimit()
00472 {
00473 return m_timeLimit;
00474 }
00475
00476
00477
00478
00479
00480 class GoGtpAssertionHandler
00481 : public SgAssertionHandler
00482 {
00483 public:
00484 GoGtpAssertionHandler(const GoGtpEngine& engine);
00485
00486 void Run();
00487
00488 private:
00489 const GoGtpEngine& m_engine;
00490 };
00491
00492
00493
00494 #endif // GO_GTPENGINE_H