/// <summary> /// 解答候補の取得 /// </summary> /// <param name="mondaiRow"></param> /// <param name="lvl"></param> /// <param name="type"></param> /// <returns></returns> internal string[] GetAnswerTexts(DataRow mondaiRow, string lvl, string type, string mode, ref DataTable answersTable) { // 解答と候補の取得 string answer = mondaiRow["answer"] as string; string choice1 = mondaiRow["choice1"] as string; string choice2 = mondaiRow["choice2"] as string; string choice3 = mondaiRow["choice3"] as string; if (string.IsNullOrEmpty(choice1) || string.IsNullOrEmpty(choice2) || string.IsNullOrEmpty(choice3)) { if (answersTable == null || answersTable.Rows.Count <= 10) { // いずれかの解答が未設定の場合 or 候補が10件以下の場合 // 解答候補を取得 YontakuSqlManager manager = new YontakuSqlManager(); answersTable = manager.GetAnswers(lvl, type, mode); } // ランダム解答候補を並べ替え DataRow[] rows = answersTable.AsEnumerable() .Where(x => !x.Field <string>("keyword").Equals(answer) && !x.Field <string>("keyword").Equals(choice1) && !x.Field <string>("keyword").Equals(choice2) && !x.Field <string>("keyword").Equals(choice3)) .OrderBy(i => Guid.NewGuid()).ToArray(); if (string.IsNullOrEmpty(choice1)) { choice1 = rows[0]["keyword"] as string; answersTable.Rows.Remove(rows[0]); } if (string.IsNullOrEmpty(choice2)) { choice2 = rows[1]["keyword"] as string; answersTable.Rows.Remove(rows[1]); } if (string.IsNullOrEmpty(choice3)) { choice3 = rows[2]["keyword"] as string; answersTable.Rows.Remove(rows[2]); } // Removeを確定 answersTable.AcceptChanges(); } // 並べ替え用に一旦リストに格納 List <string> answers = new List <string> { answer, choice1, choice2, choice3 }; // ランダムに並べ替えて返却 return(answers.OrderBy(i => Guid.NewGuid()).ToArray()); }
/// <summary> /// 問題の設定 /// </summary> /// <param name="lvl"></param> /// <param name="type"></param> private void SetMondai(string lvl, string type, string mode = null) { logger.StartMethod(MethodBase.GetCurrentMethod().Name); // 問題の取得 YontakuLogic logic = new YontakuLogic(); YontakuSqlManager manager = new YontakuSqlManager(); if (!(Session["table"] is DataTable mondaiTable) || mondaiTable.Rows.Count == 0) { // 初回 or 全問出題完了時は取得 mondaiTable = manager.GetMondai(lvl, type, mode); } // ランダムに問題を取得 DataRow mondaiRow = logic.GetRandomRow(mondaiTable); // 問題をセッションに保持 Session["table"] = mondaiTable; Session["mondai"] = mondaiRow; // タイトルの設定 if (string.IsNullOrEmpty(LabelQuestionTitle.Text)) { // 初回 LabelQuestionTitle.Text = "もんだい1"; } else { // 2回目以降 int.TryParse(LabelQuestionTitle.Text.Substring(4), out int count); LabelQuestionTitle.Text = $"もんだい{count + 1}"; } // 問題の設定 LabelQuestion.Text = logic.GetQuestionText(mondaiRow); // セッションから取得 DataTable answersTable = Session["answersTable"] as DataTable; // ランダムに解答を設定 string[] answers = logic.GetAnswerTexts(mondaiRow, lvl, type, mode, ref answersTable); Button1.Text = answers[0]; Button2.Text = answers[1]; Button3.Text = answers[2]; Button4.Text = answers[3]; // セッションに保持 Session["answersTable"] = answersTable; Button3.Visible = true; Button4.Visible = true; logger.EndMethod(MethodBase.GetCurrentMethod().Name); }