Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef GO_REGIONBOARD_H
00007 #define GO_REGIONBOARD_H
00008
00009 #include "GoBoard.h"
00010 #include "GoRegion.h"
00011
00012 class GoBlock;
00013 class GoChain;
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class GoRegionBoard
00030 {
00031 public:
00032
00033
00034 explicit GoRegionBoard(const GoBoard& board);
00035
00036
00037 virtual ~GoRegionBoard();
00038
00039
00040 void Clear();
00041
00042
00043 void CheckConsistency() const;
00044
00045
00046 bool UpToDate() const
00047 {
00048 return ! m_invalid
00049 && m_boardSize == m_board.Size()
00050 && m_code == Board().GetHashCode();
00051 }
00052
00053 bool ComputedHealthy() const
00054 {
00055 return m_computedHealthy;
00056 }
00057
00058
00059 bool ChainsUpToDate() const
00060 {
00061 return UpToDate() && m_chainsCode == Board().GetHashCode();
00062 }
00063
00064
00065 void ExecuteMovePrologue();
00066
00067
00068
00069 void OnExecutedMove(GoPlayerMove playerMove);
00070
00071
00072 void OnExecutedUncodedMove(int move, SgBlackWhite moveColor);
00073
00074
00075
00076 void OnUndoneMove();
00077
00078
00079 SgVectorOf<GoBlock>& AllBlocks(SgBlackWhite color)
00080 {
00081 return m_allBlocks[color];
00082 }
00083
00084
00085 const SgVectorOf<GoBlock>& AllBlocks(SgBlackWhite color) const
00086 {
00087 return m_allBlocks[color];
00088 }
00089
00090
00091 SgVectorOf<GoChain>& AllChains(SgBlackWhite color)
00092 {
00093 return m_allChains[color];
00094 }
00095
00096
00097 const SgVectorOf<GoChain>& AllChains(SgBlackWhite color) const
00098 {
00099 return m_allChains[color];
00100 }
00101
00102
00103 SgVectorOf<GoRegion>& AllRegions(SgBlackWhite color)
00104 {
00105 return m_allRegions[color];
00106 }
00107
00108
00109 const SgVectorOf<GoRegion>& AllRegions(SgBlackWhite color) const
00110 {
00111 return m_allRegions[color];
00112 }
00113
00114
00115 const SgPointSet& All(SgBlackWhite color) const
00116 {
00117 return Board().All(color);
00118 }
00119
00120
00121 const SgPointSet& AllEmpty() const {return Board().AllEmpty();}
00122
00123
00124 const SgPointSet& AllPoints() const {return Board().AllPoints();}
00125
00126
00127 bool IsColor(SgPoint p, int c) const {return Board().IsColor(p, c);}
00128
00129
00130 void WriteBlocks(std::ostream& stream) const;
00131
00132
00133 void WriteRegions(std::ostream& stream) const;
00134
00135
00136 void GenBlocksRegions();
00137
00138
00139
00140
00141
00142 void GenChains();
00143
00144
00145 void ReInitializeBlocksRegions();
00146
00147
00148 void SetComputedFlagForAll(GoRegionFlag flag);
00149
00150 const GoBoard& Board() const
00151 {
00152 return m_board;
00153 }
00154
00155
00156
00157 GoBlock* GetBlock(const SgPointSet& boundary,
00158 SgBlackWhite color) const;
00159
00160
00161 GoRegion* PreviousRegionAt(SgPoint p, SgBlackWhite color) const
00162 {
00163 SG_ASSERT(Board().Occupied(p));
00164 SG_ASSERT(m_region[color][p] != 0);
00165 return m_region[color][p];
00166 }
00167
00168
00169 GoRegion* RegionAt(SgPoint p, SgBlackWhite color) const
00170 {
00171 SG_ASSERT(UpToDate());
00172 SG_ASSERT(! Board().IsColor(p, color));
00173 SG_ASSERT(m_region[color][p] != 0);
00174 return m_region[color][p];
00175 }
00176
00177
00178 void RegionsAt(const SgPointSet& area, SgBlackWhite color,
00179 SgVectorOf<GoRegion>* regions) const;
00180
00181
00182 void AdjacentRegions(const SgVector<SgPoint>& points, SgBlackWhite color,
00183 SgVectorOf<GoRegion>* regions) const;
00184
00185
00186
00187
00188
00189 void PreviousBlocksAt(const SgVector<SgPoint>& area, SgBlackWhite color,
00190 SgVectorOf<GoBlock>* captures) const;
00191
00192
00193 GoBlock* BlockAt(SgPoint p) const
00194 {
00195 SG_ASSERT(m_block[p] != 0);
00196 return m_block[p];
00197 }
00198
00199
00200 GoChain* ChainAt(SgPoint p) const;
00201
00202
00203 bool IsSafeBlock(SgPoint p) const;
00204
00205
00206 void SetToSafe(SgPoint p) const;
00207
00208
00209 void SetSafeFlags(const SgBWSet& safe);
00210
00211
00212 void SetComputedHealthy();
00213
00214
00215 static bool Init();
00216
00217
00218 static void Fini();
00219
00220 private:
00221
00222
00223
00224 void GenBlocks();
00225
00226
00227 void FindBlocksWithEye();
00228
00229
00230
00231 GoBlock* GenBlock(SgPoint anchor, SgBlackWhite color);
00232
00233
00234 GoRegion* GenRegion(const SgPointSet& area, SgBlackWhite color);
00235
00236
00237 void UpdateBlock(int move, SgBlackWhite moveColor);
00238
00239
00240 void SetRegionArrays(GoRegion* r);
00241
00242
00243 void AddBlock(GoBlock* b, bool isExecute = true);
00244
00245
00246 void RemoveBlock(GoBlock* b, bool isExecute, bool removeFromRegions);
00247
00248
00249 void AddRegion(GoRegion* r, bool isExecute = true);
00250
00251
00252 void RemoveRegion(GoRegion* r, bool isExecute = true);
00253
00254
00255
00256
00257 void MergeAdjacentAndAddBlock(SgPoint move, SgBlackWhite capturedColor);
00258
00259
00260 GoRegion* MergeAll(const SgVectorOf<GoRegion>& regions,
00261 const SgPointSet& captured, SgBlackWhite color);
00262
00263
00264
00265 SgIncrementalStack m_stack;
00266
00267
00268 void PushRegion(int type, GoRegion* r);
00269
00270
00271 void PushStone(GoRegion* r, SgPoint move);
00272
00273
00274 void PushBlock(int type, GoBlock* b);
00275
00276
00277 void AppendStone(GoBlock* b, SgPoint move);
00278
00279
00280
00281 const GoBoard& m_board;
00282
00283
00284 SgBWArray<SgPointArray<GoRegion*> > m_region;
00285
00286
00287 SgPointArray<GoBlock*> m_block;
00288
00289
00290 SgBWArray<SgVectorOf<GoBlock> > m_allBlocks;
00291
00292
00293 SgBWArray<SgVectorOf<GoChain> > m_allChains;
00294
00295
00296 SgBWArray<SgVectorOf<GoRegion> > m_allRegions;
00297
00298
00299 SgHashCode m_code;
00300
00301
00302 SgHashCode m_chainsCode;
00303
00304
00305 bool m_invalid;
00306
00307
00308
00309
00310 bool m_computedHealthy;
00311
00312
00313 int m_boardSize;
00314
00315
00316 static int s_alloc, s_free;
00317
00318 };
00319
00320
00321
00322 #endif // GO_REGIONBOARD_H