00001 //---------------------------------------------------------------------------- 00002 /** @file HexBoard.hpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef HEXBOARD_H 00007 #define HEXBOARD_H 00008 00009 #include <boost/scoped_ptr.hpp> 00010 00011 #include "ChangeLog.hpp" 00012 #include "VCBuilder.hpp" 00013 #include "Hex.hpp" 00014 #include "ICEngine.hpp" 00015 #include "PatternState.hpp" 00016 #include "VCPattern.hpp" 00017 #include "Groups.hpp" 00018 00019 _BEGIN_BENZENE_NAMESPACE_ 00020 00021 //---------------------------------------------------------------------------- 00022 00023 /** Board that updates groups, pattern states, and vcs. 00024 00025 @todo Document me! 00026 */ 00027 class HexBoard 00028 { 00029 public: 00030 00031 /** Creates a rectangular board. */ 00032 HexBoard(int width, int height, const ICEngine& ice, 00033 VCBuilderParam& param); 00034 00035 /** Copy constructor. */ 00036 HexBoard(const HexBoard& other); 00037 00038 /** Destructor. */ 00039 ~HexBoard(); 00040 00041 //----------------------------------------------------------------------- 00042 00043 /** @name Parameters */ 00044 // @{ 00045 00046 /** Whether VCs are computed or not. */ 00047 bool UseVCs() const; 00048 00049 /** See UseVCs() */ 00050 void SetUseVCs(bool enable); 00051 00052 /** Whether ICE is used. */ 00053 bool UseICE() const; 00054 00055 /** See UseICE() */ 00056 void SetUseICE(bool enable); 00057 00058 /** Whether decompositions are found and filled-in. */ 00059 bool UseDecompositions() const; 00060 00061 /** See UseDecompositions() */ 00062 void SetUseDecompositions(bool enable); 00063 00064 /** Whether ICE info is backed-up in UndoMove(). */ 00065 bool BackupIceInfo() const; 00066 00067 /** See BackupIceInfo() */ 00068 void SetBackupIceInfo(bool enable); 00069 00070 // @} 00071 00072 //----------------------------------------------------------------------- 00073 00074 /** Clears history. Computes dead/vcs for current state. */ 00075 void ComputeAll(HexColor color); 00076 00077 /** Stores old state on stack, plays move to board, updates 00078 ics/vcs. Hash is modified by the move. Allows ice info to 00079 be backed-up. */ 00080 void PlayMove(HexColor color, HexPoint cell); 00081 00082 /** Stores old state on stack, plays set of stones, updates 00083 ics/vcs. HASH IS NOT MODIFIED! No ice info will be backed up, 00084 but this set of moves can be reverted with a single call to 00085 UndoMove(). */ 00086 void PlayStones(HexColor color, const bitset_t& played, 00087 HexColor color_to_move); 00088 00089 /** Reverts to last state stored on the stack, restoring all state 00090 info. If the option is on, also backs up inferior cell 00091 info. */ 00092 void UndoMove(); 00093 00094 //----------------------------------------------------------------------- 00095 00096 StoneBoard& GetPosition(); 00097 00098 const StoneBoard& GetPosition() const; 00099 00100 const ConstBoard& Const() const; 00101 00102 /** Returns the set of dead cells on the board. This is the union 00103 of all cells found dead previously during the history of moves 00104 since the last ComputeAll() call. */ 00105 bitset_t GetDead() const; 00106 00107 /** Returns the set of inferior cell. */ 00108 const InferiorCells& GetInferiorCells() const; 00109 00110 /** Returns the Inferior Cell Engine the board is using. */ 00111 const ICEngine& ICE() const; 00112 00113 const Groups& GetGroups() const; 00114 00115 Groups& GetGroups(); 00116 00117 const PatternState& GetPatternState() const; 00118 00119 PatternState& GetPatternState(); 00120 00121 /** Returns the connection set for color. */ 00122 const VCSet& Cons(HexColor color) const; 00123 00124 /** Returns the connection set for color. */ 00125 VCSet& Cons(HexColor color); 00126 00127 /** Returns the connection builder for this board. */ 00128 VCBuilder& Builder(); 00129 00130 /** Returns the connection builder for this board. */ 00131 const VCBuilder& Builder() const; 00132 00133 //----------------------------------------------------------------------- 00134 00135 int Width() const; 00136 00137 int Height() const; 00138 00139 std::string Write() const; 00140 00141 std::string Write(const bitset_t& bs) const; 00142 00143 private: 00144 00145 /** Stores state of the board. */ 00146 struct History 00147 { 00148 /** Saved board state. */ 00149 StoneBoard board; 00150 00151 /** Groups on this board state. */ 00152 Groups groups; 00153 00154 /** The inferior cell data for this state. */ 00155 InferiorCells inf; 00156 00157 /** Color to play from this state. */ 00158 HexColor to_play; 00159 00160 /** Move last played from this state. */ 00161 HexPoint last_played; 00162 00163 History(const StoneBoard& b, const Groups& g, const InferiorCells& i, 00164 HexColor tp, HexPoint lp) 00165 : board(b), groups(g), inf(i), to_play(tp), last_played(lp) 00166 { } 00167 }; 00168 00169 //----------------------------------------------------------------------- 00170 00171 /** @name Member variables. 00172 @warning If you change anything here, be sure to update the 00173 copy constructor!! 00174 */ 00175 00176 // @{ 00177 00178 StoneBoard m_brd; 00179 00180 /** ICEngine used to compute inferior cells. */ 00181 const ICEngine* m_ice; 00182 00183 Groups m_groups; 00184 00185 PatternState m_patterns; 00186 00187 /** Builder used to compute virtual connections. */ 00188 VCBuilder m_builder; 00189 00190 /** Connection sets for black and white. */ 00191 boost::scoped_ptr<VCSet> m_cons[BLACK_AND_WHITE]; 00192 00193 /** The vc changelogs for both black and white. */ 00194 ChangeLog<VC> m_log[BLACK_AND_WHITE]; 00195 00196 /** History stack. */ 00197 std::vector<History> m_history; 00198 00199 /** The set of inferior cells for the current boardstate. */ 00200 InferiorCells m_inf; 00201 00202 /** See UseVCs() */ 00203 bool m_use_vcs; 00204 00205 /** See UseICE() */ 00206 bool m_use_ice; 00207 00208 /** See UseDecompositions() */ 00209 bool m_use_decompositions; 00210 00211 /** See BackupIceInfo() */ 00212 bool m_backup_ice_info; 00213 00214 // @} 00215 00216 //----------------------------------------------------------------------- 00217 00218 /** No assignments allowed! Use the copy constructor if you must 00219 make a copy, but you shouldn't be copying boards around very 00220 often. */ 00221 void operator=(const HexBoard& other); 00222 00223 void Initialize(); 00224 00225 void ComputeInferiorCells(HexColor color_to_move); 00226 00227 void BuildVCs(); 00228 00229 void BuildVCs(const Groups& oldGroups, bitset_t added[BLACK_AND_WHITE], 00230 bool use_changelog); 00231 00232 void MarkChangeLog(); 00233 00234 void RevertVCs(); 00235 00236 void HandleVCDecomposition(HexColor color_to_move, bool use_changelog); 00237 00238 void AddStones(HexColor color, const bitset_t& played, 00239 HexColor color_to_move, bool use_changelog); 00240 00241 void ClearHistory(); 00242 00243 void PushHistory(HexColor color, HexPoint cell); 00244 00245 void PopHistory(); 00246 }; 00247 00248 inline StoneBoard& HexBoard::GetPosition() 00249 { 00250 return m_brd; 00251 } 00252 00253 inline const StoneBoard& HexBoard::GetPosition() const 00254 { 00255 return m_brd; 00256 } 00257 00258 inline const ConstBoard& HexBoard::Const() const 00259 { 00260 return m_brd.Const(); 00261 } 00262 00263 inline bitset_t HexBoard::GetDead() const 00264 { 00265 return m_inf.Dead(); 00266 } 00267 00268 inline const InferiorCells& HexBoard::GetInferiorCells() const 00269 { 00270 return m_inf; 00271 } 00272 00273 inline const ICEngine& HexBoard::ICE() const 00274 { 00275 return *m_ice; 00276 } 00277 00278 inline const Groups& HexBoard::GetGroups() const 00279 { 00280 return m_groups; 00281 } 00282 00283 inline Groups& HexBoard::GetGroups() 00284 { 00285 return m_groups; 00286 } 00287 00288 inline const PatternState& HexBoard::GetPatternState() const 00289 { 00290 return m_patterns; 00291 } 00292 00293 inline PatternState& HexBoard::GetPatternState() 00294 { 00295 return m_patterns; 00296 } 00297 00298 inline const VCSet& HexBoard::Cons(HexColor color) const 00299 { 00300 return *m_cons[color].get(); 00301 } 00302 00303 inline VCSet& HexBoard::Cons(HexColor color) 00304 { 00305 return *m_cons[color].get(); 00306 } 00307 00308 inline VCBuilder& HexBoard::Builder() 00309 { 00310 return m_builder; 00311 } 00312 00313 inline const VCBuilder& HexBoard::Builder() const 00314 { 00315 return m_builder; 00316 } 00317 00318 inline bool HexBoard::UseVCs() const 00319 { 00320 return m_use_vcs; 00321 } 00322 00323 inline void HexBoard::SetUseVCs(bool enable) 00324 { 00325 m_use_vcs = enable; 00326 } 00327 00328 inline bool HexBoard::UseICE() const 00329 { 00330 return m_use_ice; 00331 } 00332 00333 inline void HexBoard::SetUseICE(bool enable) 00334 { 00335 m_use_ice = enable; 00336 } 00337 00338 inline bool HexBoard::UseDecompositions() const 00339 { 00340 return m_use_decompositions; 00341 } 00342 00343 inline void HexBoard::SetUseDecompositions(bool enable) 00344 { 00345 m_use_decompositions = enable; 00346 } 00347 00348 inline bool HexBoard::BackupIceInfo() const 00349 { 00350 return m_backup_ice_info; 00351 } 00352 00353 inline void HexBoard::SetBackupIceInfo(bool enable) 00354 { 00355 m_backup_ice_info = enable; 00356 } 00357 00358 inline int HexBoard::Width() const 00359 { 00360 return m_brd.Width(); 00361 } 00362 00363 inline int HexBoard::Height() const 00364 { 00365 return m_brd.Height(); 00366 } 00367 00368 inline std::string HexBoard::Write() const 00369 { 00370 return m_brd.Write(); 00371 } 00372 00373 inline std::string HexBoard::Write(const bitset_t& bs) const 00374 { 00375 return m_brd.Write(bs); 00376 } 00377 00378 //---------------------------------------------------------------------------- 00379 00380 inline std::ostream& operator<<(std::ostream &os, const HexBoard& b) 00381 { 00382 os << b.Write(); 00383 return os; 00384 } 00385 00386 //---------------------------------------------------------------------------- 00387 00388 _END_BENZENE_NAMESPACE_ 00389 00390 #endif // HEXBOARD_H