algebra.c 19.9 KB
Newer Older
Bruno Coudoin's avatar
Bruno Coudoin committed
1 2
/* gcompris - algebra.c
 *
3
 * Time-stamp: <2006/08/21 23:29:57 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
 *
 * 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
 */

#include <ctype.h>
Bruno Coudoin's avatar
Bruno Coudoin committed
23
#include <string.h>
Bruno Coudoin's avatar
Bruno Coudoin committed
24 25 26

#include "gcompris/gcompris.h"

Bruno Coudoin's avatar
Bruno Coudoin committed
27
#define TIMER_X 400
28 29 30 31
#define TIMER_Y 140

#define PLUSSIGNFILE   '+'
#define MINUSSIGNFILE  '-'
32
#define BYSIGNFILE     'x'
Bruno Coudoin's avatar
Bruno Coudoin committed
33 34 35 36 37 38 39
#define DIVIDESIGNFILE ':'

/* Some constants for the numbers layout */
#define NUMBERSWIDTH       45

#define SOUNDLISTFILE PACKAGE

40
static gboolean	 board_paused = TRUE;
41 42 43 44 45 46
static int	 leavenow;
static int	 gamewon;
static void	 game_won(void);

static int	 maxtime;

Bruno Coudoin's avatar
Bruno Coudoin committed
47 48 49 50 51 52
static GList *item_list = NULL;

static GcomprisBoard *gcomprisBoard = NULL;

static char *expected_result = NULL;

53
static gboolean operation_done[11];
Bruno Coudoin's avatar
Bruno Coudoin committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

typedef struct {
  guint index;
  GnomeCanvasItem *item;
  GnomeCanvasItem *focus_item;
  GnomeCanvasItem *bad_item;
  char *next;  /* Help : Should point to a ToBeFoundItem but don't know how to recurse on it */
  char *previous;
  char value;
  gboolean in_error;
} ToBeFoundItem;
static ToBeFoundItem *currentToBeFoundItem = NULL;

static GnomeCanvasGroup *boardRootItem = NULL;

69
static char currentOperation[2];
Bruno Coudoin's avatar
Bruno Coudoin committed
70

71 72 73 74 75
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);
76
static gint		 key_press(guint keyval, gchar *commit_str, gchar *preedit_str);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static void		 process_ok(void);

static GnomeCanvasItem	*algebra_create_item(GnomeCanvasGroup *parent);
static void		 algebra_destroy_item(GnomeCanvasItem *item);
static void		 algebra_destroy_all_items(void);
static void		 display_operand(GnomeCanvasGroup *parent, 
					 double x_align,
					 double y, 
					 char *operand_str,
					 gboolean masked);
static void		 get_random_number(guint *first_operand, guint *second_operand);
static void		 algebra_next_level(void);
static gint		 item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
static void		 set_focus_item(ToBeFoundItem *toBeFoundItem, gboolean status);
static void		 init_operation(void);
Bruno Coudoin's avatar
Bruno Coudoin committed
92 93

/* Description of this plugin */
94
static BoardPlugin menu_bp =
Bruno Coudoin's avatar
Bruno Coudoin committed
95 96 97
{
   NULL,
   NULL,
98 99
   "Practice the math operation",
   "Answer some algebra questions",
Bruno Coudoin's avatar
Bruno Coudoin committed
100 101 102 103 104 105 106 107 108 109 110 111
   "Bruno Coudoin <bruno.coudoin@free.fr>",
   NULL,
   NULL,
   NULL,
   NULL,
   start_board,
   pause_board,
   end_board,
   is_our_board,
   key_press,
   process_ok,
   set_level,
112
   NULL,
113 114
   NULL,
   NULL,
115
   NULL
Bruno Coudoin's avatar
Bruno Coudoin committed
116 117 118 119 120 121 122 123
};

/*
 * Main entry point mandatory for each Gcompris's game
 * ---------------------------------------------------
 *
 */

124
GET_BPLUGIN_INFO(algebra)
Bruno Coudoin's avatar
Bruno Coudoin committed
125 126 127 128 129 130 131 132 133 134

/*
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 *
 */
