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