00001
00002
00003 #ifndef OSL_SPECULATIVEALLMOVES_H
00004 #define OSL_SPECULATIVEALLMOVES_H
00005
00006 #include "osl/game_playing/computerPlayer.h"
00007 #include "osl/game_playing/speculativeModel.h"
00008 #include "osl/misc/lightMutex.h"
00009 #include "osl/misc/fixedCapacityVector.h"
00010 #include <boost/thread/thread.hpp>
00011 #include <boost/scoped_ptr.hpp>
00012 #include <boost/shared_ptr.hpp>
00013
00014 namespace osl
00015 {
00016 namespace misc
00017 {
00018 class RealTime;
00019 }
00020 namespace game_playing
00021 {
00022 class SearchPlayer;
00026 class SpeculativeAllMoves : public SpeculativeModel
00027 {
00028 public:
00029 struct SearchAllMoves;
00030 struct ResultVector;
00031 private:
00032 boost::shared_ptr<SearchAllMoves> searcher;
00033 boost::scoped_ptr<boost::thread> thread;
00034 boost::scoped_ptr<ResultVector> results;
00035 int last_search_seconds;
00036 bool has_byoyomi;
00037 bool allowed;
00038 public:
00039 SpeculativeAllMoves();
00040 ~SpeculativeAllMoves();
00041
00042 void startSpeculative(const boost::shared_ptr<GameState> state,
00043 const SearchPlayer& main_player);
00044 void stopOtherThan(Move);
00045 void stopAll();
00046
00047 void setMaxThreads(int new_max_threads)
00048 {
00049 allowed = (new_max_threads > 0);
00050 }
00051
00052 const MoveWithComment waitResult(Move last_move, int wait_for,
00053 SearchPlayer& main_player, int byoyomi);
00054
00055 void selectBestMoveCleanUp();
00056 void clearResource();
00057 private:
00058 class Runner;
00059 };
00060
00061 class SpeculativeAllMoves::ResultVector
00062 {
00063 typedef FixedCapacityVector<std::pair<Move,MoveWithComment>,Move::MaxUniqMoves> vector_t;
00064 vector_t data;
00065 typedef LightMutex Mutex;
00066 mutable Mutex mutex;
00067 public:
00068 ResultVector();
00069 ~ResultVector();
00070
00071 void add(Move prediction, const MoveWithComment& result);
00072 const MoveWithComment* find(Move prediction) const;
00073 void clear();
00074 };
00075
00080 class SpeculativeAllMoves::SearchAllMoves
00081 {
00082 public:
00083 enum Status {
00084 INITIAL, RUNNING, PREDICTION1, PREDICTION2, SEARCH1, SEARCH2, FINISHED
00085 };
00086 class Generator;
00087 friend class Generator;
00088 friend class SpeculativeAllMoves;
00089 private:
00090 boost::shared_ptr<GameState> state;
00091 boost::shared_ptr<SearchPlayer> player;
00092 boost::scoped_ptr<Generator> generator;
00093 SpeculativeAllMoves::ResultVector& results;
00094 Move current_move;
00095 volatile Status status;
00096 int seconds;
00097 typedef boost::mutex Mutex;
00098 mutable Mutex mutex;
00099 boost::condition condition;
00101 volatile bool stop_flag;
00102 public:
00103 explicit SearchAllMoves(SpeculativeAllMoves::ResultVector&);
00104 ~SearchAllMoves();
00105
00106 void setUp(const GameState&, const SearchPlayer&, int standard_seconds,
00107 bool has_byoyomi);
00108
00109 void run();
00110
00111 void stopNow();
00112 void stopOtherThan(Move);
00113 void waitRunning();
00114 bool isFinished() const { return status == FINISHED; }
00115
00116 void setStopSchedule(const misc::RealTime& timer);
00117 const Move currentMove() const;
00118
00119 SearchPlayer* currentPlayer() { return player.get(); }
00120 private:
00121 const MoveWithComment testMove(Move);
00122 class StatusLock;
00123 };
00124 }
00125 }
00126
00127 #endif
00128
00129
00130
00131