bookPlayer.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bookPlayer.cc
Go to the documentation of this file.
1 /* bookPlayer.cc
2 */
3 #include "osl/game_playing/bookPlayer.h"
4 #include "osl/game_playing/gameState.h"
5 #include "osl/game_playing/openingBookTracer.h"
6 #include "osl/record/compactBoard.h"
7 #include "osl/container/moveStack.h"
8 #include <iostream>
9 #include <stdexcept>
10
11 osl::game_playing::
12 BookPlayer::
13 BookPlayer(OpeningBookTracer *b, ComputerPlayer *s)
14 : book(b), searcher(s), book_limit(-1), current_moves(0), valid_initial_position(true)
15 {
16 }
17
18 osl::game_playing::
19 BookPlayer::~BookPlayer()
20 {
21 }
22
23 osl::game_playing::ComputerPlayer* osl::game_playing::
24 BookPlayer::clone() const
25 {
26 return new BookPlayer(book->clone(), searcher->clone());
27 }
28
29 void osl::game_playing::
30 BookPlayer::setBookLimit(int new_limit)
31 {
32 book_limit = new_limit;
33 }
34
35 void osl::game_playing::
36 BookPlayer::setInitialState(const NumEffectState& state)
37 {
38 SimpleState usual(HIRATE);
39 valid_initial_position = (record::CompactBoard(state) == record::CompactBoard(usual));
40 if (book->isVerbose() && !valid_initial_position)
41 std::cerr << "book: end" << "\n";
42 }
43
44 void osl::game_playing::
45 BookPlayer::pushMove(Move m)
46 {
47 ++current_moves;
48 if (valid_initial_position)
49 book->update(m);
50 searcher->pushMove(m);
51 }
52 void osl::game_playing::
53 BookPlayer::popMove()
54 {
55 --current_moves;
56 if (valid_initial_position)
57 book->popMove();
58 searcher->popMove();
59 }
60
61 bool osl::game_playing::
62 BookPlayer::bookAvailable() const
63 {
64 return valid_initial_position
65 && (! book->isOutOfBook())
66 && (book_limit < 0 || current_moves < book_limit);
67 }
68
69 const osl::Move osl::game_playing::
70 BookPlayer::moveByBook(const GameState& state)
71 {
72 if (bookAvailable())
73 {
74 const Move best_move = book->selectMove();
75 if (best_move.isNormal()
76 && (! state.isIllegal(best_move)))
77 return best_move;
78 }
79 return Move::INVALID();
80 }
81
82 const osl::search::MoveWithComment osl::game_playing::
83 BookPlayer::selectBestMove(const GameState& state, int limit, int elapsed, int byoyomi)
84 {
85 const Move move = moveByBook(state);
86 if (move.isNormal())
87 return MoveWithComment(move);
88 return searcher->selectBestMove(state, limit, elapsed, byoyomi);
89 }
90
91 const osl::search::MoveWithComment osl::game_playing::
92 BookPlayer::selectBestMoveInTime(const GameState& state, const search::TimeAssigned& msec)
93 {
94 const Move move = moveByBook(state);
95 if (move.isNormal())
96 return MoveWithComment(move);
97 if (ComputerPlayerSelectBestMoveInTime *p
98 = dynamic_cast<ComputerPlayerSelectBestMoveInTime *>(searcher.get()))
99 return p->selectBestMoveInTime(state, msec);
100 throw std::runtime_error("type error in BookPlayer::selectBestMoveInTime");
101 }
102
103 void osl::game_playing::
104 BookPlayer::allowSpeculativeSearch(bool value)
105 {
106 ComputerPlayer::allowSpeculativeSearch(value);
107 searcher->allowSpeculativeSearch(value);
108 }
109
110 void osl::game_playing::
111 BookPlayer::setRootIgnoreMoves(const container::MoveVector *rim, bool prediction)
112 {
113 ComputerPlayer::setRootIgnoreMoves(rim, prediction);
114 searcher->setRootIgnoreMoves(rim, prediction);
115 }
116
117 bool osl::game_playing::
118 BookPlayer::stopSearchNow()
119 {
120 return searcher->stopSearchNow();
121 }
122
123 /* ------------------------------------------------------------------------- */
124 // ;;; Local Variables:
125 // ;;; mode:c++
126 // ;;; c-basic-offset:2
127 // ;;; End: