Example #1
0
        /// <summary>
        /// Get score for a particular question
        /// </summary>
        /// <param name="question">Question to count the score</param>
        /// <returns></returns>
        public float QuestionScore(TestQuestion question)
        {
            float score = 0;

            foreach (var questionInfo in collectedAnswers)
            {
                if (questionInfo.Key == question)
                {
                    var answers = questionInfo.Value;

                    // Mode rules
                    switch (TestState.Mode)
                    {
                    case TestState.ETestMode.Loyal:
                    {
                        foreach (var answer in answers)
                        {
                            if (answer.Correct)
                            {
                                score += question.CorrectValue;
                            }
                            else
                            {
                                score += questionInfo.Key.PunishValue;
                            }
                        }
                        if (score <= 0)
                        {
                            score = 0;
                        }
                    }
                    break;

                    case TestState.ETestMode.Punish:
                    {
                        foreach (var answer in answers)
                        {
                            if (answer.Correct)
                            {
                                score += question.CorrectValue;
                            }
                            else
                            {
                                score += questionInfo.Key.PunishValue;
                            }
                        }
                    }
                    break;

                    default:
                        throw new NotImplementedException("Неизвестный режим работы!\nUnkown test mode!");
                    }
                }
            }
            return(score);
        }
Example #2
0
 /// <summary>
 /// Get collected answers on specific question
 /// </summary>
 /// <param name="question">Question to collect the answers for</param>
 /// <returns></returns>
 public IList <TestAnswer> Answered(TestQuestion question)
 {
     foreach (var answer in collectedAnswers)
     {
         if (answer.Key == question)
         {
             return(answer.Value);
         }
     }
     return(new List <TestAnswer>());
 }
Example #3
0
 /// <summary>
 /// Remove answers on question
 /// </summary>
 /// <param name="question">Question to forget a given answer</param>
 public void ForgetAnswer(TestQuestion question)
 {
     foreach (var answer in collectedAnswers)
     {
         if (answer.Key == question)
         {
             collectedAnswers.Remove(answer);
             return;
         }
     }
 }
Example #4
0
        public object Clone()
        {
            var clone = new TestQuestion(text);

            clone.value   = value;
            clone.answers = new List <TestAnswer>();
            foreach (var answer in answers)
            {
                clone.answers.Add(answer.Clone() as TestAnswer);
            }
            return(clone);
        }
Example #5
0
 /// <summary>
 /// Answer on question
 /// </summary>
 /// <param name="question">Question to answer</param>
 /// <param name="answers">Answer given to question</param>
 public void Answer(TestQuestion question, IList <TestAnswer> answers)
 {
     collectedAnswers.Add(new KeyValuePair <TestQuestion, IList <TestAnswer> > (question, answers));
 }
Example #6
0
        // Load test
        public void LoadTest(string testFilePath)
        {
            StringReader testFile      = null;
            var          testDecryptor = new TestFileEncDec(testFilePath, TestFileEncDec.ETestFileMode.Decode);
            {
                var data = testDecryptor.Data;
                testFile = new StringReader(System.Text.Encoding.UTF8.GetString(data));
            }
            // Read whole text into memory
            //var file = File.OpenText (testFilePath);
            //var testFile = new StringReader (file.ReadToEnd ());
            //file.Close ();
            // Current question being parsed
            TestQuestion question = null;
            string       line     = null;  //testFile.ReadLine();

            do
            {
                line = testFile.ReadLine();
                // Skip commentary lines
                if (line == null || line.Length == 0 || line [0] == '#')
                {
                    continue;
                }
                // Parse the rest of the file
                var keyValue = GetKeyValueFromLine(line);
                switch (keyValue.Key)
                {
                case ETestTags.Test:
                {
                    Name = keyValue.Value;
                }
                break;

                case ETestTags.Author:
                {
                    Author = keyValue.Value;
                }
                break;

                case ETestTags.Date:
                {
                    var dateStr = keyValue.Value.Split('.');
                    LastModified = new DateTime(Convert.ToInt32(dateStr [2]), Convert.ToInt32(dateStr [1]), Convert.ToInt32(dateStr [0]));
                }
                break;

                case ETestTags.Time:
                {
                    // If time is infinite, then time = 0
                    if (keyValue.Value != "inf")
                    {
                        Time = Convert.ToInt32(keyValue.Value);
                    }
                }
                break;

                case ETestTags.Mode:
                {
                    var modeString = keyValue.Value.ToLower();
                    if (modeString == ModePunish)
                    {
                        Mode = ETestMode.Punish;
                    }
                    else
                    {
                        if (modeString == ModeLoyal)
                        {
                            Mode = ETestMode.Loyal;
                        }
                    }
                }
                break;

                case ETestTags.Question:
                {
                    // Add previous question to the list
                    if (question != null)
                    {
                        questions.Add(question);
                    }
                    question = new TestQuestion(keyValue.Value);
                }
                break;

                case ETestTags.Value:
                {
                    // Ignore this keyword if no Question keyword encountered
                    if (question != null)
                    {
                        question.Value = Convert.ToSingle(keyValue.Value);
                    }
                }
                break;

                case ETestTags.Answer:
                {
                    // Ignore this keyword if no Question keyword encountered
                    if (question != null)
                    {
                        var answer = new TestAnswer(keyValue.Value, false);
                        question.Answers.Add(answer);
                    }
                }
                break;

                case ETestTags.AnswerTrue:
                {
                    // Ignore this keyword if no Question keyword encountered
                    if (question != null)
                    {
                        var answer = new TestAnswer(keyValue.Value, true);
                        question.Answers.Add(answer);
                    }
                }
                break;

                default:
                    continue;
                }
            }while(line != null && line.Length != 0);
            // Add last question we've parsed
            if (question != null)
            {
                if (!questions.Contains(question))
                {
                    questions.Add(question);
                }
            }
            testFile.Close();
        }