【问题标题】:Unity 3D Quiz game random questionsUnity 3D问答游戏随机问题
【发布时间】:2018-09-12 09:00:56
【问题描述】:

所以我有 10 个问题,所以当游戏开始时,例如开始问题是“4 out of 10”,那么如果下一个问题是随机的,则为 “10 out of 10” 游戏结束。我想要的是随机抽取 10 个问题:

private int idMode;
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text infoAnswer;
public Text stat;
public string[] questions;          
public string[] alternativeA;   
public string[] alternativeB;
public string[] alternativeC;
public string[] alternativeD;
public string[] correct;
private int idQuestion; 
private float points;
private float fact; 
private float average;
private int results;

void Start () {
    idMode = PlayerPrefs.GetInt ("idMode");
    idQuestion = 0;
    fact = questions.Length;
    question.text = questions [idQuestion];
    answerA.text = alternativeA [idQuestion];
    answerB.text = alternativeB [idQuestion];
    answerC.text = alternativeC [idQuestion];
    answerD.text = alternativeD [idQuestion];
    infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
public void answer(string alternative)
{
    if (alternative == "A") {

        if (alternativeA [idQuestion] == correct [idQuestion]) {

            points += 1;

        } else {

        }
    }
    if (alternative == "B") {

        if (alternativeB [idQuestion] == correct [idQuestion]) {

            points += 1;
        } else {
    }

}
    if (alternative == "C") {
        if (alternativeC [idQuestion] == correct [idQuestion]) {
            points += 1;
        } else {
    }
}

    if (alternative == "D") {
        if (alternativeD [idQuestion] == correct [idQuestion]) {
            points += 1;
        } else {
    }
}
    nextQuestion ();
} 
void nextQuestion()
{
    idQuestion += Random.Range(0,10);
    if(idQuestion <= (fact-1))
    {
        question.text = questions [idQuestion];
        answerA.text = alternativeA [idQuestion];
        answerB.text = alternativeB [idQuestion];
        answerC.text = alternativeC [idQuestion];
        answerD.text = alternativeD [idQuestion];
        stat.text = " Correct: " + points.ToString () + "";
        infoAnswer.text =  (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
    }
    else
    {
        average = 10 * (points / fact);
        results = Mathf.RoundToInt (average);
        if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
            PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
            PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
        }
        PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
        PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
        Application.LoadLevel("results");
    }
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    改变你的数据结构,创建一个代表问题和可能答案的类,这样你就有一个数组而不是 6 个。

    在您开始提问shuffle the list 之前完成此操作后,只需按照新的随机顺序浏览列表即可。

    [Serializeable] 
    public class Question
    {
        public string Text;
        public string A;
        public string B;
        public string C;
        public string D;
        public string CorrectChoice; //Holds "A", "B", "C", or "D"
    }
    
    public static class RandomExtensions
    {
        public static void Shuffle<T> (this T[] array)
        {
            int n = array.Length;
            while (n > 1) 
            {
                int k = Random.Range(0, n--);
                T temp = array[n];
                array[n] = array[k];
                array[k] = temp;
            }
        }
    }
    

    然后将代码更改为

    private int idMode;
    public Text question;
    public Text answerA;
    public Text answerB;
    public Text answerC;
    public Text answerD;
    public Text infoAnswer;
    public Text stat;
    public Question[] questions;
    private int idQuestion; 
    private float points;
    private float fact; 
    private float average;
    private int results;
    
    void Start () {
        idMode = PlayerPrefs.GetInt ("idMode");
        idQuestion = 0;
        fact = questions.Length;
        questions.Shuffle();
        question.text = questions[idQuestion].Text;
        answerA.text = questions[idQuestion].A;
        answerB.text = questions[idQuestion].B;
        answerC.text = questions[idQuestion].C;
        answerD.text = questions[idQuestion].D;
        infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
    }
    public void answer(string alternative)
    {
        if (alternative == questions[idQuestion].CorrectChoice) 
        {
            points += 1;
        }
    
        nextQuestion ();
    } 
    void nextQuestion()
    {
        idQuestion += Random.Range(0,10);
        if(idQuestion <= (fact-1))
        {
            question.text = questions[idQuestion].Text;
            answerA.text = questions[idQuestion].A;
            answerB.text = questions[idQuestion].B;
            answerC.text = questions[idQuestion].C;
            answerD.text = questions[idQuestion].D;
            stat.text = " Correct: " + points.ToString () + "";
            infoAnswer.text =  (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
        }
        else
        {
            average = 10 * (points / fact);
            results = Mathf.RoundToInt (average);
            if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
                PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
                PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
            }
            PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
            PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
            Application.LoadLevel("results");
        }
    }
    

    如果您真的不想更改数据结构,这是我在 cmets 中提到的关于创建映射数组的另一个选项。

    private int idMode;
    public Text question;
    public Text answerA;
    public Text answerB;
    public Text answerC;
    public Text answerD;
    public Text infoAnswer;
    public Text stat;
    public string[] questions;          
    public string[] alternativeA;   
    public string[] alternativeB;
    public string[] alternativeC;
    public string[] alternativeD;
    public string[] correct;
    private int idQuestion; 
    private float points;
    private float fact; 
    private float average;
    private int results;
    private int[] questionMapper;
    
    void Start () {
        idMode = PlayerPrefs.GetInt ("idMode");
        idQuestion = 0;
        fact = questions.Length;
        questionMapper = new int[questions.Count];
        for(int i = 0; i < questionMapper.Count; i++)
        {
            questionMapper[i] = i;
        }
        questionMapper.Shuffle();
        question.text = questions [questionMapper[idQuestion]];
        answerA.text = alternativeA [questionMapper[idQuestion]];
        answerB.text = alternativeB [questionMapper[idQuestion]];
        answerC.text = alternativeC [questionMapper[idQuestion]];
        answerD.text = alternativeD [questionMapper[idQuestion]];
        infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
    }
    
    //...
    
    void nextQuestion()
    {
        idQuestion += Random.Range(0,10);
        if(idQuestion <= (fact-1))
        {
            question.text = questions [questionMapper[idQuestion]];
            answerA.text = alternativeA [questionMapper[idQuestion]];
            answerB.text = alternativeB [questionMapper[idQuestion]];
            answerC.text = alternativeC [questionMapper[idQuestion]];
            answerD.text = alternativeD [questionMapper[idQuestion]];
            stat.text = " Correct: " + points.ToString () + "";
            infoAnswer.text =  (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
        }
        else
        {
            average = 10 * (points / fact);
            results = Mathf.RoundToInt (average);
            if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
                PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
                PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
            }
            PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
            PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
            Application.LoadLevel("results");
        }
    }
    

    【讨论】:

    • 谢谢,但我真的需要改变我的数据结构吗?没有其他方法?
    • 还有其他方法,但最简单的维护方法是更改​​数据结构。另一种选择是创建一个由ints 组成的数组,从0 开始计数,称为questionMapper,然后对其进行洗牌。然后你做类似questions[questionMapper[idQuestion]]的事情。
    • 您应该尝试新的数据结构,您仍然应该能够将文本放入编辑器。另请参阅我的最新更新,我忘记将public 放在Question 类中的成员上。
    • @Ixion 我再次更新了问题,展示了如何执行questionMapper 解决方案。您仍然需要使用我在第一部分中展示的Shuffle() 扩展方法。
    • 非常感谢,但我在扩展时遇到错误“错误 CS1109: `GameManager.RandomExtensions.Shuffle(this T[])': Extension methods cannot be defined in an nested class”我把你的 shuffle 扩展放在我的代码上面
    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2022-10-13
    • 1970-01-01
    • 2015-09-18
    • 2021-07-24
    • 2017-07-16
    相关资源
    最近更新 更多