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