0
我正在做一個測驗/瑣事遊戲,並且存儲在XML文件中的問題,它會得到一個隨機問題。一切正常,但我想保持隨機,但不顯示相同的問題,直到每個問題已被顯示。使用Fisher-Yates隨機XML問題洗牌
public struct Question {
public string questionText;
public string answerA;
public string answerB;
public string answerC;
public string answerD;
public int correctAnswerID;
}
[XmlRoot("QuestionsRoot")]
public class QuestionData {
[XmlArray("Questions")]
[XmlArrayItem("Question")]
public List<Question>questions = new List<Question>();
public static QuestionData LoadFromText(string text) {
try {
XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));
return serializer.Deserialize(new StringReader(text)) as QuestionData;
} catch (Exception e) {
UnityEngine.Debug.LogError("Exception loading question data: " + e);
return null;
}
}
而且我用這個在我的其他腳本來得到一個隨機問題:
questionData = QuestionData.LoadFromText(questionDataXMLFile.text)
q = Random.Range(0, questionData.questions.Count);
currentQuestion = questionData.questions[q];
如果你問如何實現fisher-yates,有很多例子在這個網站上,和維基百科。如果您已經嘗試過實施它,我們需要查看該代碼,並且您需要解釋它有什麼問題。 – Blorgbeard
@Blorgbeard我試圖與 \t對(INT I = 1;我
IvayloDev
所以使用'Question x'而不是'string x'。 – Blorgbeard