41 template<
typename Action,
typename Numeric>
51 template <
typename Action,
typename Numeric>
62 template <
typename Action,
typename Numeric>
74 template<
typename Action,
typename Numeric>
79 if ( s_max_score < score_val )
81 else if ( score_val < s_min_score )
97 template <
typename Action,
typename Numeric>
99 (
const Action& a,
const Numeric& e)
110 template <
typename Action,
typename Numeric>
114 return eval < ae.
eval;
123 template <
typename Action,
typename Numeric>
127 return eval == ae.
eval;
143 template<
typename State>
146 (
int depth,
const state& current_state,
bool computer_turn )
const
151 if ( current_state.final() || (depth == 0) )
152 score_val = current_state.evaluate();
155 std::list<action> next_actions;
156 typename std::list<action>::const_iterator it;
160 current_state.next_actions( next_actions );
162 if ( next_actions.empty() )
163 score_val = current_state.evaluate();
164 else if (computer_turn)
166 score_val = current_state.min_score();
168 for (it = next_actions.begin(); it!=next_actions.end(); ++it)
170 new_state=
static_cast<state*
>(current_state.do_action(*it));
173 score s = (*this)( depth-1, *new_state, false );
184 score_val = current_state.max_score();
186 for (it = next_actions.begin(); it!=next_actions.end(); ++it)
188 new_state=
static_cast<state*
>(current_state.do_action(*it));
191 score s = (*this)( depth-1, *new_state, true );
219 template <
typename State>
221 (
int depth,
const state& current_state,
bool computer_turn )
const
224 ( depth, current_state, computer_turn, current_state.min_score(),
225 current_state.max_score() );
237 template<
typename State>
240 (
int depth,
const state& current_state,
bool computer_turn, score alpha,
246 if ( current_state.final() || (depth == 0) )
247 score_val = current_state.evaluate();
250 std::list<action> next_actions;
251 typename std::list<action>::const_iterator it;
255 current_state.next_actions( next_actions );
257 if ( next_actions.empty() )
258 score_val = current_state.evaluate();
259 else if (computer_turn)
261 score_val = current_state.min_score();
263 it = next_actions.begin();
265 while ( it!=next_actions.end() && (score_val < beta) )
267 new_state=
static_cast<state*
>(current_state.do_action(*it));
271 ( depth-1, *new_state,
false, std::max(alpha, score_val), beta );
284 score_val = current_state.max_score();
286 it = next_actions.begin();
288 while ( it!=next_actions.end() && (score_val > alpha) )
290 new_state=
static_cast<state*
>(current_state.do_action(*it));
294 ( depth-1, *new_state,
true, alpha, std::min(beta, score_val) );
326 template<
typename Method>
329 bool computer_turn )
const
332 typename std::list<action>::iterator it;
337 current_state.next_actions( l );
338 best_eval = current_state.min_score();
340 for (it=l.begin(); it!=l.end(); ++it)
346 new_state =
static_cast<state*
>(current_state.do_action(*it));
347 eval = method(depth-1, *new_state, !computer_turn);
352 if (eval > best_eval)
370 template<
typename Method>
373 bool computer_turn )
const
376 typename std::list<action>::iterator it;
382 current_state.next_actions( l );
384 for (it=l.begin(); it!=l.end(); ++it)
389 new_state =
static_cast<state*
>(current_state.do_action(*it));
392 eval.eval = method(depth-1, *new_state, !computer_turn);
400 std::size_t i = (double)rand()/(RAND_MAX + 1) * events.
get_v().size();
401 new_action = events.
get_v()[i].action;