This file is indexed.

/usr/include/claw/impl/game_ai.tpp is in libclaw-dev 1.7.4-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
  CLAW - a C++ Library Absolutely Wonderful

  CLAW is a free library without any particular aim but being useful to 
  anyone.

  Copyright (C) 2005 Sébastien Angibaud
  Copyright (C) 2005-2011 Julien Jorge

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: julien.jorge@gamned.org
*/
/**
 * \file game_ai.tpp
 * \brief Implémentation de fonctions d'intelligence artificielle.
 * \author Julien Jorge & Sébastien Angibaud
 */
#include <claw/max_vector.hpp>

#include <cstdlib>

//**************************** gamestate **************************************

/*---------------------------------------------------------------------------*/
/**
 * \brief Destructor.
 */
template<typename Action, typename Numeric>
claw::ai::game::game_state<Action, Numeric>::~game_state()
{
  // nothing to do
} // game_state::~game_state()

/*---------------------------------------------------------------------------*/
/**
 * \brief Get the minimal score a state can get.
 */
template <typename Action, typename Numeric>
typename claw::ai::game::game_state<Action, Numeric>::score
claw::ai::game::game_state<Action, Numeric>::min_score()
{
  return s_min_score; 
} // game_state::min_score()

/*---------------------------------------------------------------------------*/
/** 
 * \brief Get the maximal score a state can get.
 */
template <typename Action, typename Numeric>
typename claw::ai::game::game_state<Action, Numeric>::score
claw::ai::game::game_state<Action, Numeric>::max_score()
{
  return s_max_score; 
} // game_state::max_score()

/*---------------------------------------------------------------------------*/
/** 
 * \brief Truncate a score to fit in the range (min_score(), max_score()).
 * \param score_val The value to fit.
 */
template<typename Action, typename Numeric>
typename claw::ai::game::game_state<Action, Numeric>::score
claw::ai::game::game_state<Action, Numeric>::fit
( score score_val ) const 
{ 
  if ( s_max_score < score_val ) 
    return s_max_score;
  else if ( score_val < s_min_score )
    return s_min_score;
  else
    return score_val;
} // game_state::fit()


//**************************** action_eval ************************************


/*---------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param a The evaluated action.
 * \param e The evaluation of the action.
 */
template <typename Action, typename Numeric>
claw::ai::game::action_eval<Action, Numeric>::action_eval
( const Action& a, const Numeric& e)
  : action(a), eval(e)
{

} // action_eval::action_eval()

/*---------------------------------------------------------------------------*/
/**
 * \brief Compare with an otreh action.
 * \param ae The other action.
 */
template <typename Action, typename Numeric>
bool claw::ai::game::action_eval<Action, Numeric>::operator<
  ( const action_eval& ae ) const 
{
  return eval <  ae.eval; 
} // action_eval::operator<()

#if 0
/*---------------------------------------------------------------------------*/
/**
 * \brief Egalité de deux actions.
 * \return vrai si this->eval == ae.eval.
 */
template <typename Action, typename Numeric>
bool claw::ai::game::action_eval<Action, Numeric>::operator==
  ( const action_eval& ae ) const 
{
  return eval == ae.eval; 
} // action_eval::operator==()
#endif



//********************************* min_max ***********************************


/*---------------------------------------------------------------------------*/
/**
 * \brief Apply the min-max algorithm to find the best action.
 * \param depth Depth of the search subtree we are allowed to explore.
 * \param current_state The state of the game.
 * \param computer_turn Tell if the next action is done by the computer.
 */
template<typename State>
typename claw::ai::game::min_max<State>::score
claw::ai::game::min_max<State>::operator()
  ( int depth, const state& current_state, bool computer_turn ) const
{
  score score_val;

  // we reached a final state or we are not allowed to search more.
  if ( current_state.final() || (depth == 0) )
    score_val = current_state.evaluate();
  else
    {
      std::list<action> next_actions;
      typename std::list<action>::const_iterator it;
      state* new_state;

      // get all reachable states
      current_state.next_actions( next_actions );

      if ( next_actions.empty() )
        score_val = current_state.evaluate();   
      else if (computer_turn)
	{                                   
	  score_val = current_state.min_score();
                          
	  for (it = next_actions.begin(); it!=next_actions.end(); ++it)
	    {
	      new_state=static_cast<state*>(current_state.do_action(*it));

	      // evaluate the action of the human player
	      score s = (*this)( depth-1, *new_state, false );
                                          
	      // and keep the best action he can do.
	      if (s > score_val)
		score_val = s;

	      delete new_state;
            }
	}
      else  // human player's turn
	{           
	  score_val = current_state.max_score();

	  for (it = next_actions.begin(); it!=next_actions.end(); ++it)
	    {
	      new_state=static_cast<state*>(current_state.do_action(*it));
                                  
	      // evaluate the action of the computer player
	      score s = (*this)( depth-1, *new_state, true );
                                  
	      // and keep the worst action he can do
	      if (s < score_val)
		score_val = s;
		  
	      delete new_state;
            }
        }
    }
  
  return score_val;
} // min_max::operator()




                
//******************************** alpha_beta *********************************


/*---------------------------------------------------------------------------*/
/**
 * \brief Apply the alpha-beta algorithm to find the best action.
 * \param depth Depth of the search subtree we are allowed to explore.
 * \param current_state The state of the game.
 * \param computer_turn Tell if the next action is done by the computer.
 */
template <typename State>
typename State::score claw::ai::game::alpha_beta<State>::operator()
  ( int depth, const state& current_state, bool computer_turn ) const
{
  return this->compute
    ( depth, current_state, computer_turn, current_state.min_score(),
      current_state.max_score() );
} // alpha_beta::operator()

/*---------------------------------------------------------------------------*/
/**
 * \brief Find the best action using an alpha-beta algorithm.
 * \param depth Depth of the search subtree we are allowed to explore.
 * \param current_state The state of the game.
 * \param computer_turn Tell if the next action is done by the computer.
 * \param alpha Worst score of the current player.
 * \param beta Best score of the other player.
 */
template<typename State>
typename claw::ai::game::alpha_beta<State>::score
claw::ai::game::alpha_beta<State>::compute
( int depth, const state& current_state, bool computer_turn, score alpha,
  score beta ) const
{
  score score_val;
                
  // we reached a final state or we are not allowed to search more.
  if ( current_state.final() || (depth == 0) )
    score_val = current_state.evaluate();
  else
    {
      std::list<action> next_actions;
      typename std::list<action>::const_iterator it;
      State* new_state;

      // get all reachable states
      current_state.next_actions( next_actions );
          
      if ( next_actions.empty() )
        score_val = current_state.evaluate();
      else if (computer_turn)
	{
	  score_val = current_state.min_score();
                          
	  it = next_actions.begin();

	  while ( it!=next_actions.end() && (score_val < beta) )
	    {
	      new_state=static_cast<state*>(current_state.do_action(*it));

	      // evaluate the action of the human player
	      score s = compute
		( depth-1, *new_state, false, std::max(alpha, score_val), beta );

	      // and keep the best action he can do.
	      if (s > score_val) 
		score_val = s;
                                          
	      delete new_state;
                                          
	      ++it;
	    }
	}
      else // human player's turn
	{
	  score_val = current_state.max_score();
                                        
	  it = next_actions.begin();

	  while ( it!=next_actions.end() && (score_val > alpha) )
	    {
	      new_state=static_cast<state*>(current_state.do_action(*it));

	      // evaluate the action of the computer player
	      score s = compute
		( depth-1, *new_state, true, alpha, std::min(beta, score_val) );
                                                
	      // and keep the worst action he can do
	      if (s < score_val)
		score_val = s;
	      ++it;
                                                
	      delete new_state;
            }
        }
    }

  return score_val;
} // alpha_beta::compute()





//***************************** select_action *********************************




/*---------------------------------------------------------------------------*/
/**
 * \brief Select an action using the given method.
 * \param depth Maximum depth of the search tree.
 * \param current_state The state of the game.
 * \param new_action (in/out) Best known action.
 * \param computer_turn Tell if the action is done by the computer.
 */
template<typename Method>
void claw::ai::game::select_action<Method>::operator()
  ( int depth, const state& current_state, action& new_action, 
    bool computer_turn ) const
{
  std::list<action> l;
  typename std::list<action>::iterator it;
  score best_eval;              
  Method method;

  // get all reachable states
  current_state.next_actions( l );
  best_eval = current_state.min_score();

  for (it=l.begin(); it!=l.end(); ++it)
    {
      state* new_state;
      score eval;
                        
      // try and evaluate each action
      new_state = static_cast<state*>(current_state.do_action(*it));
      eval = method(depth-1, *new_state, !computer_turn);

      delete new_state;

      // we keep one of the best actions
      if (eval > best_eval)
        {
          best_eval = eval;
          new_action = *it;
        }
    }
} // select_action::operator()


//*************************** select_random_action ****************************

/**
 * \brief Select a random action among the best ones.
 * \param depth Maximum depth of the search tree.
 * \param current_state The state of the game.
 * \param new_action (in/out) Best known action.
 * \param computer_turn Tell if the action is done by the computer.
 */    
template<typename Method>
void claw::ai::game::select_random_action<Method>::operator()
  ( int depth, const state& current_state, action& new_action, 
    bool computer_turn ) const
{
  std::list<action> l;
  typename std::list<action>::iterator it;
  action_eval<action, score> eval( new_action, current_state.min_score() );
  Method method;
  max_vector< action_eval<action, score> > events( eval );

  // get all reachable states
  current_state.next_actions( l );

  for (it=l.begin(); it!=l.end(); ++it)
    {
      state* new_state;
                        
      // try and evaluate each action
      new_state = static_cast<state*>(current_state.do_action(*it));

      eval.action = *it;
      eval.eval = method(depth-1, *new_state, !computer_turn);

      delete new_state;

      // keep the best actions.
      events.add( eval );
    }

  std::size_t i = (double)rand()/(RAND_MAX + 1) * events.get_v().size();
  new_action = events.get_v()[i].action;
} // select_random_action::operator()