memory.c 46.8 KB
Newer Older
Bruno Coudoin's avatar
Bruno Coudoin committed
1 2
/* gcompris - memory.c
 *
3
 * Time-stamp: <2006/08/21 23:33:28 bruno>
Bruno Coudoin's avatar
Bruno Coudoin committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Copyright (C) 2000 Bruno Coudoin
 * 
 *   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 2 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, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// FIXME: Cleanup of MemoryItem created struct is not done 

#include <errno.h>
25
#include <string.h>
Bruno Coudoin's avatar
Bruno Coudoin committed
26 27 28 29 30 31 32 33

#include "gcompris/gcompris.h"


#define SOUNDLISTFILE PACKAGE
#define MAX_MEMORY_WIDTH  7
#define MAX_MEMORY_HEIGHT 6

34
//#define TEXT_FONT gc_skin_font_board_huge_bold
35 36
#define TEXT_FONT "Serif bold 28"

Bruno Coudoin's avatar
Bruno Coudoin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
static gchar *op_fonts[10] =
  {
    "",
    "Serif bold 28",
    "Serif bold 24",
    "Serif bold 20",
    "Serif bold 20",
    "Serif bold 17",
    "Serif bold 13",
    "Serif bold 13",
    "Serif bold 13",
    "Serif bold 11",
  };

Bruno Coudoin's avatar
Bruno Coudoin committed
51 52 53 54 55 56 57 58
static GcomprisBoard *gcomprisBoard = NULL;

static GnomeCanvasGroup *boardRootItem = NULL;

static gint win_id = 0;

typedef enum
{
59
  MODE_NORMAL		= 0,
60
  MODE_TUX              = 1,
Bruno Coudoin's avatar
Bruno Coudoin committed
61
} Mode;
62
static Mode currentMode = MODE_NORMAL;
Bruno Coudoin's avatar
Bruno Coudoin committed
63

64 65 66
typedef enum
{
  UIMODE_NORMAL		= 0,
Bruno Coudoin's avatar
Bruno Coudoin committed
67
  UIMODE_SOUND          = 1,
68
} UiMode;
Bruno Coudoin's avatar
Bruno Coudoin committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static UiMode currentUiMode = UIMODE_NORMAL;

typedef enum
{
  BOARDMODE_NORMAL	         = 0,
  BOARDMODE_SOUND                = 1,
  BOARDMODE_ADD                  = 2,
  BOARDMODE_MINUS                = 3,
  BOARDMODE_MULT                 = 4,
  BOARDMODE_DIV                  = 5,
  BOARDMODE_ADD_MINUS            = 6,
  BOARDMODE_MULT_DIV             = 7,
  BOARDMODE_ADD_MINUS_MULT_DIV   = 8,
} BoardMode;
static BoardMode currentBoardMode = BOARDMODE_NORMAL;
84

Bruno Coudoin's avatar
Bruno Coudoin committed
85 86 87 88 89 90 91
typedef enum
{
  ON_FRONT		= 0,
  ON_BACK		= 1,
  HIDDEN		= 2
} CardStatus;

92 93 94
static gchar *numbers;
static gchar *alphabet_lowercase;
static gchar *alphabet_uppercase;
95 96 97 98 99
static gchar *operators;
static gchar *op_add;
static gchar *op_minus;
static gchar *op_mult;
static gchar *op_div;
100

Bruno Coudoin's avatar
Bruno Coudoin committed
101
typedef struct {
102 103
  gchar *data;
  gint type;
Bruno Coudoin's avatar
Bruno Coudoin committed
104 105 106 107 108
  guint status;
  GnomeCanvasItem *rootItem;
  GnomeCanvasItem *backcardItem;
  GnomeCanvasItem *framecardItem;
  GnomeCanvasItem *frontcardItem;
109
  gboolean hidden;
Bruno Coudoin's avatar
Bruno Coudoin committed
110
  gchar *second_value;
Bruno Coudoin's avatar
Bruno Coudoin committed
111 112 113 114 115
} MemoryItem;

static MemoryItem *firstCard = NULL;
static MemoryItem *secondCard = NULL;

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* Define the page area where memory cards can be displayed for CARD MODE */
#define BASE_CARD_X1 50
#define BASE_CARD_Y1 50
#define BASE_CARD_X2 790
#define BASE_CARD_Y2 500
#define BASE_CARD_X1_TUX 200

/* Define the page area where memory cards can be displayed for SOUND MODE */
#define BASE_SOUND_X1 250
#define BASE_SOUND_Y1 30
#define BASE_SOUND_X2 600
#define BASE_SOUND_Y2 200
#define BASE_SOUND_X1_TUX BASE_SOUND_X1

/* The current page area where memory cards can be displayed */
gint base_x1;
gint base_y1;
gint base_x2;
gint base_y2;
gint base_x1_tux;
136

Bruno Coudoin's avatar
Bruno Coudoin committed
137 138 139 140 141 142 143 144 145 146 147 148
gint current_x;
gint current_y;
gint numberOfLine;
gint numberOfColumn;
gint remainingCards;

static void start_board (GcomprisBoard *agcomprisBoard);
static void pause_board (gboolean pause);
static void end_board (void);
static gboolean is_our_board (GcomprisBoard *gcomprisBoard);
static void set_level (guint level);

149
static void create_item(GnomeCanvasGroup *parent);
Bruno Coudoin's avatar
Bruno Coudoin committed
150 151 152
static void memory_destroy_all_items(void);
static void memory_next_level(void);
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, MemoryItem *memoryItem);
153
static gint compare_card (gconstpointer a, gconstpointer b);
Bruno Coudoin's avatar
Bruno Coudoin committed
154 155 156 157 158

static void player_win();

static void display_card(MemoryItem *memoryItem, CardStatus cardStatus);

159
static void sound_callback(gchar *file);
160 161
static void start_callback(gchar *file);
static gboolean playing_sound = FALSE;
162

Bruno Coudoin's avatar
Bruno Coudoin committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
// Number of images for x and y by level
static guint levelDescription[] =
{
  0,0,
  3,2,
  4,2,
  4,3,
  4,4,
  4,4,
  5,4,
  6,4,
  6,5,
  MAX_MEMORY_WIDTH,MAX_MEMORY_HEIGHT
};

static MemoryItem *memoryArray[MAX_MEMORY_WIDTH][MAX_MEMORY_HEIGHT];

180
/* List of images to use in the memory */
Bruno Coudoin's avatar
Bruno Coudoin committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static gchar *imageList[] =
{
  "gcompris/misc/apple.png",
  "gcompris/misc/bicycle.png",
  "gcompris/misc/bottle.png",
  "gcompris/misc/carot.png",
  "gcompris/misc/car.png",
  "gcompris/misc/castle.png",
  "gcompris/misc/cerise.png",
  "gcompris/misc/cocotier.png",
  "gcompris/misc/crown.png",
  "gcompris/misc/egg.png",
  "gcompris/misc/eggpot.png",
  "gcompris/misc/fishingboat.png",
  "gcompris/misc/flower.png",
  "gcompris/misc/flowerpot.png",
  "gcompris/misc/football.png",
  "gcompris/misc/fusee.png",
  "gcompris/misc/glass.png",
  "gcompris/misc/house.png",
  "gcompris/misc/lamp.png",
  "gcompris/misc/lighthouse.png",
  "gcompris/misc/light.png",
  "gcompris/misc/minivan.png",
  "gcompris/misc/peer.png",
  "gcompris/misc/pencil.png",
  "gcompris/misc/plane.png",
  "gcompris/misc/postcard.png",
  "gcompris/misc/postpoint.png",
  "gcompris/misc/rape.png",
  "gcompris/misc/raquette.png",
  "gcompris/misc/sailingboat.png",
  "gcompris/misc/sapin.png",
  "gcompris/misc/sofa.png",
  "gcompris/misc/star.png",
  "gcompris/misc/strawberry.png",
  "gcompris/misc/tree.png",
  "gcompris/misc/truck.png",
  "gcompris/misc/tuxplane.png",
  "gcompris/misc/tux.png",
  "gcompris/misc/windflag0.png",
  "gcompris/misc/windflag4.png",
  "gcompris/misc/windflag5.png",
};
Bruno Coudoin's avatar
Bruno Coudoin committed
225
#define NUMBER_OF_IMAGES G_N_ELEMENTS(imageList)
Bruno Coudoin's avatar
Bruno Coudoin committed
226

227
/* List of images to use in the memory */
228 229
static gchar *soundList[] =
{
230 231 232
   "sounds/LuneRouge/animaux/LRRain_in_garden_01_by_Lionel_Allorge_cut.ogg",
   "sounds/LuneRouge/animaux/LRBark_1_by_Lionel_Allorge_cut.ogg",
   "sounds/LuneRouge/animaux/LRBark_3_by_Lionel_Allorge_cut.ogg",
233
   "sounds/LuneRouge/animaux/LRFrogsInPondDuringStormByLionelAllorgeCut.ogg",
234 235
   "sounds/LuneRouge/engins/LRObject_falling_02_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/engins/LRTrain_slowing_down_01_by_Lionel_Allorge_cut.ogg",
236
   "sounds/LuneRouge/engins/LRStartAndStopCarEngine1ByLionelAllorgeCut.ogg",
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
   "sounds/LuneRouge/engins/LRObject_falling_01_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/humain/LRApplauses_1_by_Lionel_Allorge_cut.ogg",
   "sounds/LuneRouge/humain/LRHeart_beat_01_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/maison/LRDoor_Open_2_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/maison/LRRing_01_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/musique/LRBuddhist_gong_05_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_4_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_1_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_2_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_3_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_5_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRWeird_6_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/sf/LRET_phone_home_01_by_Lionel_Allorge_cut.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_02_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_03_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_04_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_05_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_06_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRHits_01_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFireballs_01_by_Lionel_Allorge.ogg",
   "sounds/LuneRouge/usine/LRFactory_noise_01_by_Lionel_Allorge.ogg",
258
   "sounds/LuneRouge/LRLaPause_short.ogg",
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
   "sounds/memory/plick.ogg",
   "sounds/memory/tick.ogg",
   "sounds/memory/tri.ogg",
   "sounds/chronos/space/1.ogg",
   "sounds/chronos/space/2.ogg",
   "sounds/chronos/space/2.ogg",
   "sounds/melody/guitar/melody.ogg",
   "sounds/melody/guitar/son1.ogg",
   "sounds/melody/guitar/son2.ogg",
   "sounds/melody/guitar/son3.ogg",
   "sounds/melody/guitar/son4.ogg",
   "sounds/melody/tachos/son1.ogg",
   "sounds/melody/tachos/son2.ogg",
   "sounds/melody/tachos/son3.ogg",
   "sounds/melody/tachos/son4.ogg",
   "sounds/melody/tachos/melody.ogg"
275 276
};

Bruno Coudoin's avatar
Bruno Coudoin committed
277
#define NUMBER_OF_SOUNDS G_N_ELEMENTS(soundList)
278

Bruno Coudoin's avatar
Bruno Coudoin committed
279 280 281



Bruno Coudoin's avatar
Bruno Coudoin committed
282
/* Description of this plugin */
283
static BoardPlugin menu_bp =
Bruno Coudoin's avatar
Bruno Coudoin committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
{
   NULL,
   NULL,
   N_("Memory"),
   N_("Find the matching pair"),
   "Bruno Coudoin <bruno.coudoin@free.fr>",
   NULL,
   NULL,
   NULL,
   NULL,
   start_board,
   pause_board,
   end_board,
   is_our_board,
   NULL,
   NULL,
   set_level,
301
   NULL,
302 303
   NULL,
   NULL,
304
   NULL
Bruno Coudoin's avatar
Bruno Coudoin committed
305 306
};

307 308 309 310 311 312 313 314 315 316

/*
 * Against computer version
 * ------------------------
 *
 */

static gboolean to_tux = FALSE;
static GQueue *tux_memory;
static gint tux_memory_size;
317
static gint tux_memory_sizes[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static gint tux_pairs = 0;
static gint player_pairs = 0;

static void add_card_in_tux_memory(MemoryItem *card);
static MemoryItem *find_card_in_tux_memory(MemoryItem *card);
static void remove_card_from_tux_memory(MemoryItem *card);
static gint tux_play();

typedef struct {
  MemoryItem *first;
  MemoryItem *second;
} WINNING;

static GList *winning_pairs = NULL;

static gint tux_id = 0;

Bruno Coudoin's avatar
Bruno Coudoin committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/*
 *
 * Operation versions
 *
 */


/*  max number 1 / max number 2 */
/*  to allow level with calculus like 7*12 */

static guint add_levelDescription[10][2] =
{
  {0,0},
  {5,5},
  {5,7},
  {7,7},
  {7,10},
  {10,10},
  {10,15},
  {15,15},
  {25,25},
  {50,50},
};

static guint minus_levelDescription[10][2] =
{
  {0,0},
  {5,5},
  {5,7},
  {7,7},
  {7,10},
  {10,10},
  {10,15},
  {15,15},
  {25,25},
  {50,50},
};


static guint mult_levelDescription[10][2] =
{
  {0,0},
  {5,5},
  {5,7},
  {7,7},
  {7,10},
  {10,10},
  {12,12},
  {15,15},
  {20,20},
  {25,25},
};

static guint div_levelDescription[10][2] =
{
  {0,0},
  {5,5},
  {5,7},
  {7,7},
  {7,10},
  {10,10},
  {12,12},
  {15,15},
  {20,20},
  {25,25},
};


403 404 405 406 407 408 409 410 411 412 413
/*
 * random without repeted token
 * ------------------------
 *
 */

#define TYPE_IMAGE     1
#define TYPE_NUMBER    2
#define TYPE_UPPERCASE 4
#define TYPE_LOWERCASE 8
#define TYPE_SOUND     16
Bruno Coudoin's avatar
Bruno Coudoin committed
414 415 416 417
#define TYPE_ADD       32
#define TYPE_MINUS     64
#define TYPE_MULT      128
#define TYPE_DIV       256
418 419 420

static GList *passed_token = NULL;

421 422 423
static GnomeCanvasItem *tux;
static GnomeCanvasItem *tux_score;
static GnomeCanvasItem *player_score;
424 425
static GnomeCanvasItem *tux_score_s;
static GnomeCanvasItem *player_score_s;
426

427
/* set the type of the token returned in string in returned_type */
Bruno Coudoin's avatar
Bruno Coudoin committed
428
void get_random_token(int token_type, gint *returned_type, gchar **string, gchar **second_value)
429 430
{
  gchar *result = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
431 432
  gchar *second = NULL;
  gboolean skip;
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

  gint max_token;
  gint j, i, k;
  gint type;

  typedef struct {
    gint bound;
    gint type;
  } DATUM ;

  GList *data= NULL;
  GList *list;

  max_token = 0;

  if (token_type & TYPE_IMAGE){
    max_token += NUMBER_OF_IMAGES;

    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =  TYPE_IMAGE;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_NUMBER) {
    max_token += g_utf8_strlen (numbers, -1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =  TYPE_NUMBER;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_UPPERCASE){
    max_token += g_utf8_strlen (alphabet_uppercase, -1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_UPPERCASE;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_LOWERCASE){
    max_token += g_utf8_strlen (alphabet_lowercase, -1);;
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_LOWERCASE;
    data = g_list_append(data, dat);
  }
480 481 482 483 484 485 486 487 488 489


  if (token_type & TYPE_SOUND){
    max_token += NUMBER_OF_SOUNDS;
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_SOUND;
    data = g_list_append(data, dat);
  }

Bruno Coudoin's avatar
Bruno Coudoin committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  if (token_type & TYPE_ADD){
    max_token += (add_levelDescription[gcomprisBoard->level][0]+1)*(add_levelDescription[gcomprisBoard->level][1]+1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_ADD;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_MINUS){
    max_token += (minus_levelDescription[gcomprisBoard->level][0]+1)*(minus_levelDescription[gcomprisBoard->level][1]+1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_MINUS;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_MULT){
   max_token += (mult_levelDescription[gcomprisBoard->level][0]+1)*(mult_levelDescription[gcomprisBoard->level][1]+1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_MULT;
    data = g_list_append(data, dat);
  }

  if (token_type & TYPE_DIV){
    max_token += (div_levelDescription[gcomprisBoard->level][0]+1)*(div_levelDescription[gcomprisBoard->level][1]+1);
    DATUM *dat = g_malloc0(sizeof(DATUM));
    dat->bound = max_token;
    dat->type =   TYPE_DIV;
    data = g_list_append(data, dat);
  }



524 525 526 527 528 529 530 531 532 533 534
  g_assert(max_token >0);

  i = rand()%max_token;

  for (list = data; list != NULL; list = list->next)
    if ( i < ((DATUM *)list->data)->bound)
      break;

  j=-1;

  do {
Bruno Coudoin's avatar
Bruno Coudoin committed
535
    skip = FALSE;
536
    g_free(result);
Bruno Coudoin's avatar
Bruno Coudoin committed
537 538
    result = NULL;
    g_free(second);
539 540 541 542 543 544 545 546 547 548 549 550 551 552
    j++;

    if ((i+j) == max_token) {
      list = data;
    }

    if ((i+j)%max_token == ((DATUM *)list->data)->bound)
      list = list->next;

    /* calculate index in right table */
    k = (i+j)%max_token - (list->prev ? ((DATUM *)list->prev->data)->bound : 0);


    type = ((DATUM *)list->data)->type;
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    switch (type) {
    case TYPE_IMAGE:
      result= g_strdup(imageList[k]);
      break;
    case TYPE_NUMBER:
      result = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(result, g_utf8_offset_to_pointer (numbers,k),1);
      break;
    case TYPE_UPPERCASE:
      result = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(result, g_utf8_offset_to_pointer (alphabet_uppercase,k),1);
      break;
    case TYPE_LOWERCASE:
      result = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(result, g_utf8_offset_to_pointer (alphabet_lowercase,k),1);
      break;
570
    case TYPE_SOUND:
Bruno Coudoin's avatar
Bruno Coudoin committed
571
      result = g_strdup(soundList[k]);
572
      break;
Bruno Coudoin's avatar
Bruno Coudoin committed
573 574 575 576 577
    case TYPE_ADD:
      {
	int i, j;
	i = k %  add_levelDescription[gcomprisBoard->level][0];
	j = k /  add_levelDescription[gcomprisBoard->level][0];
578
	result = g_strdup_printf("%d%s%d",i,op_add,j);
Bruno Coudoin's avatar
Bruno Coudoin committed
579 580 581 582 583 584 585 586
	second = g_strdup_printf("%d",i+j);;
	break;
      }
    case TYPE_MINUS:
      {
	int i, j;
	i = k %  minus_levelDescription[gcomprisBoard->level][0];
	j = k /  minus_levelDescription[gcomprisBoard->level][0];
587
	result = g_strdup_printf("%d%s%d",i+j,op_minus,i);
Bruno Coudoin's avatar
Bruno Coudoin committed
588 589 590 591 592 593 594 595
	second = g_strdup_printf("%d",j);;
	break;
      }
    case TYPE_MULT:
      {
	int i, j;
	i = k %  mult_levelDescription[gcomprisBoard->level][0];
	j = k /  mult_levelDescription[gcomprisBoard->level][0];
596
	result = g_strdup_printf("%d%s%d",i,op_mult,j);
Bruno Coudoin's avatar
Bruno Coudoin committed
597 598 599 600 601 602 603 604 605
	second = g_strdup_printf("%d",i*j);;
	break;
      }
    case TYPE_DIV:
      {
	int i1, i2;
	i1 = k %  div_levelDescription[gcomprisBoard->level][0];
	if (i1==0) skip=TRUE;
	i2 = k /  div_levelDescription[gcomprisBoard->level][0];
606
	result = g_strdup_printf("%d%s%d",i1*i2,op_div,i1);
Bruno Coudoin's avatar
Bruno Coudoin committed
607 608 609
	second = g_strdup_printf("%d",i2);
	break;
      }
610 611 612 613 614
    default:
      /* should never append */
      g_error("never !");
      break;
    }
615

Bruno Coudoin's avatar
Bruno Coudoin committed
616 617
  } while (skip || ((j < max_token ) 
	   && (passed_token && result && g_list_find_custom(passed_token, result, (GCompareFunc)strcmp))));
618 619 620 621 622 623 624 625 626

  g_assert (j < max_token);
	 
  passed_token = g_list_append( passed_token, result);
  
  *returned_type = type;

  *string = result;

Bruno Coudoin's avatar
Bruno Coudoin committed
627 628 629
  if (second_value)
    *second_value = second;

630 631 632 633 634 635 636 637 638 639
  for (list = data; list != NULL; list=list->next)
    g_free(list->data);

  g_list_free(data);

}




Bruno Coudoin's avatar
Bruno Coudoin committed
640 641 642 643 644 645
/*
 * Main entry point mandatory for each Gcompris's game
 * ---------------------------------------------------
 *
 */

646
GET_BPLUGIN_INFO(memory)
Bruno Coudoin's avatar
Bruno Coudoin committed
647 648 649 650 651

/*
 * in : boolean TRUE = PAUSE : FALSE = UNPAUSE
 *
 */
652 653 654

static gboolean Paused = FALSE;

Bruno Coudoin's avatar
Bruno Coudoin committed
655 656 657 658 659 660
static void pause_board (gboolean pause)
{

  if(gcomprisBoard==NULL)
    return;

661 662 663 664 665 666 667 668
  Paused = pause;

  if(pause){
    if (currentMode == MODE_TUX){
      if (tux_id){
	g_source_remove(tux_id);
	tux_id = 0;
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
669
    }
670 671 672 673 674 675 676 677 678 679 680
  }
  else {
    if (remainingCards<=0)
      memory_next_level();
    else {
      if (currentMode == MODE_TUX){
	if (to_tux){
	  tux_id = g_timeout_add (2000,
				  (GSourceFunc) tux_play, NULL);
	}
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
681
    }
682 683
    
  }
Bruno Coudoin's avatar
Bruno Coudoin committed
684 685 686 687 688 689 690 691 692 693 694 695
}
/*
 */
static void start_board (GcomprisBoard *agcomprisBoard)
{

  if(agcomprisBoard!=NULL)
    {
      gcomprisBoard=agcomprisBoard;

      gcomprisBoard->level = 1;
      gcomprisBoard->maxlevel = 9;
696
      gc_bar_set(GC_BAR_LEVEL);
Bruno Coudoin's avatar
Bruno Coudoin committed
697 698

      /* Default mode */
699 700
      
      if(!gcomprisBoard->mode){
701
	currentMode=MODE_NORMAL;
702
	currentUiMode=UIMODE_NORMAL;
Bruno Coudoin's avatar
Bruno Coudoin committed
703 704
	currentBoardMode=BOARDMODE_NORMAL;
      } else {
705
	if(g_strcasecmp(gcomprisBoard->mode, "tux")==0){
706
	  currentMode=MODE_TUX;
707
	  currentUiMode=UIMODE_NORMAL;
Bruno Coudoin's avatar
Bruno Coudoin committed
708 709
	  currentBoardMode=BOARDMODE_NORMAL;
	} else {
710 711 712
	  if(g_strcasecmp(gcomprisBoard->mode, "sound")==0){
	    currentMode=MODE_NORMAL;
	    currentUiMode=UIMODE_SOUND;
Bruno Coudoin's avatar
Bruno Coudoin committed
713
	    currentBoardMode=BOARDMODE_SOUND;
714 715 716 717
	  } else {
	    if(g_strcasecmp(gcomprisBoard->mode, "sound_tux")==0){
	      currentMode=MODE_TUX;
	      currentUiMode=UIMODE_SOUND;
Bruno Coudoin's avatar
Bruno Coudoin committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	      currentBoardMode=BOARDMODE_SOUND;
	    } else {
	      if(g_strcasecmp(gcomprisBoard->mode, "add")==0){
		currentMode=MODE_NORMAL;
		currentUiMode=UIMODE_NORMAL;
		currentBoardMode=BOARDMODE_ADD;
	      } else {
		if(g_strcasecmp(gcomprisBoard->mode, "add_tux")==0){
		  currentMode=MODE_TUX;
		  currentUiMode=UIMODE_NORMAL;
		  currentBoardMode=BOARDMODE_ADD;
		} else {
		  if(g_strcasecmp(gcomprisBoard->mode, "minus")==0){
		    currentMode=MODE_NORMAL;
		    currentUiMode=UIMODE_NORMAL;
		    currentBoardMode=BOARDMODE_MINUS;
		  } else {
		    if(g_strcasecmp(gcomprisBoard->mode, "minus_tux")==0){
		      currentMode=MODE_TUX;
		      currentUiMode=UIMODE_NORMAL;
		      currentBoardMode=BOARDMODE_MINUS;
		    } else {
		      if(g_strcasecmp(gcomprisBoard->mode, "mult")==0){
			currentMode=MODE_NORMAL;
			currentUiMode=UIMODE_NORMAL;
			currentBoardMode=BOARDMODE_MULT;
		      } else {
			if(g_strcasecmp(gcomprisBoard->mode, "mult_tux")==0){
			  currentMode=MODE_TUX;
			  currentUiMode=UIMODE_NORMAL;
			  currentBoardMode=BOARDMODE_MULT;
			} else {
			  if(g_strcasecmp(gcomprisBoard->mode, "div")==0){
			    currentMode=MODE_NORMAL;
			    currentUiMode=UIMODE_NORMAL;
			    currentBoardMode=BOARDMODE_DIV;
			  } else {
			    if(g_strcasecmp(gcomprisBoard->mode, "div_tux")==0){
			      currentMode=MODE_TUX;
			      currentUiMode=UIMODE_NORMAL;
			      currentBoardMode=BOARDMODE_DIV;
			    } else {
			      if(g_strcasecmp(gcomprisBoard->mode, "add_minus")==0){
				currentMode=MODE_NORMAL;
				currentUiMode=UIMODE_NORMAL;
				currentBoardMode=BOARDMODE_ADD_MINUS;
			      } else {
				if(g_strcasecmp(gcomprisBoard->mode, "add_minus_tux")==0){
				  currentMode=MODE_TUX;
				  currentUiMode=UIMODE_NORMAL;
				  currentBoardMode=BOARDMODE_ADD_MINUS;
				} else {
				  if(g_strcasecmp(gcomprisBoard->mode, "mult_div")==0){
				    currentMode=MODE_NORMAL;
				    currentUiMode=UIMODE_NORMAL;
				    currentBoardMode=BOARDMODE_MULT_DIV;
				  } else {
				    if(g_strcasecmp(gcomprisBoard->mode, "mult_div_tux")==0){
				      currentMode=MODE_TUX;
				      currentUiMode=UIMODE_NORMAL;
				      currentBoardMode=BOARDMODE_MULT_DIV;
				    } else {
				      if(g_strcasecmp(gcomprisBoard->mode, "add_minus_mult_div")==0){
					currentMode=MODE_NORMAL;
					currentUiMode=UIMODE_NORMAL;
					currentBoardMode=BOARDMODE_ADD_MINUS_MULT_DIV;
				      } else {
					if(g_strcasecmp(gcomprisBoard->mode, "add_minus_mult_div_tux")==0){
					  currentMode=MODE_TUX;
					  currentUiMode=UIMODE_NORMAL;
					  currentBoardMode=BOARDMODE_ADD_MINUS_MULT_DIV;
					} else {
					  currentMode=MODE_NORMAL;
					  currentUiMode=UIMODE_NORMAL;
792
					  currentBoardMode=BOARDMODE_NORMAL;
Bruno Coudoin's avatar
Bruno Coudoin committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
					  g_warning("Fallback mode set to images");
					}
				      }
				    }
				  }
				}
			      }
			    }
			  }
			}
		      }
		    }
		  }
		}
	      }
808 809 810
	    }
	  }
	}
811
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
812
    
813

814
      if (currentUiMode == UIMODE_SOUND)
815
	{
816
	  GcomprisProperties	*properties = gc_prop_get();
Bruno Coudoin's avatar
Bruno Coudoin committed
817

818 819
	  gc_sound_pause();
	  gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "images/gcompris_band.png");
820 821 822 823 824
	  base_x1 = BASE_SOUND_X1;
	  base_y1 = BASE_SOUND_Y1;
	  base_x2 = BASE_SOUND_X2;
	  base_y2 = BASE_SOUND_Y2;
	  base_x1_tux = BASE_SOUND_X1_TUX;
Bruno Coudoin's avatar
Bruno Coudoin committed
825 826

	  if(!properties->fx) {
Bruno Coudoin's avatar
Bruno Coudoin committed
827
	    gc_dialog(_("Error: this activity cannot be played with the\nsound effects disabled.\nGo to the configuration dialog to\nenable the sound"), gc_board_stop);
Bruno Coudoin's avatar
Bruno Coudoin committed
828 829 830
	    return;
	  }

831
	}
832
      else
833
	{
834
	  gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "images/scenery_background.png");
835 836 837 838 839 840
	  base_x1 = BASE_CARD_X1;
	  base_y1 = BASE_CARD_Y1;
	  base_x2 = BASE_CARD_X2;
	  base_y2 = BASE_CARD_Y2;
	  base_x1_tux = BASE_CARD_X1_TUX;
	}
Bruno Coudoin's avatar
Bruno Coudoin committed
841 842
      
      
843 844 845
      /* TRANSLATORS: Put here the numbers in your language */
      numbers=_("0123456789");
      assert(g_utf8_validate(numbers,-1,NULL)); // require by all utf8-functions
Bruno Coudoin's avatar
Bruno Coudoin committed
846
      
847 848 849
      /* TRANSLATORS: Put here the alphabet lowercase in your language */
      alphabet_lowercase=_("abcdefghijklmnopqrstuvwxyz");
      assert(g_utf8_validate(alphabet_lowercase,-1,NULL)); // require by all utf8-functions
Bruno Coudoin's avatar
Bruno Coudoin committed
850
      
851
      g_warning("Using lowercase %s", alphabet_lowercase);
Bruno Coudoin's avatar
Bruno Coudoin committed
852
      
853 854 855
      /* TRANSLATORS: Put here the alphabet uppercase in your language */
      alphabet_uppercase=_("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
      assert(g_utf8_validate(alphabet_uppercase,-1,NULL)); // require by all utf8-functions
856
      g_warning("Using uppercase %s", alphabet_uppercase);
Bruno Coudoin's avatar
Bruno Coudoin committed
857
      
858 859
      /* TRANSLATORS: Put here the mathematical operators "+-" for  your language. */
      operators=_("+-×÷");
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
      g_assert(g_utf8_validate(operators,-1,NULL)); // require by all utf8-functions
      g_warning("Using operators %s", operators);
      
      op_add = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(op_add, g_utf8_offset_to_pointer (operators,0),1);

      op_minus = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(op_minus, g_utf8_offset_to_pointer (operators,1),1);

      op_mult = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(op_mult, g_utf8_offset_to_pointer (operators,2),1);

      op_div = g_malloc0(2*sizeof(gunichar));
      g_utf8_strncpy(op_div, g_utf8_offset_to_pointer (operators,3),1);

875 876 877 878

      g_warning("Using operators %s %s %s %s", op_add, op_minus,
		                               op_mult, op_div);

879
      if (currentMode == MODE_TUX){
880
	tux_memory_size = tux_memory_sizes[gcomprisBoard->level];
881 882
	tux_memory = g_queue_new ();
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
883
      
884
      Paused = FALSE;
Bruno Coudoin's avatar
Bruno Coudoin committed
885
      
886 887 888
      to_tux = FALSE;
      if (currentUiMode == UIMODE_SOUND){
	playing_sound = TRUE;
889
	gc_sound_play_ogg_cb("sounds/LuneRouge/musique/LRBuddhist_gong_05_by_Lionel_Allorge.ogg",start_callback);
890 891
      } else
	playing_sound = FALSE;
Bruno Coudoin's avatar
Bruno Coudoin committed
892
      
Bruno Coudoin's avatar
Bruno Coudoin committed
893
      memory_next_level();
894
    }      
Bruno Coudoin's avatar
Bruno Coudoin committed
895 896 897 898 899
}

static void
end_board ()
{
900
  if (currentUiMode == UIMODE_SOUND)
901
    gc_sound_resume();
902

Bruno Coudoin's avatar
Bruno Coudoin committed
903 904 905
  if(gcomprisBoard!=NULL)
    {
      pause_board(TRUE);
906

Bruno Coudoin's avatar
Bruno Coudoin committed
907
      memory_destroy_all_items();
908 909 910 911
      if (currentMode == MODE_TUX){
	g_queue_free(tux_memory);
	tux_memory = NULL;
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
912
    }
913
  gcomprisBoard = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
914 915 916 917 918 919 920 921 922 923 924 925 926
}

static void
set_level (guint level)
{

  if(gcomprisBoard!=NULL)
    {
      gcomprisBoard->level=level;
      memory_next_level();
    }
}

927
static gboolean
Bruno Coudoin's avatar
Bruno Coudoin committed
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
is_our_board (GcomprisBoard *gcomprisBoard)
{
  if (gcomprisBoard)
    {
      if(g_strcasecmp(gcomprisBoard->type, "memory")==0)
	{
	  /* Set the plugin entry */
	  gcomprisBoard->plugin=&menu_bp;

	  return TRUE;
	}
    }
  return FALSE;
}


/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/

949 950
static void update_scores()
{
951 952 953
  gchar *tux_score_str;
  gchar *player_score_str;

954 955
  tux_score_str = g_strdup_printf("%d", tux_pairs);
  player_score_str = g_strdup_printf("%d", player_pairs);
956 957 958 959

  gnome_canvas_item_set(tux_score,      "text", tux_score_str, NULL);
  gnome_canvas_item_set(player_score,   "text", player_score_str, NULL);
  gnome_canvas_item_set(tux_score_s,    "text", tux_score_str, NULL);
960
  gnome_canvas_item_set(player_score_s, "text", player_score_str, NULL);
961

962 963
  g_free(tux_score_str);
  g_free(player_score_str);
964 965
}

Bruno Coudoin's avatar
Bruno Coudoin committed
966 967 968
/* set initial values for the next level */
static void memory_next_level() 
{
969
  gc_bar_set_level(gcomprisBoard);
970
  
Bruno Coudoin's avatar
Bruno Coudoin committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
  memory_destroy_all_items();

  boardRootItem = GNOME_CANVAS_GROUP(
				     gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
							    gnome_canvas_group_get_type (),
							    "x", (double) 0,
							    "y", (double) 0,
							    NULL));

  numberOfColumn = levelDescription[gcomprisBoard->level*2];
  numberOfLine   = levelDescription[gcomprisBoard->level*2+1];
  remainingCards = numberOfColumn * numberOfLine;

  gcomprisBoard->number_of_sublevel=1;
  gcomprisBoard->sublevel=0;

  create_item(boardRootItem);
988 989

  if (currentMode == MODE_TUX){
990
	tux_memory_size = tux_memory_sizes[gcomprisBoard->level];
991
	g_warning("tux_memory_size %d", tux_memory_size );
992 993 994
	tux_pairs = 0;
	player_pairs = 0;
	update_scores();
995
  }
Bruno Coudoin's avatar
Bruno Coudoin committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
}


/* Destroy all the items */
static void memory_destroy_all_items()
{
  gint x, y;

  firstCard = NULL;
  secondCard = NULL;

1007
  /* Remove timers first */
Bruno Coudoin's avatar
Bruno Coudoin committed
1008
  if (win_id) {
1009
    g_source_remove (win_id);
Bruno Coudoin's avatar
Bruno Coudoin committed
1010 1011 1012
  }
  win_id = 0;

1013 1014 1015 1016 1017 1018 1019 1020
  if (currentMode == MODE_TUX){
    if (tux_id) {
      g_source_remove (tux_id);
    }
    tux_id =0;
    to_tux = FALSE;
  }

1021 1022 1023 1024 1025 1026
  /* Now destroy all items */
  if(boardRootItem!=NULL)
      gtk_object_destroy (GTK_OBJECT(boardRootItem));

  boardRootItem=NULL;

Bruno Coudoin's avatar
Bruno Coudoin committed
1027 1028 1029 1030
  // Clear the memoryArray
  for(x=0; x<MAX_MEMORY_WIDTH; x++)
    for(y=0; y<MAX_MEMORY_HEIGHT; y++)
      {
Bruno Coudoin's avatar
Bruno Coudoin committed
1031 1032
	if (memoryArray[x][y])
	  g_free(memoryArray[x][y]->second_value);
Bruno Coudoin's avatar
Bruno Coudoin committed
1033 1034 1035
	g_free(memoryArray[x][y]);
	memoryArray[x][y] = NULL;	
      }
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

  GList *list;

  for (list = passed_token; list != NULL; list=list->next)
    g_free(list->data);
  
  g_list_free(passed_token);
  
  passed_token = NULL;
    
  if (currentMode == MODE_TUX){
    for (list = winning_pairs; list != NULL; list=list->next)
      g_free(list->data);
    
    g_list_free(winning_pairs);
    
    winning_pairs = NULL;
    while (g_queue_pop_head (tux_memory));
Yves Combe's avatar
Yves Combe committed
1054
    //tux_memory = NULL;
1055 1056
  }
  
Bruno Coudoin's avatar
Bruno Coudoin committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
}

/*
 * Takes care to return a random pair of images (one by one)
 * the image is loaded in memoryItem->image
 *
 */
static void get_image(MemoryItem *memoryItem, guint x, guint y)
{
  guint rx, ry;

1068 1069
  memoryItem->hidden = FALSE;

Bruno Coudoin's avatar
Bruno Coudoin committed
1070 1071 1072
  if(memoryArray[x][y])
    {
      // Get the pair's image
Bruno Coudoin's avatar
Bruno Coudoin committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
      if (memoryArray[x][y]->type & (TYPE_ADD|TYPE_MINUS|TYPE_MULT|TYPE_DIV)){
	memoryItem->data = memoryArray[x][y]->second_value;
	memoryItem->type =  memoryArray[x][y]->type;
	memoryArray[x][y] = memoryItem;
	// if created by g_malloc0, this is not usefull;
	//memoryItem->second_value = NULL;
      }
      else {
	memoryItem->data = memoryArray[x][y]->data;
	memoryItem->type =  memoryArray[x][y]->type;
	memoryArray[x][y] = memoryItem;
      }
Bruno Coudoin's avatar
Bruno Coudoin committed
1085 1086 1087
      return;
    }

1088

Bruno Coudoin's avatar
Bruno Coudoin committed
1089
  memoryArray[x][y] = memoryItem;
1090

Bruno Coudoin's avatar
Bruno Coudoin committed
1091 1092 1093
  switch (currentBoardMode) {
  case BOARDMODE_SOUND:
    get_random_token ( TYPE_SOUND, &memoryItem->type,  &memoryItem->data, NULL);
1094
    g_assert (memoryItem->type ==  TYPE_SOUND);
Bruno Coudoin's avatar
Bruno Coudoin committed
1095 1096
    break;
  case BOARDMODE_NORMAL:
1097 1098 1099 1100 1101 1102 1103 1104
    switch(gcomprisBoard->level) {

    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
      /* Image mode */
Bruno Coudoin's avatar
Bruno Coudoin committed
1105
      get_random_token ( TYPE_IMAGE, &memoryItem->type,  &memoryItem->data, NULL);
1106 1107 1108 1109 1110
      g_assert (memoryItem->type ==  TYPE_IMAGE);
      break;
      
    case 5:
      /* Limited Text mode Numbers only */
Bruno Coudoin's avatar
Bruno Coudoin committed
1111
      get_random_token ( TYPE_NUMBER, &memoryItem->type,  &memoryItem->data, NULL);
1112 1113 1114 1115 1116
      g_assert (memoryItem->type ==  TYPE_NUMBER);
      break;
      
    case 6:
      /* Limited Text mode Numbers + Capitals */
Bruno Coudoin's avatar
Bruno Coudoin committed
1117
      get_random_token ( TYPE_NUMBER | TYPE_UPPERCASE, &memoryItem->type,  &memoryItem->data, NULL);
1118 1119 1120 1121 1122
      g_assert((memoryItem->type == TYPE_NUMBER)||(memoryItem->type==TYPE_UPPERCASE));
      break;
      
    default:
      /* Text mode ALL */
Bruno Coudoin's avatar
Bruno Coudoin committed
1123
      get_random_token ( TYPE_NUMBER | TYPE_UPPERCASE | TYPE_LOWERCASE, &memoryItem->type,  &memoryItem->data, NULL);
1124 1125 1126
      g_assert (memoryItem->type & ( TYPE_NUMBER | TYPE_UPPERCASE | TYPE_LOWERCASE));
      break;
    }
Bruno Coudoin's avatar
Bruno Coudoin committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    break;
  case BOARDMODE_ADD:
    get_random_token ( TYPE_ADD, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type == TYPE_ADD);
    break;
  case BOARDMODE_MINUS:
    get_random_token ( TYPE_MINUS, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type == TYPE_MINUS);
    break;
  case BOARDMODE_MULT:
    get_random_token ( TYPE_MULT, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type == TYPE_MULT);
    break;
  case BOARDMODE_DIV:
    get_random_token ( TYPE_DIV, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type == TYPE_DIV);
    break;
  case BOARDMODE_ADD_MINUS:
    get_random_token ( TYPE_ADD | TYPE_MINUS, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type & (TYPE_ADD | TYPE_MINUS));
    break;
  case BOARDMODE_MULT_DIV:
    get_random_token ( TYPE_MULT | TYPE_DIV, &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type & (TYPE_MULT | TYPE_DIV));
    break;
  case BOARDMODE_ADD_MINUS_MULT_DIV:
    get_random_token ( TYPE_ADD | TYPE_MINUS |TYPE_MULT | TYPE_DIV , &memoryItem->type,  &memoryItem->data, &memoryItem->second_value);
    g_assert (memoryItem->type & (TYPE_ADD | TYPE_MINUS |TYPE_MULT | TYPE_DIV));
    break;
    
  default:
    g_error("Don't now in what mode run !");
    break;
1160
  }
Bruno Coudoin's avatar
Bruno Coudoin committed
1161

1162 1163 1164
  g_warning("returned token %s for item x=%d y=%d", memoryItem->data, x, y);


Bruno Coudoin's avatar
Bruno Coudoin committed
1165
  // Randomly set the pair
1166 1167 1168
  rx = (int)(numberOfColumn*((double)rand()/RAND_MAX));
  ry = (int)(numberOfLine*((double)rand()/RAND_MAX));
	
Bruno Coudoin's avatar
Bruno Coudoin committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  while(memoryArray[rx][ry])
    {
      rx++;
      // Wrap
      if(rx>=numberOfColumn)
	{
	  rx=0;
	  ry++;
	  if(ry>=numberOfLine)
	    ry=0;
	}
    }
  // Makes the pair point to this memoryItem for now
  memoryArray[rx][ry] = memoryItem;
}

1185
static void create_item(GnomeCanvasGroup *parent)
Bruno Coudoin's avatar
Bruno Coudoin committed
1186 1187 1188 1189
{
  MemoryItem *memoryItem;
  gint x, y;
  gint height, width;
Bruno Coudoin's avatar
Bruno Coudoin committed
1190
  gint height2, width2;
Bruno Coudoin's avatar
Bruno Coudoin committed
1191 1192 1193
  GdkPixbuf *pixmap = NULL;
  double xratio = 0;
  double yratio = 0;
Bruno Coudoin's avatar
Bruno Coudoin committed
1194
  double card_shadow_w, card_shadow_h;
Bruno Coudoin's avatar
Bruno Coudoin committed
1195 1196

  // Calc width and height of one card
1197 1198
  width  = (base_x2-(currentMode == MODE_TUX ? base_x1_tux : base_x1))/numberOfColumn;
  height = (base_y2-base_y1)/numberOfLine;
Bruno Coudoin's avatar
Bruno Coudoin committed
1199

Bruno Coudoin's avatar
Bruno Coudoin committed
1200
  /* Remove a little bit of space for the card shadow */
Bruno Coudoin's avatar
Bruno Coudoin committed
1201 1202
  height2 = height * 0.95;
  width2  = width  * 0.95;
Bruno Coudoin's avatar
Bruno Coudoin committed
1203

1204 1205

  if (currentUiMode == UIMODE_SOUND) {
1206
    GdkPixbuf *pixmap =  gc_pixmap_load("images/transparent_square2.png");
1207 1208 1209 1210 1211 1212 1213 1214 1215
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
			   gnome_canvas_pixbuf_get_type (),
			   "pixbuf", pixmap, 
			   "x", (double) (currentMode == MODE_TUX ? base_x1_tux : base_x1) - 20,
			   "y", (double) base_y1 - 15,
			   NULL);
    gdk_pixbuf_unref(pixmap);
  }

1216
  if (currentMode == MODE_TUX){
1217
    GdkPixbuf *pixmap_tux =  gc_pixmap_load("images/tux-teacher.png");
1218 1219 1220 1221 1222 1223 1224 1225

    tux = gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
				 gnome_canvas_pixbuf_get_type (),
				 "pixbuf", pixmap_tux, 
				 "x", (double) 50,
				 "y", (double) 20,
				 NULL);
    
1226 1227
    tux_score_s = gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
				       gnome_canvas_text_get_type (),
1228
				       "font", gc_skin_font_board_huge_bold,
1229 1230 1231 1232 1233 1234 1235 1236
				       "x", (double) 100+1.0,
				       "y", (double) 200+1.0,
				       "anchor", GTK_ANCHOR_CENTER,
				       "fill_color_rgba", 0x101010FF,
				       NULL);

    player_score_s = gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
					  gnome_canvas_text_get_type (),
1237
					  "font", gc_skin_font_board_huge_bold,
1238
					  "x", (double) 100+1.0,
1239
					  "y", (double) BASE_CARD_Y2 - 20+1.0,
1240 1241 1242 1243
					  "anchor", GTK_ANCHOR_CENTER,
					  "fill_color_rgba", 0x101010FF,
					  NULL);

1244 1245
    tux_score = gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
				       gnome_canvas_text_get_type (),
1246
				       "font", gc_skin_font_board_huge_bold,
1247 1248 1249 1250 1251 1252 1253 1254
				       "x", (double) 100,
				       "y", (double) 200,
				       "anchor", GTK_ANCHOR_CENTER,
				       "fill_color_rgba", 0xFF0F0FFF,
				       NULL);

    player_score = gnome_canvas_item_new (GNOME_CANVAS_GROUP(parent),
					  gnome_canvas_text_get_type (),
1255
					  "font", gc_skin_font_board_huge_bold,
1256
					  "x", (double) 100,
1257
					  "y", (double) BASE_CARD_Y2 - 20,
1258 1259 1260 1261
					  "anchor", GTK_ANCHOR_CENTER,
					  "fill_color_rgba", 0xFF0F0FFF,
					  NULL);
  }
Bruno Coudoin's avatar
Bruno Coudoin committed
1262

Bruno Coudoin's avatar
Bruno Coudoin committed
1263 1264 1265 1266 1267
  for(x=0; x<numberOfColumn; x++)
    {
      for(y=0; y<numberOfLine; y++)
	{

Bruno Coudoin's avatar
Bruno Coudoin committed
1268
	  memoryItem = g_malloc0(sizeof(MemoryItem));
Bruno Coudoin's avatar
Bruno Coudoin committed
1269 1270 1271 1272
	  
	  memoryItem->rootItem = \
	    gnome_canvas_item_new (parent,
				   gnome_canvas_group_get_type (),
1273 1274
				   "x", (double) (currentMode == MODE_TUX ? base_x1_tux : base_x1) + x*width,
				   "y", (double) base_y1 + y*height,
Bruno Coudoin's avatar
Bruno Coudoin committed
1275
				   NULL);
1276 1277

	  if (currentUiMode == UIMODE_SOUND)
1278
	    pixmap = gc_pixmap_load("gcompris/misc/Tux_mute.png");
1279
	  else
1280
	    pixmap = gc_pixmap_load("gcompris/misc/backcard.png");
1281

Bruno Coudoin's avatar
Bruno Coudoin committed
1282