Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

HexPoint.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file HexPoint.cpp
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #include <sstream>
00007 #include <string>
00008 #include <cstring>
00009 #include <cstdio>
00010 #include "HexPoint.hpp"
00011 
00012 using namespace benzene;
00013 
00014 //----------------------------------------------------------------------------
00015 
00016 namespace
00017 {
00018 
00019     /** Static data pertaining to HexPoints. */
00020     struct HexPointData
00021     {
00022         /** Name for each HexPoint. */
00023         std::vector<std::string> name;
00024         
00025         HexPointData();
00026     };
00027     
00028     HexPointData::HexPointData()
00029     {
00030         name.resize(FIRST_INVALID, "--bad-point--");
00031         
00032         name[INVALID_POINT] = "invalid";
00033         name[RESIGN] = "resign";
00034         name[SWAP_PIECES] = "swap-pieces";
00035         
00036         name[NORTH] = "north";
00037         name[EAST]  = "east";
00038         name[SOUTH] = "south";
00039         name[WEST]  = "west";
00040         
00041         for (int y=0; y < MAX_HEIGHT; y++) 
00042         {
00043             for (int x = 0; x < MAX_WIDTH; x++) 
00044             {
00045                 char buff[32];
00046                 HexPoint p = HexPointUtil::coordsToPoint(x, y);
00047                 sprintf(buff, "%c%d", 'a' + x, y+1);
00048                 name[p] = std::string(buff);
00049             }
00050         }
00051     }
00052 
00053     /** Returns a constant reference to the static HexPoint data
00054         allocated as a static variable of this method (this way, other
00055         globals can be initialized safely with this data).
00056     */
00057     const HexPointData& GetHexPointData()
00058     {
00059         static HexPointData s_data;
00060         return s_data;
00061     }
00062 
00063 } // anonymous namespace
00064 
00065 //----------------------------------------------------------------------------
00066 
00067 std::string HexPointUtil::ToString(HexPoint p)
00068 {
00069     HexAssert(0 <= p && p < FIRST_INVALID);
00070     return GetHexPointData().name[p];
00071 }
00072 
00073 HexPoint HexPointUtil::FromString(const std::string& name)
00074 {
00075     const char *str = name.c_str();
00076     for (int p = 0; p < FIRST_INVALID; ++p) 
00077         if (!strcasecmp(GetHexPointData().name[p].c_str(), str)) 
00078             return static_cast<HexPoint>(p);
00079     return INVALID_POINT;
00080 }
00081 
00082 void HexPointUtil::FromString(const std::string& str, PointSequence& pts)
00083 {
00084     std::istringstream is(str);
00085     std::string token;
00086     while (is >> token)
00087     {
00088         HexPoint p = HexPointUtil::FromString(token);
00089         pts.push_back(p);
00090     }
00091 }
00092 
00093 std::string HexPointUtil::ToString(const HexPointPair& p)
00094 {
00095     std::ostringstream os;
00096     os << "(" << ToString(p.first) << ", " << ToString(p.second) << ")";
00097     return os.str();
00098 }
00099 
00100 std::string HexPointUtil::ToString(const PointSequence& lst)
00101 {
00102     std::ostringstream os;
00103     for (std::size_t i = 0; i < lst.size(); ++i)
00104     {
00105         if (i) os << ' ';
00106         os << ToString(lst[i]);
00107     }
00108     return os.str();
00109 }
00110 
00111 std::string HexPointUtil::ToString(const bitset_t& b)
00112 {
00113     std::ostringstream os;
00114     for (int i = 0; i < FIRST_INVALID; i++) 
00115         if (b.test(i)) 
00116             os << ' ' << ToString(static_cast<HexPoint>(i));
00117     return os.str();
00118 }
00119 
00120 //----------------------------------------------------------------------------


6 Jan 2011 Doxygen 1.6.3