~ubuntu-branches/ubuntu/maverick/gcompris/maverick

« back to all changes in this revision

Viewing changes to src/boards/awele_utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * gcompris - awele.c
3
 
 *
4
 
 * Copyright (C) 2005 Frederic Mazzarol
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
 
20
 
#include "awele_utils.h"
21
 
#include <string.h>
22
 
 
23
 
/**
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.
33
 
*/
34
 
short int isOpponentHungry(short int player, AWALE * aw)
35
 
{
36
 
    short int i, total, start, end;
37
 
 
38
 
    start = (player == HUMAN)? START_HUMAN : START_COMPUTER;
39
 
    end   = (player == HUMAN)? END_HUMAN : END_COMPUTER;
40
 
 
41
 
    for (total = 0, i = start; i <= end; i++) {
42
 
        total += aw->board[i];
43
 
    }
44
 
 
45
 
    if (!total)
46
 
        return TRUE;
47
 
 
48
 
    return FALSE;
49
 
}
50
 
 
51
 
/**
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.
56
 
*/
57
 
AWALE *moveAwale(short int hole, AWALE * aw)
58
 
{
59
 
  AWALE *tempAw, *tempAwGs;
60
 
  gboolean has_capture = FALSE;
61
 
 
62
 
  if (!aw->board[hole]){
63
 
    return NULL;
64
 
  }
65
 
 
66
 
  short int nbBeans, j, last;
67
 
 
68
 
  tempAw = g_malloc(sizeof(AWALE));
69
 
 
70
 
  memcpy(tempAw, aw, sizeof(AWALE));
71
 
 
72
 
  tempAw->last_play = hole;
73
 
 
74
 
  nbBeans = tempAw->board[hole];
75
 
  tempAw->board[hole] = 0;
76
 
 
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;
81
 
    if (last == hole)
82
 
      last = (last +1)% 12;
83
 
  }
84
 
 
85
 
  last = (last +11) %12;
86
 
 
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));
90
 
 
91
 
  // capture
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)){
95
 
      has_capture = TRUE;
96
 
      tempAw->CapturedBeans[switch_player(tempAw->player)] += tempAw->board[last];
97
 
      tempAw->board[last] = 0;
98
 
      last = (last+11)%12;
99
 
      continue;
100
 
    }
101
 
    break;
102
 
  }
103
 
 
104
 
  if (isOpponentHungry(tempAw->player, tempAw)){
105
 
    if (has_capture){
106
 
      /* Grand Slam case */
107
 
      //g_warning("Grand Slam: no capture");
108
 
      g_free(tempAw);
109
 
      return tempAwGs;
110
 
    } else{
111
 
      /* No capture and  opponent hungry -> forbidden */
112
 
      //g_warning("isOpponentHungry %s TRUE",(tempAw->player == HUMAN)? "HUMAN" : "COMPUTER" );
113
 
      g_free(tempAw);
114
 
      g_free(tempAwGs);
115
 
      return NULL;
116
 
    }
117
 
  }
118
 
  else {
119
 
    tempAw->player = switch_player(tempAw->player);
120
 
    return tempAw;
121
 
  }
122
 
}
123
 
 
124
 
/**
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
129
 
*/
130
 
short int switch_player(short int player)
131
 
{
132
 
    return (player == HUMAN) ? COMPUTER : HUMAN;
133
 
}
134
 
 
135
 
/**
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
140
 
*/
141
 
short int randplay(AWALE * a)
142
 
{
143
 
    short int i;
144
 
    AWALE *tmp = NULL;
145
 
 
146
 
    do {
147
 
        i = 6 + g_random_int() % 6;
148
 
    } while (a->board[i] == 0 && !(tmp = moveAwale(i, a)));
149
 
 
150
 
    g_free(tmp);
151
 
    return (i);
152
 
}
153
 
 
154
 
/* last player is hungry and cannot be served ? */
155
 
gboolean diedOfHunger(AWALE *aw)
156
 
{
157
 
  gint begin = (aw->player == HUMAN) ? 6 : 0;
158
 
  gint k;
159
 
 
160
 
  if (isOpponentHungry(switch_player(aw->player), aw)){
161
 
    for (k=0; k <6; k++){
162
 
      if ( aw->board[begin+k] > 6 - k)
163
 
        return FALSE;
164
 
    }
165
 
    g_warning("%s is died of hunger", (aw->player == HUMAN) ? "HUMAN" : "COMPUTER");
166
 
    return TRUE;
167
 
  }
168
 
  else
169
 
    return FALSE;
170
 
}
171