TheChess
|
00001 /* 00002 * thechess, chess game web application written in C++ and based on Wt 00003 * Copyright (C) 2010 Boris Nagaev 00004 * 00005 * thechess is licensed under the GNU GPL Version 2. 00006 * Other versions of the GPL do not apply. 00007 * See the LICENSE file for terms of use. 00008 */ 00009 00010 // 00011 #ifndef THECHESS_XY_H_ 00012 #define THECHESS_XY_H_ 00013 00014 #include <string> 00015 00016 namespace thechess { 00017 namespace chess { 00018 00019 class Xy; 00020 00021 const char* const letters_abc = "abcdefgh"; 00022 const char* const letters_123 = "12345678"; 00023 00024 enum Xname { 00025 x_end = 8, 00026 x_a = 0, x_1 = 0, 00027 x_b = 1, x_2 = 1, 00028 x_c = 2, x_3 = 2, 00029 x_d = 3, x_4 = 3, 00030 x_e = 4, x_5 = 4, 00031 x_f = 5, x_6 = 5, 00032 x_g = 6, x_7 = 6, 00033 x_h = 7, x_8 = 7 00034 }; 00035 00036 enum Yname { 00037 y_end = 8, 00038 y_1 = 0, 00039 y_2 = 1, 00040 y_3 = 2, 00041 y_4 = 3, 00042 y_5 = 4, 00043 y_6 = 5, 00044 y_7 = 6, 00045 y_8 = 7 00046 }; 00047 00048 00049 inline char x_char(Xname x) { 00050 return letters_abc[(int)x]; 00051 } 00052 00053 inline char y_char(Yname y) { 00054 return letters_123[(int)y]; 00055 } 00056 00057 inline std::string x_string(Xname x) { 00058 return std::string(1, x_char(x)); 00059 } 00060 00061 inline std::string y_string(Yname y) { 00062 return std::string(1, y_char(y)); 00063 } 00064 00065 class Xy { 00066 public: 00067 Xy(Xname x, Yname y); 00068 Xy(unsigned x, unsigned y); 00069 Xy(unsigned i); // for rage creation 00070 Xy(); // no xy 00071 bool operator==(const Xy& xy1) const { 00072 return i() == xy1.i(); 00073 } 00074 bool operator!=(const Xy& xy1) const { 00075 return i() != xy1.i(); 00076 } 00077 00078 Xname x() const { 00079 return (Xname)x_(); 00080 } 00081 Yname y() const { 00082 return (Yname)y_(); 00083 } 00084 void x(Xname v) { 00085 x_((unsigned)v); 00086 } 00087 void y(Yname v) { 00088 y_((unsigned)v); 00089 } 00090 00091 unsigned i() const { 00092 return i_; 00093 } 00094 void i(unsigned v) { 00095 i_ = v; 00096 } 00097 00098 unsigned x_() const { 00099 return i_ / 8; 00100 } 00101 unsigned y_() const { 00102 return i_ % 8; 00103 } 00104 void x_(unsigned v) { 00105 i_ = v * 8 + y_(); 00106 } 00107 void y_(unsigned v) { 00108 i_ = x_() * 8 + v; 00109 } 00110 00111 static Xy begin() { 00112 return Xy(0); 00113 }; 00114 static Xy end() { 00115 return Xy(64); 00116 }; 00117 Xy& operator++ () { 00118 i_ += 1; 00119 return *this; 00120 } 00121 00122 operator int() const { 00123 return i_; 00124 } 00125 operator unsigned() const { 00126 return i_; 00127 } 00128 00129 std::string str() const { 00130 return std::string(1, letters_abc[x_()]) + 00131 std::string(1, letters_123[y_()]); 00132 } 00133 00134 protected: 00135 unsigned i_ : 7; // 0-63 00136 }; 00137 00138 const Xy xy_null(64); 00139 00140 00141 #define THECHESS_XY_FOREACH(xy) \ 00142 for (thechess::chess::Xy xy = thechess::chess::Xy::begin(); \ 00143 xy != thechess::chess::Xy::end(); ++xy) 00144 00145 #define THECHESS_X_FOREACH(x) \ 00146 for (thechess::chess::Xname x = thechess::chess::x_a; \ 00147 x != thechess::chess::x_end; \ 00148 x = (thechess::chess::Xname)((int)x + 1)) 00149 00150 #define THECHESS_Y_FOREACH(y) \ 00151 for (thechess::chess::Yname y = thechess::chess::y_1; \ 00152 y != thechess::chess::y_end; \ 00153 y = (thechess::chess::Yname)((int)y + 1)) 00154 00155 } 00156 } 00157 00158 #endif // THECHESS_XY_H_ 00159