00001 //---------------------------------------------------------------------------- 00002 /** @file SgEvaluatedMoves.h 00003 Data structure for keeping move values. */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SG_EVALUATEDMOVES_H 00007 #define SG_EVALUATEDMOVES_H 00008 00009 #include "SgPointSet.h" 00010 #include "SgVector.h" 00011 00012 //---------------------------------------------------------------------------- 00013 00014 /** Simple data structure keeps a best move value and a list of all moves 00015 with that value. 00016 @todo Originally worked with any SgMove, but introduction of SgPointSet 00017 relevant now requires SgMove=SgPoint. Should be made independent of that 00018 again, otherwise, why not merge with SgEvaluatedMovesArray? */ 00019 class SgEvaluatedMoves 00020 { 00021 public: 00022 explicit SgEvaluatedMoves(const SgPointSet& relevant) 00023 : m_bestValue(s_minValue), 00024 m_relevant(relevant) 00025 { } 00026 00027 SgEvaluatedMoves(const SgEvaluatedMoves& original) 00028 : m_bestValue(original.m_bestValue), 00029 m_moveList(original.m_moveList), 00030 m_relevant(original.m_relevant) 00031 { } 00032 00033 virtual ~SgEvaluatedMoves() 00034 { } 00035 00036 virtual void AddMove(SgPoint move, int value); 00037 00038 virtual void AddMoves(const SgPointSet& moves, int value); 00039 00040 virtual void AddMoves(const SgVector<SgPoint>& moves, int value); 00041 00042 virtual void Clear() 00043 { 00044 m_bestValue = s_minValue; 00045 m_moveList.Clear(); 00046 } 00047 00048 SgPoint BestMove(); 00049 00050 int BestValue() 00051 { 00052 return m_bestValue; 00053 } 00054 00055 const SgPointSet& Relevant() const 00056 { 00057 return m_relevant; 00058 } 00059 00060 bool IsRelevant(SgPoint p) const 00061 { 00062 return m_relevant[p]; 00063 } 00064 00065 void Disable(SgPoint p) 00066 { 00067 m_relevant.Exclude(p); 00068 } 00069 00070 void Enable(SgPoint p) 00071 { 00072 m_relevant.Include(p); 00073 } 00074 00075 virtual SgEvaluatedMoves* Duplicate() const 00076 { 00077 return new SgEvaluatedMoves(*this); 00078 } 00079 00080 virtual int GetEvaluation(SgPoint p) const 00081 { 00082 if (m_moveList.Contains(p)) 00083 return m_bestValue; 00084 else 00085 return 0; 00086 } 00087 00088 /** Compute list of the n best moves. */ 00089 virtual void BestMoves(SgVector<SgPoint>& best, int nuMoves) const; 00090 00091 protected: 00092 int m_bestValue; 00093 00094 SgVector<SgPoint> m_moveList; 00095 00096 SgPointSet m_relevant; 00097 00098 // DS: INT_MIN is sometimes used to mark illegal moves 00099 static const int s_minValue = INT_MIN + 1; 00100 }; 00101 00102 //---------------------------------------------------------------------------- 00103 00104 /** Simple data structure keeps an integer value for each point on a board. 00105 @todo better name: SgEvaluatedPoints? */ 00106 class SgEvaluatedMovesArray 00107 : public SgEvaluatedMoves 00108 { 00109 public: 00110 explicit SgEvaluatedMovesArray(const SgPointSet& relevant, 00111 int boardSize = SG_MAX_SIZE); 00112 00113 virtual ~SgEvaluatedMovesArray() 00114 { } 00115 00116 SgEvaluatedMovesArray(const SgEvaluatedMovesArray& original) 00117 : SgEvaluatedMoves(original), 00118 m_boardSize(original.m_boardSize) 00119 { 00120 for (int i = 0; i < SG_MAXPOINT; ++i) 00121 m_value[i] = original.m_value[i]; 00122 } 00123 00124 virtual void AddMove(SgPoint move, int value); 00125 00126 virtual void ReduceMove(SgPoint move, int value); 00127 00128 virtual void Clear(); 00129 00130 void Write() const; 00131 00132 virtual SgEvaluatedMoves* Duplicate() const 00133 { 00134 return new SgEvaluatedMovesArray(*this); 00135 } 00136 00137 virtual int GetEvaluation(SgPoint p) const 00138 { 00139 return m_value[p]; 00140 } 00141 00142 virtual void BestMoves(SgVector<SgPoint>& best, int nuMoves) const; 00143 00144 private: 00145 int m_value[SG_MAXPOINT]; 00146 00147 int m_boardSize; 00148 00149 SgPoint SelectNextBest(SgVector<SgPoint>& bestSoFar) const; 00150 }; 00151 00152 //---------------------------------------------------------------------------- 00153 00154 #endif // SG_EVALUATEDMOVES_H