QuestionManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. public class QuestionManager : MonoBehaviour
  5. {
  6. private List<QuestionData> allQuestions;
  7. private List<QuestionData> currentQuestions;
  8. private int currentQuestionIndex = 0;
  9. void Start()
  10. {
  11. LoadQuestions();
  12. ShuffleQuestions();
  13. GenerateWrongAnswers();
  14. }
  15. void LoadQuestions()
  16. {
  17. Debug.Log("开始加载题目");
  18. TextAsset jsonFile = Resources.Load<TextAsset>("multiplication_table");
  19. if (jsonFile != null)
  20. {
  21. Debug.Log("JSON文件加载成功:" + jsonFile.text);
  22. // 直接将 JSON 数组包装在一个对象中
  23. QuestionDataList questionList = JsonUtility.FromJson<QuestionDataList>("{\"questions\":" + jsonFile.text + "}");
  24. if (questionList != null && questionList.questions != null && questionList.questions.Length > 0)
  25. {
  26. allQuestions = new List<QuestionData>(questionList.questions);
  27. currentQuestions = new List<QuestionData>(allQuestions);
  28. Debug.Log($"成功加载 {currentQuestions.Count} 道题目");
  29. // 打印第一道题目以验证
  30. if (currentQuestions.Count > 0)
  31. {
  32. var firstQ = currentQuestions[0];
  33. Debug.Log($"第一道题目: {firstQ.multiplier1} × {firstQ.multiplier2} = {firstQ.result}");
  34. }
  35. }
  36. else
  37. {
  38. Debug.LogError("JSON解析失败!questionList 为空或没有题目");
  39. }
  40. }
  41. else
  42. {
  43. Debug.LogError("无法加载题目文件!请确保 multiplication_table.json 文件位于 Resources 文件夹中!");
  44. }
  45. }
  46. void GenerateWrongAnswers()
  47. {
  48. foreach (var question in currentQuestions)
  49. {
  50. // 生成一个错误答案,确保与正确答案不同
  51. do
  52. {
  53. // 在正确答案的±5范围内生成错误答案
  54. int offset = Random.Range(-5, 6);
  55. question.wrongAnswer = question.result + offset;
  56. } while (question.wrongAnswer == question.result);
  57. // 随机决定正确答案放在A还是B选项
  58. question.isAnswerA = Random.value > 0.5f;
  59. }
  60. }
  61. void ShuffleQuestions()
  62. {
  63. if (currentQuestions != null)
  64. {
  65. int n = currentQuestions.Count;
  66. while (n > 1)
  67. {
  68. n--;
  69. int k = Random.Range(0, n + 1);
  70. QuestionData temp = currentQuestions[k];
  71. currentQuestions[k] = currentQuestions[n];
  72. currentQuestions[n] = temp;
  73. }
  74. }
  75. }
  76. public QuestionData GetCurrentQuestion()
  77. {
  78. if (currentQuestions != null && currentQuestionIndex < currentQuestions.Count)
  79. {
  80. return currentQuestions[currentQuestionIndex];
  81. }
  82. return null;
  83. }
  84. public bool CheckAnswer(bool selectedA)
  85. {
  86. QuestionData currentQuestion = GetCurrentQuestion();
  87. if (currentQuestion != null)
  88. {
  89. return selectedA == currentQuestion.isAnswerA;
  90. }
  91. return false;
  92. }
  93. public void NextQuestion()
  94. {
  95. currentQuestionIndex++;
  96. if (currentQuestionIndex >= currentQuestions.Count)
  97. {
  98. // 所有题目都已完成,重新开始
  99. currentQuestionIndex = 0;
  100. ShuffleQuestions();
  101. GenerateWrongAnswers();
  102. }
  103. }
  104. public float GetProgress()
  105. {
  106. return (float)currentQuestionIndex / currentQuestions.Count;
  107. }
  108. }