#include <stdio.h>
#include <conio.h>

#define EQUAL 0
#define COUNTQUESTIONS 8
#define COUNTMESSAGES 6
/*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*
*** FILE:
***   Arithmetica.c
*** DESCRIPTION:
***   This is a rudimentary version of a program that
***   provides practice to students of mathematics.
***   The emphasis is on repetitive learning with
***   occasional tuition of certain concepts.
***   This was written with the Delorie port of the
***   gcc compiler (DJGPP). This compiler can be found
***   at http://www.delorie.com/djgpp/zip-picker.html
*** NOTES:
***   At the moment it uses some non ansi or posix
***   functions (clrscr and textcolor).
*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/

struct MathsQuestion
{
  int idNumber;             //-- A primary key for the questions
  char questionText[100];   //-- The question to be answered.
  char answerText[100];     //-- The answer to the question
  int conceptIds[10];       //-- A foreign key to maths concepts.
};

//-- A structure to contain (congratulations)
struct WellDoneMessage
{
  int idNumber;            //-- A primary key.
  char messageText[100];   //-- The message to the user.
};

int fnMathsQuestionToString(char* ,struct MathsQuestion*);


void main(int argc, char* argv[])
{

  //=========================
  // DECLARATIONS
  //=========================
  int ii;                        //-- general loop variable.
  char sDisplay[100];            //-- utility string buffer.
  int iQuestionIndex;            //-- Index for the Questions array
  int iMessageIndex;
  int iQuestionsCorrect;         //-- The number of question got right.
  int iQuestionsTotal;           //-- Total number of questions asked.
  char sUserResponse[150];       //-- What the user types.
  enum COLORS clTextColor;

  struct MathsQuestion aaQuestions[20] =
  {
    {1, "5 x 0 = ", "0"},
    {2, "1 X 5 = ", "5"},
    {3, "6 X 1 = ", "6"},
    {4, "10 X 2 = ", "20"},
    {5, "20 X 1 = ", "20"},
    {6, "4 X 4 = ", "16"},
    {7, "3 X 1 = ", "3"},
    {8, "100 x 2 = ", "200"}
  };
  struct WellDoneMessage aaMessages[20] =
  {
    {1, "Good on you"},
    {2, "Well done"},
    {3, "Correct"},
    {4, "Thats right"},
    {5, "Correct Answer"},
    {6, "Too easy"}
  };
  
  //========================
  // INITIALIZATIONS
  //========================
  iQuestionsCorrect = 0;
  iQuestionsTotal = 0;
  iQuestionIndex = 0;
  iMessageIndex = 0;
  strcpy(sDisplay, "");

    
  //========================
  // CODE
  //========================
  //clTextColor = LIGHTGREEN;
  textcolor(26);
  //--textattr(BLINK | (BLUE << 4) | WHITE);
  printf("\n\n\n\n\n\n\n\n\n\n\n\n");
  clrscr();
  printf("\n *--*--*--*--* A r i t h m e t i c a --*--*--*--* ");
  printf("\n *** This is an arithmetic practicing program");
  printf("\n *** Press 'q' + [return] to end the program.");
  printf("\n *--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--* \n");
  //clTextColor = LIGHTGRAY;
  //textcolor(clTextColor);
  for(ii = 0; ii < 20; ii++)
  {
    iQuestionIndex = rand() % COUNTQUESTIONS;
    
    printf("\n ========================= \n");
    printf(aaQuestions[iQuestionIndex].questionText);
    scanf("%s", sUserResponse);
    if (strcmp(sUserResponse, "q") == EQUAL)
    {
      //-- Change this logic structure to a 'while'
      //-- statment at the beginning of the loop
      break;
    }
    else if (strcmp
        (
           aaQuestions[iQuestionIndex].answerText,
           sUserResponse
        ) == EQUAL
       )
    {
      iQuestionsCorrect++;
      iMessageIndex = rand() % COUNTMESSAGES;
      printf(aaMessages[iMessageIndex].messageText);
    }
    else
    {
      printf("\n Not quite right ");
      printf("\n The correct answer is: ");
      printf(aaQuestions[iQuestionIndex].answerText);
    } /* if, else */
    //fnMathsQuestionToString(sDisplay, &aaQuestions[ii]);
    //puts(sDisplay);
    iQuestionsTotal++;
  } /* for loop */

  printf("\n ***********************");
  printf("\n See you later ");
  printf("\n Your score this time was: %i / %i ",
    iQuestionsCorrect, iQuestionsTotal);
  printf("\n ***********************");
} /* main */

/*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*
*** FUNCTION:
***   fnMathsQuestionToString()
*** DESCRIPTION:
***   This function creates a displayable string based
***   on the value of a MathsQuestion variable.
***   This is inspired by the java 'toString' method.
***   This function is incomplete.
*** DATE:
***   Sept 2001
*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/

int fnMathsQuestionToString
(
  char* sReturn,
  struct MathsQuestion* pqxQuestion
)
{
  strcpy(sReturn, "");
  sprintf(sReturn,
    "\n idNumber: %i"
    "\n questionText: %s"
    "\n answerText: %s",
      pqxQuestion->idNumber,
      pqxQuestion->questionText,
      pqxQuestion->answerText);
  //-- conceptId is unimplemented
  return(1);
} /* fnStringMathsQuestion */

