00001 //---------------------------------------------------------------------------- 00002 /** @file FuegoTestEngine.cpp 00003 See FuegoTestEngine.h */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 #include "FuegoTestEngine.h" 00008 00009 #include <boost/preprocessor/stringize.hpp> 00010 #include <boost/algorithm/string.hpp> 00011 #include "GoGtpCommandUtil.h" 00012 #include "GoGtpExtraCommands.h" 00013 #include "SpAveragePlayer.h" 00014 #include "SpCapturePlayer.h" 00015 #include "SpDumbTacticalPlayer.h" 00016 #include "SpGreedyPlayer.h" 00017 #include "SpInfluencePlayer.h" 00018 #include "SpLadderPlayer.h" 00019 #include "SpLibertyPlayer.h" 00020 #include "SpMaxEyePlayer.h" 00021 #include "SpMinLibPlayer.h" 00022 #include "SpRandomPlayer.h" 00023 #include "SpSafePlayer.h" 00024 00025 using namespace std; 00026 using boost::trim_copy; 00027 00028 //---------------------------------------------------------------------------- 00029 00030 FuegoTestEngine::FuegoTestEngine(int fixedBoardSize, const char* programPath, 00031 const string& player) 00032 : GoGtpEngine(fixedBoardSize, programPath), 00033 m_extraCommands(Board()), 00034 m_safetyCommands(Board()) 00035 { 00036 Register("fuegotest_param", &FuegoTestEngine::CmdParam, this); 00037 m_extraCommands.Register(*this); 00038 m_safetyCommands.Register(*this); 00039 SetPlayer(player); 00040 } 00041 00042 FuegoTestEngine::~FuegoTestEngine() 00043 { 00044 } 00045 00046 void FuegoTestEngine::CmdAnalyzeCommands(GtpCommand& cmd) 00047 { 00048 GoGtpEngine::CmdAnalyzeCommands(cmd); 00049 m_extraCommands.AddGoGuiAnalyzeCommands(cmd); 00050 m_safetyCommands.AddGoGuiAnalyzeCommands(cmd); 00051 cmd << 00052 "param/FuegoTest Param/fuegotest_param\n"; 00053 string response = cmd.Response(); 00054 cmd.SetResponse(GoGtpCommandUtil::SortResponseAnalyzeCommands(response)); 00055 } 00056 00057 void FuegoTestEngine::CmdName(GtpCommand& cmd) 00058 { 00059 if (m_playerId == "") 00060 cmd << "FuegoTest"; 00061 else 00062 GoGtpEngine::CmdName(cmd); 00063 } 00064 00065 /** Player selection. 00066 This command is compatible with the GoGui analyze command type "param". 00067 00068 Parameters: 00069 @arg @c player Player id as in FuegoTestEngine::SetPlayer */ 00070 void FuegoTestEngine::CmdParam(GtpCommand& cmd) 00071 { 00072 cmd.CheckNuArgLessEqual(2); 00073 if (cmd.NuArg() == 0) 00074 { 00075 cmd << 00076 "[list/<none>/average/capture/dumbtactic/greedy/influence/" 00077 "ladder/liberty/maxeye/minlib/no-search/random/safe] player " 00078 << (m_playerId == "" ? "<none>" : m_playerId) << '\n'; 00079 } 00080 else if (cmd.NuArg() >= 1 && cmd.NuArg() <= 2) 00081 { 00082 string name = cmd.Arg(0); 00083 if (name == "player") 00084 { 00085 try 00086 { 00087 string id = trim_copy(cmd.RemainingLine(0)); 00088 if (id == "<none>") 00089 id = ""; 00090 SetPlayer(id); 00091 } 00092 catch (const SgException& e) 00093 { 00094 throw GtpFailure(e.what()); 00095 } 00096 } 00097 else 00098 throw GtpFailure() << "unknown parameter: " << name; 00099 } 00100 else 00101 throw GtpFailure() << "need 0 or 2 arguments"; 00102 } 00103 00104 void FuegoTestEngine::CmdVersion(GtpCommand& cmd) 00105 { 00106 #ifdef VERSION 00107 cmd << BOOST_PP_STRINGIZE(VERSION); 00108 #else 00109 cmd << "(" __DATE__ ")"; 00110 #endif 00111 #ifndef NDEBUG 00112 cmd << " (dbg)"; 00113 #endif 00114 } 00115 00116 GoPlayer* FuegoTestEngine::CreatePlayer(const string& playerId) 00117 { 00118 const GoBoard& bd = Board(); 00119 if (playerId == "") 00120 return 0; 00121 if (playerId == "average") 00122 return new SpAveragePlayer(bd); 00123 if (playerId == "capture") 00124 return new SpCapturePlayer(bd); 00125 if (playerId == "dumbtactic") 00126 return new SpDumbTacticalPlayer(bd); 00127 if (playerId == "greedy") 00128 return new SpGreedyPlayer(bd); 00129 if (playerId == "influence") 00130 return new SpInfluencePlayer(bd); 00131 if (playerId == "ladder") 00132 return new SpLadderPlayer(bd); 00133 if (playerId == "liberty") 00134 return new SpLibertyPlayer(bd); 00135 if (playerId == "maxeye") 00136 return new SpMaxEyePlayer(bd, true); 00137 if (playerId == "minlib") 00138 return new SpMinLibPlayer(bd); 00139 if (playerId == "random") 00140 return new SpRandomPlayer(bd); 00141 if (playerId == "safe") 00142 return new SpSafePlayer(bd); 00143 throw SgException("unknown player " + playerId); 00144 } 00145 00146 void FuegoTestEngine::SetPlayer(const string& playerId) 00147 { 00148 GoPlayer* player = CreatePlayer(playerId); 00149 GoGtpEngine::SetPlayer(player); 00150 m_playerId = playerId; 00151 } 00152 00153 //----------------------------------------------------------------------------