Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/awele_utils.c
blob: 657549fe5a34af8fef2ed43789c44c0188b32f14 (plain)
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
/*
 * gcompris - awele.c
 *
 * Copyright (C) 2005 Frederic Mazzarol
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "awele_utils.h"
#include <string.h>

/**
*  Fonction test si famine
*  Test si le mouvement demandee provoque une \n
*  famine dans le camp oppose. Met a jour la variable string errorMsg\n
*  pour affichage sur le plateau de jeu.
*  @param aw un pointeur sur la structure awale sur laquelle faire le test
*  @param start un entier donnant la premiere case de l'opposant
*  @param end un entier donnant la derniere case de l'opposant
*  @return 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.
*/
short int isOpponentHungry(short int player, AWALE * aw)
{
    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;
}

/**
*  Fonction de test si case non vide
*  Test si la case choisie n'est pas vide
*  @param hole entier designant la case du plateau choisie
*  @param aw pointeur sur la structure AWALE courante.
*/
AWALE *moveAwale(short int hole, AWALE * aw)
{
  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;
  }
}

/**
* Fonction de chgt de joueur
* Cette fonction permet de renvoyer la valeur de l'opposant
* @param player un entier representant le joueur courant
* @return un entier representant l'opposant
*/
short int switch_player(short int player)
{
    return (player == HUMAN) ? COMPUTER : HUMAN;
}

/**
* Fonction coup Aleatoire
* Cette fonction permet de generer un coup aleatoire
* @param a pointeur sur la structure AWALE courante
* @return un entier representant le coup a jouer
*/
short int randplay(AWALE * a)
{
    short int i;
    AWALE *tmp = NULL;

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

    g_free(tmp);
    return (i);
}

/* last player is hungry and cannot be served ? */
gboolean diedOfHunger(AWALE *aw)
{
  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;
}