CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Types | Public Member Functions

claw::ai::game::min_max< State > Class Template Reference

Find an action with the MinMax algorithm. More...

#include <game_ai.hpp>

List of all members.

Public Types

typedef State state
typedef State::action action
typedef State::score score

Public Member Functions

score operator() (int depth, const state &current_state, bool computer_turn) const
 Apply the min-max algorithm to find the best action.

Detailed Description

template<typename State>
class claw::ai::game::min_max< State >

Find an action with the MinMax algorithm.

Template parameters:

Author:
Julien Jorge

Definition at line 139 of file game_ai.hpp.


Member Typedef Documentation

template<typename State>
typedef State::action claw::ai::game::min_max< State >::action

Definition at line 143 of file game_ai.hpp.

template<typename State>
typedef State::score claw::ai::game::min_max< State >::score

Definition at line 144 of file game_ai.hpp.

template<typename State>
typedef State claw::ai::game::min_max< State >::state

Definition at line 142 of file game_ai.hpp.


Member Function Documentation

template<typename State >
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.

Parameters:
depthDepth of the search subtree we are allowed to explore.
current_stateThe state of the game.
computer_turnTell 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()

The documentation for this class was generated from the following files: