Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgPoint.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgPoint.cpp
00003     See SgPoint.h */
00004 //----------------------------------------------------------------------------
00005 
00006 #include "SgSystem.h"
00007 #include "SgPoint.h"
00008 
00009 #include <iostream>
00010 #include <sstream>
00011 #include "SgUtil.h"
00012 
00013 using namespace std;
00014 
00015 //----------------------------------------------------------------------------
00016 
00017 std::string SgPointUtil::PointToString(SgPoint p)
00018 {
00019     ostringstream buffer;
00020     if (p == SG_NULLMOVE)
00021         buffer << "NULL";
00022     else if (p == SG_PASS)
00023         buffer << "PASS";
00024     else if (p == SG_COUPONMOVE)
00025         buffer << "COUPON";
00026     else if (p == SG_COUPONMOVE_VIRTUAL)
00027         buffer << "COUPON_VIRTUAL";
00028     else if (p == SG_RESIGN)
00029         buffer << "RESIGN";
00030     else
00031         buffer << Letter(Col(p)) << Row(p);
00032     return buffer.str();
00033 }
00034 
00035 SgPoint SgPointUtil::Rotate(int rotation, SgPoint p, int size)
00036 {
00037     SG_ASSERT(rotation < 8);
00038     if (p == SG_PASS)
00039         return SG_PASS;
00040     int col = Col(p);
00041     int row = Row(p);
00042     switch (rotation)
00043     {
00044     case 0:
00045         return Pt(col, row);
00046     case 1:
00047         return Pt(size - col + 1, row);
00048     case 2:
00049         return Pt(col, size - row + 1);
00050     case 3:
00051         return Pt(row, col);
00052     case 4:
00053         return Pt(size - row + 1, col);
00054     case 5:
00055         return Pt(row, size - col + 1);
00056     case 6:
00057         return Pt(size - col + 1, size - row + 1);
00058     case 7:
00059         return Pt(size - row + 1, size - col + 1);
00060     default:
00061         SG_ASSERT(false);
00062         return p;
00063     }
00064 }
00065 
00066 int SgPointUtil::InvRotation(int rotation)
00067 {
00068     switch (rotation)
00069     {
00070     case 0:
00071         return 0;
00072     case 1:
00073         return 1;
00074     case 2:
00075         return 2;
00076     case 3:
00077         return 3;
00078     case 4:
00079         return 5;
00080     case 5:
00081         return 4;
00082     case 6:
00083         return 6;
00084     case 7:
00085         return 7;
00086     default:
00087         SG_ASSERT(false);
00088         return 0;
00089     }
00090 }
00091 
00092 //----------------------------------------------------------------------------
00093 
00094 void SgReadPoint::Read(std::istream& in) const
00095 {
00096     string s;
00097     in >> s;
00098     if (! in)
00099         return;
00100     SG_ASSERT(s.length() > 0);
00101     if (s == "PASS" || s == "pass")
00102     {
00103         m_point = SG_PASS;
00104         return;
00105     }
00106     if (s == "COUPON" || s == "coupon")
00107     {
00108         m_point = SG_COUPONMOVE;
00109         return;
00110     }
00111     if (s == "COUPON_VIRTUAL" || s == "coupon_virtual")
00112     {
00113         m_point = SG_COUPONMOVE_VIRTUAL;
00114         return;
00115     }
00116     if (s == "RESIGN" || s == "resign")
00117     {
00118         m_point = SG_RESIGN;
00119         return;
00120     }
00121     char c = s[0];
00122     if (c >= 'A' && c <= 'Z')
00123         c = char(c - 'A' + 'a');
00124     else if (c < 'a' || c > 'z')
00125     {
00126         in.setstate(ios::failbit);
00127         return;
00128     }
00129     int col = c - 'a' + 1;
00130     if (c >= 'j')
00131         --col;
00132     istringstream sin(s.substr(1));
00133     int row;
00134     sin >> row;
00135     if (! sin || ! SgUtil::InRange(col, 1, SG_MAX_SIZE)
00136         || ! SgUtil::InRange(row, 1, SG_MAX_SIZE))
00137     {
00138         in.setstate(ios::failbit);
00139         return;
00140     }
00141     m_point = SgPointUtil::Pt(col, row);
00142 }
00143 
00144 //----------------------------------------------------------------------------
00145 
00146 ostream& operator<<(ostream& out, const SgWritePoint& writePoint)
00147 {
00148     out << SgPointUtil::PointToString(writePoint.m_p);
00149     return out;
00150 }
00151 
00152 //----------------------------------------------------------------------------
00153 


Sun Mar 13 2011 Doxygen 1.7.1