game_ai.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005 Sébastien Angibaud
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_GAME_AI_HPP__
#define __CLAW_GAME_AI_HPP__
#include <list>
namespace claw
{
namespace ai
{
namespace game
{
//**************************** game_state ********************************
template<typename Action, typename Numeric = int>
class game_state
{
public:
typedef Numeric score;
typedef Action action;
public:
virtual ~game_state();
virtual score evaluate() const = 0;
static score min_score();
static score max_score();
virtual void next_actions( std::list<action>& l ) const = 0;
virtual game_state* do_action( const action& a ) const = 0;
virtual bool final() const = 0;
protected :
score fit( score score_val ) const;
protected :
static const score s_min_score;
static const score s_max_score;
}; // class game_state
//**************************** action_eval ******************************
template <typename Action, typename Numeric>
class action_eval
{
public:
action_eval( const Action& a, const Numeric& e);
bool operator< ( const action_eval& ae ) const;
//bool operator==( const action_eval& ae ) const;
public:
Action action;
Numeric eval;
}; // class action_eval
//*************************** min_max ***********************************
template <typename State>
class min_max
{
public:
typedef State state;
typedef typename State::action action;
typedef typename State::score score;
score operator()
( int depth, const state& current_state, bool computer_turn ) const;
}; // class min_max
//*************************** alpha_beta ********************************
template <typename State>
class alpha_beta
{
public:
typedef State state;
typedef typename State::action action;
typedef typename State::score score;
score operator()
( int depth, const state& current_state, bool computer_turn ) const;
private:
score compute
( int depth, const state& current_state, bool computer_turn,
score alpha, score beta ) const;
}; // class alpha_beta
//*************************** select_action *****************************
template<typename Method>
class select_action
{
public:
typedef typename Method::state state;
typedef typename Method::action action;
typedef typename Method::score score;
void operator()
( int depth, const state& current_state, action& new_action,
bool computer_turn ) const;
}; // class select_action
//************************ select_random_action *************************
template<typename Method>
class select_random_action
{
public:
typedef typename Method::state state;
typedef typename Method::action action;
typedef typename Method::score score;
void operator()( int depth, const state& current_state,
action& new_action, bool computer_turn ) const;
}; // class select_random_action
} // namespace game
} // namespace it
} // namespace claw
#include <claw/impl/game_ai.tpp>
#endif // __CLAW_IA_JEUX_HPP__
claw::ai::game::action_eval
A score associated with an action.
Definition: game_ai.hpp:110
claw::ai::game::action_eval::action
Action action
The action.
Definition: game_ai.hpp:120
claw::ai::game::select_random_action
Select a random action among the best ones.
Definition: game_ai.hpp:227
claw::ai::game::select_action::score
Method::score score
The type used to represent the score.
Definition: game_ai.hpp:209
claw::ai::game::alpha_beta::score
State::score score
The type used to represent the score.
Definition: game_ai.hpp:177
claw::ai::game::min_max
Find an action with the MinMax algorithm.
Definition: game_ai.hpp:139
claw::ai::game::min_max::state
State state
The type of a state in the game.
Definition: game_ai.hpp:143
claw::ai::game::action_eval::eval
Numeric eval
The score of the action.
Definition: game_ai.hpp:123
claw::ai::game::select_random_action::state
Method::state state
The type of a state in the game.
Definition: game_ai.hpp:231
claw::ai::game::game_state::score
Numeric score
The type used for evaluationg the players&#39; scores.
Definition: game_ai.hpp:58
claw::ai::game::select_random_action::score
Method::score score
The type used to represent the score.
Definition: game_ai.hpp:237
claw::ai::game::game_state::evaluate
virtual score evaluate() const =0
Evaluate this state of the game.
claw::ai::game::game_state::action
Action action
A type representing an action of a player.
Definition: game_ai.hpp:61
claw::ai::game::game_state
A state of a game.
Definition: game_ai.hpp:54
claw::ai::game::game_state::s_max_score
static const score s_max_score
Maximal score that can be given to a state.
Definition: game_ai.hpp:96
claw::ai::game::alpha_beta::action
State::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:174
claw::ai::game::game_state::next_actions
virtual void next_actions(std::list< action > &l) const =0
Get all actions that can be done from this state.
claw::ai::game::alpha_beta::state
State state
The type of a state in the game.
Definition: game_ai.hpp:171
claw::ai::game::min_max::score
State::score score
The type used to represent the score.
Definition: game_ai.hpp:149
claw::ai::game::game_state::s_min_score
static const score s_min_score
Minimal score that can be given to a state.
Definition: game_ai.hpp:93
claw::ai::game::select_action::action
Method::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:206
claw::ai::game::select_action
Select an action using a given method (min_max, alpha_beta).
Definition: game_ai.hpp:199
claw::ai::game::select_random_action::action
Method::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:234
claw::ai::game::min_max::action
State::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:146
claw::ai::game::select_action::state
Method::state state
The type of a state in the game.
Definition: game_ai.hpp:203
claw::ai::game::game_state::do_action
virtual game_state * do_action(const action &a) const =0
Get a new state obtained when applying an action.
claw::ai::game::alpha_beta
Find an action with the alpha-beta algorithm.
Definition: game_ai.hpp:167
claw
This is the main namespace.
Definition: algorithm.hpp:33