claw  1.9.0
game_ai.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005 Sébastien Angibaud
8  Copyright (C) 2005-2011 Julien Jorge
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24  contact: julien.jorge@stuff-o-matic.com
25 */
31 #ifndef __CLAW_GAME_AI_HPP__
32 #define __CLAW_GAME_AI_HPP__
33 
34 #include <list>
35 
36 namespace claw
37 {
38  namespace ai
39  {
40  namespace game
41  {
42  //**************************** game_state
43  //********************************
44 
54  template <typename Action, typename Numeric = int>
55  class game_state
56  {
57  public:
59  typedef Numeric score;
60 
62  typedef Action action;
63 
64  public:
65  virtual ~game_state();
66 
68  virtual score evaluate() const = 0;
69 
70  static score min_score();
71  static score max_score();
72 
77  virtual void next_actions(std::list<action>& l) const = 0;
78 
84  virtual game_state* do_action(const action& a) const = 0;
85 
87  virtual bool final() const = 0;
88 
89  protected:
90  score fit(score score_val) const;
91 
92  protected:
94  static const score s_min_score;
95 
97  static const score s_max_score;
98 
99  }; // class game_state
100 
101  //**************************** action_eval ******************************
102 
110  template <typename Action, typename Numeric>
112  {
113  public:
114  action_eval(const Action& a, const Numeric& e);
115 
116  bool operator<(const action_eval& ae) const;
117  // bool operator==( const action_eval& ae ) const;
118 
119  public:
121  Action action;
122 
124  Numeric eval;
125 
126  }; // class action_eval
127 
128  //*************************** min_max ***********************************
129 
139  template <typename State>
140  class min_max
141  {
142  public:
144  typedef State state;
145 
148  typedef typename State::action action;
149 
151  typedef typename State::score score;
152 
153  score operator()(int depth, const state& current_state,
154  bool computer_turn) const;
155  }; // class min_max
156 
157  //*************************** alpha_beta ********************************
158 
168  template <typename State>
170  {
171  public:
173  typedef State state;
174 
177  typedef typename State::action action;
178 
180  typedef typename State::score score;
181 
182  score operator()(int depth, const state& current_state,
183  bool computer_turn) const;
184 
185  private:
186  score compute(int depth, const state& current_state,
187  bool computer_turn, score alpha, score beta) const;
188  }; // class alpha_beta
189 
190  //*************************** select_action *****************************
191 
200  template <typename Method>
202  {
203  public:
205  typedef typename Method::state state;
206 
209  typedef typename Method::action action;
210 
212  typedef typename Method::score score;
213 
214  void operator()(int depth, const state& current_state,
215  action& new_action, bool computer_turn) const;
216  }; // class select_action
217 
218  //************************ select_random_action *************************
219 
228  template <typename Method>
230  {
231  public:
233  typedef typename Method::state state;
234 
237  typedef typename Method::action action;
238 
240  typedef typename Method::score score;
241 
242  void operator()(int depth, const state& current_state,
243  action& new_action, bool computer_turn) const;
244  }; // class select_random_action
245 
246  }
247  }
248 }
249 
250 #include <claw/game_ai.tpp>
251 
252 #endif // __CLAW_IA_JEUX_HPP__
A score associated with an action.
Definition: game_ai.hpp:111
Action action
The action.
Definition: game_ai.hpp:121
Select a random action among the best ones.
Definition: game_ai.hpp:229
Method::score score
The type used to represent the score.
Definition: game_ai.hpp:212
State::score score
The type used to represent the score.
Definition: game_ai.hpp:180
Find an action with the MinMax algorithm.
Definition: game_ai.hpp:140
State state
The type of a state in the game.
Definition: game_ai.hpp:144
Numeric eval
The score of the action.
Definition: game_ai.hpp:124
Method::state state
The type of a state in the game.
Definition: game_ai.hpp:233
Numeric score
The type used for evaluationg the players&#39; scores.
Definition: game_ai.hpp:59
Method::score score
The type used to represent the score.
Definition: game_ai.hpp:240
virtual score evaluate() const =0
Evaluate this state of the game.
Action action
A type representing an action of a player.
Definition: game_ai.hpp:62
A state of a game.
Definition: game_ai.hpp:55
static const score s_max_score
Maximal score that can be given to a state.
Definition: game_ai.hpp:97
State::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:177
virtual void next_actions(std::list< action > &l) const =0
Get all actions that can be done from this state.
State state
The type of a state in the game.
Definition: game_ai.hpp:173
State::score score
The type used to represent the score.
Definition: game_ai.hpp:151
static const score s_min_score
Minimal score that can be given to a state.
Definition: game_ai.hpp:94
Method::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:209
Select an action using a given method (min_max, alpha_beta).
Definition: game_ai.hpp:201
Method::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:237
State::action action
The type of the actions that change the state of the game.
Definition: game_ai.hpp:148
Method::state state
The type of a state in the game.
Definition: game_ai.hpp:205
virtual game_state * do_action(const action &a) const =0
Get a new state obtained when applying an action.
Find an action with the alpha-beta algorithm.
Definition: game_ai.hpp:169
This is the main namespace.
Definition: application.hpp:49