Back to index

gcompris  8.2.2
Functions
awele_utils.c File Reference
#include "awele_utils.h"
#include <string.h>

Go to the source code of this file.

Functions

short int isOpponentHungry (short int player, AWALE *aw)
 Fonction test si famine Test si le mouvement demandee provoque une
famine dans le camp oppose.
AWALEmoveAwale (short int hole, AWALE *aw)
 Fonction de test si case non vide Test si la case choisie n'est pas vide.
short int switch_player (short int player)
 Fonction de chgt de joueur Cette fonction permet de renvoyer la valeur de l'opposant.
short int randplay (AWALE *a)
 Fonction coup Aleatoire Cette fonction permet de generer un coup aleatoire.
gboolean diedOfHunger (AWALE *aw)
 Fonction de manipulation de l'awale.

Function Documentation

gboolean diedOfHunger ( AWALE aw)

Fonction de manipulation de l'awale.

Definition at line 150 of file awele_utils.c.

{
  gint begin = (aw->player == HUMAN) ? 6 : 0;
  gint k;

  if (isOpponentHungry(switch_player(aw->player), aw)){
    for (k=0; k <6; k++){
      if ( aw->board[begin+k] > 6 - k)
       return FALSE;
    }
    g_warning("%s is died of hunger", (aw->player == HUMAN) ? "HUMAN" : "COMPUTER");
    return TRUE;
  }
  else
    return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

short int isOpponentHungry ( short int  player,
AWALE aw 
)

Fonction test si famine Test si le mouvement demandee provoque une
famine dans le camp oppose.

Met a jour la variable string errorMsg
pour affichage sur le plateau de jeu.

Parameters:
awun pointeur sur la structure awale sur laquelle faire le test
startun entier donnant la premiere case de l'opposant
endun entier donnant la derniere case de l'opposant
Returns:
TRUE si ce mouvement ne declenche pas une famine, FALSE sinon player est le dernier à avoir joué. C'est son coté qui peut être vide.

Definition at line 29 of file awele_utils.c.

{
    short int i, total, start, end;

    start = (player == HUMAN)? START_HUMAN : START_COMPUTER;
    end   = (player == HUMAN)? END_HUMAN : END_COMPUTER;

    for (total = 0, i = start; i <= end; i++) {
       total += aw->board[i];
    }

    if (!total)
       return TRUE;

    return FALSE;
}

Here is the caller graph for this function:

AWALE* moveAwale ( short int  hole,
AWALE aw 
)

Fonction de test si case non vide Test si la case choisie n'est pas vide.

Parameters:
holeentier designant la case du plateau choisie
awpointeur sur la structure AWALE courante.

Definition at line 52 of file awele_utils.c.

{
  AWALE *tempAw, *tempAwGs;
  gboolean has_capture = FALSE;

  if (!aw->board[hole]){
    return NULL;
  }

  short int nbBeans, j, last;
  
  tempAw = g_malloc(sizeof(AWALE));
  
  memcpy(tempAw, aw, sizeof(AWALE));

  tempAw->last_play = hole;

  nbBeans = tempAw->board[hole];
  tempAw->board[hole] = 0;
  
  // Déplacement des graines
  for (j = 1, last = (hole+1)%12 ; j <= nbBeans; j++) {
    tempAw->board[last] += 1;
    last = (last + 1) % 12;
    if (last == hole)
      last = (last +1)% 12;
  }

  last = (last +11) %12;

  /* Grand Slam (play and no capture because this let other player hungry */
  tempAwGs = g_malloc(sizeof(AWALE));
  memcpy(tempAwGs, tempAw, sizeof(AWALE));

  // capture
  while ((last >= ((tempAw->player == HUMAN)? 0 : 6))
         && (last < ((tempAw->player == HUMAN)? 6 : 12))){
    if ((tempAw->board[last] == 2) || (tempAw->board[last] == 3)){
      has_capture = TRUE;
      tempAw->CapturedBeans[switch_player(tempAw->player)] += tempAw->board[last];
      tempAw->board[last] = 0;
      last = (last+11)%12;
      continue;
    }
    break;
  }

  if (isOpponentHungry(tempAw->player, tempAw)){
    if (has_capture){
      /* Grand Slam case */
      //g_warning("Grand Slam: no capture");
      g_free(tempAw);
      return tempAwGs;
    } else{
      /* No capture and  opponent hungry -> forbidden */
      //g_warning("isOpponentHungry %s TRUE",(tempAw->player == HUMAN)? "HUMAN" : "COMPUTER" );
      g_free(tempAw);
      g_free(tempAwGs);
      return NULL;
    }
  }    
  else {
    tempAw->player = switch_player(tempAw->player);
    return tempAw;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

short int randplay ( AWALE a)

Fonction coup Aleatoire Cette fonction permet de generer un coup aleatoire.

Parameters:
apointeur sur la structure AWALE courante
Returns:
un entier representant le coup a jouer

Definition at line 136 of file awele_utils.c.

{
    short int i;
    AWALE *tmp = NULL;

    do {
       i = 6 + rand() % 6;
    } while (a->board[i] == 0 && !(tmp = moveAwale(i, a)));

    g_free(tmp);
    return (i);
}

Here is the call graph for this function:

short int switch_player ( short int  player)

Fonction de chgt de joueur Cette fonction permet de renvoyer la valeur de l'opposant.

Parameters:
playerun entier representant le joueur courant
Returns:
un entier representant l'opposant

Definition at line 125 of file awele_utils.c.

{
    return (player == HUMAN) ? COMPUTER : HUMAN;
}

Here is the caller graph for this function: