簡體   English   中英

Unity 3D Quiz游戲隨機問題

[英]Unity 3D Quiz game random questions

所以我有10個問題,因此當游戲開始時,示例開始的問題是“ 10個問題中的4個 ”,如果下一個問題隨機出現到“ 10個問題中的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");
    }
}
}

更改您的數據結構,創建一個代表問題和可能答案的類,以便您使用一個數組而不是6。

一旦這樣做,您就可以在開始提問之前, 將列表隨機排序,然后以新的隨機順序瀏覽列表。

[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");
    }
}

如果您確實不想更改數據結構,那么這是我在有關創建映射數組的注釋中提到的另一個選項。

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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM