00001 //---------------------------------------------------------------------------- 00002 /** @file GoStaticSafetySolver.h 00003 Recognize safe stones and territories statically. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef GO_STATICSAFETYSOLVER_H 00008 #define GO_STATICSAFETYSOLVER_H 00009 00010 #include "GoBoard.h" 00011 #include "GoRegion.h" 00012 #include "GoRegionBoard.h" 00013 00014 //---------------------------------------------------------------------------- 00015 00016 /** Common algorithm for static safety. 00017 The algorithm is used to implement both Benson's algorithm for 00018 unconditional safety and a solver using the extended 1-vitality and 00019 2-vitality definitions from Martin Mueller's thesis [Mueller 95, p. 62-63] 00020 and from [Mueller 97b]. 00021 */ 00022 class GoStaticSafetySolver 00023 { 00024 public: 00025 /** Constructor. If regions = 0, allocates own. */ 00026 GoStaticSafetySolver(const GoBoard& board, GoRegionBoard* regions = 0); 00027 00028 /** Destructor, deallocates if there are own regions */ 00029 virtual ~GoStaticSafetySolver(); 00030 00031 /** @name Accessors */ 00032 // @{ 00033 00034 /** our board */ 00035 const GoBoard& Board() const; 00036 00037 // @} // @name 00038 00039 00040 /** @name Forwarding accessors for GoRegionBoard */ 00041 // @{ 00042 00043 /** See GoRegionBoard::UpToDate */ 00044 virtual bool UpToDate() const; 00045 00046 /** our regions */ 00047 const GoRegionBoard* Regions() const; 00048 00049 // @} // @name 00050 00051 protected: 00052 00053 /** our regions */ 00054 GoRegionBoard* Regions(); 00055 00056 /** Main step of Benson's algorithm */ 00057 virtual void FindTestSets(SgVectorOf<SgVectorOf<GoBlock> >* sets, 00058 SgBlackWhite color) const; 00059 00060 /** Compute closure of blocks set for Benson's algorithm. 00061 Expand set of blocks until all blocks adjacent to all adjacent 00062 regions are in set. 00063 see [Benson] for explanation. 00064 */ 00065 virtual void FindClosure(SgVectorOf<GoBlock>* blocks) const; 00066 00067 /** Compute all GoBlock's and GoRegion's on board*/ 00068 virtual void GenBlocksRegions(); 00069 00070 /** Is r healthy for b? Implements Benson, override for better tests 00071 Benson's classic healthyness test: all empty points of region must be 00072 liberties of the block. 00073 */ 00074 virtual bool RegionHealthyForBlock(const GoRegion& r, 00075 const GoBlock& b) const; 00076 00077 /** Main function, compute all safe points on board */ 00078 virtual void FindSafePoints(SgBWSet* safe); 00079 00080 00081 /** Find healthy regions for block, calls RegionHealthyForBlock */ 00082 virtual void FindHealthy(); // 00083 00084 /** Test if list of Benson blocks forms a living group. 00085 Each block must have a sure liberty count of at least 2. 00086 A region provides one sure liberty if it is healthy and its 00087 boundary consists only of blocks in the list. 00088 */ 00089 void TestAlive(SgVectorOf<GoBlock>* blocks, SgBWSet* safe, 00090 SgBlackWhite color); 00091 00092 /** Reduce regions: keep only if completely surrounded by blocks */ 00093 void TestAdjacent(SgVectorOf<GoRegion>* regions, 00094 const SgVectorOf<GoBlock>& blocks) const; 00095 00096 private: 00097 /** The board we are computing on */ 00098 const GoBoard& m_board; 00099 00100 /** Contains the GoRegion's and GoBlock's we are using */ 00101 GoRegionBoard* m_regions; 00102 00103 /** Did we allocate the GoRegionBoard or did the user supply it? */ 00104 bool m_allocRegion; 00105 00106 /** not implemented */ 00107 GoStaticSafetySolver(const GoStaticSafetySolver&); 00108 00109 /** not implemented */ 00110 GoStaticSafetySolver& operator=(const GoStaticSafetySolver&); 00111 }; 00112 00113 00114 inline const GoBoard& GoStaticSafetySolver::Board() const 00115 { 00116 return m_board; 00117 } 00118 00119 inline GoRegionBoard* GoStaticSafetySolver::Regions() 00120 { 00121 SG_ASSERT(m_regions); 00122 return m_regions; 00123 } 00124 00125 inline const GoRegionBoard* GoStaticSafetySolver::Regions() const 00126 { 00127 SG_ASSERT(m_regions); 00128 return m_regions; 00129 } 00130 00131 //---------------------------------------------------------------------------- 00132 00133 #endif // GO_STATICSAFETYSOLVER_H