Back to index

gcompris  8.2.2
awele_utils.c
Go to the documentation of this file.
00001 /*
00002  * gcompris - awele.c Copyright (C) 2005 Frederic Mazzarol This program is
00003  * free software; you can redistribute it and/or modify it under the terms 
00004  * of the GNU General Public License as published by the Free Software
00005  * Foundation; either version 2 of the License, or (at your option) any
00006  * later version.  This program is distributed in the hope that it will
00007  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00008  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00009  * General Public License for more details.  You should have received a
00010  * copy of the GNU General Public License along with this program; if not, 
00011  * write to the Free Software Foundation, Inc., 59 Temple Place, Suite
00012  * 330, Boston, MA 02111-1307 USA 
00013  */
00014 
00015 #include "awele_utils.h"
00016 #include <string.h>
00017 
00029 short int isOpponentHungry(short int player, AWALE * aw)
00030 {
00031     short int i, total, start, end;
00032 
00033     start = (player == HUMAN)? START_HUMAN : START_COMPUTER;
00034     end   = (player == HUMAN)? END_HUMAN : END_COMPUTER;
00035 
00036     for (total = 0, i = start; i <= end; i++) {
00037        total += aw->board[i];
00038     }
00039 
00040     if (!total)
00041        return TRUE;
00042 
00043     return FALSE;
00044 }
00045 
00052 AWALE *moveAwale(short int hole, AWALE * aw)
00053 {
00054   AWALE *tempAw, *tempAwGs;
00055   gboolean has_capture = FALSE;
00056 
00057   if (!aw->board[hole]){
00058     return NULL;
00059   }
00060 
00061   short int nbBeans, j, last;
00062   
00063   tempAw = g_malloc(sizeof(AWALE));
00064   
00065   memcpy(tempAw, aw, sizeof(AWALE));
00066 
00067   tempAw->last_play = hole;
00068 
00069   nbBeans = tempAw->board[hole];
00070   tempAw->board[hole] = 0;
00071   
00072   // D├ęplacement des graines
00073   for (j = 1, last = (hole+1)%12 ; j <= nbBeans; j++) {
00074     tempAw->board[last] += 1;
00075     last = (last + 1) % 12;
00076     if (last == hole)
00077       last = (last +1)% 12;
00078   }
00079 
00080   last = (last +11) %12;
00081 
00082   /* Grand Slam (play and no capture because this let other player hungry */
00083   tempAwGs = g_malloc(sizeof(AWALE));
00084   memcpy(tempAwGs, tempAw, sizeof(AWALE));
00085 
00086   // capture
00087   while ((last >= ((tempAw->player == HUMAN)? 0 : 6))
00088          && (last < ((tempAw->player == HUMAN)? 6 : 12))){
00089     if ((tempAw->board[last] == 2) || (tempAw->board[last] == 3)){
00090       has_capture = TRUE;
00091       tempAw->CapturedBeans[switch_player(tempAw->player)] += tempAw->board[last];
00092       tempAw->board[last] = 0;
00093       last = (last+11)%12;
00094       continue;
00095     }
00096     break;
00097   }
00098 
00099   if (isOpponentHungry(tempAw->player, tempAw)){
00100     if (has_capture){
00101       /* Grand Slam case */
00102       //g_warning("Grand Slam: no capture");
00103       g_free(tempAw);
00104       return tempAwGs;
00105     } else{
00106       /* No capture and  opponent hungry -> forbidden */
00107       //g_warning("isOpponentHungry %s TRUE",(tempAw->player == HUMAN)? "HUMAN" : "COMPUTER" );
00108       g_free(tempAw);
00109       g_free(tempAwGs);
00110       return NULL;
00111     }
00112   }    
00113   else {
00114     tempAw->player = switch_player(tempAw->player);
00115     return tempAw;
00116   }
00117 }
00118 
00125 short int switch_player(short int player)
00126 {
00127     return (player == HUMAN) ? COMPUTER : HUMAN;
00128 }
00129 
00136 short int randplay(AWALE * a)
00137 {
00138     short int i;
00139     AWALE *tmp = NULL;
00140 
00141     do {
00142        i = 6 + rand() % 6;
00143     } while (a->board[i] == 0 && !(tmp = moveAwale(i, a)));
00144 
00145     g_free(tmp);
00146     return (i);
00147 }
00148 
00149 /* last player is hungry and cannot be served ? */
00150 gboolean diedOfHunger(AWALE *aw)
00151 {
00152   gint begin = (aw->player == HUMAN) ? 6 : 0;
00153   gint k;
00154 
00155   if (isOpponentHungry(switch_player(aw->player), aw)){
00156     for (k=0; k <6; k++){
00157       if ( aw->board[begin+k] > 6 - k)
00158        return FALSE;
00159     }
00160     g_warning("%s is died of hunger", (aw->player == HUMAN) ? "HUMAN" : "COMPUTER");
00161     return TRUE;
00162   }
00163   else
00164     return FALSE;
00165 }
00166