gameState.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gameState.cc
Go to the documentation of this file.
1 /* gameState.cc
2 */
3 #include "osl/game_playing/gameState.h"
4 #include "osl/game_playing/openingBookTracer.h"
5 #include "osl/checkmate/immediateCheckmate.h"
6 #include "osl/search/moveStackRejections.h"
7 #include "osl/move_classifier/pawnDropCheckmate.h"
8 #include "osl/move_classifier/moveAdaptor.h"
9 #include "osl/move_generator/legalMoves.h"
10 #include "osl/effect_util/effectUtil.h"
11 #include "osl/state/historyState.h"
12 #include "osl/hash/hashKeyStack.h"
13 #include "osl/container/moveStack.h"
14 #include "osl/misc/align16New.h"
15 #include "osl/repetitionCounter.h"
16 #include "osl/sennichite.h"
17 #include "osl/enter_king/enterKing.h"
18 #include <boost/foreach.hpp>
19
20 struct osl::game_playing::GameState::State
21 #if OSL_WORDSIZE == 32
22 : public osl::misc::Align16New
23 #endif
24 {
25 HistoryState state;
26 RepetitionCounter counter;
27 MoveStack move_history;
28 vector<int> eval_stack;
29
30 State(const SimpleState& initial_state)
31 : state(initial_state), counter(state.state())
32 {
33 move_history.reserve(1024);
34 }
35 };
36
37 osl::game_playing::
38 GameState::GameState(const SimpleState& initial_state)
39 : stack(new State(initial_state))
40 {
41 }
42
43 osl::game_playing::
44 GameState::GameState(const State& src)
45 : stack(new State(src)) // clone
46 {
47 }
48
49 osl::game_playing::
50 GameState::~GameState()
51 {
52 }
53
54 const osl::Sennichite osl::game_playing::
55 GameState::pushMove(Move m, int eval)
56 {
57 stack->move_history.push(m);
58 const Sennichite result
59 = stack->counter.isSennichite(state(), m);
60 stack->counter.push(state(), m);
61 stack->state.makeMove(m);
62 stack->eval_stack.push_back(eval);
63 return result;
64 }
65
66 osl::game_playing::GameState::MoveType osl::game_playing::
67 GameState::isIllegal(Move m) const
68 {
69 if (! state().isValidMove(m, false))
70 return OTHER_INVALID;
71 typedef move_classifier::PlayerMoveAdaptor<move_classifier::PawnDropCheckmate>
72 PawnDropCheckmate_t;
73 if (PawnDropCheckmate_t::isMember(state(), m))
74 return PAWN_DROP_FOUL;
75
76 stack->state.makeMove(m);
77 const bool unsafe_king = state().inCheck(alt(state().turn()));
78 stack->state.unmakeMove();
79
80 if (unsafe_king)
81 return UNSAFE_KING;
82
83 return VALID;
84 }
85
86 const osl::Move osl::game_playing::
87 GameState::popMove()
88 {
89 const Move result = stack->move_history.lastMove();
90 assert(canPopMove());
91 stack->move_history.pop();
92 stack->counter.pop();
93 stack->state.unmakeMove();
94 stack->eval_stack.pop_back();
95 return result;
96 }
97
98 const osl::NumEffectState& osl::game_playing::
99 GameState::state() const
100 {
101 return stack->state.state();
102 }
103
104 int osl::game_playing::
105 GameState::moves() const
106 {
107 return stack->move_history.size();
108 }
109
110 const osl::MoveStack& osl::game_playing::
111 GameState::moveHistory() const
112 {
113 return stack->move_history;
114 }
115
116 const osl::hash::HashKeyStack& osl::game_playing::
117 GameState::hashHistory() const
118 {
119 return stack->counter.history();
120 }
121
122 const osl::RepetitionCounter& osl::game_playing::
123 GameState::counter() const
124 {
125 return stack->counter;
126 }
127
128 bool osl::game_playing::
129 GameState::canPopMove() const
130 {
131 return ! stack->state.empty();
132 }
133
134 const boost::shared_ptr<osl::game_playing::GameState> osl::game_playing::
135 GameState::clone() const
136 {
137 boost::shared_ptr<GameState> result(new GameState(*stack));
138 return result;
139 }
140
141 const osl::state::SimpleState& osl::game_playing::
142 GameState::getInitialState() const
143 {
144 return stack->state.initialState();
145 }
146
147 const osl::vector<int>& osl::game_playing::
148 GameState::evalStack() const
149 {
150 return stack->eval_stack;
151 }
152
153 void osl::game_playing::
154 GameState::generateMoves(container::MoveVector& normal,
155 container::MoveVector& win,
156 container::MoveVector& draw,
157 container::MoveVector& loss) const
158 {
159 MoveVector all;
160 LegalMoves::generate(state(), all);
161 NumEffectState copy;
162 const HashKey key(state());
163 BOOST_FOREACH(Move m, all) {
164 if (isIllegal(m) != VALID) {
165 loss.push_back(m);
166 continue;
167 }
168 const Sennichite result
169 = counter().isAlmostSennichite(key.newMakeMove(m));
170 if (! result.isNormal()) {
171 if (! result.hasWinner())
172 draw.push_back(m);
173 else {
174 if (result.winner() == alt(state().turn()))
175 loss.push_back(m);
176 else
177 win.push_back(m);
178 }
179 continue;
180 }
181 if (rejectByStack(m)) {
182 loss.push_back(m);
183 continue;
184 }
185 copy.copyFrom(state());
186 copy.makeMove(m);
187 if (! copy.inCheck()) {
188 if (checkmate::ImmediateCheckmate::hasCheckmateMove(copy.turn(), copy)
189 || EnterKing::canDeclareWin(copy)) {
190 loss.push_back(m);
191 continue;
192 }
193 }
194 normal.push_back(m);
195 }
196 }
197
198 void osl::game_playing::
199 GameState::generateNotLosingMoves(MoveVector& normal_or_win_or_draw,
200 MoveVector& loss) const
201 {
202 MoveVector win, draw;
203 generateMoves(normal_or_win_or_draw, win, draw, loss);
204 normal_or_win_or_draw.push_back(win.begin(), win.end());
205 normal_or_win_or_draw.push_back(draw.begin(), draw.end());
206 }
207
208 bool osl::game_playing::
209 GameState::rejectByStack(Move move) const
210 {
211 const int max_depth = 8;
212 if (move.player() == BLACK)
213 return search::MoveStackRejections::probe<BLACK>
214 (state(), moveHistory(), std::min(max_depth,moves()), move, 0,
215 counter().checkCount(alt(move.player())));
216 else
217 return search::MoveStackRejections::probe<WHITE>
218 (state(), moveHistory(), std::min(max_depth,moves()), move, 0,
219 counter().checkCount(alt(move.player())));
220 }
221
222 /* ------------------------------------------------------------------------- */
223 // ;;; Local Variables:
224 // ;;; mode:c++
225 // ;;; c-basic-offset:2
226 // ;;; End: