00001
00002
00003
00004
00005
00006
00007 #ifndef GO_GTPENGINE_H
00008 #define GO_GTPENGINE_H
00009
00010 #include <sstream>
00011 #include <boost/filesystem/path.hpp>
00012 #include <boost/scoped_ptr.hpp>
00013 #include "GoBoard.h"
00014 #include "GoBook.h"
00015 #include "GoAutoBook.h"
00016 #include "GoGame.h"
00017 #include "GoGtpTimeSettings.h"
00018 #include "GoStaticLadder.h"
00019 #include "GtpEngine.h"
00020 #include "SgGtpCommands.h"
00021 #include "SgPointArray.h"
00022 #include "SgMpiSynchronizer.h"
00023
00024
00025
00026
00027
00028
00029 class GoGtpEngine
00030 : public GtpEngine
00031 {
00032 public:
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
00081 virtual void CmdAllLegal(GtpCommand&);
00082 virtual void CmdAllMoveValues(GtpCommand&);
00083 virtual void CmdAnalyzeCommands(GtpCommand&);
00084 virtual void CmdBoard(GtpCommand&);
00085 virtual void CmdBoardSize(GtpCommand&);
00086 virtual void CmdClearBoard(GtpCommand&);
00087 virtual void CmdClock(GtpCommand&);
00088 virtual void CmdFinalScore(GtpCommand&);
00089 virtual void CmdFixedHandicap(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
00139
00140
00141 GoGtpEngine(GtpInputStream& in, GtpOutputStream& out, int fixedBoardSize,
00142 const char* programPath = 0, bool noPlayer = false,
00143 bool noHandicap = false);
00144
00145 ~GoGtpEngine();
00146
00147 GoBook& Book();
00148
00149
00150 void DumpState(std::ostream& out) const;
00151
00152
00153
00154
00155
00156 void SetAutoSave(const std::string& prefix);
00157
00158
00159
00160
00161
00162
00163 void SetStatisticsFile(const std::string& fileName);
00164
00165
00166
00167
00168 void SetAutoShowBoard(bool showBoard);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 void SetDebugToComment(bool debugToComment);
00179
00180
00181
00182
00183
00184
00185
00186 void SetMaxClearBoard(int n);
00187
00188
00189
00190
00191
00192
00193 void SetNamedRules(const std::string& namedRules);
00194
00195
00196 void SetTimeLimit(double timeLimit);
00197
00198
00199 double TimeLimit();
00200
00201 #if GTPENGINE_PONDER
00202
00203
00204
00205 void Ponder();
00206
00207
00208
00209
00210 void StopPonder();
00211
00212
00213
00214
00215 void InitPonder();
00216 #endif // GTPENGINE_PONDER
00217
00218 #if GTPENGINE_INTERRUPT
00219
00220
00221
00222 void Interrupt();
00223 #endif // GTPENGINE_INTERRUPT
00224
00225 void SetMpiSynchronizer(const SgMpiSynchronizerHandle &m_handle);
00226
00227 SgMpiSynchronizerHandle MpiSynchronizer();
00228
00229 const SgMpiSynchronizerHandle MpiSynchronizer() const;
00230
00231 protected:
00232
00233
00234
00235
00236
00237
00238
00239
00240 GoPlayer* m_player;
00241
00242 boost::scoped_ptr<GoAutoBook> m_autoBook;
00243
00244
00245
00246
00247
00248
00249 void BeforeHandleCommand();
00250
00251
00252
00253
00254
00255 void BeforeWritingResponse();
00256
00257 GoBoard& Board();
00258
00259 const GoBoard& Board() const;
00260
00261 void BoardChanged();
00262
00263 void CheckLegal(std::string message, SgBlackWhite color, SgPoint move,
00264 bool checkOnlyOccupied);
00265
00266 void CheckMaxClearBoard();
00267
00268 void CheckMoveStackOverflow() const;
00269
00270 SgPoint GenMove(SgBlackWhite color, bool ignoreClock);
00271
00272 GoGame& GetGame();
00273
00274 const GoGame& GetGame() const;
00275
00276
00277 GoPlayer& Player() const;
00278
00279
00280
00281
00282
00283
00284 void RespondNumberArray(GtpCommand& cmd, const SgPointArray<int>& array,
00285 int scale);
00286
00287
00288
00289
00290 void SetPlayer(GoPlayer* player);
00291
00292 void Init(int size);
00293
00294
00295
00296
00297 void Play(SgBlackWhite color, SgPoint move);
00298
00299 protected:
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 virtual std::vector<std::string> CreateStatisticsSlots();
00316
00317
00318
00319
00320 virtual void AddPlayStatistics();
00321
00322
00323 void AddStatistics(const std::string& key, const std::string& value);
00324
00325
00326 template<typename T>
00327 void AddStatistics(const std::string& key, const T& value);
00328
00329
00330
00331
00332 SgBlackWhite BlackWhiteArg(const GtpCommand& cmd,
00333 std::size_t number) const;
00334
00335 SgEmptyBlackWhite EmptyBlackWhiteArg(const GtpCommand& cmd,
00336 std::size_t number) const;
00337
00338 SgPoint EmptyPointArg(const GtpCommand& cmd, std::size_t number) const;
00339
00340 SgPoint MoveArg(const GtpCommand& cmd, std::size_t number) const;
00341
00342
00343 SgPoint PointArg(const GtpCommand& cmd) const;
00344
00345 SgPoint PointArg(const GtpCommand& cmd, std::size_t number) const;
00346
00347 SgVector<SgPoint> PointListArg(const GtpCommand& cmd,
00348 std::size_t number) const;
00349
00350 SgVector<SgPoint> PointListArg(const GtpCommand& cmd) const;
00351
00352 SgPoint StoneArg(const GtpCommand& cmd, std::size_t number) const;
00353
00354 void RulesChanged();
00355
00356 private:
00357 bool m_noPlayer;
00358
00359
00360 bool m_acceptIllegal;
00361
00362
00363 bool m_autoSave;
00364
00365
00366 bool m_autoShowBoard;
00367
00368
00369 bool m_debugToComment;
00370
00371 int m_fixedBoardSize;
00372
00373
00374
00375
00376
00377 int m_maxClearBoard;
00378
00379
00380
00381
00382 int m_numberClearBoard;
00383
00384 double m_timeLastMove;
00385
00386
00387 double m_timeLimit;
00388
00389
00390 double m_overhead;
00391
00392
00393 GoGtpTimeSettings m_timeSettings;
00394
00395
00396
00397
00398 GoRules m_defaultRules;
00399
00400 GoBoard m_board;
00401
00402
00403 GoGame m_game;
00404
00405 SgGtpCommands m_sgCommands;
00406
00407 GoBook m_book;
00408
00409 GoBookCommands m_bookCommands;
00410
00411 std::string m_autoSaveFileName;
00412
00413 std::string m_autoSavePrefix;
00414
00415
00416 boost::filesystem::path m_sentinelFile;
00417
00418 std::string m_statisticsFile;
00419
00420
00421 std::vector<std::string> m_statisticsSlots;
00422
00423
00424 std::vector<std::string> m_statisticsValues;
00425
00426 SgMpiSynchronizerHandle m_mpiSynchronizer;
00427
00428 void AddPlayerProp(SgBlackWhite color, const std::string& name,
00429 bool overwrite);
00430
00431 void ApplyTimeSettings();
00432
00433 void AutoSave() const;
00434
00435 void CheckBoardEmpty() const;
00436
00437 void CreateAutoSaveFileName();
00438
00439 void GameFinished();
00440
00441 void InitStatistics();
00442
00443 void PlaceHandicap(const SgVector<SgPoint>& stones);
00444
00445 void SaveGame(const std::string& fileName) const;
00446
00447 void SaveStatistics();
00448
00449 void StartStatistics();
00450
00451 void Undo(int n);
00452
00453 static void WriteBoardInfo(GtpCommand& cmd, const GoBoard& bd);
00454 };
00455
00456 template<typename T>
00457 void GoGtpEngine::AddStatistics(const std::string& key, const T& value)
00458 {
00459 std::ostringstream s;
00460 s << value;
00461 AddStatistics(key, s.str());
00462 }
00463
00464 inline GoBoard& GoGtpEngine::Board()
00465 {
00466 return m_board;
00467 }
00468
00469 inline const GoBoard& GoGtpEngine::Board() const
00470 {
00471 return m_board;
00472 }
00473
00474 inline GoBook& GoGtpEngine::Book()
00475 {
00476 return m_book;
00477 }
00478
00479 inline GoGame& GoGtpEngine::GetGame()
00480 {
00481 return m_game;
00482 }
00483
00484 inline const GoGame& GoGtpEngine::GetGame() const
00485 {
00486 return m_game;
00487 }
00488
00489 inline void GoGtpEngine::SetDebugToComment(bool debugToComment)
00490 {
00491 m_debugToComment = debugToComment;
00492 }
00493
00494 inline void GoGtpEngine::SetMaxClearBoard(int n)
00495 {
00496 m_maxClearBoard = n;
00497 }
00498
00499 inline void GoGtpEngine::SetTimeLimit(double timeLimit)
00500 {
00501 m_timeLimit = timeLimit;
00502 }
00503
00504 inline double GoGtpEngine::TimeLimit()
00505 {
00506 return m_timeLimit;
00507 }
00508
00509
00510
00511
00512
00513
00514 class GoGtpAssertionHandler
00515 : public SgAssertionHandler
00516 {
00517 public:
00518 GoGtpAssertionHandler(const GoGtpEngine& engine);
00519
00520 void Run();
00521
00522 private:
00523 const GoGtpEngine& m_engine;
00524 };
00525
00526
00527
00528 #endif // GO_GTPENGINE_H