Claw  1.7.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@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:
142  typedef State state;
143  typedef typename State::action action;
144  typedef typename State::score score;
145 
146  score operator()
147  ( int depth, const state& current_state, bool computer_turn ) const;
148  }; // class min_max
149 
150  //*************************** alpha_beta ********************************
151 
161  template <typename State>
163  {
164  public:
165  typedef State state;
166  typedef typename State::action action;
167  typedef typename State::score score;
168 
169  score operator()
170  ( int depth, const state& current_state, bool computer_turn ) const;
171  private:
172  score compute
173  ( int depth, const state& current_state, bool computer_turn,
174  score alpha, score beta ) const;
175  }; // class alpha_beta
176 
177  //*************************** select_action *****************************
178 
187  template<typename Method>
189  {
190  public:
191  typedef typename Method::state state;
192  typedef typename Method::action action;
193  typedef typename Method::score score;
194 
195  void operator()
196  ( int depth, const state& current_state, action& new_action,
197  bool computer_turn ) const;
198  }; // class select_action
199 
200  //************************ select_random_action *************************
201 
210  template<typename Method>
212  {
213  public:
214  typedef typename Method::state state;
215  typedef typename Method::action action;
216  typedef typename Method::score score;
217 
218  void operator()( int depth, const state& current_state,
219  action& new_action, bool computer_turn ) const;
220  }; // class select_random_action
221 
222  } // namespace game
223  } // namespace it
224 } // namespace claw
225 
226 #include <claw/impl/game_ai.tpp>
227 
228 #endif // __CLAW_IA_JEUX_HPP__