~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/boards/awele.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2006-12-15 23:08:17 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215230817-exr5ks1hd73s3tlk
Tags: 8.2.2-1
* New upstream bugfix release, fixes among other things the support for
  the version of gnucap shipped in etch.
* Add missing dependency on python-gtk2 (Closes: #396523).
* Removed reference to non-existent sound file from memory.c (upstream
  fix - impacts 8.2 as well).  
* Now suggests gnuchess, gnucap, and tuxpaint.
* Updated extended description for the main package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * gcompris - awele.c Copyright (C) 2005 Frederic Mazzarol This program is
 
3
 * free software; you can redistribute it and/or modify it under the terms
 
4
 * of the GNU General Public License as published by the Free Software
 
5
 * Foundation; either version 2 of the License, or (at your option) any
 
6
 * later version.  This program is distributed in the hope that it will
 
7
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 
8
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
9
 * General Public License for more details.  You should have received a
 
10
 * copy of the GNU General Public License along with this program; if not,
 
11
 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 
12
 * 330, Boston, MA 02111-1307 USA
 
13
 */
 
14
 
 
15
#include "gcompris/gcompris.h"
 
16
#include <awele_utils.h>
 
17
#include <awele.h>
 
18
#include <string.h>
 
19
#include <unistd.h>
 
20
 
 
21
 
 
22
static GcomprisBoard *gcomprisBoard = NULL;
 
23
static gboolean board_paused = TRUE;
 
24
 
 
25
char errorMsg[30];
 
26
AWALE *staticAwale;
 
27
int caseCoord[12] =
 
28
        { 102, 206, 309, 413, 522, 628, 626, 520, 411, 307, 201, 100 };
 
29
static GRAPHICS_ELT *graphsElt = NULL;
 
30
 
 
31
static void start_board (GcomprisBoard * agcomprisBoard);
 
32
static void pause_board (gboolean pause);
 
33
static void end_board (void);
 
34
static void set_level (guint level);
 
35
static int gamewon;
 
36
static void game_won (void);
 
37
static void repeat(void);
 
38
 
 
39
static GnomeCanvasGroup *boardRootItem = NULL;
 
40
 
 
41
static gboolean is_our_board (GcomprisBoard * gcomprisBoard);
 
42
static GnomeCanvasItem *awele_create_item (GnomeCanvasGroup * parent);
 
43
static void awele_destroy_all_items (void);
 
44
static void awele_next_level (void);
 
45
static gboolean  to_computer(gpointer data);
 
46
static gint timeout = 0;
 
47
static gboolean computer_turn = FALSE;
 
48
static gboolean sublevel_finished = FALSE;
 
49
 
 
50
/*=============================================*/
 
51
static GcomprisAnimation *animation;
 
52
static GcomprisAnimCanvasItem *anim_item;
 
53
 
 
54
/*
 
55
 * Description of this plugin
 
56
 */
 
57
static BoardPlugin menu_bp = {
 
58
        NULL,
 
59
        NULL,
 
60
        "The awele game",       /* The name that describe this board */
 
61
        "African strategy board game",  /* The description that describes
 
62
                                         * this board */
 
63
        "Frederic Mazzarol <fmazzarol@gmail.com>",      /* The author of this
 
64
                                                         * board */
 
65
        NULL,                   /* Called when the plugin is loaded */
 
66
        NULL,                   /* Called when gcompris exit */
 
67
        NULL,                   /* Show the about box */
 
68
        NULL,                   /* Show the configuration dialog */
 
69
        start_board,            /* Callback to start_board implementation */
 
70
        pause_board,
 
71
        end_board,
 
72
        is_our_board,           /* Return 1 if the plugin can handle the board file */
 
73
        NULL,
 
74
        NULL,
 
75
        set_level,
 
76
        NULL,
 
77
        repeat,
 
78
        NULL,
 
79
        NULL
 
80
};
 
81
 
 
82
/*
 
83
 * Main entry point mandatory for each Gcompris's game
 
84
 * ---------------------------------------------------
 
85
 *
 
86
 */
 
87
 
 
88
GET_BPLUGIN_INFO (awele)
 
89
/*
 
90
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 
91
 *
 
92
 */
 
93
static void pause_board (gboolean pause)
 
94
{
 
95
        if (gcomprisBoard == NULL)
 
96
          return;
 
97
 
 
98
        board_paused = pause;
 
99
 
 
100
        if (pause == FALSE) {
 
101
          if (gamewon == TRUE)
 
102
            game_won ();
 
103
          else
 
104
            if (computer_turn){
 
105
              timeout = g_timeout_add (2000,
 
106
                                       (GSourceFunc) to_computer,
 
107
                                       NULL);
 
108
              anim_item = gc_anim_activate( boardRootItem,
 
109
                                                       animation );
 
110
              gnome_canvas_item_show(GNOME_CANVAS_ITEM(anim_item->canvas));
 
111
            }
 
112
        }
 
113
        else{
 
114
          if (computer_turn){
 
115
            gc_anim_deactivate(anim_item);
 
116
            if (timeout){
 
117
              g_source_remove(timeout);
 
118
              timeout = 0;
 
119
            }
 
120
          }
 
121
        }
 
122
}
 
123
 
 
124
/*
 
125
 */
 
126
static void
 
127
start_board (GcomprisBoard * agcomprisBoard)
 
128
{
 
129
 
 
130
        if (agcomprisBoard != NULL)
 
131
        {
 
132
                gchar *str;
 
133
                GdkPixbuf *pixmap = NULL;
 
134
 
 
135
                gcomprisBoard = agcomprisBoard;
 
136
                gcomprisBoard->level = 1;
 
137
                gcomprisBoard->maxlevel = 9;
 
138
                gcomprisBoard->sublevel = 1;
 
139
                gcomprisBoard->number_of_sublevel = 1;  /* Go to next level after
 
140
                                                         * this number of 'play' */
 
141
 
 
142
                str = gc_skin_image_get("button_reload.png");
 
143
                pixmap = gc_pixmap_load(str);
 
144
                g_free(str);
 
145
                if(pixmap) {
 
146
                  gc_bar_set_repeat_icon(pixmap);
 
147
                  gdk_pixbuf_unref(pixmap);
 
148
                  gc_bar_set(GC_BAR_LEVEL|GC_BAR_REPEAT_ICON);
 
149
                } else {
 
150
                  gc_bar_set(GC_BAR_LEVEL|GC_BAR_REPEAT);
 
151
                }
 
152
 
 
153
                animation = gc_anim_load( "connect4/sablier.txt" );
 
154
 
 
155
                awele_next_level ();
 
156
 
 
157
                gamewon = FALSE;
 
158
                pause_board (FALSE);
 
159
        }
 
160
}
 
161
 
 
162
/*
 
163
 * =======================================
 
164
 */
 
165
static void
 
166
end_board ()
 
167
{
 
168
        if (gcomprisBoard != NULL)
 
169
        {
 
170
                pause_board (TRUE);
 
171
                gc_anim_free(animation);
 
172
                awele_destroy_all_items ();
 
173
        }
 
174
        gcomprisBoard = NULL;
 
175
}
 
176
 
 
177
static gboolean
 
178
is_our_board (GcomprisBoard * gcomprisBoard)
 
179
{
 
180
        if (gcomprisBoard)
 
181
        {
 
182
                if (g_strcasecmp (gcomprisBoard->type, "awele") == 0)
 
183
                {
 
184
                        /*
 
185
                         * Set the plugin entry
 
186
                         */
 
187
                        gcomprisBoard->plugin = &menu_bp;
 
188
 
 
189
                        return TRUE;
 
190
                }
 
191
        }
 
192
        return FALSE;
 
193
}
 
194
 
 
195
/*
 
196
 * Repeat let the user restart the current level
 
197
 *
 
198
 */
 
199
static void repeat (){
 
200
  if (computer_turn){
 
201
    gc_anim_deactivate(anim_item);
 
202
    if (timeout){
 
203
      g_source_remove(timeout);
 
204
      timeout = 0;
 
205
    }
 
206
  }
 
207
  awele_next_level();
 
208
}
 
209
 
 
210
 
 
211
static void
 
212
set_level (guint level)
 
213
{
 
214
 
 
215
  if(gcomprisBoard!=NULL)
 
216
    {
 
217
      gcomprisBoard->level=level;
 
218
      gcomprisBoard->sublevel = 1;
 
219
 
 
220
      if (computer_turn){
 
221
        gc_anim_deactivate(anim_item);
 
222
        if (timeout){
 
223
          g_source_remove(timeout);
 
224
          timeout = 0;
 
225
        }
 
226
      }
 
227
      awele_next_level();
 
228
 
 
229
    }
 
230
}
 
231
 
 
232
 
 
233
/*-------------------------------------------------------------------------------*/
 
234
/*-------------------------------------------------------------------------------*/
 
235
/*
 
236
 * set initial values for the next level
 
237
 */
 
238
static void
 
239
awele_next_level ()
 
240
{
 
241
        gchar *img;
 
242
 
 
243
        img = gc_skin_image_get ("gcompris-bg.jpg");
 
244
        gc_set_background (gnome_canvas_root (gcomprisBoard->canvas),
 
245
                                 img);
 
246
        g_free(img);
 
247
 
 
248
        gc_bar_set_level (gcomprisBoard);
 
249
 
 
250
        awele_destroy_all_items ();
 
251
        gamewon = FALSE;
 
252
        computer_turn = FALSE;
 
253
 
 
254
        /*
 
255
         * Create the level
 
256
         */
 
257
        awele_create_item (gnome_canvas_root (gcomprisBoard->canvas));
 
258
 
 
259
        if ((gcomprisBoard->level % 2) ==0){
 
260
          computer_turn = TRUE;
 
261
          staticAwale->player = HUMAN;
 
262
          timeout = g_timeout_add (2000,
 
263
                                   (GSourceFunc) to_computer,
 
264
                                   NULL);
 
265
          anim_item = gc_anim_activate( boardRootItem,
 
266
                                                   animation );
 
267
          gnome_canvas_item_show(GNOME_CANVAS_ITEM(anim_item->canvas));
 
268
 
 
269
        } else {
 
270
          computer_turn = FALSE;
 
271
        }
 
272
}
 
273
 
 
274
/*
 
275
 * ====================================
 
276
 */
 
277
/*
 
278
 * Destroy all the items
 
279
 */
 
280
static void
 
281
awele_destroy_all_items ()
 
282
{
 
283
        int i;
 
284
 
 
285
        if (boardRootItem != NULL)
 
286
                gtk_object_destroy (GTK_OBJECT (boardRootItem));
 
287
 
 
288
        boardRootItem = NULL;
 
289
 
 
290
        if(graphsElt)
 
291
          {
 
292
            for (i = 0; i < NBHOLE / 2; i++)
 
293
              {
 
294
                gdk_pixbuf_unref(graphsElt->pixbufButton[i]);
 
295
                gdk_pixbuf_unref(graphsElt->pixbufButtonNotify[i]);
 
296
                gdk_pixbuf_unref(graphsElt->pixbufButtonClicked[i]);
 
297
              }
 
298
            g_free(graphsElt);
 
299
            graphsElt = NULL;
 
300
          }
 
301
 
 
302
}
 
303
 
 
304
/*
 
305
 * ====================================
 
306
 */
 
307
static GnomeCanvasItem *
 
308
awele_create_item (GnomeCanvasGroup * parent)
 
309
{
 
310
 
 
311
        GdkPixbuf *pixmap = NULL;
 
312
        gint i = 0, x1 = 0;
 
313
        gchar buffer[2];
 
314
        gchar xpmFile[35] = BOUTON;
 
315
        gchar xpmFileNotify[35] = BOUTON_NOTIFY;
 
316
        gchar xpmFileClic[35] = BOUTON_CLIC;
 
317
 
 
318
        boardRootItem =
 
319
                GNOME_CANVAS_GROUP (gnome_canvas_item_new
 
320
                                    (gnome_canvas_root
 
321
                                     (gcomprisBoard->canvas),
 
322
                                     gnome_canvas_group_get_type (), "x",
 
323
                                     (double) 0, "y", (double) 0, NULL));
 
324
 
 
325
        /*
 
326
         * Load the cute frame
 
327
         */
 
328
        pixmap = gc_pixmap_load ("awele/awele_frame.png");
 
329
 
 
330
        gnome_canvas_item_new (boardRootItem,
 
331
                               gnome_canvas_pixbuf_get_type (),
 
332
                               "pixbuf", pixmap,
 
333
                               "x", (double) 0,
 
334
                               "y", (double) 0,
 
335
                               "width",
 
336
                               (double) gdk_pixbuf_get_width (pixmap),
 
337
                               "height",
 
338
                               (double) gdk_pixbuf_get_height (pixmap),
 
339
                               "width_set", TRUE, "height_set", TRUE, NULL);
 
340
        gdk_pixbuf_unref(pixmap);
 
341
 
 
342
        /*
 
343
         * Display text
 
344
         */
 
345
        {
 
346
          int x, y;
 
347
 
 
348
          x = 35;
 
349
          y = 190;
 
350
          gnome_canvas_item_new (boardRootItem,
 
351
                                 gnome_canvas_text_get_type (),
 
352
                                 "text", _("NORTH"),
 
353
                                 "font", gc_skin_font_board_medium,
 
354
                                 "x", (double) x + 1,
 
355
                                 "y", (double) y + 1,
 
356
                                 "anchor", GTK_ANCHOR_CENTER,
 
357
                                 "fill_color_rgba", gc_skin_color_shadow,
 
358
                                 NULL);
 
359
 
 
360
          gnome_canvas_item_new (boardRootItem,
 
361
                                 gnome_canvas_text_get_type (),
 
362
                                 "text", _("NORTH"),
 
363
                                 "font", gc_skin_font_board_medium,
 
364
                                 "x", (double) x,
 
365
                                 "y", (double) y,
 
366
                                 "anchor", GTK_ANCHOR_CENTER,
 
367
                                 "fill_color_rgba", gc_skin_color_text_button,
 
368
                                 NULL);
 
369
 
 
370
          x = 765;
 
371
          y = 295;
 
372
          gnome_canvas_item_new (boardRootItem,
 
373
                                 gnome_canvas_text_get_type (),
 
374
                                 "text", _("SOUTH"),
 
375
                                 "font", gc_skin_font_board_medium,
 
376
                                 "x", (double) x + 1,
 
377
                                 "y", (double) y + 1,
 
378
                                 "anchor", GTK_ANCHOR_CENTER,
 
379
                                 "fill_color_rgba", gc_skin_color_shadow,
 
380
                                 NULL);
 
381
 
 
382
          gnome_canvas_item_new (boardRootItem,
 
383
                                 gnome_canvas_text_get_type (),
 
384
                                 "text", _("SOUTH"),
 
385
                                 "font", gc_skin_font_board_medium,
 
386
                                 "x", (double) x,
 
387
                                 "y", (double) y,
 
388
                                 "anchor", GTK_ANCHOR_CENTER,
 
389
                                 "fill_color_rgba", gc_skin_color_text_button,
 
390
                                 NULL);
 
391
 
 
392
        }
 
393
 
 
394
        staticAwale = (AWALE *) g_malloc (sizeof (AWALE));
 
395
 
 
396
        if (!staticAwale)
 
397
                exit (1);
 
398
 
 
399
        for (i = 0; i < NBHOLE; i++)
 
400
        {
 
401
                staticAwale->board[i] = NBBEANSPERHOLE;
 
402
        }
 
403
 
 
404
        /* ->player is last player */
 
405
        /* next is human */
 
406
        staticAwale->player = COMPUTER;
 
407
 
 
408
        for (i = 0; i < NBPLAYER; i++)
 
409
        {
 
410
                staticAwale->CapturedBeans[i] = 0;
 
411
        }
 
412
 
 
413
        graphsElt = (GRAPHICS_ELT *) g_malloc (sizeof (GRAPHICS_ELT));
 
414
 
 
415
        /*
 
416
         * Boucle pour creer et positionner les boutons qui serviront
 
417
         * a selectionner la case a jouer
 
418
         */
 
419
        for (i = 0; i < NBHOLE / 2; i++)
 
420
        {
 
421
                sprintf (buffer, "%d", i + 1);
 
422
                xpmFile[12] = buffer[0];
 
423
                graphsElt->pixbufButton[i] = gc_pixmap_load (xpmFile);
 
424
                xpmFileNotify[12] = buffer[0];
 
425
                graphsElt->pixbufButtonNotify[i] =
 
426
                        gc_pixmap_load (xpmFileNotify);
 
427
                xpmFileClic[12] = buffer[0];
 
428
                graphsElt->pixbufButtonClicked[i] =
 
429
                        gc_pixmap_load (xpmFileClic);
 
430
 
 
431
                /*
 
432
                 * Ajustement de l'ordonnee x, pour positionner le bouton sur la barre de boutons.
 
433
                 */
 
434
                switch (i)
 
435
                {
 
436
                case 0:
 
437
                        x1 = 120;
 
438
                        break;
 
439
                case 1:
 
440
                        x1 = 220;
 
441
                        break;
 
442
                case 2:
 
443
                        x1 = 325;
 
444
                        break;
 
445
                case 3:
 
446
                        x1 = 432;
 
447
                        break;
 
448
                case 4:
 
449
                        x1 = 539;
 
450
                        break;
 
451
                case 5:
 
452
                        x1 = 643;
 
453
                        break;
 
454
                }
 
455
 
 
456
                /*
 
457
                 * Ajout des boutons comme items sur le rootGroup du canevas.
 
458
                 * et sauvegarde dans tableau button de type Gnome Canvas Item
 
459
                 * pour attacher les pointeurs de la fonction de rappel buttonClick
 
460
                 * qui servira a detecter quel est l'evenement souris, et en fonction
 
461
                 * declencher la procedure associee. Passage en argument a cette fonction
 
462
                 * du numero de case selectionne par tableau chaine
 
463
                 */
 
464
                graphsElt->button[i] = gnome_canvas_item_new (boardRootItem,
 
465
                                                              gnome_canvas_pixbuf_get_type
 
466
                                                              (), "x",
 
467
                                                              (double) x1,
 
468
                                                              "y",
 
469
                                                              (double)
 
470
                                                              Y_BOUTONS,
 
471
                                                              "pixbuf",
 
472
                                                              graphsElt->
 
473
                                                              pixbufButton[i],
 
474
                                                              NULL);
 
475
 
 
476
                gtk_signal_connect (GTK_OBJECT (graphsElt->button[i]),
 
477
                                    "event", GTK_SIGNAL_FUNC (buttonClick),
 
478
                                    GINT_TO_POINTER(i));
 
479
 
 
480
 
 
481
        }
 
482
 
 
483
        /**
 
484
        *       Affichage initial du nombre de graine courant dans le trou i.
 
485
        *       Sauvegarde des items d'affichage dans nbBeansHole, pour mise a jour
 
486
        *       pdt la partie.
 
487
        */
 
488
        for (i = 11; i >= 0; i--)
 
489
        {
 
490
 
 
491
                sprintf (buffer, "%d", staticAwale->board[i]);
 
492
 
 
493
                graphsElt->nbBeansHole[i] =
 
494
                        gnome_canvas_item_new (boardRootItem,
 
495
                                               gnome_canvas_text_get_type (),
 
496
                                               "text", buffer,
 
497
                                               "font", "sans 12",
 
498
                                               "size", 14000,
 
499
                                               "x", (double) (caseCoord[i] + 45),
 
500
                                               "y", (double) ((i < 6) ? 378 : 94),
 
501
                                               "fill_color", "black", NULL);
 
502
        }
 
503
 
 
504
        /**
 
505
        *       Affichage initial du nombre de graine capturees par chaque joueur.
 
506
        *       Sauvegarde des items d'affichage dans Captures[i], pour mise a jour
 
507
        *       pdt la partie.
 
508
        */
 
509
        for (i = 0; i < 2; i++)
 
510
        {
 
511
 
 
512
                x1 = (i == 1) ? 32 : 762;
 
513
 
 
514
                sprintf (buffer, "%d", staticAwale->CapturedBeans[i]);
 
515
 
 
516
                graphsElt->Captures[i] = gnome_canvas_item_new (boardRootItem,
 
517
                                                                gnome_canvas_text_get_type
 
518
                                                                (), "text",
 
519
                                                                buffer,
 
520
                                                                "font",
 
521
                                                                "sans 12",
 
522
                                                                "size", 20000,
 
523
                                                                "x",
 
524
                                                                (double) x1,
 
525
                                                                "y",
 
526
                                                                (double) 246,
 
527
                                                                "fill_color",
 
528
                                                                "black",
 
529
                                                                NULL);
 
530
        }
 
531
 
 
532
        /**
 
533
        *       Initialisation du buffer xpmFile avec le chemin relatif des fichiers graine
 
534
        *       Creation des pixbuf et sauvegarde dans variable pixbufBeans
 
535
        */
 
536
        strcpy (xpmFile, BEAN);
 
537
        for (i = 0; i < 4; i++)
 
538
        {
 
539
                sprintf (buffer, "%d", i + 1);
 
540
                xpmFile[12] = buffer[0];
 
541
                graphsElt->pixbufBeans[i] = gc_pixmap_load (xpmFile);
 
542
        }
 
543
 
 
544
        /**
 
545
        *       Reservation d'un espace memoire egal a NBTOTALBEAN x taille struct BEANHOLE_LINK
 
546
        *       pour y stocker chaque item de graine ainsi que la case dans laquelle se trouve la graine.
 
547
        *       Puis creation de toutes les graines et affichage sur le plateau.
 
548
        */
 
549
        initBoardGraphics (graphsElt);
 
550
 
 
551
        graphsElt->msg = gnome_canvas_item_new (boardRootItem,
 
552
                                                gnome_canvas_text_get_type (),
 
553
                                                "text", _("Choose a house"),
 
554
                                                "font", "sans 12",
 
555
                                                "size", 20000,
 
556
                                                "x", (double) 400,
 
557
                                                "y", (double) 500,
 
558
                                                "fill_color", "red",
 
559
                                                "anchor", GTK_ANCHOR_CENTER,
 
560
                                                NULL);
 
561
 
 
562
        return NULL;
 
563
}
 
564
 
 
565
 
 
566
/*
 
567
 * ====================================
 
568
 */
 
569
static void
 
570
game_won ()
 
571
{
 
572
  if (sublevel_finished){
 
573
    gcomprisBoard->sublevel++;
 
574
 
 
575
    if (gcomprisBoard->sublevel > gcomprisBoard->number_of_sublevel)
 
576
      {
 
577
        /*
 
578
         * Try the next level
 
579
         */
 
580
        gcomprisBoard->sublevel = 1;
 
581
        gcomprisBoard->level++;
 
582
        if (gcomprisBoard->level > gcomprisBoard->maxlevel)
 
583
          {             // the
 
584
                        // current
 
585
                        // board
 
586
                        // is
 
587
                        // finished
 
588
                        // : bail
 
589
                        // out
 
590
            gc_bonus_end_display (BOARD_FINISHED_RANDOM);
 
591
            return;
 
592
          }
 
593
 
 
594
      }
 
595
  }
 
596
  sublevel_finished = FALSE;
 
597
  awele_next_level ();
 
598
}
 
599
 
 
600
/**
 
601
*  Fonction effectuant l'initialisation des graines sur le plateau
 
602
*  Cette fonction est appelee a chaque debut de partie
 
603
*  @param data un pointeur de type void, pour passer en argument a la fonction\n
 
604
*  les elements graphiques a modifier.
 
605
*  @return void
 
606
*/
 
607
static void
 
608
initBoardGraphics (GRAPHICS_ELT * graphsElt)
 
609
{
 
610
 
 
611
        int i, j, k, idxTabBeans = 0;
 
612
 
 
613
 
 
614
        //if (graphsElt->ptBeansHoleLink != NULL)
 
615
        //      free(graphsElt->ptBeansHoleLink);
 
616
 
 
617
        graphsElt->ptBeansHoleLink =
 
618
                (BEANHOLE_LINK *) malloc (NBTOTALBEAN *
 
619
                                          sizeof (BEANHOLE_LINK));
 
620
 
 
621
        for (i = NBHOLE - 1; i >= 0; i--)
 
622
        {
 
623
                for (j = 0;
 
624
                     j < staticAwale->board[i] && idxTabBeans < NBTOTALBEAN;
 
625
                     j++, idxTabBeans++)
 
626
                {
 
627
                        k = 0 + rand () % 4;
 
628
                        graphsElt->ptBeansHoleLink[idxTabBeans].beanPixbuf =
 
629
                                gnome_canvas_item_new (boardRootItem,
 
630
                                                       gnome_canvas_pixbuf_get_type
 
631
                                                       (), "x",
 
632
                                                       (double) caseCoord[i] +
 
633
                                                       rand () % 50, "y",
 
634
                                                       (double) (((i <
 
635
                                                                   6) ? 260 :
 
636
                                                                  130) +
 
637
                                                                 rand () %
 
638
                                                                 60),
 
639
                                                       "pixbuf",
 
640
                                                       graphsElt->
 
641
                                                       pixbufBeans[k], NULL);
 
642
 
 
643
                        graphsElt->ptBeansHoleLink[idxTabBeans].hole = i;
 
644
                }
 
645
        }
 
646
}
 
647
 
 
648
 
 
649
static gboolean  to_computer(gpointer data)
 
650
{
 
651
  short int coup;
 
652
 
 
653
  if (!computer_turn){
 
654
    g_warning ("to_computer called but not compter_turn");
 
655
    return FALSE;
 
656
  }
 
657
 
 
658
  if (board_paused){
 
659
    g_warning ("to_computer called but board paused");
 
660
    timeout = 0;
 
661
    return TRUE;
 
662
  }
 
663
 
 
664
  coup = think (staticAwale, gcomprisBoard->level);
 
665
 
 
666
  gc_anim_deactivate(anim_item);
 
667
  computer_turn = FALSE;
 
668
 
 
669
  if (coup >= 0){
 
670
    AWALE *tmpAw = staticAwale;
 
671
    staticAwale = moveAwale (coup, tmpAw);
 
672
    if (!staticAwale){
 
673
      g_error("le coup devrait être bon !");
 
674
    }
 
675
    gboolean IAmHungry = diedOfHunger(staticAwale);
 
676
    if (!IAmHungry){
 
677
      g_free(tmpAw);
 
678
      updateNbBeans (0);
 
679
      updateCapturedBeans ();
 
680
      g_object_set (graphsElt->msg, "text",
 
681
                    _("Your turn to play ..."), NULL);
 
682
    } else {
 
683
      /* computer hungry but human can't give it beans */
 
684
      /* Human player win by catching all the beans left. */
 
685
      gamewon = TRUE;
 
686
      sublevel_finished = TRUE;
 
687
      gc_bonus_display(TRUE, BONUS_FLOWER);
 
688
    }
 
689
  } else {
 
690
    /* computer can't play. Why? human is hungry and i cannot give it
 
691
       to eat */
 
692
    /* if human has 24 beans, it's draw (human win in gcompris) */
 
693
    /* if not, all staying are captured by computer and computer win */
 
694
    gamewon = TRUE;
 
695
    sublevel_finished = (staticAwale->CapturedBeans[HUMAN] ==  24);
 
696
    gc_bonus_display(sublevel_finished, BONUS_FLOWER);
 
697
  }
 
698
 
 
699
  timeout = 0;
 
700
  return FALSE;
 
701
}
 
702
 
 
703
/**
 
704
*  Fonction effectuant la procedure associe a un clic sur pixmap
 
705
*  Cette fonction est appelee quand un clic sur un bouton est effectue.\n
 
706
*  Selon l'event->Type declenchement de procedure differentes, modification de l'aspect des boutons\n
 
707
*  et declenchement d'un mouvement choisi par le joueur, puis lancement du coup de la machine.
 
708
*  @param widget pointeur sur le widget ayant declenche l'evenement eventDelete
 
709
*  @param event pointeur sur le type d'evenement
 
710
*  @param data un pointeur de type void, pour passer en argument a la fonction\n
 
711
*  les elements graphiques a modifier.
 
712
*  @return un entier
 
713
*/
 
714
static gint
 
715
buttonClick (GtkWidget * item, GdkEvent * event, gpointer data)
 
716
{
 
717
        gint numeroCase = GPOINTER_TO_INT(data);
 
718
 
 
719
        switch (event->type)
 
720
        {
 
721
        case GDK_ENTER_NOTIFY:
 
722
                g_object_set (GTK_OBJECT
 
723
                              (graphsElt->button[numeroCase]),
 
724
                              "pixbuf",
 
725
                              graphsElt->pixbufButtonNotify[numeroCase],
 
726
                              "y", (double) Y_BOUTONS, NULL);
 
727
                break;
 
728
        case GDK_LEAVE_NOTIFY:
 
729
                g_object_set (GTK_OBJECT
 
730
                              (graphsElt->button[numeroCase]),
 
731
                              "pixbuf",
 
732
                              graphsElt->pixbufButton[numeroCase],
 
733
                              "y", (double) Y_BOUTONS, NULL);
 
734
                break;
 
735
        case GDK_BUTTON_PRESS:
 
736
          if (computer_turn)
 
737
            return TRUE;
 
738
 
 
739
          g_object_set (GTK_OBJECT
 
740
                        (graphsElt->button[numeroCase]),
 
741
                        "pixbuf",
 
742
                        graphsElt->pixbufButtonClicked[numeroCase],
 
743
                        "y", (double) Y_BOUTONS + 3, NULL);
 
744
 
 
745
          g_object_set (graphsElt->msg, "text", "", NULL);
 
746
 
 
747
          AWALE *tmpaw = moveAwale (numeroCase, staticAwale);
 
748
          if (!tmpaw)
 
749
            {
 
750
              g_object_set (graphsElt->msg, "text", _("Not allowed! Try again !"),
 
751
                            NULL);
 
752
            }
 
753
          else
 
754
            {
 
755
              g_free(staticAwale);
 
756
              staticAwale = tmpaw;
 
757
              updateNbBeans (0);
 
758
              updateCapturedBeans ();
 
759
              if (!gamewon){
 
760
                computer_turn = TRUE;
 
761
                timeout = g_timeout_add (2000,
 
762
                                         (GSourceFunc) to_computer,
 
763
                                         NULL);
 
764
                anim_item = gc_anim_activate( boardRootItem,
 
765
                                                         animation );
 
766
              }
 
767
            }
 
768
 
 
769
          break;
 
770
        case GDK_BUTTON_RELEASE:
 
771
          g_object_set (GTK_OBJECT
 
772
                        (graphsElt->button[numeroCase]),
 
773
                        "pixbuf",
 
774
                        graphsElt->pixbufButtonNotify[numeroCase],
 
775
                        "y", (double) Y_BOUTONS, NULL);
 
776
          break;
 
777
        default:
 
778
                break;
 
779
        }
 
780
 
 
781
        return FALSE;
 
782
}
 
783
 
 
784
/**
 
785
*  Fonction de gestion des graines dessinees sur le plateau
 
786
*  Cette fonction est appelee apres chaque mouvement, \n
 
787
*  pour remettre a jour le nombre de graines dessinees sur le plateau, \n
 
788
*  et diminuer la zone d'allocation ou les pixmap des graines sont stockees.
 
789
*  @param nbBeansHole[NBHOLE] Tableau de pointeur sur les gnomeCanvasItem\n
 
790
*  affichant le nombre de graine par case
 
791
*  @param rootGroup Pointeur sur le groupe contenant tous les items inseres dans le canevas
 
792
*  @param ptLink pointeur sur zone memoire ou sont stockees toutes les images des graines du plateau.
 
793
*  @param alpha entier pour differencier une mise a jour du plateau ou le lancement d'une nouvelle partie.
 
794
*  @return Renvoi du pointeur sur la zone memoire apres redimension (n'a probablement pas changé d'adresse).
 
795
*/
 
796
static BEANHOLE_LINK *
 
797
updateNbBeans (int alpha)
 
798
{
 
799
 
 
800
  char buffer[3];               //Manipulation chaines de caracteres
 
801
  int i, j, k, idxTabBeans = 0; //Compteur Boucle Manipulation Elements graphiques
 
802
  static short int nbActiveBean = NBTOTALBEAN;  //nbre graine restant sur plateau
 
803
  static short int nbOldActiveBean;     //nbre graine restant sur plateau au tour precedent
 
804
  BEANHOLE_LINK *ptBeansHoleLink = NULL;        //pointeur sur structures stockant les item graines et la case dans laquelle elles se trouvent.
 
805
 
 
806
  /**
 
807
   *    Sauvegarde du nombre de graines restantes sur le plateau de jeu
 
808
   *    pour le prochain appel a la fonction.
 
809
   *    Mise a jour de nbActiveBean avec nouvelle configuration du plateau de jeu.
 
810
   */
 
811
  if (alpha)
 
812
    {
 
813
      nbOldActiveBean = 48;
 
814
    }
 
815
  else
 
816
    {
 
817
      nbOldActiveBean = nbActiveBean;
 
818
    }
 
819
 
 
820
  nbActiveBean =
 
821
    NBTOTALBEAN - (staticAwale->CapturedBeans[HUMAN] +
 
822
                   staticAwale->CapturedBeans[COMPUTER]);
 
823
 
 
824
  /**
 
825
   *    Destruction d'autant d'elements graphiques graines
 
826
   *    qu'il y a eu de captures pdt ce tour de jeu
 
827
   */
 
828
  for (ptBeansHoleLink = &(graphsElt->ptBeansHoleLink)[nbActiveBean], i = 0;
 
829
       i < nbOldActiveBean - nbActiveBean; i++, ptBeansHoleLink++)
 
830
    {
 
831
      gtk_object_destroy (GTK_OBJECT (ptBeansHoleLink->beanPixbuf));
 
832
    }
 
833
 
 
834
 
 
835
  /**
 
836
   *    Allocation d'un nouvel espace memoire stockant les item graines
 
837
   *    et la case dans laquelle elles se trouvent. Puis liberation de la fin de
 
838
   *    l'ancien espace memoire.
 
839
   */
 
840
 
 
841
  ptBeansHoleLink =
 
842
    (BEANHOLE_LINK *) realloc (graphsElt->ptBeansHoleLink,
 
843
                               nbActiveBean *
 
844
                               sizeof (BEANHOLE_LINK));
 
845
 
 
846
  /**
 
847
   *    Pour chaque case du plateau, mise a jour du nbre de graines qu'elle contient.
 
848
   *    Et pour chaque graine de cette case, deplacement d'un element graphique type graine
 
849
   *    dans cette case. Et mise a jour de l'information hole dans la structure BEANHOLE_LINK.
 
850
   */
 
851
  for (i = NBHOLE - 1; i >= 0; i--)
 
852
    {
 
853
      sprintf (buffer, "%d", staticAwale->board[i]);
 
854
      gnome_canvas_item_set (graphsElt->nbBeansHole[i], "text", buffer, NULL);
 
855
 
 
856
      for (j = 0;
 
857
           j < staticAwale->board[i] && idxTabBeans < nbActiveBean;
 
858
           j++, idxTabBeans++)
 
859
        {
 
860
 
 
861
          k = 0 + rand () % 4;
 
862
 
 
863
          gnome_canvas_item_set (ptBeansHoleLink[idxTabBeans].
 
864
                                 beanPixbuf, "x",
 
865
                                 (double) caseCoord[i] +
 
866
                                 rand () % 50, "y",
 
867
                                 (double) (((i <
 
868
                                             6) ? 260 : 130) +
 
869
                                           rand () % 60), NULL);
 
870
 
 
871
          ptBeansHoleLink[idxTabBeans].hole = i;
 
872
        }
 
873
 
 
874
    }
 
875
 
 
876
  /**
 
877
   *    Renvoi du pointeur sur la zone memoire retaillee (n'a probablement pas change d'adresse).
 
878
   */
 
879
 
 
880
  graphsElt->ptBeansHoleLink = ptBeansHoleLink;
 
881
  return ptBeansHoleLink;
 
882
}
 
883
 
 
884
 
 
885
/**
 
886
*  Fonction de gestion de l'affichage des scores
 
887
*  Cette fonction est appelee apres chaque mouvement, \n
 
888
*  pour remettre a jour le score des joueurs
 
889
*  @param Captures[2] pointeur sur les gnomeCanvasItem d'affichage des scores
 
890
*/
 
891
static void
 
892
updateCapturedBeans ()
 
893
{
 
894
 
 
895
        short int i;
 
896
        char buffer[3];
 
897
 
 
898
        for (i = 0; i < 2; i++)
 
899
        {
 
900
                sprintf (buffer, "%d", staticAwale->CapturedBeans[i]);
 
901
                g_object_set (graphsElt->Captures[i], "text", buffer, NULL);
 
902
                if (staticAwale->CapturedBeans[i] > 24)
 
903
                  {
 
904
                    gamewon = TRUE;
 
905
                    sublevel_finished = (i==0);
 
906
                    gc_bonus_display(sublevel_finished, BONUS_FLOWER);
 
907
                  }
 
908
        }
 
909
}
 
910