Example #1
0
        /**
         * @param proficiency 表示只选择这个熟练度以下(包括当前)的,当取ProficiencyCount时表示不限熟练度
         * @param bAllTime 选取全部时间范围的词
         */
        public bool InitQuiz(ArrayList wordPads, QuizType type, int testWordCount, NewWordItem.ProficiencyLevel proficiency, DateTime starDate, DateTime endDate, bool bAllTime = true)
        {
            HashSet <int> alreadyChoosedWordIndice = new HashSet <int>();
            ArrayList     totalWords          = new ArrayList();
            ArrayList     totalWordsForOption = new ArrayList(); //不考虑时间的所有词,作为选项候选

            int totalWordCount = 0;

            System.Collections.IEnumerator iterator = wordPads.GetEnumerator();

            DateTime earliestTime = new DateTime(8000, 12, 31);
            DateTime latestTime   = new DateTime(1000, 1, 1);

            while (iterator.MoveNext())
            {
                WordPad wordPad = iterator.Current as WordPad;

                totalWordsForOption.AddRange(wordPad.Words);

                if (bAllTime)
                {
                    totalWordCount += wordPad.Words.Count;
                    totalWords.AddRange(wordPad.Words);
                }
                else
                {
                    ArrayList wordsWithinTimeRange = wordPad.GetWordsByTimeRange(starDate, endDate);
                    totalWordCount += wordsWithinTimeRange.Count;
                    totalWords.AddRange(wordsWithinTimeRange);

                    if (earliestTime.CompareTo(wordPad.EarliestAddTime) > 0)
                    {
                        earliestTime = wordPad.EarliestAddTime;
                    }

                    if (latestTime.CompareTo(wordPad.LatestAddTime) < 0)
                    {
                        latestTime = wordPad.LatestAddTime;
                    }
                }
            }

            if (!bAllTime &&
                (starDate.CompareTo(latestTime) > 0 || // not in time range
                 endDate.CompareTo(earliestTime) < 0)
                )
            {
                MessageBox.Show("specified time range exceed all the words' total time range!");
                return(false);
            }

            if (totalWords.Count <= 3)
            {
                MessageBox.Show("Not sufficient words for a valid test!");
                return(false);
            }

            if (testWordCount > totalWords.Count)
            {
                testWordCount = totalWords.Count;
                MessageBox.Show("test word count is more than word count in all selected wordpads, so clamp to the actual count: " + totalWords.Count);
            }

            Random randGenerator = new Random(Convert.ToInt32(DateTime.Now.Millisecond));

            for (int i = 0; i < testWordCount; i++)
            {
                int         globalIndex   = randGenerator.Next(0, totalWordCount);
                NewWordItem candidateWord = (NewWordItem)totalWords[globalIndex];

                // exclude words not fit condition
                while (alreadyChoosedWordIndice.Contains(globalIndex) ||
                       candidateWord.Meaning == "" ||
                       candidateWord.Proficiency > proficiency ||
                       (!bAllTime &&
                        (candidateWord.AddTime.CompareTo(starDate) < 0 ||  // not in time range
                         candidateWord.AddTime.CompareTo(endDate) > 0)
                       )
                       )
                {
                    globalIndex   = randGenerator.Next(0, totalWordCount);
                    candidateWord = (NewWordItem)totalWords[globalIndex];
                }

                // add test word
                m_testWords.Add(totalWords[globalIndex]);

                // add meaning options for words
                HashSet <int> optionsChoosedForCurWord = new HashSet <int>();
                optionsChoosedForCurWord.Add(globalIndex);

                ArrayList optionWords = new ArrayList();
                for (int j = 0; j < 3; j++)
                {
                    int optionWordIndex = randGenerator.Next(0, totalWordsForOption.Count);
                    while (optionsChoosedForCurWord.Contains(optionWordIndex) ||
                           ((NewWordItem)totalWordsForOption[optionWordIndex]).Meaning == "")
                    {
                        optionWordIndex = randGenerator.Next(0, totalWordsForOption.Count);
                    }

                    optionWords.Add(totalWordsForOption[optionWordIndex]);
                    optionsChoosedForCurWord.Add(optionWordIndex);
                }

                // insert correct word option
                int insertPos = randGenerator.Next(0, 3);
                optionWords.Insert(insertPos, totalWords[globalIndex]);

                m_wordToOptionMap.Add(((NewWordItem)totalWords[globalIndex]).Name, optionWords);
                alreadyChoosedWordIndice.Add(globalIndex);
            }

            m_curTestWordIndex = 0;
            m_startTime        = DateTime.Now;
            UpdateWordUI();
            return(true);
        }