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


Sun Mar 13 2011 Doxygen 1.7.1