|
CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
Select a random action among the best ones. More...
#include <game_ai.hpp>
Public Types | |
| typedef Method::state | state |
| typedef Method::action | action |
| typedef Method::score | score |
Public Member Functions | |
| void | operator() (int depth, const state ¤t_state, action &new_action, bool computer_turn) const |
| Select a random action among the best ones. | |
Select a random action among the best ones.
Template parameters:
Definition at line 211 of file game_ai.hpp.
| typedef Method::action claw::ai::game::select_random_action< Method >::action |
Definition at line 215 of file game_ai.hpp.
| typedef Method::score claw::ai::game::select_random_action< Method >::score |
Definition at line 216 of file game_ai.hpp.
| typedef Method::state claw::ai::game::select_random_action< Method >::state |
Definition at line 214 of file game_ai.hpp.
| void claw::ai::game::select_random_action< Method >::operator() | ( | int | depth, |
| const state & | current_state, | ||
| action & | new_action, | ||
| bool | computer_turn | ||
| ) | const |
Select a random action among the best ones.
| depth | Maximum depth of the search tree. |
| current_state | The state of the game. |
| new_action | (in/out) Best known action. |
| computer_turn | Tell if the action is done by the computer. |
Definition at line 372 of file game_ai.tpp.
References claw::max_vector< E, Comp, Container >::add(), and claw::max_vector< E, Comp, Container >::get_v().
{
std::list<action> l;
typename std::list<action>::iterator it;
action_eval<action, score> eval( new_action, current_state.min_score() );
Method method;
max_vector< action_eval<action, score> > events( eval );
// get all reachable states
current_state.next_actions( l );
for (it=l.begin(); it!=l.end(); ++it)
{
state* new_state;
// try and evaluate each action
new_state = static_cast<state*>(current_state.do_action(*it));
eval.action = *it;
eval.eval = method(depth-1, *new_state, !computer_turn);
delete new_state;
// keep the best actions.
events.add( eval );
}
std::size_t i = (double)rand()/(RAND_MAX + 1) * events.get_v().size();
new_action = events.get_v()[i].action;
} // select_random_action::operator()
1.7.3