static void pause_board (gboolean pause)
{
  if(gcomprisBoard==NULL)
    return;

135
  /* Make the timer follow our pause status */
136
  gc_timer_pause(pause);
137

138 139 140 141
  if(gamewon == TRUE && pause == FALSE) /* the game is won */
      algebra_next_level();

  if(leavenow == TRUE && pause == FALSE)
142
    gc_bonus_end_display(BOARD_FINISHED_RANDOM);
143 144

  board_paused = pause;
Bruno Coudoin's avatar
Bruno Coudoin committed
145 146 147 148 149 150 151 152 153 154 155
}

/*
 */
static void start_board (GcomprisBoard *agcomprisBoard)
{

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

156 157 158
      /* disable im_context */
      gcomprisBoard->disable_im_context = TRUE;

159
      gc_set_background(gnome_canvas_root(gcomprisBoard->canvas), "images/scenery2_background.png");
Bruno Coudoin's avatar
Bruno Coudoin committed
160 161 162 163

      /* set initial values for this level */
      gcomprisBoard->level=1;
      gcomprisBoard->maxlevel=9;
Bruno Coudoin's avatar
Bruno Coudoin committed
164
      gcomprisBoard->sublevel=1;
Bruno Coudoin's avatar
Bruno Coudoin committed
165
      gcomprisBoard->number_of_sublevel=10; /* Go to next level after this number of 'play' */
166
      gc_score_start(SCORESTYLE_NOTE, 
Bruno Coudoin's avatar
Bruno Coudoin committed
167 168 169
			   gcomprisBoard->width - 220, 
			   gcomprisBoard->height - 50, 
			   gcomprisBoard->number_of_sublevel);
170
      gc_bar_set(GC_BAR_LEVEL|GC_BAR_OK);
Bruno Coudoin's avatar
Bruno Coudoin committed
171 172 173 174 175

      /* The mode defines the operation */

      /* Default mode */
      if(!gcomprisBoard->mode)
176
	currentOperation[0]=PLUSSIGNFILE;
177
      else if(g_strncasecmp(gcomprisBoard->mode, "+", 1)==0)
178
	currentOperation[0]=PLUSSIGNFILE;
Bruno Coudoin's avatar
Bruno Coudoin committed
179
      else if(g_strncasecmp(gcomprisBoard->mode, "-", 1)==0)
180
	currentOperation[0]=MINUSSIGNFILE;
Bruno Coudoin's avatar
Bruno Coudoin committed
181
      else if(g_strncasecmp(gcomprisBoard->mode, "*", 1)==0)
182
	currentOperation[0]=BYSIGNFILE;
Bruno Coudoin's avatar
Bruno Coudoin committed
183
      else if(g_strncasecmp(gcomprisBoard->mode, "/", 1)==0)
184 185
	currentOperation[0]=DIVIDESIGNFILE;
      currentOperation[1]='\0';
Bruno Coudoin's avatar
Bruno Coudoin committed
186 187 188 189

      init_operation();
      algebra_next_level();

190 191
      gamewon = FALSE;
      leavenow = FALSE;
Bruno Coudoin's avatar
Bruno Coudoin committed
192 193 194 195 196 197 198 199 200 201 202
      pause_board(FALSE);
    }

}

static void
end_board ()
{
  if(gcomprisBoard!=NULL)
    {
      pause_board(TRUE);
203
      gc_score_end();
Bruno Coudoin's avatar
Bruno Coudoin committed
204 205
      algebra_destroy_all_items();
    }
206
  gcomprisBoard = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
207 208 209 210 211 212 213 214 215 216
}

static void
set_level (guint level)
{


  if(gcomprisBoard!=NULL)
    {
      gcomprisBoard->level=level;
Bruno Coudoin's avatar
Bruno Coudoin committed
217
      gcomprisBoard->sublevel=1;
Bruno Coudoin's avatar
Bruno Coudoin committed
218 219 220 221
      algebra_next_level();
    }
}

