All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
openingBook.h
Go to the documentation of this file.
1 #ifndef _OPENING_BOOK_H
2 #define _OPENING_BOOK_H
3 #include "osl/move.h"
4 #include "osl/stl/vector.h"
7 #include <fstream>
8 #include <functional>
9 
10 namespace osl
11 {
12  namespace record
13  {
14  namespace opening
15  {
16  class OMove
17  {
18  public:
19  OMove(int i) { value = i; }
21  {
22  const Square from = m.from();
23  const Square to = m.to();
24  const int bitFrom = (from.isPieceStand() ? 0 :
25  (from.x() << 4 | from.y()));
26  const int bitTo = (to.isPieceStand() ? 0 :
27  (to.x() << 12 | to.y() << 8));
28  value = (bitFrom | bitTo |
29  static_cast<unsigned int>(m.isPromotion()) << 19 |
30  static_cast<unsigned int>(m.capturePtype()) << 20 |
31  static_cast<unsigned int>(m.ptype()) << 24 |
32  static_cast<int>(m.player()) << 28);
33  }
35  {
36  if ((value & 0xff) == 0)
37  return Square::STAND();
38  else
39  return Square((value >> 4) & 0xf, value & 0xf);
40  }
42  {
43  if (((value >> 8) & 0xff) == 0)
44  return Square::STAND();
45  else
46  return Square((value >> 12) & 0xf, (value >> 8) & 0xf);
47  }
48  bool isPromotion()
49  {
50  return (value >> 19) & 1;
51  }
53  {
54  return static_cast<Ptype>((value >> 20) & 0xf);
55  }
57  {
58  return static_cast<Ptype>((value >> 24) & 0xf);
59  }
61  {
62  return static_cast<Player>((value) >> 28);
63  }
64 
65  operator Move() { return Move(getFrom(), getTo(), getPtype(),
67  getPlayer()); }
68  operator int() { return value; }
69  private:
70  int value;
71  };
72 
73  class OBMove
74  {
77  public:
78  OBMove(Move m,int i) :move(m),stateIndex(i) {}
79  Move getMove() const { return move; }
80  int getStateIndex() const { return stateIndex; }
81  };
82 
99  {
100  int nStates;
101  std::ifstream ifs;
102  public:
103  WinCountBook(const char *filename);
104  ~WinCountBook();
105  int getWinCount(int stateIndex);
106  int getLoseCount(int stateIndex);
107  vector<OBMove> getMoves(int stateIndex);
108  private:
109  int readInt();
110  void seek(int offset);
111  };
112 
113  class WMove;
114  std::ostream& operator<<(std::ostream& os, const WMove& w);
115  std::istream& operator>>(std::istream& is, WMove& w);
116  class WMove
117  {
120  int weight;
121  public:
122  WMove() {}
123  WMove(Move m, int i, int w)
124  : move(m), stateIndex(i), weight(w) {}
125  Move getMove() const { return move; }
126  int getStateIndex() const { return stateIndex; }
127  int getWeight() const { return weight; }
128  void setWeight(const int w) { weight = w; };
129  friend std::ostream& operator<<(std::ostream& os, const WMove& w);
130  friend std::istream& operator>>(std::istream& is, WMove& w);
131  };
132  inline bool operator==(const WMove& l, const WMove& r)
133  {
134  return l.getMove() == r.getMove() && l.getStateIndex() == r.getStateIndex()
135  && l.getWeight() == r.getWeight();
136  }
137 
141  struct WMoveSort : public std::binary_function<WMove, WMove, bool>
142  {
143  bool operator()(const WMove& l, const WMove& r) const
144  {
145  return l.getWeight() > r.getWeight();
146  }
147  };
148 
152  struct WMoveMoveSort : public std::binary_function<WMove, WMove, bool>
153  {
154  bool operator()(const WMove& l, const WMove& r) const
155  {
156  return l.getMove().intValue() < r.getMove().intValue();
157  }
158  };
159 
163  struct WMoveWeightMoveSort : public std::binary_function<WMove, WMove, bool>
164  {
165  bool operator()(const WMove& l, const WMove& r) const
166  {
167  if (l.getWeight() != r.getWeight())
168  return l.getWeight() > r.getWeight();
169  return l.getMove().intValue() < r.getMove().intValue();
170  }
171  };
172 
195  {
196  int nStates;
197  int nMoves;
199  std::ifstream ifs;
200  public:
201  typedef vector<WMove> WMoveContainer;
202 
203  WeightedBook(const char *filename);
204  ~WeightedBook();
210  WMoveContainer getMoves(int stateIndex, const bool zero_include = true);
211  int getWhiteWinCount(int stateIndex);
212  int getBlackWinCount(int stateIndex);
214  SimpleState getBoard(int stateIndex);
215  int getTotalState() const { return nStates; }
216  int getStartState() const { return startState; }
217  void validate();
223  std::vector<int> getParents(const int stateIndex);
235  int getStateIndex(const SimpleState& state,
236  const bool visit_zero = true,
237  const Player player = BLACK);
246  int getStateIndex(const vector<Move>& moves);
247  private:
248  void seek(int offset);
249  static const int HEADER_SIZE = 16;
250  static const int STATE_SIZE = 16;
251  static const int MOVE_SIZE = 12;
252  static const int BOARD_SIZE = 41 * 4;
253  };
254  }
255  } // namespace record
256 } // namespace osl
257 #endif // _OPENING_BOOK_H
258 // ;;; Local Variables:
259 // ;;; mode:c++
260 // ;;; c-basic-offset:2
261 // ;;; End: