Find an action with the MinMax algorithm. More...
#include <game_ai.hpp>
Public Types | |
typedef State | state |
typedef State::action | action |
typedef State::score | score |
Public Member Functions | |
score | operator() (int depth, const state ¤t_state, bool computer_turn) const |
Apply the min-max algorithm to find the best action. |
Find an action with the MinMax algorithm.
Template parameters:
Definition at line 139 of file game_ai.hpp.
typedef State::action claw::ai::game::min_max< State >::action |
Definition at line 143 of file game_ai.hpp.
typedef State::score claw::ai::game::min_max< State >::score |
Definition at line 144 of file game_ai.hpp.
typedef State claw::ai::game::min_max< State >::state |
Definition at line 142 of file game_ai.hpp.
claw::ai::game::min_max< State >::score claw::ai::game::min_max< State >::operator() | ( | int | depth, |
const state & | current_state, | ||
bool | computer_turn | ||
) | const |
Apply the min-max algorithm to find the best action.
depth | Depth of the search subtree we are allowed to explore. |
current_state | The state of the game. |
computer_turn | Tell if the next action is done by the computer. |
Definition at line 146 of file game_ai.tpp.
{ score score_val; // we reached a final state or we are not allowed to search more. if ( current_state.final() || (depth == 0) ) score_val = current_state.evaluate(); else { std::list<action> next_actions; typename std::list<action>::const_iterator it; state* new_state; // get all reachable states current_state.next_actions( next_actions ); if ( next_actions.empty() ) score_val = current_state.evaluate(); else if (computer_turn) { score_val = current_state.min_score(); for (it = next_actions.begin(); it!=next_actions.end(); ++it) { new_state=static_cast<state*>(current_state.do_action(*it)); // evaluate the action of the human player score s = (*this)( depth-1, *new_state, false ); // and keep the best action he can do. if (s > score_val) score_val = s; delete new_state; } } else // human player's turn { score_val = current_state.max_score(); for (it = next_actions.begin(); it!=next_actions.end(); ++it) { new_state=static_cast<state*>(current_state.do_action(*it)); // evaluate the action of the computer player score s = (*this)( depth-1, *new_state, true ); // and keep the worst action he can do if (s < score_val) score_val = s; delete new_state; } } } return score_val; } // min_max::operator()