222
static gint key_press(guint keyval, gchar *commit_str, gchar *preedit_str)
Bruno Coudoin's avatar
Bruno Coudoin committed
223 224 225 226
{
  guint c;
  gboolean stop = FALSE;

Yves Combe's avatar
Yves Combe committed
227
  if(!boardRootItem)
228
    return FALSE;
Bruno Coudoin's avatar
Bruno Coudoin committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

  /* Add some filter for control and shift key */
  switch (keyval)
    {
      /* Avoid all this keys to be interpreted by this game */
    case GDK_Shift_L:
    case GDK_Shift_R:
    case GDK_Control_L:
    case GDK_Control_R:
    case GDK_Caps_Lock:
    case GDK_Shift_Lock:
    case GDK_Meta_L:
    case GDK_Meta_R:
    case GDK_Alt_L:
    case GDK_Alt_R:
    case GDK_Super_L:
    case GDK_Super_R:
    case GDK_Hyper_L:
    case GDK_Hyper_R:
    case GDK_Num_Lock:
      return FALSE; 
    case GDK_KP_Enter:
    case GDK_Return:
      process_ok();
      return TRUE;
    case GDK_Right:
      if(currentToBeFoundItem->previous!=NULL)
	{
	  set_focus_item(currentToBeFoundItem, FALSE);
	  currentToBeFoundItem = (ToBeFoundItem *)currentToBeFoundItem->previous;
	  set_focus_item(currentToBeFoundItem, TRUE);
	}
      return TRUE;
262 263
    case GDK_Delete:
    case GDK_BackSpace:
Bruno Coudoin's avatar
Bruno Coudoin committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    case GDK_Left:
      if(currentToBeFoundItem->next!=NULL)
	{
	  set_focus_item(currentToBeFoundItem, FALSE);
	  currentToBeFoundItem = (ToBeFoundItem *)currentToBeFoundItem->next;
	  set_focus_item(currentToBeFoundItem, TRUE);
	}
      return TRUE;
    }

  c = tolower(keyval); 

  if(currentToBeFoundItem!=NULL &&
     keyval>=GDK_0 && keyval<=GDK_9)
    {
279 280 281 282
      char number[2];
      number[1] = '\0';
      number[0] = (char)c;

Bruno Coudoin's avatar
Bruno Coudoin committed
283 284
      currentToBeFoundItem->value = c;
      gnome_canvas_item_set (currentToBeFoundItem->item,
285
			     "text", (char *)&number,
Bruno Coudoin's avatar
Bruno Coudoin committed
286 287 288 289 290 291 292 293 294 295 296
			     NULL);

      /* Not a failure (yet) */
      gnome_canvas_item_hide(currentToBeFoundItem->bad_item);
      currentToBeFoundItem->in_error = FALSE;

      set_focus_item(currentToBeFoundItem, FALSE);

      /* Move the focus to the next appropriate digit */
      while(!stop)
	{
297 298
	  if(currentToBeFoundItem->previous!=NULL)
	    currentToBeFoundItem = (ToBeFoundItem *)currentToBeFoundItem->previous;
Bruno Coudoin's avatar
Bruno Coudoin committed
299
	  else
300
	    stop = TRUE;
Bruno Coudoin's avatar
Bruno Coudoin committed
301 302 303 304 305 306 307 308 309 310 311 312 313

	  if(currentToBeFoundItem->in_error==TRUE ||
	     currentToBeFoundItem->value=='?')
	    stop = TRUE;
	}

      set_focus_item(currentToBeFoundItem, TRUE);
    }
  return TRUE;
}



314
static gboolean
Bruno Coudoin's avatar
Bruno Coudoin committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
is_our_board (GcomprisBoard *gcomprisBoard)
{
  if (gcomprisBoard)
    {
      if(g_strcasecmp(gcomprisBoard->type, "algebra")==0)
	{
	  /* Set the plugin entry */
	  gcomprisBoard->plugin=&menu_bp;

	  return TRUE;
	}
    }
  return FALSE;
}


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

336 337 338 339 340
static void timer_end()
{
  gamewon = FALSE;
  leavenow = TRUE;
  algebra_destroy_all_items();
341
  gc_bonus_display(gamewon, BONUS_SMILEY);
342 343 344
}


Bruno Coudoin's avatar
Bruno Coudoin committed
345 346 347 348
/* set initial values for the next level */
static void algebra_next_level() 
{

349
  gc_bar_set_level(gcomprisBoard);
350
  gc_score_set(gcomprisBoard->sublevel);
Bruno Coudoin's avatar
Bruno Coudoin committed
351 352 353 354 355 356 357 358 359 360

  algebra_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));

361
  maxtime = 20;
362
  gc_timer_display(TIMER_X, TIMER_Y, 
363
			 GCOMPRIS_TIMER_BALLOON, maxtime, timer_end);
364

Bruno Coudoin's avatar
Bruno Coudoin committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  /* Try the next level */
  algebra_create_item(boardRootItem);

}


