Back to index

gcompris  8.2.2
Typedefs | Functions
gcompris_alphabeta.h File Reference
#include "gcompris.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef gint(* EvalFunction )(gpointer treeGame)
typedef gboolean(* LeafFunction )(gpointer treeGame)
typedef gpointer(* FirstChildGameFunction )(gpointer treeGame)
typedef gpointer(* NextSiblingGameFunction )(gpointer treeGame)

Functions

gint gc_alphabeta (gboolean maximize, gpointer treeGame, EvalFunction heuristic, gint *bestChild, FirstChildGameFunction firstChild, NextSiblingGameFunction nextSibling, gint alpha, gint beta, gint depth)

Typedef Documentation

typedef gint(* EvalFunction)(gpointer treeGame)

Definition at line 38 of file gcompris_alphabeta.h.

typedef gpointer(* FirstChildGameFunction)(gpointer treeGame)

Definition at line 40 of file gcompris_alphabeta.h.

typedef gboolean(* LeafFunction)(gpointer treeGame)

Definition at line 39 of file gcompris_alphabeta.h.

typedef gpointer(* NextSiblingGameFunction)(gpointer treeGame)

Definition at line 41 of file gcompris_alphabeta.h.


Function Documentation

gint gc_alphabeta ( gboolean  maximize,
gpointer  treeGame,
EvalFunction  heuristic,
gint *  bestChild,
FirstChildGameFunction  firstChild,
NextSiblingGameFunction  nextSibling,
gint  alpha,
gint  beta,
gint  depth 
)

Definition at line 34 of file gcompris_alphabeta.c.

{
  gpointer child;
  gint m, t, nextBest, index;

  g_assert (depth >= 0);
  
  child = firstChild(treeGame);

  *bestChild = -1;

/*   g_warning("gc_alphabeta %d %d %d", depth, alpha, beta); */
  
  /* directly return value for leaf node*/
  if ((!child) || (depth == 0)){
/*     g_warning("gc_alphabeta %d returns %d bestChild %d", depth, heuristic(treeGame), *bestChild); */
    return heuristic(treeGame);
  }

  index = 0;
      
  if (maximize) {
    m = alpha;
    while (child){
      t = gc_alphabeta (!maximize,
                           child,
                           heuristic,
                           &nextBest,
                           firstChild,
                           nextSibling,
                           m,
                           beta,
                           depth - 1
                           );
      if (t > m){
       m = t;
       *bestChild = index;
      }
      if ( m >= beta){
/*     g_warning("gc_alphabeta %d returns %d bestChild %d", depth, heuristic(treeGame), *bestChild); */
       return m;
      }
      index++;
      child = nextSibling(child);
    }
/*     g_warning("gc_alphabeta %d returns %d bestChild %d", depth, heuristic(treeGame), *bestChild); */
    return m;
  }
  else {
    /* minimize */
    m = beta;
    while (child){
      t = gc_alphabeta (!maximize,
                           child,
                           heuristic,
                           &nextBest,
                           firstChild,
                           nextSibling,
                           alpha,
                           m,
                           depth - 1
                           );
      if (t < m){
       m = t;
       *bestChild = index;
      }
      if ( m <= alpha){
/*     g_warning("gc_alphabeta %d returns %d bestChild %d", depth, heuristic(treeGame), *bestChild); */
       return m;
      }
      index++;
      child = nextSibling(child);
    }
/*     g_warning("gc_alphabeta %d returns %d bestChild %d", depth, heuristic(treeGame), *bestChild); */
    return m;

  }

}

Here is the call graph for this function:

Here is the caller graph for this function: