private void ConfirmAnswer() { var history = _wordUserHistories.FirstOrDefault(x => x.Language == SelectedDictionaryQuestion.Language && x.Word == CurrentQuestionWord.Text && x.UserId == _currentUser.Id); if (history == null) { history = _dao.CreateWordUserHistory(); history.Language = SelectedDictionaryQuestion.Language; history.Word = CurrentQuestionWord.Text; history.UserId = _currentUser.Id; history.TimesEncountered = 0; history.TimesRight = 0; } if (_correctAnswers.Contains(SelectedAnswerWord)) { CorrectAnswers = _answeredCorrectly + 1; AnswerResultMessage = "Correct!\n"; history.TimesRight++; } else { AnswerResultMessage = "Wrong!\n"; foreach (var word in _correctAnswers) { if (CurrentAnswerWords.Contains(word)) { AnswerResultMessage += "Correct answer: '" + word.Text + "'.\n"; break; } } } if (_currentQuestionSet.Count == 0) { AnswerResultMessage += "No more questions!"; } Tries = _tries + 1; history.TimesEncountered++; _dao.SaveWordUserHistory(history); _answeredThisQuestion = true; }
private void NextQuestion() { AnswerResultMessage = ""; CurrentQuestionWord = _currentQuestionSet.First(); _currentQuestionSet.Remove(CurrentQuestionWord); _correctAnswers = SelectedDictionaryAnswer.Words.Where(x => AreWordsConnected(x, SelectedDictionaryAnswer.Language, CurrentQuestionWord, SelectedDictionaryQuestion.Language)); CurrentAnswerWords.Clear(); int n = _answersPerQuestion; bool insertedCorrectAnswer = false; while (n > 0) { if ((!insertedCorrectAnswer && _random.Next(_answersPerQuestion) == 0) || (!insertedCorrectAnswer && n == 1)) { CurrentAnswerWords.Add(_correctAnswers.ElementAt(_random.Next(_correctAnswers.Count()))); insertedCorrectAnswer = true; n--; } else { IWord word; do { word = SelectedDictionaryAnswer.Words.ElementAt(_random.Next(SelectedDictionaryAnswer.Words.Count)); } while (CurrentAnswerWords.Contains(word)); if (_correctAnswers.Contains(word)) { insertedCorrectAnswer = true; } CurrentAnswerWords.Add(word); n--; } } SelectedAnswerWord = null; _answeredThisQuestion = false; }
public void OnEntry() { Tries = 0; CorrectAnswers = 0; AnswersPerQuestion = 4; QuestionsPerSet = 2; SelectedDictionaryAnswer = null; SelectedDictionaryQuestion = null; CurrentQuestionWord = null; CurrentAnswerWords.Clear(); _dictionaries = _dao.GetAllDictionaries(); _dictionariesQuestion = new CollectionViewSource() { Source = _dictionaries }.View; _dictionariesAnswer = new CollectionViewSource() { Source = _dictionaries }.View; _wordConnections = _dao.GetAllWordConnections(); _wordUserHistories = _dao.GetAllHistoriesForUser(_currentUser); _availableQuestions = new List <IWord>(); _currentQuestionSet = new List <IWord>(); }