static void algebra_destroy_item(GnomeCanvasItem *item)
{
  item_list = g_list_remove (item_list, item);
  gtk_object_destroy (GTK_OBJECT(item));
}

/* Destroy all the items */
static void algebra_destroy_all_items()
{
  GnomeCanvasItem *item;
  gboolean stop = FALSE;

383
  gc_timer_end();
384

Bruno Coudoin's avatar
Bruno Coudoin committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  while(g_list_length(item_list)>0) 
    {
      item = g_list_nth_data(item_list, 0);
      algebra_destroy_item(item);
    }

  if(currentToBeFoundItem!=NULL)
    {
      /* Move toBeFoundItem to the most next digit */
      while(!stop)
	{
	  if(currentToBeFoundItem->next!=NULL)
	    currentToBeFoundItem = (ToBeFoundItem *)currentToBeFoundItem->next;
	  else
	    stop = TRUE;
	}
      
      /* Now free toBeFoundItems */
      while(!stop)
	{
	  if(currentToBeFoundItem->previous!=NULL)
	    {
	      currentToBeFoundItem = (ToBeFoundItem *)currentToBeFoundItem->previous;
	      free(currentToBeFoundItem->next);
	    }
	  else
	    stop = TRUE;
	}
    }

  if(boardRootItem!=NULL)
    gtk_object_destroy (GTK_OBJECT(boardRootItem));
  
  boardRootItem=NULL;
}

static void display_operand(GnomeCanvasGroup *parent, 
			    double x_align,
			    double y, 
			    char *operand_str,
			    gboolean masked)
{
  GnomeCanvasItem *item, *focus_item = NULL, *bad_item = NULL;
  int i;
  ToBeFoundItem *toBeFoundItem=NULL;
  ToBeFoundItem *previousToBeFoundItem=NULL;;

  for(i=strlen(operand_str)-1; i>=0; i--)
    {
434
      char operand[2] = "?";
Bruno Coudoin's avatar
Bruno Coudoin committed
435

436 437 438
      if(!masked)
	operand[0] = operand_str[i];
      
Bruno Coudoin's avatar
Bruno Coudoin committed
439
      item = gnome_canvas_item_new (parent,
440 441
				    gnome_canvas_text_get_type (),
				    "text", &operand, 
442
				    "font", gc_skin_font_board_huge_bold,
443 444 445 446
				    "anchor", GTK_ANCHOR_CENTER,
				    "x", x_align-((strlen(operand_str)-i)*NUMBERSWIDTH),
				    "y", y,
				    "fill_color_rgba", 0x2c2cFFFF,
Bruno Coudoin's avatar
Bruno Coudoin committed
447 448 449 450 451 452
				    NULL);
      item_list = g_list_append (item_list, item);

      if(masked) 
	{
	  focus_item = gnome_canvas_item_new (parent,
453 454
					      gnome_canvas_text_get_type (),
					      "text", "_",
455
					      "font", gc_skin_font_board_huge_bold,
456 457 458 459
					      "anchor", GTK_ANCHOR_CENTER,
					      "x", x_align-((strlen(operand_str)-i)*NUMBERSWIDTH),
					      "y", y,
					      "fill_color_rgba", 0x00ae00FF,
Bruno Coudoin's avatar
Bruno Coudoin committed
460 461 462 463 464
					      NULL);

	  item_list = g_list_append (item_list, focus_item);

	  bad_item = gnome_canvas_item_new (parent,
465 466
					    gnome_canvas_text_get_type (),
					    "text", "/",
467
					    "font", gc_skin_font_board_huge_bold,
468 469 470 471
					    "anchor", GTK_ANCHOR_CENTER,
					    "x", x_align-((strlen(operand_str)-i)*NUMBERSWIDTH),
					    "y", y,
					    "fill_color_rgba", 0xFF0000FF,
Bruno Coudoin's avatar
Bruno Coudoin committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
					    NULL);
	  gnome_canvas_item_hide(bad_item);

	  item_list = g_list_append (item_list, bad_item);

	}

      if(masked) 
	{
	  toBeFoundItem = malloc(sizeof(ToBeFoundItem));
	  toBeFoundItem->bad_item=bad_item;
	  toBeFoundItem->index=i;
	  toBeFoundItem->value='?';
	  toBeFoundItem->item=item;
	  toBeFoundItem->focus_item=focus_item;
	  toBeFoundItem->previous=(char *)previousToBeFoundItem;
	  toBeFoundItem->next=NULL;

	  /* I Create a double linked list with the toBeFoundItem in order to navigate through them */
	  if(previousToBeFoundItem!=NULL)
	    previousToBeFoundItem->next=(char *)toBeFoundItem;

	  previousToBeFoundItem=toBeFoundItem;

496 497
	  /* Init the focussed item on the right digit */
	  if(i==0)
Bruno Coudoin's avatar
Bruno Coudoin committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	    {
	      currentToBeFoundItem=toBeFoundItem;
	      set_focus_item(toBeFoundItem, TRUE);
	    }
	  else
	    {
	      set_focus_item(toBeFoundItem, FALSE);
	    }
	  gtk_signal_connect(GTK_OBJECT(item), "event",
			     (GtkSignalFunc) item_event,
			     toBeFoundItem);
	}
    }
}

