Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005 Sébastien Angibaud 00008 Copyright (C) 2005-2011 Julien Jorge 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Lesser General Public 00012 License as published by the Free Software Foundation; either 00013 version 2.1 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this library; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00023 00024 contact: julien.jorge@gamned.org 00025 */ 00031 #ifndef __CLAW_GAME_AI_HPP__ 00032 #define __CLAW_GAME_AI_HPP__ 00033 00034 #include <list> 00035 00036 namespace claw 00037 { 00038 namespace ai 00039 { 00040 namespace game 00041 { 00042 //**************************** game_state ******************************** 00043 00053 template<typename Action, typename Numeric = int> 00054 class game_state 00055 { 00056 public: 00058 typedef Numeric score; 00059 00061 typedef Action action; 00062 00063 public: 00064 virtual ~game_state(); 00065 00067 virtual score evaluate() const = 0; 00068 00069 static score min_score(); 00070 static score max_score(); 00071 00076 virtual void next_actions( std::list<action>& l ) const = 0; 00077 00083 virtual game_state* do_action( const action& a ) const = 0; 00084 00086 virtual bool final() const = 0; 00087 00088 protected : 00089 score fit( score score_val ) const; 00090 00091 protected : 00093 static const score s_min_score; 00094 00096 static const score s_max_score; 00097 00098 }; // class game_state 00099 00100 //**************************** action_eval ****************************** 00101 00109 template <typename Action, typename Numeric> 00110 class action_eval 00111 { 00112 public: 00113 action_eval( const Action& a, const Numeric& e); 00114 00115 bool operator< ( const action_eval& ae ) const; 00116 //bool operator==( const action_eval& ae ) const; 00117 00118 public: 00120 Action action; 00121 00123 Numeric eval; 00124 00125 }; // class action_eval 00126 00127 //*************************** min_max *********************************** 00128 00138 template <typename State> 00139 class min_max 00140 { 00141 public: 00143 typedef State state; 00144 00146 typedef typename State::action action; 00147 00149 typedef typename State::score score; 00150 00151 score operator() 00152 ( int depth, const state& current_state, bool computer_turn ) const; 00153 }; // class min_max 00154 00155 //*************************** alpha_beta ******************************** 00156 00166 template <typename State> 00167 class alpha_beta 00168 { 00169 public: 00171 typedef State state; 00172 00174 typedef typename State::action action; 00175 00177 typedef typename State::score score; 00178 00179 score operator() 00180 ( int depth, const state& current_state, bool computer_turn ) const; 00181 00182 private: 00183 score compute 00184 ( int depth, const state& current_state, bool computer_turn, 00185 score alpha, score beta ) const; 00186 }; // class alpha_beta 00187 00188 //*************************** select_action ***************************** 00189 00198 template<typename Method> 00199 class select_action 00200 { 00201 public: 00203 typedef typename Method::state state; 00204 00206 typedef typename Method::action action; 00207 00209 typedef typename Method::score score; 00210 00211 void operator() 00212 ( int depth, const state& current_state, action& new_action, 00213 bool computer_turn ) const; 00214 }; // class select_action 00215 00216 //************************ select_random_action ************************* 00217 00226 template<typename Method> 00227 class select_random_action 00228 { 00229 public: 00231 typedef typename Method::state state; 00232 00234 typedef typename Method::action action; 00235 00237 typedef typename Method::score score; 00238 00239 void operator()( int depth, const state& current_state, 00240 action& new_action, bool computer_turn ) const; 00241 }; // class select_random_action 00242 00243 } // namespace game 00244 } // namespace it 00245 } // namespace claw 00246 00247 #include <claw/impl/game_ai.tpp> 00248 00249 #endif // __CLAW_IA_JEUX_HPP__