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
36namespace claw
37{
38 namespace ai
39 {
40 namespace game
41 {
42 //**************************** game_state ********************************
43
53 template<typename Action, typename Numeric = int>
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>
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__
A score associated with an action.
Definition game_ai.hpp:111
Numeric eval
The score of the action.
Definition game_ai.hpp:123
Action action
The action.
Definition game_ai.hpp:120
Find an action with the alpha-beta algorithm.
Definition game_ai.hpp:168
State state
The type of a state in the game.
Definition game_ai.hpp:171
State::action action
The type of the actions that change the state of the game.
Definition game_ai.hpp:174
State::score score
The type used to represent the score.
Definition game_ai.hpp:177
A state of a game.
Definition game_ai.hpp:55
static const score s_min_score
Minimal score that can be given to a state.
Definition game_ai.hpp:93
Numeric score
The type used for evaluationg the players' scores.
Definition game_ai.hpp:58
static const score s_max_score
Maximal score that can be given to a state.
Definition game_ai.hpp:96
virtual void next_actions(std::list< action > &l) const =0
Get all actions that can be done from this state.
virtual game_state * do_action(const action &a) const =0
Get a new state obtained when applying an action.
Action action
A type representing an action of a player.
Definition game_ai.hpp:61
virtual score evaluate() const =0
Evaluate this state of the game.
Find an action with the MinMax algorithm.
Definition game_ai.hpp:140
State::score score
The type used to represent the score.
Definition game_ai.hpp:149
State state
The type of a state in the game.
Definition game_ai.hpp:143
State::action action
The type of the actions that change the state of the game.
Definition game_ai.hpp:146
Select an action using a given method (min_max, alpha_beta).
Definition game_ai.hpp:200
Method::score score
The type used to represent the score.
Definition game_ai.hpp:209
Method::state state
The type of a state in the game.
Definition game_ai.hpp:203
Method::action action
The type of the actions that change the state of the game.
Definition game_ai.hpp:206
Select a random action among the best ones.
Definition game_ai.hpp:228
Method::state state
The type of a state in the game.
Definition game_ai.hpp:231
Method::action action
The type of the actions that change the state of the game.
Definition game_ai.hpp:234
Method::score score
The type used to represent the score.
Definition game_ai.hpp:237
This is the main namespace.
Definition algorithm.hpp:34