HexPoint.hpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 #ifndef HEXPOINT_HPP
00007 #define HEXPOINT_HPP
00008 
00009 #include <map>
00010 #include <string>
00011 #include <vector>
00012 #include <utility>
00013 
00014 #include "Benzene.hpp"
00015 #include "Bitset.hpp"
00016 #include "HexAssert.hpp"
00017 #include "HexColor.hpp"
00018 
00019 _BEGIN_BENZENE_NAMESPACE_
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 #if defined(SUPPORT_19x19)
00076 
00077 
00078 static const int MAX_WIDTH  = 19;
00079 
00080 
00081 static const int MAX_HEIGHT = 19;
00082 
00083 #elif defined(SUPPORT_14x14)
00084 
00085 
00086 static const int MAX_WIDTH  = 14;
00087 
00088 
00089 static const int MAX_HEIGHT = 14;
00090 
00091 #elif defined(SUPPORT_13x13)
00092 
00093 
00094 static const int MAX_WIDTH  = 13;
00095 
00096 
00097 static const int MAX_HEIGHT = 13;
00098 
00099 #else
00100 
00101 
00102 static const int MAX_WIDTH  = 11;
00103 
00104 
00105 static const int MAX_HEIGHT = 11;
00106 
00107 #endif
00108 
00109 
00110 
00111 
00112 
00113 #if defined(SUPPORT_19x19)
00114 
00115 #include "HexPoints19x19.hpp"
00116 
00117 #elif defined(SUPPORT_14x14)
00118 
00119 #include "HexPoints14x14.hpp"
00120 
00121 #elif defined(SUPPORT_13x13)
00122 
00123 #include "HexPoints13x13.hpp"
00124 
00125 #else
00126 
00127 #include "HexPoints11x11.hpp"
00128 
00129 #endif
00130 
00131 
00132 static const HexPoint FIRST_SPECIAL = RESIGN;
00133 
00134 
00135 static const HexPoint FIRST_EDGE = NORTH;
00136 
00137 
00138 static const HexPoint FIRST_CELL = HEX_CELL_A1;
00139     
00140 
00141 
00142 
00143 typedef std::map<HexPoint, HexPoint> PointToPoint;
00144 
00145 
00146 typedef std::pair<HexPoint, HexPoint> HexPointPair;
00147 
00148 
00149 typedef std::set<HexPoint> HexPointSet;
00150 
00151 
00152 typedef std::map<HexPoint, bitset_t> PointToBitset;
00153 
00154 
00155 typedef std::vector<HexPoint> PointSequence;
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 enum HexDirection 
00174 { 
00175     DIR_EAST=0, 
00176     DIR_NORTH_EAST, 
00177     DIR_NORTH, 
00178     DIR_WEST, 
00179     DIR_SOUTH_WEST, 
00180     DIR_SOUTH, 
00181     NUM_DIRECTIONS 
00182 };
00183 
00184 
00185 
00186 
00187 
00188 namespace HexPointUtil
00189 {
00190 
00191     std::string ToString(HexPoint p);
00192 
00193 
00194     std::string ToString(const HexPointPair& p);
00195 
00196 
00197     std::string ToString(const PointSequence& lst);
00198 
00199 
00200     std::string ToString(const bitset_t& b);
00201 
00202 
00203     HexPoint FromString(const std::string& name);
00204 
00205 
00206 
00207     void FromString(const std::string& str, PointSequence& pts);
00208 
00209 
00210     bool isSwap(HexPoint c);
00211 
00212 
00213     bool isEdge(HexPoint c);
00214 
00215 
00216     bool isInteriorCell(HexPoint c);
00217 
00218 
00219     HexPoint oppositeEdge(HexPoint edge);
00220 
00221 
00222     HexPoint leftEdge(HexPoint edge);
00223 
00224 
00225     HexPoint rightEdge(HexPoint edge);
00226 
00227 
00228  
00229     HexPoint colorEdge1(HexColor color);
00230 
00231 
00232 
00233     HexPoint colorEdge2(HexColor color);
00234 
00235 
00236     bool isColorEdge(HexPoint cell, HexColor color);
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247     void pointToCoords(HexPoint cell, int& x, int& y);
00248 
00249 
00250 
00251 
00252 
00253 
00254     HexPoint coordsToPoint(int x, int y);
00255 
00256 
00257 
00258     int DeltaX(int dir);
00259 
00260 
00261 
00262     int DeltaY(int dir);
00263 
00264 } 
00265 
00266 
00267 
00268 inline bool HexPointUtil::isSwap(HexPoint c)
00269 {
00270     return (c==SWAP_PIECES);
00271 }
00272 
00273 inline bool HexPointUtil::isEdge(HexPoint c)
00274 { 
00275     return (c==NORTH || c==SOUTH || c==EAST || c==WEST); 
00276 }
00277 
00278 inline bool HexPointUtil::isInteriorCell(HexPoint c)
00279 {
00280     return (FIRST_CELL <= c && c < FIRST_INVALID);
00281 }
00282 
00283 inline HexPoint HexPointUtil::oppositeEdge(HexPoint edge)
00284 {
00285     HexAssert(isEdge(edge));
00286     if (edge == NORTH) return SOUTH;
00287     if (edge == SOUTH) return NORTH;
00288     if (edge == EAST)  return WEST;
00289     HexAssert(edge == WEST);
00290     return EAST;
00291 }
00292 
00293 inline HexPoint HexPointUtil::leftEdge(HexPoint edge)
00294 {
00295     HexAssert(isEdge(edge));
00296     if (edge == NORTH) return EAST;
00297     if (edge == SOUTH) return WEST;
00298     if (edge == EAST)  return SOUTH;
00299     HexAssert(edge == WEST);
00300     return NORTH;
00301 }
00302 
00303 inline HexPoint HexPointUtil::rightEdge(HexPoint edge)
00304 {
00305     HexAssert(isEdge(edge));
00306     if (edge == NORTH) return WEST;
00307     if (edge == SOUTH) return EAST;
00308     if (edge == EAST)  return NORTH;
00309     HexAssert(edge == WEST);
00310     return SOUTH;
00311 }
00312 
00313 inline HexPoint HexPointUtil::colorEdge1(HexColor color)
00314 {
00315     HexAssert(HexColorUtil::isBlackWhite(color));
00316     return (color == VERTICAL_COLOR) ? NORTH : EAST;
00317 }
00318 
00319 inline HexPoint HexPointUtil::colorEdge2(HexColor color)
00320 {
00321     HexAssert(HexColorUtil::isBlackWhite(color));
00322     return (color == VERTICAL_COLOR) ? SOUTH : WEST;
00323 }
00324 
00325 inline bool HexPointUtil::isColorEdge(HexPoint cell, HexColor color)
00326 {
00327     HexAssert(HexColorUtil::isBlackWhite(color));
00328     return (cell == colorEdge1(color) || cell == colorEdge2(color));
00329 }
00330 
00331 inline void HexPointUtil::pointToCoords(HexPoint cell, int& x, int& y)
00332 {
00333     HexAssert(FIRST_CELL <= cell && cell < FIRST_INVALID);
00334     x = (cell-FIRST_CELL) % MAX_WIDTH;
00335     y = (cell-FIRST_CELL) / MAX_WIDTH;
00336 }
00337 
00338 inline HexPoint HexPointUtil::coordsToPoint(int x, int y)
00339 {
00340     HexAssert(0 <= x && x < MAX_WIDTH);
00341     HexAssert(0 <= y && y < MAX_HEIGHT);
00342     return static_cast<HexPoint>(FIRST_CELL + (y*MAX_WIDTH) + x);
00343 }
00344 
00345 inline int HexPointUtil::DeltaX(int dir)
00346 {
00347     HexAssert(DIR_EAST <= dir && dir < NUM_DIRECTIONS);
00348     static const int dx[] = {1,  1,  0, -1, -1, 0};
00349     return dx[dir];
00350 }
00351 
00352 inline int HexPointUtil::DeltaY(int dir)
00353 {
00354     HexAssert(DIR_EAST <= dir && dir < NUM_DIRECTIONS);
00355     static const int dy[] = {0, -1, -1,  0,  1, 1};
00356     return dy[dir];
00357 }
00358 
00359 
00360 
00361 
00362 inline std::ostream& operator<<(std::ostream& os, HexPoint p)
00363 {
00364     return os << HexPointUtil::ToString(p);
00365 }
00366 
00367 
00368 
00369 _END_BENZENE_NAMESPACE_
00370 
00371 #endif // HEXPOINT_HPP