00001 //---------------------------------------------------------------------------- 00002 /** @file BenzeneTestEngine.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 00008 #include "Misc.hpp" 00009 #include "BenzeneTestEngine.hpp" 00010 #include "PerfectPlayer.hpp" 00011 #include "SwapCheck.hpp" 00012 00013 using namespace benzene; 00014 00015 //---------------------------------------------------------------------------- 00016 00017 BenzeneTestEngine::BenzeneTestEngine(int boardsize) 00018 : BenzeneHtpEngine(boardsize), 00019 m_player(0) 00020 { 00021 RegisterCmd("set_player", &BenzeneTestEngine::CmdSetPlayer); 00022 RegisterCmd("param_player", &BenzeneTestEngine::CmdParamPlayer); 00023 } 00024 00025 BenzeneTestEngine::~BenzeneTestEngine() 00026 { 00027 } 00028 00029 //---------------------------------------------------------------------------- 00030 00031 void BenzeneTestEngine::RegisterCmd(const std::string& name, 00032 GtpCallback<BenzeneTestEngine>::Method method) 00033 { 00034 Register(name, new GtpCallback<BenzeneTestEngine>(this, method)); 00035 } 00036 00037 //---------------------------------------------------------------------------- 00038 00039 double BenzeneTestEngine::TimeForMove(HexColor color) 00040 { 00041 return m_game.TimeRemaining(color); 00042 } 00043 00044 /** Generates a move. */ 00045 HexPoint BenzeneTestEngine::GenMove(HexColor color, bool useGameClock) 00046 { 00047 if (m_player == 0) 00048 throw HtpFailure() << "No player specified!"; 00049 SG_UNUSED(useGameClock); 00050 if (SwapCheck::PlaySwap(m_game, color)) 00051 return SWAP_PIECES; 00052 HexEval score; 00053 double maxTime = TimeForMove(color); 00054 return m_player->GenMove(HexState(m_game.Board(), color), m_game, 00055 m_pe.SyncBoard(m_game.Board()), maxTime, score); 00056 } 00057 00058 void BenzeneTestEngine::CmdSetPlayer(HtpCommand& cmd) 00059 { 00060 cmd.CheckNuArg(1); 00061 std::string name = cmd.Arg(0); 00062 if (name == "perfect") 00063 m_player.reset(new PerfectPlayer(m_dfpnSolver, m_dfpnPositions)); 00064 else if (name == "none") 00065 m_player.reset(0); 00066 else 00067 throw HtpFailure() << "Unknown player name!"; 00068 } 00069 00070 void BenzeneTestEngine::CmdParamPlayer(HtpCommand& cmd) 00071 { 00072 if (m_player.get() == 0) 00073 throw HtpFailure() << "No player specified!"; 00074 if (m_player->Name() == "perfect") 00075 { 00076 PerfectPlayer* player = dynamic_cast<PerfectPlayer*>(m_player.get()); 00077 if (!player) 00078 throw HtpFailure() << "Not an instance of PerfectPlayer!"; 00079 if (cmd.NuArg() == 0) 00080 { 00081 cmd << '\n' 00082 << "[bool] propagate_backwards " 00083 << player->PropagateBackwards() << '\n' 00084 << "[string] max_time " 00085 << player->MaxTime() << '\n'; 00086 } 00087 else if (cmd.NuArg() == 2) 00088 { 00089 std::string name = cmd.Arg(0); 00090 if (name == "max_time") 00091 player->SetMaxTime(cmd.FloatArg(1)); 00092 else if (name == "propagate_backwards") 00093 player->SetPropagateBackwards(cmd.BoolArg(1)); 00094 else 00095 throw HtpFailure() << "Unknown parameter: " << name; 00096 } 00097 else 00098 throw HtpFailure("Expected 0 or 2 arguments"); 00099 } 00100 else 00101 throw HtpFailure("No parameters for this player!"); 00102 } 00103 00104 //---------------------------------------------------------------------------- 00105 // Pondering 00106 00107 #if GTPENGINE_PONDER 00108 00109 void BenzeneTestEngine::InitPonder() 00110 { 00111 } 00112 00113 void BenzeneTestEngine::Ponder() 00114 { 00115 } 00116 00117 void BenzeneTestEngine::StopPonder() 00118 { 00119 } 00120 00121 #endif // GTPENGINE_PONDER 00122 00123 //----------------------------------------------------------------------------