PatternState.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef PATTERNBOARD_HPP
00007 #define PATTERNBOARD_HPP
00008
00009 #include "Hex.hpp"
00010 #include "HashedPatternSet.hpp"
00011 #include "Pattern.hpp"
00012 #include "StoneBoard.hpp"
00013
00014 _BEGIN_BENZENE_NAMESPACE_
00015
00016
00017
00018
00019 class PatternHit
00020 {
00021 public:
00022
00023
00024 PatternHit(const Pattern* pat, HexPoint move);
00025
00026
00027
00028 PatternHit(const Pattern* pat, const std::vector<HexPoint>& moves1);
00029
00030
00031 PatternHit(const Pattern* pat,
00032 const std::vector<HexPoint>& moves1,
00033 const std::vector<HexPoint>& moves2);
00034
00035
00036 const Pattern* pattern() const;
00037
00038
00039 const std::vector<HexPoint>& moves1() const;
00040
00041 const std::vector<HexPoint>& moves2() const;
00042
00043 private:
00044
00045 const Pattern* m_pattern;
00046
00047 std::vector<HexPoint> m_moves1;
00048
00049 std::vector<HexPoint> m_moves2;
00050 };
00051
00052 inline PatternHit::PatternHit(const Pattern* pat, HexPoint move)
00053 : m_pattern(pat),
00054 m_moves1(1, move),
00055 m_moves2()
00056 {
00057 }
00058
00059 inline PatternHit::PatternHit(const Pattern* pat,
00060 const std::vector<HexPoint>& moves1)
00061 : m_pattern(pat),
00062 m_moves1(moves1),
00063 m_moves2()
00064 {
00065 }
00066
00067 inline PatternHit::PatternHit(const Pattern* pat,
00068 const std::vector<HexPoint>& moves1,
00069 const std::vector<HexPoint>& moves2)
00070 : m_pattern(pat),
00071 m_moves1(moves1),
00072 m_moves2(moves2)
00073 {
00074 }
00075
00076 inline const Pattern* PatternHit::pattern() const
00077 {
00078 return m_pattern;
00079 }
00080
00081 inline const std::vector<HexPoint>& PatternHit::moves1() const
00082 {
00083 return m_moves1;
00084 }
00085
00086 inline const std::vector<HexPoint>& PatternHit::moves2() const
00087 {
00088 return m_moves2;
00089 }
00090
00091
00092 typedef std::vector<PatternHit> PatternHits;
00093
00094
00095
00096
00097 class PatternMatcherData
00098 {
00099 public:
00100
00101
00102 static const PatternMatcherData* Get(const ConstBoard* brd);
00103
00104
00105 const ConstBoard* brd;
00106
00107
00108 int played_in_slice[BITSETSIZE][BITSETSIZE];
00109
00110
00111 int played_in_godel[BITSETSIZE][BITSETSIZE];
00112
00113
00114 int played_in_edge[BITSETSIZE][4][Pattern::NUM_SLICES];
00115
00116
00117 HexPoint inverse_slice_godel[BITSETSIZE][Pattern::NUM_SLICES][32];
00118
00119
00120
00121 HexPoint GetRotatedMove(HexPoint cell, int slice,
00122 int bit, int angle) const;
00123
00124 private:
00125
00126
00127 PatternMatcherData(const ConstBoard* brd);
00128
00129 void Initialize();
00130 };
00131
00132
00133
00134
00135 class PatternState
00136 {
00137 public:
00138
00139 explicit PatternState(StoneBoard& brd);
00140
00141 ~PatternState();
00142
00143
00144
00145
00146 const StoneBoard& Board() const;
00147
00148
00149 StoneBoard& Board();
00150
00151
00152
00153
00154 int UpdateRadius() const;
00155
00156
00157 void SetUpdateRadius(int radius);
00158
00159
00160
00161
00162
00163 void Update();
00164
00165
00166
00167
00168 void Update(HexPoint cell);
00169
00170
00171
00172 void Update(const bitset_t& changed);
00173
00174
00175 void UpdateRingGodel(HexPoint cell);
00176
00177
00178
00179
00180 void CopyState(const PatternState& other);
00181
00182
00183
00184
00185 typedef enum
00186 {
00187
00188 STOP_AT_FIRST_HIT,
00189
00190
00191 MATCH_ALL
00192
00193 } MatchMode;
00194
00195
00196
00197
00198
00199 void MatchOnCell(const HashedPatternSet& patset,
00200 HexPoint cell, MatchMode mode,
00201 PatternHits& hits) const;
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 bitset_t MatchOnBoard(const bitset_t& consider,
00215 const HashedPatternSet& patset,
00216 MatchMode mode,
00217 std::vector<PatternHits>& hits) const;
00218
00219
00220
00221
00222
00223
00224 bitset_t MatchOnBoard(const bitset_t& consider,
00225 const HashedPatternSet& patset) const;
00226
00227
00228
00229
00230 void ClearPatternCheckStats();
00231
00232
00233 std::string DumpPatternCheckStats() const;
00234
00235 private:
00236
00237
00238
00239
00240 struct Statistics
00241 {
00242
00243 size_t pattern_checks;
00244
00245
00246 size_t ring_checks;
00247
00248
00249 size_t slice_checks;
00250 };
00251
00252
00253
00254 StoneBoard& m_brd;
00255
00256 const PatternMatcherData* m_data;
00257
00258
00259 int m_update_radius;
00260
00261 int m_slice_godel[BITSETSIZE][BLACK_AND_WHITE][Pattern::NUM_SLICES];
00262
00263 RingGodel m_ring_godel[BITSETSIZE];
00264
00265 mutable Statistics m_statistics;
00266
00267
00268
00269
00270 void operator=(const PatternState& other);
00271
00272
00273 PatternState(const PatternState& other);
00274
00275 void ClearGodels();
00276
00277
00278
00279 bool CheckRotatedSlices(HexPoint cell,
00280 const Pattern& pat, int angle) const;
00281
00282 bool CheckRotatedSlices(HexPoint cell,
00283 const RotatedPattern& rotpat) const;
00284
00285 bool CheckRingGodel(HexPoint cell,
00286 const Pattern& pattern, int angle) const;
00287
00288 bool CheckRingGodel(HexPoint cell,
00289 const RotatedPattern& rotpat) const;
00290
00291 bool CheckRotatedPattern(HexPoint cell,
00292 const RotatedPattern& rotpat,
00293 std::vector<HexPoint>& moves1,
00294 std::vector<HexPoint>& moves2) const;
00295 };
00296
00297 inline const StoneBoard& PatternState::Board() const
00298 {
00299 return m_brd;
00300 }
00301
00302 inline StoneBoard& PatternState::Board()
00303 {
00304 return m_brd;
00305 }
00306
00307 inline void PatternState::SetUpdateRadius(int radius)
00308 {
00309 HexAssert(1 <= radius);
00310 HexAssert(radius <= Pattern::MAX_EXTENSION);
00311 m_update_radius = radius;
00312 }
00313
00314 inline int PatternState::UpdateRadius() const
00315 {
00316 return m_update_radius;
00317 }
00318
00319
00320
00321 _END_BENZENE_NAMESPACE_
00322
00323 #endif // PATTERNBOARD_HPP