00001
00002
00003 #ifndef _OFFSET_H
00004 #define _OFFSET_H
00005
00006 #include "osl/player.h"
00007 #include "osl/misc/loki.h"
00008 #include <iosfwd>
00009
00010 namespace osl
00011 {
00015 class Offset
00016 {
00017 public:
00018 enum {
00019 OFFSET_MIN=-0x100,
00020 ONBOARD_OFFSET_MIN=-0x88,
00021 OFFSET_ZERO=0,
00022 ONBOARD_OFFSET_MAX=0x88,
00023 OFFSET_MAX=0x100,
00024 ONBOARD_OFFSET_SIZE=0x88*2+1
00025 };
00026 static const int BOARD_HEIGHT=16;
00027 private:
00028 int offset;
00029 explicit Offset(int o) : offset(o)
00030 {
00031 }
00032 public:
00033 static const Offset makeDirect(int value) { return Offset(value); }
00034 int intValue() const { return offset; }
00035 public:
00036 static int makeOffset(int dx,int dy) { return dx*BOARD_HEIGHT + dy; }
00037 Offset(int dx,int dy) : offset(makeOffset(dx,dy))
00038 {
00039 }
00040 Offset() : offset(OFFSET_ZERO)
00041 {
00042 }
00043 static const Offset ZERO() { return Offset(OFFSET_ZERO); }
00044 int dx() const;
00045 int dy() const;
00046 unsigned int index() const { return offset - OFFSET_MIN; }
00047
00048 Offset& operator+=(Offset other)
00049 {
00050 offset += other.offset;
00051 return *this;
00052 }
00053 Offset& operator-=(Offset other){
00054 offset -= other.offset;
00055 return *this;
00056 }
00057 const Offset operator+(Offset other) const
00058 {
00059 Offset result(*this);
00060 return result += other;
00061 }
00062 const Offset operator-(const Offset other) const
00063 {
00064 Offset result(*this);
00065 return result -= other;
00066 }
00067 const Offset operator*(const int mult) const {
00068 return static_cast<Offset>(static_cast<int>(offset)*mult);
00069 }
00070 const Offset operator-() const { return Offset(-offset); }
00071 #if 0
00072 inline Offset operator*(const Offset off1,const Offset off2){
00073 return static_cast<Offset>(static_cast<int>(off1)*static_cast<int>(off2));
00074 }
00075 #endif
00076 private:
00077 const Offset blackOffset(Int2Type<BLACK>) const { return *this; }
00078 const Offset blackOffset(Int2Type<WHITE>) const { return -(*this); }
00079 public:
00083 template <Player P>
00084 const Offset blackOffset() const { return blackOffset(Int2Type<P>()); }
00085
00086 bool zero() const { return offset == OFFSET_ZERO; }
00087 };
00088
00092 inline Offset newOffset(int dx,int dy){
00093 return Offset(dx,dy);
00094 }
00095
00096 inline bool operator==(Offset l, Offset r)
00097 {
00098 return l.intValue() == r.intValue();
00099 }
00100 inline bool operator!=(Offset l, Offset r)
00101 {
00102 return ! (l == r);
00103 }
00104 inline bool operator<(Offset l, Offset r)
00105 {
00106 return l.intValue() < r.intValue();
00107 }
00108
00109
00110 std::ostream& operator<<(std::ostream&, Offset);
00111
00112 }
00113
00114 #endif
00115
00116
00117
00118