[Tux4kids-commits] r579 - in tuxmath/branches/mathcards_newarch: data/missions/arcade src

cheezmeister-guest at alioth.debian.org cheezmeister-guest at alioth.debian.org
Fri Aug 1 18:56:32 UTC 2008


Author: cheezmeister-guest
Date: 2008-08-01 18:56:32 +0000 (Fri, 01 Aug 2008)
New Revision: 579

Modified:
   tuxmath/branches/mathcards_newarch/data/missions/arcade/ranger
   tuxmath/branches/mathcards_newarch/src/game.c
   tuxmath/branches/mathcards_newarch/src/mathcards.c
   tuxmath/branches/mathcards_newarch/src/mathcards.h
Log:
Branch: better support for different answer formats

Modified: tuxmath/branches/mathcards_newarch/data/missions/arcade/ranger
===================================================================
--- tuxmath/branches/mathcards_newarch/data/missions/arcade/ranger	2008-07-30 00:01:05 UTC (rev 578)
+++ tuxmath/branches/mathcards_newarch/data/missions/arcade/ranger	2008-08-01 18:56:32 UTC (rev 579)
@@ -9,7 +9,7 @@
 division_allowed = 1
 allow_negatives = 0
 max_formula_nums = 2
-min_formula_nums = 3
+min_formula_nums = 2
 min_augend = 0
 max_augend = 20
 min_addend = 0

Modified: tuxmath/branches/mathcards_newarch/src/game.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/game.c	2008-07-30 00:01:05 UTC (rev 578)
+++ tuxmath/branches/mathcards_newarch/src/game.c	2008-08-01 18:56:32 UTC (rev 579)
@@ -112,8 +112,7 @@
 static float comet_feedback_height;
 static float danger_level;
 
-#define MAX_DIGITS 3 //should really be determined by max_answer_size
-static int digits[MAX_DIGITS];
+static int digits[MC_MAX_DIGITS];
 
 static comet_type* comets = NULL;
 static city_type* cities = NULL;