static GnomeCanvasItem *algebra_create_item(GnomeCanvasGroup *parent)
{
  GnomeCanvasItem *item;
Bruno Coudoin's avatar
Bruno Coudoin committed
516 517
  guint first_operand = 0;
  guint second_operand = 0;
Bruno Coudoin's avatar
Bruno Coudoin committed
518 519
  char *first_operand_str = NULL;
  char *second_operand_str = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
520
  char *audioOperand = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
521 522 523 524 525 526 527 528
  guint longuest;
  double x_align;
  double x, y;
  double y_firstline = 0;



  /* Some globals for this board layout */
Bruno Coudoin's avatar
Bruno Coudoin committed
529
  y_firstline = gcomprisBoard->height/5;
Bruno Coudoin's avatar
Bruno Coudoin committed
530 531 532 533 534 535 536 537 538 539


  get_random_number(&first_operand, &second_operand);
  first_operand_str = g_strdup_printf("%d", first_operand);

  second_operand_str = g_strdup_printf("%d", second_operand);

  /* Calc the longuest value */
  longuest = MAX(strlen(first_operand_str), strlen(second_operand_str));

540
  x_align = (gcomprisBoard->width - (longuest*3*NUMBERSWIDTH))/2 + NUMBERSWIDTH*(strlen(first_operand_str)) - 200;
Bruno Coudoin's avatar
Bruno Coudoin committed
541 542 543 544 545

  /* First operand */
  display_operand(parent, x_align, y_firstline, first_operand_str, FALSE);

  /* Second operand */
546 547
  display_operand(parent, x_align + NUMBERSWIDTH*(strlen(second_operand_str)+1),
		  y_firstline, second_operand_str, FALSE);
Bruno Coudoin's avatar
Bruno Coudoin committed
548 549

  /* Display the operator */
550 551
  x=(double) x_align;
  y=(double) y_firstline;
Bruno Coudoin's avatar
Bruno Coudoin committed
552
  item = gnome_canvas_item_new (parent,
553 554
				gnome_canvas_text_get_type (),
				"text", currentOperation, 
555
				"font", gc_skin_font_board_huge_bold,
556 557 558 559
				"x", x,
				"y", y,
				"anchor", GTK_ANCHOR_CENTER,
				"fill_color_rgba", 0xFF3333FF,
Bruno Coudoin's avatar
Bruno Coudoin committed
560 561 562
				NULL);
  item_list = g_list_append (item_list, item);

563 564 565 566
  /* Now the equal sign*/
  item = gnome_canvas_item_new (parent,
				gnome_canvas_text_get_type (),
				"text", "=", 
567
				"font", gc_skin_font_board_huge_bold,
568 569 570 571 572
				"x", x_align + NUMBERSWIDTH*(strlen(second_operand_str)+1),
				"y", y,
				"anchor", GTK_ANCHOR_CENTER,
				"fill_color_rgba", 0xFF3333FF,
				NULL);
Bruno Coudoin's avatar
Bruno Coudoin committed
573 574 575 576
  item_list = g_list_append (item_list, item);

  /* Display the empty area */
  g_free(expected_result);
577
  switch(currentOperation[0])
Bruno Coudoin's avatar
Bruno Coudoin committed
578 579 580
    {
    case PLUSSIGNFILE:
      expected_result = g_strdup_printf("%d", first_operand+second_operand);
581
      audioOperand = "sounds/$LOCALE/misc/plus.ogg";
Bruno Coudoin's avatar
Bruno Coudoin committed
582 583 584
      break;
    case MINUSSIGNFILE:
      expected_result = g_strdup_printf("%d", first_operand-second_operand);
585
      audioOperand = "sounds/$LOCALE/misc/minus.ogg";
Bruno Coudoin's avatar
Bruno Coudoin committed
586 587 588
      break;
    case BYSIGNFILE:
      expected_result = g_strdup_printf("%d", first_operand*second_operand);
589
      audioOperand = "sounds/$LOCALE/misc/by.ogg";
Bruno Coudoin's avatar
Bruno Coudoin committed
590 591 592
      break;
    case DIVIDESIGNFILE:
      expected_result = g_strdup_printf("%d", first_operand/second_operand);
593
      audioOperand = "sounds/$LOCALE/misc/outof.ogg";
Bruno Coudoin's avatar
Bruno Coudoin committed
594 595 596 597
      break;
    default:
      g_error("Bad Operation");
    }
598 599 600 601
  display_operand(parent, x_align + NUMBERSWIDTH*(strlen(second_operand_str)+
						  strlen(expected_result)+2),
		  y_firstline, expected_result, TRUE);
  
Bruno Coudoin's avatar
Bruno Coudoin committed
602

603 604 605
  {
    gchar *str1 = NULL;
    gchar *str2 = NULL;
Bruno Coudoin's avatar
Bruno Coudoin committed
606

607
    if(strlen(first_operand_str)==1)
608
      str1 = gc_sound_alphabet(first_operand_str);
609 610
    else
      str1 = g_strdup_printf("%s.ogg", first_operand_str);
Bruno Coudoin's avatar
Bruno Coudoin committed
611

612
    if(strlen(second_operand_str)==1)
613
      str2 = gc_sound_alphabet(second_operand_str);
614 615 616 617 618 619 620 621 622
    else
      str2 = g_strdup_printf("%s.ogg", second_operand_str);

    g_free(first_operand_str);
    g_free(second_operand_str);

    first_operand_str = g_strdup_printf("sounds/$LOCALE/alphabet/%s", str1);
    second_operand_str = g_strdup_printf("sounds/$LOCALE/alphabet/%s", str2);

623
    gc_sound_play_ogg(first_operand_str, audioOperand , second_operand_str, "sounds/$LOCALE/misc/equal.ogg", NULL);
624 625 626 627 628 629 630

    g_free(str1);
    g_free(str2);

    g_free(first_operand_str);
    g_free(second_operand_str);
  }
Bruno Coudoin's avatar
Bruno Coudoin committed
631 632 633 634 635 636 637
  return (item);
}

static void set_focus_item(ToBeFoundItem *toBeFoundItem, gboolean status)
{
  if(status)
    {
638
      gnome_canvas_item_show (toBeFoundItem->focus_item);
Bruno Coudoin's avatar
Bruno Coudoin committed
639 640 641
    }
  else
    {
642
      gnome_canvas_item_hide (toBeFoundItem->focus_item);
Bruno Coudoin's avatar
Bruno Coudoin committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    }
}

static void process_ok()
{
  ToBeFoundItem *toBeFoundItem;
  ToBeFoundItem *hasfail=NULL;

  set_focus_item(currentToBeFoundItem, FALSE);
  /* Go to the rightmost digit */
  while(currentToBeFoundItem->previous!=NULL)
    {
      currentToBeFoundItem=(ToBeFoundItem *)currentToBeFoundItem->previous;
    }
  
  toBeFoundItem=currentToBeFoundItem;
  /* Check the numbers one by one and redline errors */
  while(currentToBeFoundItem!=NULL)
    {
      if(currentToBeFoundItem->value!=expected_result[currentToBeFoundItem->index])
	{
	  gnome_canvas_item_show(currentToBeFoundItem->bad_item);
	  currentToBeFoundItem->in_error = TRUE;
666 667
	  /* remember the appropriate digit to focus next */
	  hasfail=currentToBeFoundItem;
Bruno Coudoin's avatar
Bruno Coudoin committed
668 669 670 671 672 673 674
	}
      currentToBeFoundItem=(ToBeFoundItem *)currentToBeFoundItem->next;
    }
  currentToBeFoundItem=toBeFoundItem;
  
  if(hasfail==NULL)
    {
675
      game_won();
Bruno Coudoin's avatar
Bruno Coudoin committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    }
  else
    {
      set_focus_item(hasfail, TRUE);
      currentToBeFoundItem=hasfail;
    }
}

