All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gameState.cc
Go to the documentation of this file.
1 /* gameState.cc
2  */
11 #include "osl/state/historyState.h"
12 #include "osl/hash/hashKeyStack.h"
14 #include "osl/misc/align16New.h"
15 #include "osl/repetitionCounter.h"
16 #include "osl/sennichite.h"
18 #include <boost/foreach.hpp>
19 
21 #if OSL_WORDSIZE == 32
22  : public osl::misc::Align16New
23 #endif
24 {
25  HistoryState state;
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 
38 GameState::GameState(const SimpleState& initial_state)
39  : stack(new State(initial_state))
40 {
41 }
42 
45  : stack(new State(src)) // clone
46 {
47 }
48 
51 {
52 }
53 
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 
68 {
69  if (! state().isValidMove(m, false))
70  return OTHER_INVALID;
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 
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::
100 {
101  return stack->state.state();
102 }
103 
106 {
107  return stack->move_history.size();
108 }
109 
110 const osl::MoveStack& osl::game_playing::
112 {
113  return stack->move_history;
114 }
115 
118 {
119  return stack->counter.history();
120 }
121 
124 {
125  return stack->counter;
126 }
127 
130 {
131  return ! stack->state.empty();
132 }
133 
134 const boost::shared_ptr<osl::game_playing::GameState> osl::game_playing::
136 {
137  boost::shared_ptr<GameState> result(new GameState(*stack));
138  return result;
139 }
140 
143 {
144  return stack->state.initialState();
145 }
146 
147 const osl::vector<int>& osl::game_playing::
149 {
150  return stack->eval_stack;
151 }
152 
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()) {
189  || EnterKing::canDeclareWin(copy)) {
190  loss.push_back(m);
191  continue;
192  }
193  }
194  normal.push_back(m);
195  }
196 }
197 
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 
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: