All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
winCountTracer.cc
Go to the documentation of this file.
1 /* winCountTracer.cc
2  */
6 #include "osl/stl/vector.h"
7 #include "osl/misc/random.h"
8 #include <iostream>
9 
12  : book(b), state_index(0), turn(BLACK), randomness(r), verbose(v)
13 {
14  if (randomness < 0)
15  randomness = 0;
16 }
17 
20  : OpeningBookTracer(copy),
21  book(copy.book), state_index(copy.state_index), turn(copy.turn),
22  randomness(copy.randomness), verbose(copy.verbose),
23  state_stack(copy.state_stack)
24 {
25 }
26 
29 {
30  return new WinCountTracer(*this);
31 }
32 
35 {
36  state_stack.push(state_index);
37  assert(move.player() == turn);
38  turn = alt(turn);
39  if (! isOutOfBook())
40  {
41  const vector<record::opening::OBMove>& moves = book.getMoves(state_index);
42  for (size_t i=0; i<moves.size(); i++)
43  {
44  if(moves[i].getMove() == move)
45  {
46  state_index = moves[i].getStateIndex();
47  if (verbose)
48  std::cerr << "book: "
49  << state_stack.top() << "->" << state_index << "\n";
50  return;
51  }
52  }
53  if (verbose)
54  std::cerr << "book: end" << "\n";
55  }
56  state_index = -1;
57 }
58 
61 {
62  state_index = state_stack.top();
63  state_stack.pop();
64  turn = alt(turn);
65 }
66 
69 {
70  return state_index < 0;
71 }
72 
75 {
76  assert(randomness >= 0);
77 
78  int maxWin = -1, maxIndex = -1;
79  vector<record::opening::OBMove> moves = book.getMoves(state_index);
80 
81  for (size_t index=0; index<moves.size(); ++index)
82  {
83  Move move=moves[index].getMove();
84  const int curIndex = moves[index].getStateIndex();
85  const int winNum = (book.getWinCount(curIndex)
86  + (randomness ? (time_seeded_random() % randomness) : 0));
87  const int loseNum = (book.getLoseCount(curIndex)
88  + (randomness ? (time_seeded_random() % randomness) : 0));
89  if (verbose)
90  std::cerr << move << "," << winNum << "/" << loseNum << std::endl;
91  if (turn == BLACK)
92  {
93  if (winNum >= maxWin + randomness)
94  {
95  maxWin=winNum;
96  maxIndex=index;
97  }
98  }
99  else
100  {
101  if (loseNum >= maxWin + randomness)
102  {
103  maxWin=winNum;
104  maxIndex=index;
105  }
106  }
107  }
108  if (verbose)
109  std::cerr << std::endl;
110 
111  if (maxIndex >= 0)
112  return moves[maxIndex].getMove();
113  return Move::INVALID();
114 }
115 
116 
117 /* ------------------------------------------------------------------------- */
118 // ;;; Local Variables:
119 // ;;; mode:c++
120 // ;;; c-basic-offset:2
121 // ;;; End: