00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #ifndef GIRLPOINT_HPP
00028 #define GIRLPOINT_HPP
00029
00030
00031
00032 #include "girlIO.hpp"
00033 #include "girlDirection.hpp"
00034
00035 namespace girl {
00036
00040 template <typename T>
00041 class TPoint
00042 {
00043 public:
00044
00048 TPoint(T px=0, T py=0)
00049 : m_x(px), m_y(py)
00050 {}
00051
00055 inline
00056 T x() const
00057 {
00058 return m_x;
00059 }
00060
00064 inline
00065 T y() const
00066 {
00067 return m_y;
00068 }
00069
00073 void set(T px, T py)
00074 {
00075 m_x = px;
00076 m_y = py;
00077 }
00078
00082 void setX(T px)
00083 {
00084 m_x = px;
00085 }
00086
00090 void setY(T py)
00091 {
00092 m_y = py;
00093 }
00094
00098 bool operator==(const girl::TPoint<T> &pt) const
00099 {
00100 return m_x == pt.m_x && m_y == pt.m_y;
00101 }
00102
00106 bool operator!=(const girl::TPoint<T> &pt) const
00107 {
00108 return m_x != pt.m_x || m_y != pt.m_y;
00109 }
00110
00111
00112 protected:
00116 T m_x;
00117
00121 T m_y;
00122
00123 };
00124
00125
00126 typedef short POINT_TYPE;
00127
00133 class point : public TPoint<POINT_TYPE>
00134 {
00135 public:
00136
00140 point(POINT_TYPE px=0, POINT_TYPE py=0)
00141 : TPoint<POINT_TYPE>(px, py)
00142 {}
00143
00144
00148 void move(girl::direction d)
00149 {
00150 switch (d)
00151 {
00152 case RIGHT:
00153 setX(static_cast<POINT_TYPE>(x()+1));
00154 break;
00155 case UP:
00156 setY(static_cast<POINT_TYPE>(y()-1));
00157 break;
00158 case LEFT:
00159 setX(static_cast<POINT_TYPE>(x()-1));
00160 break;
00161 case DOWN:
00162 setY(static_cast<POINT_TYPE>(y()+1));
00163 break;
00164 case INVALID:
00165 default:
00166 assert(false);
00167 break;
00168 }
00169 }
00170
00176 girl::direction directionTo(const girl::point &pt) const
00177 {
00178 if (pt.m_x == m_x+1 && pt.m_y == m_y)
00179 return RIGHT;
00180 else if (pt.m_x == m_x && pt.m_y == m_y-1)
00181 return UP;
00182 else if (pt.m_x == m_x-1 && pt.m_y == m_y)
00183 return LEFT;
00184 else if (pt.m_x == m_x && pt.m_y == m_y+1)
00185 return DOWN;
00186
00187 return INVALID;
00188 }
00189
00195 void write(girl::io::Writer &w, girl::io::Type ioType) const
00196 {
00197 if (ioType == girl::io::MEMORY) {
00198 w.write(&(m_x), sizeof(m_x));
00199 w.write(&(m_y), sizeof(m_y));
00200 }
00201 else {
00202 assert(ioType == girl::io::DISK);
00203 girl::io::writeInt(m_x, w);
00204 girl::io::writeInt(m_y, w);
00205 }
00206 }
00207
00215 bool read(girl::io::Reader &r, girl::io::Type ioType)
00216 {
00217 if (ioType == girl::io::MEMORY) {
00218
00219 if (r.read(&(m_x), sizeof(m_x)) != sizeof(m_x))
00220 return false;
00221 if (r.read(&(m_y), sizeof(m_y)) != sizeof(m_x))
00222 return false;
00223
00224 return true;
00225 }
00226 else {
00227 assert(ioType == girl::io::DISK);
00228 int rx, ry;
00229 if (! girl::io::readInt(&rx, r))
00230 return false;
00231 if (! girl::io::readInt(&ry, r))
00232 return false;
00233 m_x = static_cast<POINT_TYPE>(rx);
00234 m_y = static_cast<POINT_TYPE>(ry);
00235
00236 return true;
00237 }
00238 }
00239
00240 };
00241
00242
00243 }
00244
00245
00246
00247
00248 #endif