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