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 enum {
00018 OFFSET_MIN=-0x100,
00019 OFFSET_ZERO=0,
00020 OFFSET_MAX=0x100
00021 };
00022 public:
00023 static const int BOARD_HEIGHT=16;
00024 private:
00025 int offset;
00026 explicit Offset(int o) : offset(o)
00027 {
00028 }
00029 public:
00030 static const Offset makeDirect(int value) { return Offset(value); }
00031 int intValue() const { return offset; }
00032 public:
00033 static int makeOffset(int dx,int dy) { return dx*BOARD_HEIGHT + dy; }
00034 Offset(int dx,int dy) : offset(makeOffset(dx,dy))
00035 {
00036 }
00037 Offset() : offset(OFFSET_ZERO)
00038 {
00039 }
00040 static const Offset ZERO() { return Offset(OFFSET_ZERO); }
00041 int dx() const;
00042 int dy() const;
00043 unsigned int index() const { return offset - OFFSET_MIN; }
00044
00045 Offset& operator+=(Offset other)
00046 {
00047 offset += other.offset;
00048 return *this;
00049 }
00050 Offset& operator-=(Offset other){
00051 offset -= other.offset;
00052 return *this;
00053 }
00054 const Offset operator+(Offset other) const
00055 {
00056 Offset result(*this);
00057 return result += other;
00058 }
00059 const Offset operator-(const Offset other) const
00060 {
00061 Offset result(*this);
00062 return result -= other;
00063 }
00064 const Offset operator*(const int mult) const {
00065 return static_cast<Offset>(static_cast<int>(offset)*mult);
00066 }
00067 const Offset operator-() const { return Offset(-offset); }
00068 #if 0
00069 inline Offset operator*(const Offset off1,const Offset off2){
00070 return static_cast<Offset>(static_cast<int>(off1)*static_cast<int>(off2));
00071 }
00072 #endif
00073 private:
00074 const Offset blackOffset(Int2Type<BLACK>) const { return *this; }
00075 const Offset blackOffset(Int2Type<WHITE>) const { return -(*this); }
00076 public:
00080 template <Player P>
00081 const Offset blackOffset() const { return blackOffset(Int2Type<P>()); }
00082
00083 bool zero() const { return offset == OFFSET_ZERO; }
00084 };
00085
00089 inline Offset newOffset(int dx,int dy){
00090 return Offset(dx,dy);
00091 }
00092
00093 inline bool operator==(Offset l, Offset r)
00094 {
00095 return l.intValue() == r.intValue();
00096 }
00097 inline bool operator!=(Offset l, Offset r)
00098 {
00099 return ! (l == r);
00100 }
00101 inline bool operator<(Offset l, Offset r)
00102 {
00103 return l.intValue() < r.intValue();
00104 }
00105
00106
00107 std::ostream& operator<<(std::ostream&, Offset);
00108
00109 }
00110
00111 #endif
00112
00113
00114
00115