00001 //---------------------------------------------------------------------------- 00002 /** @file VCCommands.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "BoardUtils.hpp" 00007 #include "EndgameUtils.hpp" 00008 #include "VCCommands.hpp" 00009 #include "VCUtils.hpp" 00010 00011 using namespace benzene; 00012 00013 //---------------------------------------------------------------------------- 00014 00015 VCCommands::VCCommands(Game& game, HexEnvironment& env) 00016 : m_game(game), 00017 m_env(env) 00018 { 00019 } 00020 00021 VCCommands::~VCCommands() 00022 { 00023 } 00024 00025 void VCCommands::Register(GtpEngine& e) 00026 { 00027 Register(e, "vc-between-cells", &VCCommands::CmdGetVCsBetween); 00028 Register(e, "vc-connected-to", &VCCommands::CmdGetCellsConnectedTo); 00029 Register(e, "vc-get-mustplay", &VCCommands::CmdGetMustPlay); 00030 Register(e, "vc-intersection", &VCCommands::CmdVCIntersection); 00031 Register(e, "vc-union", &VCCommands::CmdVCUnion); 00032 Register(e, "vc-build", &VCCommands::CmdBuildStatic); 00033 Register(e, "vc-build-incremental", &VCCommands::CmdBuildIncremental); 00034 Register(e, "vc-undo-incremental", &VCCommands::CmdUndoIncremental); 00035 Register(e, "vc-set-stats", &VCCommands::CmdSetInfo); 00036 Register(e, "vc-builder-stats", &VCCommands::CmdBuilderStats); 00037 } 00038 00039 void VCCommands::Register(GtpEngine& engine, const std::string& command, 00040 GtpCallback<VCCommands>::Method method) 00041 { 00042 engine.Register(command, new GtpCallback<VCCommands>(this, method)); 00043 } 00044 00045 VC::Type VCCommands::VCTypeArg(const HtpCommand& cmd, std::size_t number) const 00046 { 00047 return VCTypeUtil::fromString(cmd.ArgToLower(number)); 00048 } 00049 00050 //---------------------------------------------------------------------------- 00051 00052 /** Builds VCs for both players and displays ice info for the given 00053 color in the current board-state. 00054 Usage: "vc-build [color]" 00055 */ 00056 void VCCommands::CmdBuildStatic(HtpCommand& cmd) 00057 { 00058 cmd.CheckNuArg(1); 00059 HexColor color = HtpUtil::ColorArg(cmd, 0); 00060 HexBoard& brd = m_env.SyncBoard(m_game.Board()); 00061 brd.ComputeAll(color); 00062 cmd << brd.GetInferiorCells().GuiOutput(); 00063 if (!EndgameUtils::IsDeterminedState(brd, color)) 00064 { 00065 bitset_t consider = EndgameUtils::MovesToConsider(brd, color); 00066 cmd << BoardUtils::GuiDumpOutsideConsiderSet(brd.GetPosition(), 00067 consider, 00068 brd.GetInferiorCells().All()); 00069 } 00070 cmd << '\n'; 00071 } 00072 00073 /** Builds VCs incrementally for both players and displays ice info 00074 the given color in the current board-state. 00075 Usage: "vc-build-incremental [color] [move]" 00076 */ 00077 void VCCommands::CmdBuildIncremental(HtpCommand& cmd) 00078 { 00079 cmd.CheckNuArgLessEqual(2); 00080 HexColor color = HtpUtil::ColorArg(cmd, 0); 00081 HexPoint point = HtpUtil::MoveArg(cmd, 1); 00082 HexBoard& brd = *m_env.brd; // <-- NOTE: no call to SyncBoard()! 00083 brd.PlayMove(color, point); 00084 cmd << brd.GetInferiorCells().GuiOutput(); 00085 if (!EndgameUtils::IsDeterminedState(brd, color)) 00086 { 00087 bitset_t consider = EndgameUtils::MovesToConsider(brd, color); 00088 cmd << BoardUtils::GuiDumpOutsideConsiderSet(brd.GetPosition(), consider, 00089 brd.GetInferiorCells().All()); 00090 } 00091 cmd << '\n'; 00092 } 00093 00094 /** Reverts VCs built incrementally. 00095 Usage: "vc-undo-incremental" 00096 */ 00097 void VCCommands::CmdUndoIncremental(HtpCommand& cmd) 00098 { 00099 UNUSED(cmd); 00100 m_env.brd->UndoMove(); 00101 } 00102 00103 /** Returns a list of VCs between the given two cells. 00104 Usage: "vc-between-cells x y c t", where x and y are the cells, c 00105 is the color of the player, and t is the type of connection 00106 (full,semi). 00107 */ 00108 void VCCommands::CmdGetVCsBetween(HtpCommand& cmd) 00109 { 00110 cmd.CheckNuArg(4); 00111 HexPoint from = HtpUtil::MoveArg(cmd, 0); 00112 HexPoint to = HtpUtil::MoveArg(cmd, 1); 00113 HexColor color = HtpUtil::ColorArg(cmd, 2); 00114 VC::Type ctype = VCTypeArg(cmd, 3); 00115 HexBoard& brd = *m_env.brd; 00116 HexPoint fcaptain = brd.GetGroups().CaptainOf(from); 00117 HexPoint tcaptain = brd.GetGroups().CaptainOf(to); 00118 std::vector<VC> vc; 00119 brd.Cons(color).VCs(fcaptain, tcaptain, ctype, vc); 00120 const VCList& lst = brd.Cons(color).GetList(ctype, fcaptain, tcaptain); 00121 cmd << '\n'; 00122 std::size_t i = 0; 00123 for (; i < (std::size_t)lst.softlimit() && i < vc.size(); ++i) 00124 cmd << color << " " << vc.at(i) << '\n'; 00125 if (i >= vc.size()) 00126 return; 00127 cmd << color << " " << fcaptain << " " << tcaptain << " "; 00128 cmd << "softlimit ----------------------"; 00129 cmd << '\n'; 00130 for (; i < vc.size(); ++i) 00131 cmd << color << " " << vc.at(i) << '\n'; 00132 } 00133 00134 /** Returns a list of cells the given cell shares a vc with. 00135 Usage: "vc-connected-to x c t", where x is the cell in question, 00136 c is the color of the player, and t is the type of vc. 00137 */ 00138 void VCCommands::CmdGetCellsConnectedTo(HtpCommand& cmd) 00139 { 00140 cmd.CheckNuArg(3); 00141 HexPoint from = HtpUtil::MoveArg(cmd, 0); 00142 HexColor color = HtpUtil::ColorArg(cmd, 1); 00143 VC::Type ctype = VCTypeArg(cmd, 2); 00144 bitset_t pt = VCSetUtil::ConnectedTo(m_env.brd->Cons(color), 00145 m_env.brd->GetGroups(), from, ctype); 00146 cmd << HexPointUtil::ToString(pt); 00147 } 00148 00149 /** Prints the cells in the current mustplay for the given player. 00150 Usage: "vc-get-mustplay [color]" 00151 */ 00152 void VCCommands::CmdGetMustPlay(HtpCommand& cmd) 00153 { 00154 cmd.CheckNuArg(1); 00155 HexColor color = HtpUtil::ColorArg(cmd, 0); 00156 bitset_t mustplay = VCUtils::GetMustplay(*m_env.brd, color); 00157 InferiorCells inf(m_env.brd->GetInferiorCells()); 00158 inf.ClearVulnerable(); 00159 inf.ClearReversible(); 00160 inf.ClearDominated(); 00161 cmd << inf.GuiOutput(); 00162 if (!EndgameUtils::IsDeterminedState(*m_env.brd, color)) 00163 { 00164 bitset_t consider = EndgameUtils::MovesToConsider(*m_env.brd, color); 00165 cmd << BoardUtils::GuiDumpOutsideConsiderSet(m_env.brd->GetPosition(), 00166 consider, 00167 inf.All()); 00168 } 00169 } 00170 00171 /** Prints the cells in the all connections between given endpoints. 00172 Usage: "vc-intersection [from] [to] [color] [vctype]" 00173 */ 00174 void VCCommands::CmdVCIntersection(HtpCommand& cmd) 00175 { 00176 cmd.CheckNuArg(4); 00177 HexPoint from = HtpUtil::MoveArg(cmd, 0); 00178 HexPoint to = HtpUtil::MoveArg(cmd, 1); 00179 HexColor color = HtpUtil::ColorArg(cmd, 2); 00180 VC::Type ctype = VCTypeArg(cmd, 3); 00181 HexBoard& brd = *m_env.brd; 00182 HexPoint fcaptain = brd.GetGroups().CaptainOf(from); 00183 HexPoint tcaptain = brd.GetGroups().CaptainOf(to); 00184 const VCList& lst = brd.Cons(color).GetList(ctype, fcaptain, tcaptain); 00185 bitset_t intersection = lst.hardIntersection(); 00186 cmd << HexPointUtil::ToString(intersection); 00187 } 00188 00189 /** Prints the cells in union of connections between given endpoints. 00190 Usage: "vc-union [from] [to] [color] [vctype]" 00191 */ 00192 void VCCommands::CmdVCUnion(HtpCommand& cmd) 00193 { 00194 cmd.CheckNuArg(4); 00195 HexPoint from = HtpUtil::MoveArg(cmd, 0); 00196 HexPoint to = HtpUtil::MoveArg(cmd, 1); 00197 HexColor color = HtpUtil::ColorArg(cmd, 2); 00198 VC::Type ctype = VCTypeArg(cmd, 3); 00199 HexBoard& brd = *m_env.brd; 00200 HexPoint fcaptain = brd.GetGroups().CaptainOf(from); 00201 HexPoint tcaptain = brd.GetGroups().CaptainOf(to); 00202 const VCList& lst = brd.Cons(color).GetList(ctype, fcaptain, tcaptain); 00203 bitset_t un = lst.getGreedyUnion(); // FIXME: shouldn't be greedy!! 00204 cmd << HexPointUtil::ToString(un); 00205 } 00206 00207 //---------------------------------------------------------------------------- 00208 00209 /** Obtains statistics on connection set. */ 00210 void VCCommands::CmdSetInfo(HtpCommand& cmd) 00211 { 00212 if (cmd.NuArg() == 0) 00213 throw HtpFailure("Need at least the color!"); 00214 int maxConnections = 50; 00215 int numBins = 10; 00216 HexColor color = HtpUtil::ColorArg(cmd, 0); 00217 if (cmd.NuArg() == 3) 00218 { 00219 maxConnections = cmd.IntArg(1, 1); 00220 numBins = cmd.IntArg(2, 1); 00221 } 00222 HexBoard& brd = *m_env.brd; 00223 VCSetStatistics stats 00224 = VCSetUtil::ComputeStatistics(brd.Cons(color), brd.GetGroups(), 00225 maxConnections, numBins); 00226 cmd << stats.Write(); 00227 } 00228 00229 /** Obtains statistics on connection set. */ 00230 void VCCommands::CmdBuilderStats(HtpCommand& cmd) 00231 { 00232 cmd.CheckNuArg(1); 00233 HexColor color = HtpUtil::ColorArg(cmd, 0); 00234 HexBoard& brd = *m_env.brd; 00235 VCBuilderStatistics stats = brd.Builder().Statistics(color); 00236 cmd << stats.ToString(); 00237 } 00238 00239 //---------------------------------------------------------------------------- 00240 00241