/* Callback for the 'toBeFoundItem' */
static gint
item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
{
  ToBeFoundItem *toBeFoundItem;

  toBeFoundItem=(ToBeFoundItem *)data;

692 693 694
  if(board_paused)
    return FALSE;

Bruno Coudoin's avatar
Bruno Coudoin committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
  switch (event->type) 
    {
    case GDK_ENTER_NOTIFY:
      break;
    case GDK_LEAVE_NOTIFY:

      break;
    case GDK_BUTTON_PRESS:
      /* Remove focus from previous selection */
      set_focus_item(currentToBeFoundItem, FALSE);

      /* Set focus on current selection */
      set_focus_item(toBeFoundItem, TRUE);

      currentToBeFoundItem=toBeFoundItem;

711
      gc_sound_play_ogg ("sounds/gobble.ogg", NULL);
Bruno Coudoin's avatar
Bruno Coudoin committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
      break;
      
    default:
      break;
    }
  return FALSE;
}

/*
 * Return the next non already chosed operand
 *
 */
static gint get_operand()
{
  gint j = 10;
  gint i = (1+rand()%10);
  
  // Get the next free slot
Bruno Coudoin's avatar
Bruno Coudoin committed
730
  while(operation_done[i]==TRUE && j>=0)
Bruno Coudoin's avatar
Bruno Coudoin committed
731 732 733
    {
      j--;
      i++;
734 735
      if(i>10)
	i=1;
Bruno Coudoin's avatar
Bruno Coudoin committed
736 737 738 739 740 741 742 743 744
    }
  operation_done[i]=TRUE;
  return i;
}

static void init_operation()
{
  int i;

745
  for(i=1; i<=10; i++)
Bruno Coudoin's avatar
Bruno Coudoin committed
746 747 748 749 750 751 752 753
    operation_done[i] = FALSE;
}

/* Returns a random int based on the current level */
static void get_random_number(guint *first_operand, guint *second_operand)
{
  guint min, max;

754
  switch(currentOperation[0])
Bruno Coudoin's avatar
Bruno Coudoin committed
755 756
    {
    case PLUSSIGNFILE:
757 758 759
      *first_operand = get_operand();
      *second_operand  = gcomprisBoard->level;
      break;
Bruno Coudoin's avatar
Bruno Coudoin committed
760 761 762 763 764
    case BYSIGNFILE:
      *first_operand  = gcomprisBoard->level;
      *second_operand = get_operand();
      break;
    case MINUSSIGNFILE:
765 766
      *first_operand  = gcomprisBoard->level+9;
      *second_operand = get_operand();
Bruno Coudoin's avatar
Bruno Coudoin committed
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 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
      break;
    case DIVIDESIGNFILE:
      switch(gcomprisBoard->level)
	{
	case 1 :
	  min = 1;
	  max = 2;
	  break;
	case 2 :
	  min = 2;
	  max = 3;
	  break;
	case 3 :
	  min = 4;
	  max = 5;
	  break;
	case 4 :
	  min = 6;
	  max = 7;
	  break;
	case 5 :
	  min = 8;
	  max = 9;
	  break;
	case 6 :
	  min = 1;
	  max = 10;
	  break;
	default :
	  min = 1;
	  max = 10;
	}
      *second_operand = (min+rand()%(max-min+1));
      *first_operand  = *second_operand*(min+rand()%max);
      break;
    default:
      g_error("Bad Operation");
    }
}

807 808 809 810 811 812 813 814 815 816
/* ==================================== */
static void game_won()
{
  gcomprisBoard->sublevel++;

  if(gcomprisBoard->sublevel>gcomprisBoard->number_of_sublevel) {
    /* Try the next level */
    gcomprisBoard->sublevel=1;
    gcomprisBoard->level++;
    if(gcomprisBoard->level>gcomprisBoard->maxlevel) { // the current board is finished : bail out
817
      gc_bonus_end_display(BOARD_FINISHED_RANDOM);
818 819 820 821 822
      return;
    }

    gamewon = TRUE;
    algebra_destroy_all_items();
823
    gc_bonus_display(gamewon, BONUS_SMILEY);
824 825 826 827 828
    init_operation();
    return;
  }
  algebra_next_level();
}