00001 //---------------------------------------------------------------------------- 00002 /** @file SgMiaiStrategy.h 00003 Simple strategy for winning a specified goal using paired moves. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_MIAISTRATEGY_H 00008 #define SG_MIAISTRATEGY_H 00009 00010 #include <utility> 00011 #include "SgArray.h" 00012 #include "SgBlackWhite.h" 00013 #include "SgBWArray.h" 00014 #include "SgHash.h" 00015 #include "SgPointSet.h" 00016 #include "SgStrategy.h" 00017 #include "SgVector.h" 00018 00019 //---------------------------------------------------------------------------- 00020 00021 typedef std::pair<SgPoint, SgPoint> SgMiaiPair; 00022 00023 //---------------------------------------------------------------------------- 00024 00025 namespace SgMiaiPairUtil 00026 { 00027 SgPoint Other(const SgMiaiPair& pair, SgPoint p); 00028 } 00029 00030 //---------------------------------------------------------------------------- 00031 00032 /** Pair of moves strategy - if opponent plays one we must play other */ 00033 class SgMiaiStrategy 00034 : public SgStrategy 00035 { 00036 public: 00037 /** Initialized to empty strategy; use AddPair */ 00038 SgMiaiStrategy(SgBlackWhite player) 00039 : SgStrategy(player), 00040 m_failed(false) 00041 { } 00042 00043 /** Add a miai strategy on pair of points */ 00044 void AddPair(const SgMiaiPair& miaiPair); 00045 00046 /** Set whole strategy */ 00047 void SetStrategy(const SgVector<SgMiaiPair>& miaiStrategies) 00048 { 00049 //SG_ASSERT(m_miaiStrategies.IsEmpty()); 00050 m_miaiStrategies = miaiStrategies; 00051 } 00052 00053 /** See m_miaiStrategies */ 00054 const SgVector<SgMiaiPair>& MiaiStrategies() const 00055 { 00056 return m_miaiStrategies; 00057 } 00058 00059 /** Pairs that overlap give a double threat */ 00060 bool HasOverlappingMiaiPairs() const; 00061 00062 /** All points on which this strategy depends */ 00063 SgPointSet Dependency() const; 00064 00065 /** See SgStrategyStatus */ 00066 SgStrategyStatus Status() const; 00067 00068 /** See m_openThreats */ 00069 const SgVector<SgPoint>& OpenThreats() const; 00070 00071 /** If exactly one open threat, return that move. 00072 If no active threat, return SG_NULLPOINT. 00073 */ 00074 SgPoint OpenThreatMove() const; 00075 00076 /** See SgStrategy::ExecuteMove */ 00077 void ExecuteMove(SgPoint p, SgBlackWhite player); 00078 00079 /** See SgStrategy::UndoMove */ 00080 void UndoMove(); 00081 00082 /** See SgStrategy::Clear */ 00083 void Clear(); 00084 00085 /** Write object to stream. Do not call directly, use operator<< */ 00086 void Write(std::ostream& stream) const; 00087 private: 00088 /** strategy has failed - set m_failed flag and release resources */ 00089 void StrategyFailed(); 00090 00091 /** move pairs - must play one in each pair */ 00092 SgVector<SgMiaiPair> m_miaiStrategies; 00093 00094 /** open threats must be answered to achieve strategy. 00095 More than one open threat means strategy cannot be achieved. 00096 */ 00097 SgVector<SgPoint> m_openThreats; 00098 00099 /** Strategy has failed if opponent has occupied both points in a pair */ 00100 bool m_failed; 00101 }; 00102 00103 //---------------------------------------------------------------------------- 00104 00105 #endif // SG_MIAISTRATEGY_H