00001 #ifndef _OPENING_BOOK_H
00002 #define _OPENING_BOOK_H
00003 #include "osl/move.h"
00004 #include "osl/stl/vector.h"
00005 #include "osl/state/simpleState.h"
00006 #include <fstream>
00007
00008 namespace osl
00009 {
00010 namespace record
00011 {
00012 namespace opening
00013 {
00014 class OMove
00015 {
00016 public:
00017 OMove(int i) { value = i; }
00018 OMove(Move m)
00019 {
00020 const Position from = m.from();
00021 const Position to = m.to();
00022 const int bitFrom = (from.isPieceStand() ? 0 :
00023 (from.x() << 4 | from.y()));
00024 const int bitTo = (to.isPieceStand() ? 0 :
00025 (to.x() << 12 | to.y() << 8));
00026 value = (bitFrom | bitTo |
00027 static_cast<unsigned int>(m.isPromote()) << 19 |
00028 static_cast<unsigned int>(m.capturePtype()) << 20 |
00029 static_cast<unsigned int>(m.ptype()) << 24 |
00030 static_cast<int>(m.player()) << 28);
00031 }
00032 Position getFrom()
00033 {
00034 if ((value & 0xff) == 0)
00035 return Position::STAND();
00036 else
00037 return Position((value >> 4) & 0xf, value & 0xf);
00038 }
00039 Position getTo()
00040 {
00041 if (((value >> 8) & 0xff) == 0)
00042 return Position::STAND();
00043 else
00044 return Position((value >> 12) & 0xf, (value >> 8) & 0xf);
00045 }
00046 bool isPromote()
00047 {
00048 return (value >> 19) & 1;
00049 }
00050 Ptype getCapturePtype()
00051 {
00052 return static_cast<Ptype>((value >> 20) & 0xf);
00053 }
00054 Ptype getPtype()
00055 {
00056 return static_cast<Ptype>((value >> 24) & 0xf);
00057 }
00058 Player getPlayer()
00059 {
00060 return static_cast<Player>((value) >> 28);
00061 }
00062
00063 operator Move() { return Move(getFrom(), getTo(), getPtype(),
00064 getCapturePtype(), isPromote(),
00065 getPlayer()); }
00066 operator int() { return value; }
00067 private:
00068 int value;
00069 };
00070
00071 class OBMove
00072 {
00073 Move move;
00074 int stateIndex;
00075 public:
00076 OBMove(Move m,int i) :move(m),stateIndex(i) {}
00077 Move getMove() const { return move; }
00078 int getStateIndex() const { return stateIndex; }
00079 };
00080
00096 class WinCountBook
00097 {
00098 int nStates;
00099 std::ifstream ifs;
00100 public:
00101 WinCountBook(const char *filename);
00102 ~WinCountBook();
00103 int getWinCount(int stateIndex);
00104 int getLoseCount(int stateIndex);
00105 vector<OBMove> getMoves(int stateIndex);
00106 private:
00107 int readInt();
00108 void seek(int offset);
00109 };
00110
00111 class WMove;
00112 std::ostream& operator<<(std::ostream& os, const WMove& w);
00113 std::istream& operator>>(std::istream& is, WMove& w);
00114 class WMove
00115 {
00116 Move move;
00117 int stateIndex;
00118 int weight;
00119 public:
00120 WMove() {}
00121 WMove(Move m, int i, int w)
00122 : move(m), stateIndex(i), weight(w) {}
00123 Move getMove() const { return move; }
00124 int getStateIndex() const { return stateIndex; }
00125 int getWeight() const { return weight; }
00126 void setWeight(const int w) { weight = w; };
00127 friend std::ostream& operator<<(std::ostream& os, const WMove& w);
00128 friend std::istream& operator>>(std::istream& is, WMove& w);
00129 };
00130
00131 struct WMoveSort
00132 {
00133 bool operator()(const WMove& l, const WMove& r) const
00134 {
00135 return l.getWeight() > r.getWeight();
00136 }
00137 };
00138
00160 class WeightedBook
00161 {
00162 int nStates;
00163 int nMoves;
00164 int startState;
00165 std::ifstream ifs;
00166 public:
00167 WeightedBook(const char *filename);
00168 ~WeightedBook();
00169 vector<WMove> getMoves(int stateIndex);
00170 int getWhiteWinCount(int stateIndex);
00171 int getBlackWinCount(int stateIndex);
00172 SimpleState getBoard(int stateIndex);
00173 int getTotalState() const { return nStates; }
00174 int getStartState() const { return startState; }
00175 void validate();
00176 private:
00177 void seek(int offset);
00178 static const int HEADER_SIZE = 16;
00179 static const int STATE_SIZE = 16;
00180 static const int MOVE_SIZE = 12;
00181 static const int BOARD_SIZE = 41 * 4;
00182 };
00183 }
00184 }
00185 }
00186 #endif // _OPENING_BOOK_H
00187
00188
00189
00190