@@ -1053,7 +1052,7 @@
 void game_handle_answer(void)
 {
   int i, j, lowest, lowest_y;
-  char ans[MAX_DIGITS+2]; //extra space for negative, and for final '\0'
+  char ans[MC_MAX_DIGITS+2]; //extra space for negative, and for final '\0'
   Uint32 ctime;
 
   if (!doing_answer)
@@ -1070,8 +1069,8 @@
   /* negative answer support DSB */
   
   ans[0] = '-'; //for math questions only, this is just replaced.
-  for (i = 0; i < MAX_DIGITS - 1 && !digits[i]; ++i); //skip leading 0s
-  for (j = neg_answer_picked ? 1 : 0; i < MAX_DIGITS; ++i, ++j)
+  for (i = 0; i < MC_MAX_DIGITS - 1 && !digits[i]; ++i); //skip leading 0s
+  for (j = neg_answer_picked ? 1 : 0; i < MC_MAX_DIGITS; ++i, ++j)
     ans[j] = digits[i] + '0';
   ans[j] = '\0';
   
@@ -1079,7 +1078,7 @@
   if (neg_answer_picked)
   {
     ans[0] = '-';
-    for (i = j = 0; i < MAX_DIGITS; ++i)
+    for (i = j = 0; i < MC_MAX_DIGITS; ++i)
     {
       if (digits[i] == 0)
         continue;
@@ -1088,7 +1087,7 @@
   }
   else
   {
-    for (i = j = 0; i < MAX_DIGITS; ++i)
+    for (i = j = 0; i < MC_MAX_DIGITS; ++i)
     {
       if (digits[i] == 0)
         continue;
@@ -1108,7 +1107,7 @@
     if (comets[i].alive &&
         comets[i].expl < COMET_EXPL_END &&
         //comets[i].answer == num &&
-        0 == strncmp(comets[i].flashcard.answer_string, ans, MAX_DIGITS+1) &&
+        0 == strncmp(comets[i].flashcard.answer_string, ans, MC_MAX_DIGITS+1) &&
         comets[i].y > lowest_y)
     {
       lowest = i;
@@ -1192,7 +1191,7 @@
   }
 
   /* Clear digits: */
-  for (i = 0; i < MAX_DIGITS; ++i)
+  for (i = 0; i < MC_MAX_DIGITS; ++i)
     digits[i] = 0;
   neg_answer_picked = 0;
 }
@@ -2287,7 +2286,7 @@
 
   /* Clear LED F: */
 
-  for (i = 0; i < MAX_DIGITS; ++i)
+  for (i = 0; i < MC_MAX_DIGITS; ++i)
     digits[i] = 0;
   neg_answer_picked = 0;
 
@@ -2943,7 +2942,7 @@
   else
     dest.x = ((screen->w - ((images[IMG_LEDNUMS]->w) / 10) * 3) / 2);
 
-  for (i = -1; i < MAX_DIGITS; i++) /* -1 is special case to allow minus sign */
+  for (i = -1; i < MC_MAX_DIGITS; i++) /* -1 is special case to allow minus sign */
                               /* with minimal modification of existing code DSB */
   {
     if (-1 == i)
@@ -3242,9 +3241,9 @@
   if (key >= SDLK_0 && key <= SDLK_9)
   {
     /* [0]-[9]: Add a new digit: */
-    for (i = 0; i < MAX_DIGITS-1; ++i)
+    for (i = 0; i < MC_MAX_DIGITS-1; ++i)
       digits[i] = digits[i+1];
-    digits[MAX_DIGITS-1] = key - SDLK_0;
+    digits[MC_MAX_DIGITS-1] = key - SDLK_0;
     
 //    digits[0] = digits[1];
 //    digits[1] = digits[2];
@@ -3254,9 +3253,9 @@
   else if (key >= SDLK_KP0 && key <= SDLK_KP9)
   {
     /* Keypad [0]-[9]: Add a new digit: */
-    for (i = 0; i < MAX_DIGITS-1; ++i)
+    for (i = 0; i < MC_MAX_DIGITS-1; ++i)
       digits[i] = digits[i+1];
-    digits[MAX_DIGITS-1] = key - SDLK_KP0;
+    digits[MC_MAX_DIGITS-1] = key - SDLK_KP0;
     
 //    digits[0] = digits[1];
 //    digits[1] = digits[2];
@@ -3283,7 +3282,7 @@
            key == SDLK_DELETE)
   {
     /* [BKSP]: Clear digits! */
-    for (i = 0; i < MAX_DIGITS; ++i)
+    for (i = 0; i < MC_MAX_DIGITS; ++i)
       digits[i] = 0;
     tux_pressing = 1;
   }
@@ -3301,6 +3300,7 @@
 void add_score(int inc)
 {
   score += inc;
+  tmdprintf("Score is now: %d\n", score);
 }
 
 

Modified: tuxmath/branches/mathcards_newarch/src/mathcards.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/mathcards.c	2008-07-30 00:01:05 UTC (rev 578)
+++ tuxmath/branches/mathcards_newarch/src/mathcards.c	2008-08-01 18:56:32 UTC (rev 579)
@@ -238,7 +238,7 @@
 /* Functions for new mathcards architecture */
 static void free_node(MC_MathQuestion* mq); //wrapper for free() that also frees card
 static MC_FlashCard generate_random_flashcard(void);
-static MC_FlashCard generate_random_ooo_card_of_length(int length);
+static MC_FlashCard generate_random_ooo_card_of_length(int length, int reformat);
 static void copy_card(const MC_FlashCard* src, MC_FlashCard* dest); //deep copy a flashcard
 static MC_MathQuestion* allocate_node(void); //allocate space for a node
 static int compare_card(const MC_FlashCard* a, const MC_FlashCard* b); //test for identical cards
@@ -1490,8 +1490,7 @@
                        MC_GetOpt(MIN_FORMULA_NUMS) + 1) //avoid div by 0
                     +  MC_GetOpt(MIN_FORMULA_NUMS);
     mcdprintf(" of length %d", length);
-    ret = generate_random_ooo_card_of_length(length);
-    strncat(ret.formula_string, " = ?", max_formula_size - strlen(ret.formula_string) );
+    ret = generate_random_ooo_card_of_length(length, 1);
     #ifdef MC_DEBUG
     print_card(ret);
     #endif
@@ -1509,10 +1508,12 @@
 the formula and on the operators used. Problems have a 'base' difficulty of
 1 for binary operations, 3 for 3 numbers, 6, 10, etc. Each operator adds to
 the score: 0, 1, 2, and 3 respectively for addition, subtraction,
-multiplication and division.
+multiplication and division.If reformat is 0, FORMAT_ANS_LAST will be used,
+otherwise a format is chosen at random.
 */
-MC_FlashCard generate_random_ooo_card_of_length(int length)
-{
+MC_FlashCard generate_random_ooo_card_of_length(int length, int reformat)
+{
+  int format = 0;
   int r1 = 0;
   int r2 = 0;
   int ans = 0;
@@ -1531,7 +1532,8 @@
          MC_GetOpt(op + ADDITION_ALLOWED) == 0; //make sure it's allowed
          op = rand() % MC_NUM_OPERS);
 
-    mcdprintf("Operation is %c\n", operchars[op]);
+    mcdprintf("Operation is %c\n", operchars[op]);
+    /*
     if (op == MC_OPER_ADD)
     {
       r1 = rand() % (math_opts->iopts[MAX_AUGEND] - math_opts->iopts[MIN_AUGEND] + 1) + math_opts->iopts[MIN_AUGEND];
@@ -1557,25 +1559,49 @@
       if (r2 == 0)
         r2 = 1;
       r1 = ans * r2;
-    }
-    else
+    }
+    */
+    if (op > MC_OPER_DIV || op < MC_OPER_ADD)
     {
       mcdprintf("Invalid operator: value %d\n", op);
       return DEFAULT_CARD;
+    }
+    //choose two numbers in the proper range and get their result
+    
+    else
+    {
+      r1 = rand() % (math_opts->iopts[MAX_AUGEND+4*op] - math_opts->iopts[MIN_AUGEND+4*op] + 1) + math_opts->iopts[MIN_AUGEND+4*op];    
+      r2 = rand() % (math_opts->iopts[MAX_ADDEND+4*op] - math_opts->iopts[MIN_ADDEND+4*op] + 1) + math_opts->iopts[MIN_ADDEND+4*op]; 
+
+      if (op == MC_OPER_ADD)
+        ans = r1 + r2;
+      if (op == MC_OPER_SUB)
+        ans = r1 - r2;
+      if (op == MC_OPER_MULT)
+        ans = r1 * r2;
+      if (op == MC_OPER_DIV)  
+      {
+        if (r2 == 0)
+          r2 = 1;
+        ret.difficulty = r1;
+        r1 *= r2;
+        ans = ret.difficulty;
+      }
     }
-
+
+
     mcdprintf("Constructing answer_string\n");
     snprintf(ret.answer_string, max_answer_size+1, "%d", ans);
-//    mcdprintf("'%s' vs '%d'\n", ret.answer_string, ans);
     mcdprintf("Constructing formula_string\n");
     snprintf(ret.formula_string, max_formula_size, "%d %c %d",
-             r1, operchars[op], r2);
+             r1, operchars[op], r2);
     ret.answer = ans;
-    ret.difficulty = op + 1;
+    ret.difficulty = op + 1;
+
   }
   else //recurse
   {
-    ret = generate_random_ooo_card_of_length(length - 1);
+    ret = generate_random_ooo_card_of_length(length - 1, 0);
 
     if (strchr(ret.formula_string, '+') || strchr(ret.formula_string, '-') )
     {
@@ -1644,6 +1670,18 @@
     snprintf(ret.answer_string, max_answer_size, "%d", ret.answer);
     ret.difficulty += (length - 1) + op;
   }
+  
+  if (reformat)
+  {
+    mcdprintf("Reformatting...\n");
+    do {
+      format = rand() % MC_NUM_FORMATS;
+    } while (!MC_GetOpt(FORMAT_ANSWER_LAST + format) && 
+             !MC_GetOpt(FORMAT_ADD_ANSWER_LAST + op * 3 + format) );
+   
+    strncat(ret.formula_string, " = ?", max_formula_size - strlen(ret.formula_string) );
+    reformat_arithmetic(&ret, format );     
+  }
   return ret;
 }
 
@@ -1712,8 +1750,10 @@
       }
       else if (length < cl) //if too many questions, chop off tail end of list
       {
-        mcdprintf("Cutting list to %d questions\n", length);
-        delete_list(find_node(list, length) );
+        mcdprintf("Cutting list to %d questions\n", length);
+        end_of_list = find_node(list, length);
+        delete_list(end_of_list->next);
+        end_of_list->next = NULL;
       }
     }
   }
