4
* Copyright (C) 2005 Frederic Mazzarol
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, see <http://www.gnu.org/licenses/>.
20
#include "awele_utils.h"
24
* Fonction test si famine
25
* Test si le mouvement demandee provoque une \n
26
* famine dans le camp oppose. Met a jour la variable string errorMsg\n
27
* pour affichage sur le plateau de jeu.
28
* @param aw un pointeur sur la structure awale sur laquelle faire le test
29
* @param start un entier donnant la premiere case de l'opposant
30
* @param end un entier donnant la derniere case de l'opposant
31
* @return TRUE si ce mouvement ne declenche pas une famine, FALSE sinon
32
* player est le dernier à avoir joué. C'est son coté qui peut être vide.
34
short int isOpponentHungry(short int player, AWALE * aw)
36
short int i, total, start, end;
38
start = (player == HUMAN)? START_HUMAN : START_COMPUTER;
39
end = (player == HUMAN)? END_HUMAN : END_COMPUTER;
41
for (total = 0, i = start; i <= end; i++) {
42
total += aw->board[i];
52
* Fonction de test si case non vide
53
* Test si la case choisie n'est pas vide
54
* @param hole entier designant la case du plateau choisie
55
* @param aw pointeur sur la structure AWALE courante.
57
AWALE *moveAwale(short int hole, AWALE * aw)
59
AWALE *tempAw, *tempAwGs;
60
gboolean has_capture = FALSE;
62
if (!aw->board[hole]){
66
short int nbBeans, j, last;
68
tempAw = g_malloc(sizeof(AWALE));
70
memcpy(tempAw, aw, sizeof(AWALE));
72
tempAw->last_play = hole;
74
nbBeans = tempAw->board[hole];
75
tempAw->board[hole] = 0;
77
// Déplacement des graines
78
for (j = 1, last = (hole+1)%12 ; j <= nbBeans; j++) {
79
tempAw->board[last] += 1;
80
last = (last + 1) % 12;
85
last = (last +11) %12;
87
/* Grand Slam (play and no capture because this let other player hungry */
88
tempAwGs = g_malloc(sizeof(AWALE));
89
memcpy(tempAwGs, tempAw, sizeof(AWALE));
92
while ((last >= ((tempAw->player == HUMAN)? 0 : 6))
93
&& (last < ((tempAw->player == HUMAN)? 6 : 12))){
94
if ((tempAw->board[last] == 2) || (tempAw->board[last] == 3)){
96
tempAw->CapturedBeans[switch_player(tempAw->player)] += tempAw->board[last];
97
tempAw->board[last] = 0;
104
if (isOpponentHungry(tempAw->player, tempAw)){
106
/* Grand Slam case */
107
//g_warning("Grand Slam: no capture");
111
/* No capture and opponent hungry -> forbidden */
112
//g_warning("isOpponentHungry %s TRUE",(tempAw->player == HUMAN)? "HUMAN" : "COMPUTER" );
119
tempAw->player = switch_player(tempAw->player);
125
* Fonction de chgt de joueur
126
* Cette fonction permet de renvoyer la valeur de l'opposant
127
* @param player un entier representant le joueur courant
128
* @return un entier representant l'opposant
130
short int switch_player(short int player)
132
return (player == HUMAN) ? COMPUTER : HUMAN;
136
* Fonction coup Aleatoire
137
* Cette fonction permet de generer un coup aleatoire
138
* @param a pointeur sur la structure AWALE courante
139
* @return un entier representant le coup a jouer
141
short int randplay(AWALE * a)
147
i = 6 + g_random_int() % 6;
148
} while (a->board[i] == 0 && !(tmp = moveAwale(i, a)));
154
/* last player is hungry and cannot be served ? */
155
gboolean diedOfHunger(AWALE *aw)
157
gint begin = (aw->player == HUMAN) ? 6 : 0;
160
if (isOpponentHungry(switch_player(aw->player), aw)){
161
for (k=0; k <6; k++){
162
if ( aw->board[begin+k] > 6 - k)
165
g_warning("%s is died of hunger", (aw->player == HUMAN) ? "HUMAN" : "COMPUTER");