00001 //---------------------------------------------------------------------------- 00002 /** @file SgEvaluatedMoves.cpp 00003 See SgEvaluatedMoves.h */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 #include "SgEvaluatedMoves.h" 00008 00009 #include <iomanip> 00010 #include "SgDebug.h" 00011 #include "SgRandom.h" 00012 #include "SgWrite.h" 00013 00014 using namespace std; 00015 00016 //---------------------------------------------------------------------------- 00017 00018 void SgEvaluatedMoves::AddMove(SgPoint move, int value) 00019 { 00020 if (SgPointUtil::InBoardRange(move) && m_relevant[move]) 00021 { 00022 if (value > m_bestValue) 00023 { 00024 m_bestValue = value; 00025 m_moveList.Clear(); 00026 } 00027 if (value >= m_bestValue) 00028 m_moveList.PushBack(move); 00029 } 00030 } 00031 00032 void SgEvaluatedMoves::AddMoves(const SgPointSet& moves, int value) 00033 { 00034 for (SgSetIterator it(moves); it; ++it) 00035 AddMove(*it, value); 00036 } 00037 00038 void SgEvaluatedMoves::AddMoves(const SgVector<SgPoint>& moves, int value) 00039 { 00040 for (SgVectorIterator<SgPoint> it(moves); it; ++it) 00041 AddMove(*it, value); 00042 } 00043 00044 SgPoint SgEvaluatedMoves::BestMove() 00045 { 00046 if (m_moveList.IsEmpty()) 00047 return SG_PASS; 00048 else 00049 return m_moveList[SgRandom::Global().Int(m_moveList.Length())]; 00050 } 00051 00052 void SgEvaluatedMoves::BestMoves(SgVector<SgPoint>& best, int nuMoves) const 00053 { 00054 SG_UNUSED(nuMoves); 00055 best = m_moveList; // AR: cut off at 'nuMoves'?? 00056 } 00057 00058 //---------------------------------------------------------------------------- 00059 00060 SgEvaluatedMovesArray::SgEvaluatedMovesArray(const SgPointSet& relevant, 00061 int boardSize) 00062 : SgEvaluatedMoves(relevant), 00063 m_boardSize(boardSize) 00064 { 00065 for (int i = 0; i < SG_MAXPOINT; ++i) 00066 m_value[i] = 0; 00067 } 00068 00069 void SgEvaluatedMovesArray::AddMove(SgPoint move, int value) 00070 { 00071 if (SgPointUtil::InBoardRange(move) && m_relevant[move]) 00072 { 00073 m_value[move] += value; 00074 SgEvaluatedMoves::AddMove(move, m_value[move]); 00075 } 00076 } 00077 00078 void SgEvaluatedMovesArray::ReduceMove(SgPoint move, int value) 00079 { 00080 if (SgPointUtil::InBoardRange(move) && m_relevant[move]) 00081 { 00082 m_value[move] -= value; 00083 SgEvaluatedMoves::AddMove(move, m_value[move]); 00084 } 00085 } 00086 00087 SgPoint SgEvaluatedMovesArray::SelectNextBest(SgVector<SgPoint>& bestSoFar) 00088 const 00089 { 00090 int bestValue = s_minValue; SgPoint best = 0; 00091 for (SgPoint p = 0; p < SG_MAXPOINT; ++p) 00092 { 00093 if ((m_value[p] > bestValue) && ! bestSoFar.Contains(p)) 00094 { 00095 bestValue = m_value[p]; 00096 best = p; 00097 } 00098 } 00099 return best; 00100 } 00101 00102 void SgEvaluatedMovesArray::BestMoves(SgVector<SgPoint>& best, int nuMoves) 00103 const 00104 { 00105 best.Clear(); 00106 while (--nuMoves >= 0) 00107 { 00108 int nextBest = SelectNextBest(best); 00109 best.PushBack(nextBest); 00110 } 00111 } 00112 00113 void SgEvaluatedMovesArray::Write() const 00114 { 00115 int i, j; 00116 SgDebug() << " "; 00117 for (j=1; j <= m_boardSize; ++j) 00118 { 00119 SgDebug() << SgPointUtil::Letter(j) << " "; 00120 } 00121 00122 for (i = 1; i <= m_boardSize; ++i) 00123 { 00124 SgDebug() << '\n' << setw(2) << i; 00125 for (j = 1; j <= m_boardSize; ++j) 00126 SgDebug() << setw(5) << m_value[SgPointUtil::Pt(j, i)]; 00127 SgDebug() << '\n'; 00128 } 00129 } 00130 00131 void SgEvaluatedMovesArray::Clear() 00132 { 00133 for (SgPoint p = 0; p < SG_MAXPOINT; ++p) 00134 m_value[p] = 0; 00135 } 00136 00137 //---------------------------------------------------------------------------- 00138