@@ -1977,13 +2017,49 @@
         end_of_list = tnode;
       }
     }
-  }
+  }
+  mcdprintf("Exiting add_all_valid()\n");
   return list;
 }
 
-static MC_MathQuestion* find_node(const MC_MathQuestion* list, int num)
+MC_MathQuestion* find_node(const MC_MathQuestion* list, int num)
 {
   while (--num > 0 && list)
     list = list->next;
   return list;
 }
+void reformat_arithmetic(MC_FlashCard* card, MC_Format f)
+{
+  int i, j;
+  char* beg = 0;
+  char* end = 0;
+  char nans[max_answer_size];
+  char nformula[max_formula_size + max_answer_size]; //gets a bit larger than usual in the meantime
+  
+  {
+    snprintf(nans, max_answer_size, "%s", card->answer_string);
+   
+    //insert old answer where question mark was
+    for (i = 0, j = 0; card->formula_string[j] != '?'; ++i, ++j)
+      nformula[i] = card->formula_string[j];
+    i += snprintf(nformula + i, max_answer_size-1, "%s", card->answer_string);
+    snprintf(nformula + i, max_formula_size - i, "%s", card->formula_string + j + 1);
+
+    //replace the new answer with a question mark
+    if (f == MC_FORMAT_ANS_LAST)
+      beg = strrchr(nformula, ' ') + 1;
+    if (f == MC_FORMAT_ANS_FIRST)
+      beg = nformula;
+    if (f == MC_FORMAT_ANS_MIDDLE)
+      beg = strchr(nformula, ' ') + 3;
+    end = strchr(beg + 1, ' ');
+    if (!end)
+      end = "";
+    //we now have beg = first digit of number to replace, end = the char after
+    sscanf(beg, "%s", nans);
+    *beg = 0; //sequester the first half of the string
+    snprintf(card->formula_string, max_formula_size, "%s?%s", nformula, end);
+    snprintf(card->answer_string, max_answer_size, nans);
+    card->answer = atoi(card->answer_string);
+  }
+}

