00001
00002
00003 #ifndef EFFECT_UTIL_NEIGHBORING8EFFECT_H
00004 #define EFFECT_UTIL_NEIGHBORING8EFFECT_H
00005
00006 #include "osl/state/simpleState.h"
00007 #include "osl/direction.h"
00008 #include <iostream>
00009
00010 namespace osl
00011 {
00012 namespace effect_util
00013 {
00018 class Neighboring8Effect
00019 {
00020 class Table
00021 {
00022 struct Entry
00023 {
00024 bool has_unblockable_effect;
00025 Offset nearest;
00026 Entry() : has_unblockable_effect(false), nearest(Offset::ZERO())
00027 {
00028 }
00029 };
00030 CArray2d<Entry,PTYPEO_SIZE,Offset32::SIZE> table;
00031 void init(Player);
00032
00033 template <int maxCount>
00034 bool hasAtMaxPieceBetween(const SimpleState& state,
00035 Position from, Position target) const
00036 {
00037 assert(from.isOnBoard());
00038 Offset offset=Board_Table.getShortOffset(Offset32(target, from));
00039 assert(! offset.zero());
00040 int count = 0;
00041 for (Position pos=from+offset; pos != target; pos+=offset)
00042 {
00043 if (!state.getPieceAt(pos).isEmpty())
00044 {
00045 count++;
00046 if (count > maxCount) return false;
00047 }
00048 }
00049 return true;
00050 }
00051
00052 public:
00053 Table();
00054
00055 bool hasEffect(const SimpleState& state,
00056 PtypeO ptypeo, Position from,
00057 Position target) const
00058 {
00059 const Offset32 offset32 = Offset32(target, from);
00060 const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
00061 if (e.has_unblockable_effect)
00062 return true;
00063 if (e.nearest.zero())
00064 return false;
00065 assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
00066 const Position nearest = from+e.nearest;
00067 if (! nearest.isOnBoard())
00068 {
00069 return false;
00070 }
00071 return hasAtMaxPieceBetween<1>(state, from, nearest);
00072 }
00073 };
00074 static const Table table;
00075 public:
00079 static bool hasEffect(const SimpleState& state,
00080 PtypeO ptypeo, Position from,
00081 Position target)
00082 {
00083 return table.hasEffect(state, ptypeo, from, target);
00084 }
00085 private:
00086 static bool hasEffectFromTo(const SimpleState& state,
00087 PtypeO ptypeo, Position from,
00088 Position target, Direction d);
00089 public:
00090 static bool hasEffectNaive(const SimpleState& state,
00091 PtypeO ptypeo, Position from,
00092 Position target);
00093 };
00094
00095 }
00096 using effect_util::Neighboring8Effect;
00097 }
00098
00099 #endif
00100
00101
00102
00103