00001 //---------------------------------------------------------------------------- 00002 /** @file HexEnvironment.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "HexEnvironment.hpp" 00007 00008 using namespace benzene; 00009 00010 //---------------------------------------------------------------------------- 00011 00012 HexEnvironment::HexEnvironment(int width, int height) 00013 : brd(0) 00014 { 00015 brd.reset(new HexBoard(width, height, ice, buildParam)); 00016 } 00017 00018 void HexEnvironment::NewGame(int width, int height) 00019 { 00020 if (brd->GetPosition().Width() != width 00021 && brd->GetPosition().Height() != height) 00022 { 00023 /** @todo Make board resizable? Until then, make sure all 00024 HexBoard parameters are copied here! */ 00025 bool use_vcs = brd->UseVCs(); 00026 bool use_ice = brd->UseICE(); 00027 bool use_dec = brd->UseDecompositions(); 00028 bool backup = brd->BackupIceInfo(); 00029 brd.reset(new HexBoard(width, height, ice, buildParam)); 00030 brd->SetUseVCs(use_vcs); 00031 brd->SetUseICE(use_ice); 00032 brd->SetUseDecompositions(use_dec); 00033 brd->SetBackupIceInfo(backup); 00034 } 00035 brd->GetPosition().StartNewGame(); 00036 } 00037 00038 HexBoard& HexEnvironment::SyncBoard(const StoneBoard& board) 00039 { 00040 brd->GetPosition().SetPosition(board); 00041 return *brd.get(); 00042 } 00043 00044 //---------------------------------------------------------------------------- 00045 00046 HexEnvironmentCommands::HexEnvironmentCommands(HexEnvironment& env) 00047 : m_env(env) 00048 { 00049 } 00050 00051 HexEnvironmentCommands::~HexEnvironmentCommands() 00052 { 00053 } 00054 00055 void HexEnvironmentCommands::Register(GtpEngine& e, const std::string name) 00056 { 00057 Register(e, "param_"+ name+ "_ice", &HexEnvironmentCommands::ParamICE); 00058 Register(e, "param_"+ name+ "_vc", &HexEnvironmentCommands::ParamVC); 00059 Register(e, "param_"+ name+ "_board", &HexEnvironmentCommands::ParamBoard); 00060 } 00061 00062 void HexEnvironmentCommands::Register(GtpEngine& engine, 00063 const std::string& command, 00064 GtpCallback<HexEnvironmentCommands>::Method method) 00065 { 00066 engine.Register(command, 00067 new GtpCallback<HexEnvironmentCommands>(this, method)); 00068 } 00069 00070 void HexEnvironmentCommands::ParamICE(HtpCommand& cmd) 00071 { 00072 ICEngine& ice = m_env.ice; 00073 if (cmd.NuArg() == 0) 00074 { 00075 cmd << '\n' 00076 << "[bool] backup_opponent_dead " 00077 << ice.BackupOpponentDead() << '\n' 00078 << "[bool] find_all_pattern_dominators " 00079 << ice.FindAllPatternDominators() << '\n' 00080 << "[bool] find_all_pattern_killers " 00081 << ice.FindAllPatternKillers() << '\n' 00082 << "[bool] find_permanently_inferior " 00083 << ice.FindPermanentlyInferior() << '\n' 00084 << "[bool] find_presimplicial_pairs " 00085 << ice.FindPresimplicialPairs() << '\n' 00086 << "[bool] find_three_sided_dead_regions " 00087 << ice.FindThreeSidedDeadRegions() << '\n' 00088 << "[bool] iterative_dead_regions " 00089 << ice.IterativeDeadRegions() << '\n' 00090 << "[bool] use_hand_coded_patterns " 00091 << ice.UseHandCodedPatterns() << '\n'; 00092 } 00093 else if (cmd.NuArg() == 2) 00094 { 00095 std::string name = cmd.Arg(0); 00096 if (name == "backup_opponent_dead") 00097 ice.SetBackupOpponentDead(cmd.BoolArg(1)); 00098 else if (name == "find_all_pattern_dominators") 00099 ice.SetFindAllPatternDominators(cmd.BoolArg(1)); 00100 else if (name == "find_all_pattern_killers") 00101 ice.SetFindAllPatternKillers(cmd.BoolArg(1)); 00102 else if (name == "find_permanently_inferior") 00103 ice.SetFindPermanentlyInferior(cmd.BoolArg(1)); 00104 else if (name == "find_presimplicial_pairs") 00105 ice.SetFindPresimplicialPairs(cmd.BoolArg(1)); 00106 else if (name == "find_three_sided_dead_regions") 00107 ice.SetFindThreeSidedDeadRegions(cmd.BoolArg(1)); 00108 else if (name == "iterative_dead_regions") 00109 ice.SetIterativeDeadRegions(cmd.BoolArg(1)); 00110 else if (name == "use_hand_coded_patterns") 00111 ice.SetUseHandCodedPatterns(cmd.BoolArg(1)); 00112 else 00113 throw HtpFailure() << "Unknown parameter: " << name; 00114 } 00115 else 00116 throw HtpFailure() << "Expected 0 or 2 arguments"; 00117 } 00118 00119 void HexEnvironmentCommands::ParamVC(HtpCommand& cmd) 00120 { 00121 HexBoard& brd = *m_env.brd; 00122 VCBuilderParam& param = brd.Builder().Parameters(); 00123 if (cmd.NuArg() == 0) 00124 { 00125 cmd << '\n' 00126 << "[bool] abort_on_winning_connection " 00127 << param.abort_on_winning_connection << '\n' 00128 << "[bool] and_over_edge " 00129 << param.and_over_edge << '\n' 00130 << "[bool] use_greedy_union " 00131 << param.use_greedy_union << '\n' 00132 << "[bool] use_patterns " 00133 << param.use_patterns << '\n' 00134 << "[bool] use_non_edge_patterns " 00135 << param.use_non_edge_patterns << '\n' 00136 << "[string] max_ors " 00137 << param.max_ors << '\n' 00138 << "[string] softlimit_full " 00139 << brd.Cons(BLACK).SoftLimit(VC::FULL) << '\n' 00140 << "[string] softlimit_semi " 00141 << brd.Cons(BLACK).SoftLimit(VC::SEMI) << '\n'; 00142 } 00143 else if (cmd.NuArg() == 2) 00144 { 00145 std::string name = cmd.Arg(0); 00146 if (name == "abort_on_winning_connection") 00147 param.abort_on_winning_connection = cmd.BoolArg(1); 00148 else if (name == "and_over_edge") 00149 param.and_over_edge = cmd.BoolArg(1); 00150 else if (name == "use_greedy_union") 00151 param.use_greedy_union = cmd.BoolArg(1); 00152 else if (name == "use_patterns") 00153 param.use_patterns = cmd.BoolArg(1); 00154 else if (name == "use_non_edge_patterns") 00155 param.use_non_edge_patterns = cmd.BoolArg(1); 00156 else if (name == "max_ors") 00157 param.max_ors = cmd.IntArg(1); 00158 else if (name == "softlimit_full") 00159 { 00160 int limit = cmd.IntArg(1, 0); 00161 brd.Cons(BLACK).SetSoftLimit(VC::FULL, limit); 00162 brd.Cons(WHITE).SetSoftLimit(VC::FULL, limit); 00163 } 00164 else if (name == "softlimit_semi") 00165 { 00166 int limit = cmd.IntArg(1, 0); 00167 brd.Cons(BLACK).SetSoftLimit(VC::SEMI, limit); 00168 brd.Cons(WHITE).SetSoftLimit(VC::SEMI, limit); 00169 } 00170 else 00171 throw HtpFailure() << "Unknown parameter: " << name; 00172 } 00173 else 00174 throw HtpFailure() << "Expected 0 or 2 arguments"; 00175 } 00176 00177 void HexEnvironmentCommands::ParamBoard(HtpCommand& cmd) 00178 { 00179 HexBoard& brd = *m_env.brd; 00180 if (cmd.NuArg() == 0) 00181 { 00182 cmd << '\n' 00183 << "[bool] backup_ice_info " 00184 << brd.BackupIceInfo() << '\n' 00185 << "[bool] use_decompositions " 00186 << brd.UseDecompositions() << '\n' 00187 << "[bool] use_ice " 00188 << brd.UseICE() << '\n' 00189 << "[bool] use_vcs " 00190 << brd.UseVCs() << '\n'; 00191 } 00192 else if (cmd.NuArg() == 2) 00193 { 00194 std::string name = cmd.Arg(0); 00195 if (name == "backup_ice_info") 00196 brd.SetBackupIceInfo(cmd.BoolArg(1)); 00197 else if (name == "use_decompositions") 00198 brd.SetUseDecompositions(cmd.BoolArg(1)); 00199 else if (name == "use_ice") 00200 brd.SetUseICE(cmd.BoolArg(1)); 00201 else if (name == "use_vcs") 00202 brd.SetUseVCs(cmd.BoolArg(1)); 00203 else 00204 throw HtpFailure() << "Unknown parameter: " << name; 00205 } 00206 else 00207 throw HtpFailure() << "Expected 0 or 2 arguments"; 00208 } 00209 00210 //----------------------------------------------------------------------------