Modified: tuxmath/branches/mathcards_newarch/src/mathcards.h
===================================================================
--- tuxmath/branches/mathcards_newarch/src/mathcards.h	2008-07-30 00:01:05 UTC (rev 578)
+++ tuxmath/branches/mathcards_newarch/src/mathcards.h	2008-08-01 18:56:32 UTC (rev 579)
@@ -15,7 +15,7 @@
 #ifndef MATHCARDS_H
 #define MATHCARDS_H
 
-#define MC_DEBUG
+//#define MC_DEBUG
 #ifdef MC_DEBUG
 #define mcdprintf(...) printf(__VA_ARGS__)
 #else
@@ -44,7 +44,8 @@
 typedef enum _MC_Format {
   MC_FORMAT_ANS_LAST,     /* a + b = ? */
   MC_FORMAT_ANS_FIRST,    /* ? + b = c */
-  MC_FORMAT_ANS_MIDDLE    /* a + ? = c */
+  MC_FORMAT_ANS_MIDDLE,    /* a + ? = c */
+  MC_NUM_FORMATS
 } MC_Format;
 
 
@@ -133,6 +134,7 @@
 extern const char operchars[MC_NUM_OPERS];
 
 /* default values for math_options */
+#define MC_MAX_DIGITS 3 
 #define MC_GLOBAL_MAX 999          /* This is the largest absolute value that */
                                    /* can be entered for math question values.*/
 #define MC_MATH_OPTS_INVALID -9999 /* Return value for accessor functions     */
@@ -287,5 +289,5 @@
 void MC_FreeFlashcard(MC_FlashCard* fc);
 void MC_ResetFlashCard(MC_FlashCard* fc);
 int MC_FlashCardGood(const MC_FlashCard* fc); //verifies a flashcard is valid
-
+void reformat_arithmetic(MC_FlashCard* card, MC_Format f);
 #endif




More information about the Tux4kids-commits mailing list