Claw
1.7.0
|
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: 00142 typedef State state; 00143 typedef typename State::action action; 00144 typedef typename State::score score; 00145 00146 score operator() 00147 ( int depth, const state& current_state, bool computer_turn ) const; 00148 }; // class min_max 00149 00150 //*************************** alpha_beta ******************************** 00151 00161 template <typename State> 00162 class alpha_beta 00163 { 00164 public: 00165 typedef State state; 00166 typedef typename State::action action; 00167 typedef typename State::score score; 00168 00169 score operator() 00170 ( int depth, const state& current_state, bool computer_turn ) const; 00171 private: 00172 score compute 00173 ( int depth, const state& current_state, bool computer_turn, 00174 score alpha, score beta ) const; 00175 }; // class alpha_beta 00176 00177 //*************************** select_action ***************************** 00178 00187 template<typename Method> 00188 class select_action 00189 { 00190 public: 00191 typedef typename Method::state state; 00192 typedef typename Method::action action; 00193 typedef typename Method::score score; 00194 00195 void operator() 00196 ( int depth, const state& current_state, action& new_action, 00197 bool computer_turn ) const; 00198 }; // class select_action 00199 00200 //************************ select_random_action ************************* 00201 00210 template<typename Method> 00211 class select_random_action 00212 { 00213 public: 00214 typedef typename Method::state state; 00215 typedef typename Method::action action; 00216 typedef typename Method::score score; 00217 00218 void operator()( int depth, const state& current_state, 00219 action& new_action, bool computer_turn ) const; 00220 }; // class select_random_action 00221 00222 } // namespace game 00223 } // namespace it 00224 } // namespace claw 00225 00226 #include <claw/impl/game_ai.tpp> 00227 00228 #endif // __CLAW_IA_JEUX_HPP__