You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameRoot : MonoBehaviour
{
public static GameRoot Instance = null;
//记录答案字典
public Dictionary<int, Dictionary<int, int>> _answerDict = new Dictionary<int, Dictionary<int, int>>();
private void Awake()
{
DontDestroyOnLoad(this);
Instance = this;
}
// Start is called before the first frame update
void Start()
{
SubmitAnswer(1,1,1);
SubmitAnswer(1, 2, 2);
SubmitAnswer(2, 1, 2);
CheckAnswer();
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 公共提交答案函数
/// PassIndex:关卡索引
/// QuestionIndex:问题索引
/// answer:记录用户所选的答案(ABCD对应0123)
/// </summary>
/// <param name="PassIndex">关卡索引</param>
/// <param name="QuestionIndex">问题索引</param>
/// <param name="answer">记录用户所选的答案(ABCD对应0123)</param>
public static void SubmitAnswer(int PassIndex,int QuestionIndex,int answer)
{
if (GameRoot.Instance == null) return;
//如果不存在则创建并放入字典
if (!GameRoot.Instance._answerDict.ContainsKey(PassIndex))
{
Dictionary<int, int> dict = new Dictionary<int, int>();
dict.Add(QuestionIndex, answer);
GameRoot.Instance._answerDict.Add(PassIndex, dict);
Debug.Log("记录第" + PassIndex + "关卡" + QuestionIndex + "的问题答案为" + answer);
}
else
{
//继续记录
Dictionary<int, int> dict = new Dictionary<int, int>();
if (GameRoot.Instance._answerDict.TryGetValue(PassIndex, out dict))
{
dict.Add(QuestionIndex, answer);
GameRoot.Instance._answerDict[PassIndex] = dict;
Debug.Log("关卡字典已存在,更新" + QuestionIndex + "问题的答案为" + answer);
}
}
}
public void CheckAnswer()
{
int passCount = _answerDict.Keys.Count;
Debug.Log("总关卡:"+passCount);
for (int i = 0; i < passCount+1; i++)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
if(_answerDict.TryGetValue(i, out dict))
{
int questionCount = dict.Keys.Count;
// Debug.Log("第" + i + "关有" + questionCount + "道题");
for (int j = 0; j < questionCount+1; j++)
{
if (dict.ContainsKey(j))
{
Debug.Log("第"+i+"关第" + j + "题用户作答为" + dict[j]);
}
}
}
}
}
}