Claw  1.7.3
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@gamned.org
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 
53  template<typename Action, typename Numeric = int>
54  class game_state
55  {
56  public:
58  typedef Numeric score;
59 
61  typedef Action action;
62 
63  public:
64  virtual ~game_state();
65 
67  virtual score evaluate() const = 0;
68 
69  static score min_score();
70  static score max_score();
71 
76  virtual void next_actions( std::list<action>& l ) const = 0;
77 
83  virtual game_state* do_action( const action& a ) const = 0;
84 
86  virtual bool final() const = 0;
87 
88  protected :
89  score fit( score score_val ) const;
90 
91  protected :
93  static const score s_min_score;
94 
96  static const score s_max_score;
97 
98  }; // class game_state
99 
100  //**************************** action_eval ******************************
101 
109  template <typename Action, typename Numeric>
111  {
112  public:
113  action_eval( const Action& a, const Numeric& e);
114 
115  bool operator< ( const action_eval& ae ) const;
116  //bool operator==( const action_eval& ae ) const;
117 
118  public:
120  Action action;
121 
123  Numeric eval;
124 
125  }; // class action_eval
126 
127  //*************************** min_max ***********************************
128 
138  template <typename State>
139  class min_max
140  {
141  public:
143  typedef State state;
144 
146  typedef typename State::action action;
147 
149  typedef typename State::score score;
150 
151  score operator()
152  ( int depth, const state& current_state, bool computer_turn ) const;
153  }; // class min_max
154 
155  //*************************** alpha_beta ********************************
156 
166  template <typename State>
168  {
169  public:
171  typedef State state;
172 
174  typedef typename State::action action;
175 
177  typedef typename State::score score;
178 
179  score operator()
180  ( int depth, const state& current_state, bool computer_turn ) const;
181 
182  private:
183  score compute
184  ( int depth, const state& current_state, bool computer_turn,
185  score alpha, score beta ) const;
186  }; // class alpha_beta
187 
188  //*************************** select_action *****************************
189 
198  template<typename Method>
200  {
201  public:
203  typedef typename Method::state state;
204 
206  typedef typename Method::action action;
207 
209  typedef typename Method::score score;
210 
211  void operator()
212  ( int depth, const state& current_state, action& new_action,
213  bool computer_turn ) const;
214  }; // class select_action
215 
216  //************************ select_random_action *************************
217 
226  template<typename Method>
228  {
229  public:
231  typedef typename Method::state state;
232 
234  typedef typename Method::action action;
235 
237  typedef typename Method::score score;
238 
239  void operator()( int depth, const state& current_state,
240  action& new_action, bool computer_turn ) const;
241  }; // class select_random_action
242 
243  } // namespace game
244  } // namespace it
245 } // namespace claw
246 
247 #include <claw/impl/game_ai.tpp>
248 
249 #endif // __CLAW_IA_JEUX_HPP__