Go to the documentation of this file.00001
00002
00003 #include "osl/game_playing/bookPlayer.h"
00004 #include "osl/game_playing/gameState.h"
00005 #include "osl/game_playing/openingBookTracer.h"
00006 #include "osl/record/compactBoard.h"
00007 #include "osl/container/moveStack.h"
00008 #include <iostream>
00009 #include <stdexcept>
00010
00011 osl::game_playing::
00012 BookPlayer::
00013 BookPlayer(OpeningBookTracer *b, ComputerPlayer *s)
00014 : book(b), searcher(s), book_limit(-1), current_moves(0), valid_initial_position(true)
00015 {
00016 }
00017
00018 osl::game_playing::
00019 BookPlayer::~BookPlayer()
00020 {
00021 }
00022
00023 osl::game_playing::ComputerPlayer* osl::game_playing::
00024 BookPlayer::clone() const
00025 {
00026 return new BookPlayer(book->clone(), searcher->clone());
00027 }
00028
00029 void osl::game_playing::
00030 BookPlayer::setBookLimit(int new_limit)
00031 {
00032 book_limit = new_limit;
00033 }
00034
00035 void osl::game_playing::
00036 BookPlayer::setInitialState(const NumEffectState& state)
00037 {
00038 SimpleState usual(HIRATE);
00039 valid_initial_position = (record::CompactBoard(state) == record::CompactBoard(usual));
00040 if (book->isVerbose() && !valid_initial_position)
00041 std::cerr << "book: end" << "\n";
00042 }
00043
00044 void osl::game_playing::
00045 BookPlayer::pushMove(Move m)
00046 {
00047 ++current_moves;
00048 if (valid_initial_position)
00049 book->update(m);
00050 searcher->pushMove(m);
00051 }
00052 void osl::game_playing::
00053 BookPlayer::popMove()
00054 {
00055 --current_moves;
00056 if (valid_initial_position)
00057 book->popMove();
00058 searcher->popMove();
00059 }
00060
00061 bool osl::game_playing::
00062 BookPlayer::bookAvailable() const
00063 {
00064 return valid_initial_position
00065 && (! book->isOutOfBook())
00066 && (book_limit < 0 || current_moves < book_limit);
00067 }
00068
00069 const osl::Move osl::game_playing::
00070 BookPlayer::moveByBook(const GameState& state)
00071 {
00072 if (bookAvailable())
00073 {
00074 const Move best_move = book->selectMove();
00075 if (best_move.isNormal()
00076 && (! state.isIllegal(best_move)))
00077 return best_move;
00078 }
00079 return Move::INVALID();
00080 }
00081
00082 const osl::search::MoveWithComment osl::game_playing::
00083 BookPlayer::selectBestMove(const GameState& state, int limit, int elapsed, int byoyomi)
00084 {
00085 const Move move = moveByBook(state);
00086 if (move.isNormal())
00087 return MoveWithComment(move);
00088 return searcher->selectBestMove(state, limit, elapsed, byoyomi);
00089 }
00090
00091 const osl::search::MoveWithComment osl::game_playing::
00092 BookPlayer::selectBestMoveInTime(const GameState& state, const search::TimeAssigned& msec)
00093 {
00094 const Move move = moveByBook(state);
00095 if (move.isNormal())
00096 return MoveWithComment(move);
00097 if (ComputerPlayerSelectBestMoveInTime *p
00098 = dynamic_cast<ComputerPlayerSelectBestMoveInTime *>(searcher.get()))
00099 return p->selectBestMoveInTime(state, msec);
00100 throw std::runtime_error("type error in BookPlayer::selectBestMoveInTime");
00101 }
00102
00103 void osl::game_playing::
00104 BookPlayer::allowSpeculativeSearch(bool value)
00105 {
00106 ComputerPlayer::allowSpeculativeSearch(value);
00107 searcher->allowSpeculativeSearch(value);
00108 }
00109
00110 void osl::game_playing::
00111 BookPlayer::setRootIgnoreMoves(const container::MoveVector *rim, bool prediction)
00112 {
00113 ComputerPlayer::setRootIgnoreMoves(rim, prediction);
00114 searcher->setRootIgnoreMoves(rim, prediction);
00115 }
00116
00117 bool osl::game_playing::
00118 BookPlayer::stopSearchNow()
00119 {
00120 return searcher->stopSearchNow();
00121 }
00122
00123
00124
00125
00126
00127