[Tux4kids-commits] r1167 - in tuxmath/branches/lan: server src

David Bruce dbruce-guest at alioth.debian.org
Thu Jul 9 19:51:47 UTC 2009


Author: dbruce-guest
Date: 2009-07-09 19:51:38 +0000 (Thu, 09 Jul 2009)
New Revision: 1167

Added:
   tuxmath/branches/lan/src/transtruct.h
Modified:
   tuxmath/branches/lan/server/mathcards.c
   tuxmath/branches/lan/server/server.c
   tuxmath/branches/lan/src/Makefile.am
Log:
Update of mathcards to handle MC_AnsweredCorrectly() correctly



Modified: tuxmath/branches/lan/server/mathcards.c
===================================================================
--- tuxmath/branches/lan/server/mathcards.c	2009-07-09 12:06:49 UTC (rev 1166)
+++ tuxmath/branches/lan/server/mathcards.c	2009-07-09 19:51:38 UTC (rev 1167)
@@ -484,9 +484,8 @@
 {
   mcdprintf("\nEntering MC_NextQuestion()\n");
 
-  /* (so we can free the node after removed from list:) */
+  /* (so we can move the node into active_quests:) */
   MC_MathQuestion* ptr;
-  ptr = question_list;
 
   if (!fc )
   {
@@ -509,11 +508,12 @@
   copy_card(&question_list->card, fc);
  
   /* take first question node out of list and move it into active_quests list: */
-  question_list = remove_node(question_list, question_list);
+  ptr = question_list;
+  question_list = remove_node(question_list, ptr);
 //  free_node(ptr);
   quest_list_length--;
   questions_pending++;
-  append_node(active_quests, ptr);
+  active_quests = append_node(active_quests, ptr);
 
   #ifdef MC_DEBUG
   printf("\nnext question is:");
@@ -534,6 +534,8 @@
 {
   mcdprintf("\nEntering MC_AnsweredCorrectly()");
 
+  MC_MathQuestion* quest = NULL;
+
   if (!fc)
   {
     fprintf(stderr, "\nMC_AnsweredCorrectly() passed invalid pointer as argument!\n");
@@ -549,22 +551,43 @@
   print_card(*fc);
   #endif
 
-  //FIXME we need to take the question out of the active_quests list
+  if(!active_quests) // Means we didn't find matching card - something is wrong:
+  {
+    fprintf(stderr, "MC_AnsweredCorrectly() - active_quests empty\n");
+    return 0;
+  }
+
+  //First take the question out of the active_quests list
+  quest = active_quests;  
+  // Loop until quest is NULL or we find card with same id:
+  while(quest && (fc->question_id != quest->card.question_id))
+    quest = quest->next;
+  if(!quest) // Means we didn't find matching card - something is wrong:
+  {
+    fprintf(stderr, "MC_AnsweredCorrectly() - matching question not found!\n");
+    return 0;
+  }
+  #ifdef MC_DEBUG
+  printf("\nMatching question is:");
+  print_card(quest->card);
+  #endif
+
+  //We found a matching question, now we take it out of the 
+  //"active_quests" list and either put it back into the 
+  //main question list in a random location, or delete it:
+  active_quests = remove_node(active_quests, quest);
+  questions_pending--;  //the length of the 'active_quests' list
   answered_correctly++;
-  questions_pending--;
 
   if (!math_opts->iopts[PLAY_THROUGH_LIST])
   /* reinsert question into question list at random location */
   {
     mcdprintf("\nReinserting question into list");
 
-    MC_MathQuestion* ptr1;
-    MC_MathQuestion* ptr2;
-    /* make new node using values from flashcard */
-    ptr1 = create_node_from_card(fc);
+    MC_MathQuestion* rand_spot;
     /* put it into list */
-    ptr2 = pick_random(quest_list_length, question_list);
-    question_list = insert_node(question_list, ptr2, ptr1);
+    rand_spot = pick_random(quest_list_length, question_list);
+    question_list = insert_node(question_list, rand_spot, quest);
     quest_list_length++;
     /* unanswered does not change - was not decremented when */
     /* question allocated!                                   */
@@ -572,6 +595,7 @@
   else
   {
     mcdprintf("\nNot reinserting question into list");
+    free_node(quest);
     /* not recycling questions so fewer questions remain:      */
     unanswered--;
   }
@@ -584,16 +608,42 @@
   return 1;
 }
 
+
+
 int MC_AnsweredCorrectly_id(int id)
 {
-  return 1;
+  MC_MathQuestion* mq;
+  MC_FlashCard* fc;
+
+  if(!active_quests)
+  {
+    mcdprintf("MC_AnsweredCorrectly_id() - active_quests is empty\n");
+    return 0;
+  }
+  //Find the question with the given id, if it exists:
+    //First take the question out of the active_quests list
+  mq = active_quests;  
+  // Loop until mq is NULL or card found with matching id:
+  while(mq && (id != mq->card.question_id))
+  {
+    mcdprintf("id is %d, mq->card.question_id is %d\n", id, mq->card.question_id);
+    mq = mq->next;
+  }
+  if(!mq) // Means we didn't find matching card - something is wrong:
+  {
+    fprintf(stderr, "MC_AnsweredCorrectly_id() - matching question not found!\n");
+    return 0;
+  }
+  //Now just pass address of card field to MC_AnsweredCorrectly():
+  fc = &(mq->card);
+  return MC_AnsweredCorrectly(fc);
 }
 
 /*  MC_NotAnsweredCorrectly() is how the user interface    */
 /*  tells MathCards that the player failed to answer the  */
 /*  question correctly. Returns 1 if no errors.           */
-/*  Note: this gets triggered only if a player's city     */
-/*  gets hit by a question, not if they "miss".           */
+/*  Note: this gets triggered only if a player's igloo/city */
+/*  gets hit by a question, not if they "miss".             */
 int MC_NotAnsweredCorrectly(MC_FlashCard* fc)
 {
   mcdprintf("\nEntering MC_NotAnsweredCorrectly()");
@@ -965,6 +1015,9 @@
   return time_per_question_list[length_time_per_question_list/2];
 }
 
+
+
+
 /* Implementation of "private methods" - (cannot be called from outside
 of this file) */
 
@@ -1066,7 +1119,9 @@
 
 /* this puts the node into the list AFTER the node pointed to by current */
 /* and returns a pointer to the top of the modified list  */
-MC_MathQuestion* insert_node(MC_MathQuestion* first, MC_MathQuestion* current, MC_MathQuestion* new_node)
+MC_MathQuestion* insert_node(MC_MathQuestion* first,
+                             MC_MathQuestion* current,
+                             MC_MathQuestion* new_node)
 {
   /* return pointer to list unchanged if new_node doesn't exist*/
   if (!new_node)
@@ -1194,8 +1249,8 @@
 #ifdef MC_DEBUG
 void print_card(MC_FlashCard card)
 {
-  printf("\nprint_card():");
-  printf("question_id=%d\nformula_string = %s\nanswer_string = %s\ndifficulty = %d\n\n",
+  printf("\nprint_card():\n");
+  printf("question_id: %d\nformula_string: %s\nanswer_string: %s\ndifficulty: %d\n\n",
          card.question_id,
          card.formula_string,
          card.answer_string,
@@ -1214,6 +1269,7 @@
   printf("\nanswered_wrong = \t%d", answered_wrong);
   printf("\nlist_length(wrong_quests) = \t%d", list_length(wrong_quests));
   printf("\nquestions_pending = \t%d", questions_pending);
+  printf("\nlist_length(active_quests) = \t%d", list_length(active_quests));
 }
 
 // /* a "copy constructor", so to speak */
@@ -1870,6 +1926,10 @@
   return list;
 }
 
+/* NOTE - returns 0 (i.e. "false") if *identical*, and */
+/* 1 (i.e. "true") if *different* - counterintuitive,  */
+/* but same behavior as e.g. strcmp()                  */
+
 static int compare_card(const MC_FlashCard* a, const MC_FlashCard* b)
 {
   if (strncmp(a->formula_string, b->formula_string, MC_FORMULA_LEN) )

Modified: tuxmath/branches/lan/server/server.c
===================================================================
--- tuxmath/branches/lan/server/server.c	2009-07-09 12:06:49 UTC (rev 1166)
+++ tuxmath/branches/lan/server/server.c	2009-07-09 19:51:38 UTC (rev 1167)
@@ -510,11 +510,11 @@
  
 }
 
-void game_msg_correct_answer(int i,int id)
+void game_msg_correct_answer(int i, int id)
 {
   int n;
   char buf[NET_BUF_LEN];
-
+printf("game_msg_correct_answer() called\n");
   //Announcement for server and all clients:
   snprintf(buf, NET_BUF_LEN, 
           "question id %d was answered correctly by %s\n",
@@ -853,7 +853,7 @@
   return 1;
 }
 
-
+/* Send the message to all clients: */
 void broadcast_msg(char* msg)
 {
   int i = 0;
@@ -864,7 +864,9 @@
 }
 
 
-
+/* Simple function that returns a minimum of 'loop_msec' */
+/* milliseconds after it returned the previous time it   */
+/* was called.                                           */
 void throttle(int loop_msec)
 {
   static Uint32 now_t, last_t; //These will be zero first time through

Modified: tuxmath/branches/lan/src/Makefile.am
===================================================================
--- tuxmath/branches/lan/src/Makefile.am	2009-07-09 12:06:49 UTC (rev 1166)
+++ tuxmath/branches/lan/src/Makefile.am	2009-07-09 19:51:38 UTC (rev 1167)
@@ -87,7 +87,8 @@
 	gettext.h	\
 	scandir.h	\
 	pixels.h	\
-	compiler.h	
+	compiler.h	\
+	transtruct.h
         
 
 

Added: tuxmath/branches/lan/src/transtruct.h
===================================================================
--- tuxmath/branches/lan/src/transtruct.h	                        (rev 0)
+++ tuxmath/branches/lan/src/transtruct.h	2009-07-09 19:51:38 UTC (rev 1167)
@@ -0,0 +1,51 @@
+/*
+
+        transtruct.h
+
+        Description: contains headers for the data structures
+        that would be transferred between the server and the client
+        during the multiplayer LAN game.
+
+        Author: David Bruce ,Akash Gangil and the TuxMath team, (C) 2009
+
+        Copyright: See COPYING file that comes with this distribution (briefly, GNU GPL version 2 or later)
+
+*/
+#ifndef TRANSTRUCT_H
+#define TRANSTRUCT_H
+
+#define LAN_DEBUG
+#define NET_BUF_LEN 512
+#define DEFAULT_PORT 4779
+#define NAME_SIZE 50
+
+#define MC_USE_NEWARC
+#define MC_FORMULA_LEN 40
+#define MC_ANSWER_LEN 5
+
+
+
+#ifndef MC_USE_NEWARC
+/* struct for individual "flashcard" */
+typedef struct MC_FlashCard {
+  int num1;
+  int num2;
+  int num3;
+  int operation;
+  int format;
+  char formula_string[MC_FORMULA_LEN];
+  char answer_string[MC_ANSWER_LEN];
+} MC_FlashCard;
+#else
+/* experimental struct for a more generalized flashcard */
+typedef struct _MC_FlashCard {
+  char formula_string[MC_FORMULA_LEN];
+  char answer_string[MC_ANSWER_LEN];
+  int question_id;
+  int answer;
+  int difficulty;
+} MC_FlashCard;
+#endif
+
+
+#endif




More information about the Tux4kids-commits mailing list