1

我创建了一个问答游戏,并将我的问题存储在 XML 文件中(一个用于简单、中等和困难问题的 xml 文件)并创建了一个数组:

        var quiz = XDocument.Load(path);
        _questions = quiz.Descendants("question")
            .Select(q => new Question()
            {
                ID = int.Parse(q.Attribute("id").Value),
                Difficulty = int.Parse(q.Attribute("difficulty").Value),
                QuestionText = q.Element("text").Value,
                Answers = q.Element("answers")
                    .Descendants()
                    .Select(a => a.Value)
                    .ToArray(),
                CorrectAnswer = q.Element("answers")
                    .Descendants()
                    .Select((a, i) => new { node = a.Name, index = i })
                    .First(a => a.node == "correctAnswer").index
            });
    }

我想知道是否有随机化的方法

  1. 显示的问题和
  2. 哪个答案在哪个按钮上。

我问这个是因为,在这一刻,问题以完全相同的顺序显示(遍历数组并按照它们的写入顺序逐字显示它们),并且每次都在同一个按钮上正确答案。我想知道是否有一种方法可以随机化/随机播放这些不会完全破坏我的程序的方法?

编辑:我现在对随机问题部分进行了排序,只需要弄清楚如何随机化答案出现在按钮上的顺序。XML 文件示例:

<question id="2" difficulty="1">
<text></text>
<answers>

</answers>

感谢您的任何帮助和/或回复。

4

1 回答 1

3

从 xml 读取数据时使用随机数排序:

   var rand = new Random();
   var quiz = XDocument.Load(path);
    _questions = quiz.Descendants("question")
        .Select(q => new Question()
        {
            ID = (int)q.Attribute("id"),
            Difficulty = (int)q.Attribute("difficulty"),
            QuestionText = (string)q.Element("text"),
            Answers = q.Element("answers")
                .Descendants()
                .Select(a => (string)a)
                .OrderBy(a => rand.Next()) // randomizing answers
                .ToArray(),
            CorrectAnswer = (string)q.Element("answers")
                .Descendants("correctAnswer")
                .First() // use value instead of index
        })
        .OrderBy(q => rand.Next()); // randomizing questions

您的程序的其余部分将保持原样。


我对您的项目进行了重构。你可以在这里得到结果。你有很多重复的重复代码代码。尝试对应用程序中的任何信息进行单一表示。这将使您的项目易于维护。

关于重构的要点:

  • 使用用户控件将相关控件组合在一起。我提取了与难度选择相关的DifficultySelectionControl控件,以及与显示问题和选择答案相关的控件QuestionControl
  • 更改控件的 Z 顺序的用途BringToFront和方法SendToBack
  • Use events to notify clients of user control about something happened in user control. I created DifficultySelected event for notifying that use selected difficulty. And QuestionAnswered event for notifying whether current question was answered correctly or not.
  • Use custom event arguments to pass additional data to event handlers
  • Use enums. Difficulty.Medium is much more readable than 2.
  • If you have similar methods in your code, then try to extract common logic into one method. Take a look on handler of question buttons clicks, which I used instead four different handlers in your code.
  • Use descriptive names of controls instead of label1 or button4.

There still places for improving. E.g. I see duplication in DifficultySelectionControl - all handlers look similar. Also there is duplication of button background colors. You should have single place where color is defined for different states of buttons. Also there is ugly hack for filtering by difficulty (I casted enum to int). And I'd moved answers randomizing into QuestionControl.

于 2013-03-10T14:09:54.353 回答