00001
00002
00003 #include "osl/game_playing/winCountTracer.h"
00004 #include "osl/game_playing/openingBookTracer.h"
00005 #include "osl/record/opening/openingBook.h"
00006 #include "osl/stl/vector.h"
00007 #include "osl/misc/random.h"
00008 #include <iostream>
00009
00010 osl::game_playing::
00011 WinCountTracer::WinCountTracer(WinCountBook& b, int r, bool v)
00012 : book(b), state_index(0), turn(BLACK), randomness(r), verbose(v)
00013 {
00014 if (randomness < 0)
00015 randomness = 0;
00016 }
00017
00018 osl::game_playing::
00019 WinCountTracer::WinCountTracer(const WinCountTracer& copy)
00020 : OpeningBookTracer(copy),
00021 book(copy.book), state_index(copy.state_index), turn(copy.turn),
00022 randomness(copy.randomness), verbose(copy.verbose),
00023 state_stack(copy.state_stack)
00024 {
00025 }
00026
00027 osl::game_playing::OpeningBookTracer* osl::game_playing::
00028 WinCountTracer::clone() const
00029 {
00030 return new WinCountTracer(*this);
00031 }
00032
00033 void osl::game_playing::
00034 WinCountTracer::update(Move move)
00035 {
00036 state_stack.push(state_index);
00037 assert(move.player() == turn);
00038 changeTurn(turn);
00039 if (! isOutOfBook())
00040 {
00041 const vector<record::opening::OBMove>& moves = book.getMoves(state_index);
00042 for (size_t i=0; i<moves.size(); i++)
00043 {
00044 if(moves[i].getMove() == move)
00045 {
00046 state_index = moves[i].getStateIndex();
00047 if (verbose)
00048 std::cerr << "book: "
00049 << state_stack.top() << "->" << state_index << "\n";
00050 return;
00051 }
00052 }
00053 if (verbose)
00054 std::cerr << "book: end" << "\n";
00055 }
00056 state_index = -1;
00057 }
00058
00059 void osl::game_playing::
00060 WinCountTracer::popMove()
00061 {
00062 state_index = state_stack.top();
00063 state_stack.pop();
00064 changeTurn(turn);
00065 }
00066
00067 bool osl::game_playing::
00068 WinCountTracer::isOutOfBook() const
00069 {
00070 return state_index < 0;
00071 }
00072
00073 const osl::Move osl::game_playing::
00074 WinCountTracer::selectMove() const
00075 {
00076 assert(randomness >= 0);
00077
00078 int maxWin = -1, maxIndex = -1;
00079 vector<record::opening::OBMove> moves = book.getMoves(state_index);
00080
00081 for (size_t index=0; index<moves.size(); ++index)
00082 {
00083 Move move=moves[index].getMove();
00084 const int curIndex = moves[index].getStateIndex();
00085 const int winNum = (book.getWinCount(curIndex)
00086 + (randomness ? (time_seeded_random() % randomness) : 0));
00087 const int loseNum = (book.getLoseCount(curIndex)
00088 + (randomness ? (time_seeded_random() % randomness) : 0));
00089 if (verbose)
00090 std::cerr << move << "," << winNum << "/" << loseNum << std::endl;
00091 if (turn == BLACK)
00092 {
00093 if (winNum >= maxWin + randomness)
00094 {
00095 maxWin=winNum;
00096 maxIndex=index;
00097 }
00098 }
00099 else
00100 {
00101 if (loseNum >= maxWin + randomness)
00102 {
00103 maxWin=winNum;
00104 maxIndex=index;
00105 }
00106 }
00107 }
00108 if (verbose)
00109 std::cerr << std::endl;
00110
00111 if (maxIndex >= 0)
00112 return moves[maxIndex].getMove();
00113 return Move::INVALID();
00114 }
00115
00116
00117
00118
00119
00120
00121