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