00001 //---------------------------------------------------------------------------- 00002 /** @file GoAutoBook.h 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef GOAUTOBOOK_H 00007 #define GOAUTOBOOK_H 00008 00009 #include <cmath> 00010 #include <iostream> 00011 #include <fstream> 00012 #include <set> 00013 #include <map> 00014 #include "SgBookBuilder.h" 00015 #include "SgThreadedWorker.h" 00016 #include "GoBoard.h" 00017 #include "GoBoardSynchronizer.h" 00018 00019 //---------------------------------------------------------------------------- 00020 00021 /** Tracks canonical hash. */ 00022 class GoAutoBookState 00023 { 00024 public: 00025 GoAutoBookState(const GoBoard& brd); 00026 00027 ~GoAutoBookState(); 00028 00029 GoBoard& Board(); 00030 00031 const GoBoard& Board() const; 00032 00033 void Play(SgMove move); 00034 00035 void Undo(); 00036 00037 SgHashCode GetHashCode() const; 00038 00039 void Synchronize(); 00040 00041 private: 00042 GoBoardSynchronizer m_synchronizer; 00043 00044 GoBoard m_brd[8]; 00045 00046 SgHashCode m_hash; 00047 00048 void ComputeHashCode(); 00049 }; 00050 00051 inline GoBoard& GoAutoBookState::Board() 00052 { 00053 return m_brd[0]; 00054 } 00055 00056 inline const GoBoard& GoAutoBookState::Board() const 00057 { 00058 return m_brd[0]; 00059 } 00060 00061 //---------------------------------------------------------------------------- 00062 00063 class GoAutoBook 00064 { 00065 public: 00066 GoAutoBook(const std::string& filename) throw(); 00067 00068 ~GoAutoBook(); 00069 00070 bool Get(const GoAutoBookState& state, SgBookNode& node) const; 00071 00072 void Put(const GoAutoBookState& state, const SgBookNode& node); 00073 00074 void Flush(); 00075 00076 void Save(const std::string& filename) const; 00077 00078 SgMove LookupMove(const GoBoard& brd) const; 00079 00080 SgMove FindBestChild(GoAutoBookState& state) const; 00081 00082 private: 00083 typedef std::map<SgHashCode, SgBookNode> Map; 00084 00085 Map m_data; 00086 00087 std::string m_filename; 00088 }; 00089 00090 //---------------------------------------------------------------------------- 00091 00092 #endif // GOAUTOBOOK_H