Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoGtpExtraCommands.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file GoGtpExtraCommands.cpp
00003 */
00004 //----------------------------------------------------------------------------
00005 
00006 #include "SgSystem.h"
00007 #include "GoGtpExtraCommands.h"
00008 
00009 #include <limits>
00010 #include <boost/format.hpp>
00011 #include "GoBoard.h"
00012 #include "GoBoardUtil.h"
00013 #include "GoGtpCommandUtil.h"
00014 #include "GoLadder.h"
00015 #include "GoStaticLadder.h"
00016 
00017 using namespace std;
00018 using boost::format;
00019 using GoGtpCommandUtil::PointArg;
00020 using GoGtpCommandUtil::StoneArg;
00021 
00022 //----------------------------------------------------------------------------
00023 
00024 GoGtpExtraCommands::GoGtpExtraCommands(GoBoard& bd)
00025     : m_bd(bd)
00026 {
00027 }
00028 
00029 void GoGtpExtraCommands::AddGoGuiAnalyzeCommands(GtpCommand& cmd)
00030 {
00031     cmd <<
00032         "sboard/Go CFG Distance/go_cfg_distance %p\n"
00033         "sboard/Go CFG Distance N/go_cfg_distance %p %s\n"
00034         "string/Go Ladder/go_ladder %p\n"
00035         "string/Go Static Ladder/go_static_ladder %p\n";
00036 }
00037 
00038 /** Compute the distance from a point as defined in GoBoardUtil::CfgDistance.
00039     This command is compatible with GoGui's analyze command type
00040     @c sboard <br>
00041     Argument: point [max_dist]<br>
00042     Returns: Board of integer numbers
00043 */
00044 void GoGtpExtraCommands::CmdCfgDistance(GtpCommand& cmd)
00045 {
00046     cmd.CheckNuArgLessEqual(2);
00047     SgPoint p = PointArg(cmd, 0, m_bd);
00048     int maxDist = numeric_limits<int>::max();
00049     if (cmd.NuArg() > 1)
00050         maxDist = cmd.IntArg(1, 0);
00051     SgPointArray<int> distance = GoBoardUtil::CfgDistance(m_bd, p, maxDist);
00052     // distance elements are only defined for empty points or block anchors
00053     SgPointArray<string> stringArray("\"\"");
00054     for (GoBoard::Iterator it(m_bd); it; ++it)
00055         if (m_bd.IsEmpty(*it) || m_bd.Anchor(*it) == *it)
00056             stringArray[*it] = str(format("%i") % distance[*it]);
00057     cmd << '\n' << SgWritePointArray<string>(stringArray, m_bd.Size());
00058 }
00059 
00060 /** Return fast ladder status.
00061     Arguments: prey point<br>
00062     Returns: escaped|captured|unsettled<br>
00063     @see FastLadderStatus
00064 */
00065 void GoGtpExtraCommands::CmdLadder(GtpCommand& cmd)
00066 {
00067     cmd.CheckNuArg(1);
00068     SgPoint prey = StoneArg(cmd, 0, m_bd);
00069     GoLadderStatus status = GoLadderUtil::LadderStatus(m_bd, prey);
00070     switch (status)
00071     {
00072     case GO_LADDER_ESCAPED:
00073         cmd << "escaped";
00074         break;
00075     case GO_LADDER_CAPTURED:
00076         cmd << "captured";
00077         break;
00078     case GO_LADDER_UNSETTLED:
00079         cmd << "unsettled";
00080         break;
00081     default:
00082         throw GtpFailure() << "Unexpected ladder status: " << status;
00083     }
00084 }
00085 
00086 /** Return static ladder status.
00087     Arguments: prey point<br>
00088     Returns: escaped|captured|unsettled<br>
00089     @see GoStaticLadder
00090 */
00091 void GoGtpExtraCommands::CmdStaticLadder(GtpCommand& cmd)
00092 {
00093     cmd.CheckNuArg(1);
00094     SgPoint p = StoneArg(cmd, 0, m_bd);
00095     SgBlackWhite c = m_bd.GetColor(p);
00096     if (GoStaticLadder::IsLadder(m_bd, p, c))
00097         cmd << "captured";
00098     else if (GoStaticLadder::IsLadder(m_bd, p, SgOppBW(c)))
00099         cmd << "unsettled";
00100     else
00101         cmd << "escaped";
00102 }
00103 
00104 void GoGtpExtraCommands::Register(GtpEngine& e)
00105 {
00106     Register(e, "go_cfg_distance", &GoGtpExtraCommands::CmdCfgDistance);
00107     Register(e, "go_ladder", &GoGtpExtraCommands::CmdLadder);
00108     Register(e, "go_static_ladder", &GoGtpExtraCommands::CmdStaticLadder);
00109 }
00110 
00111 void GoGtpExtraCommands::Register(GtpEngine& engine,
00112                                const std::string& command,
00113                                GtpCallback<GoGtpExtraCommands>::Method method)
00114 {
00115     engine.Register(command,
00116                     new GtpCallback<GoGtpExtraCommands>(this, method));
00117 }
00118 
00119 //----------------------------------------------------------------------------


17 Jun 2010 Doxygen 1.4.7