2016-10-03 5 views
0

Так я получил 10 вопросов, поэтому, когда начинается игра, например, исходный вопрос: «4 из 10», то, если следующий вопрос случайным «10 из 10» игра заканчивается. то, что я хочу, чтобы случайно 10 вопросов:Unity 3D Quiz игра Случайные вопросы

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

ответ

2

Изменение структуры данных, сделать класс, который представляет вопрос и возможные ответы, так что вы имеете один массив вместо 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"); 
    } 
} 

Если вы действительно не хотите, чтобы изменить структуру данных, это другой вариант, я уже упоминал в комментариях о создании массива отображения.

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

спасибо, но мне действительно нужно изменить свою структуру данных? нет других путей? – Magillanica

+1

Существуют и другие способы, но самым простым способом его сохранения было бы изменить структуру данных. Другой вариант - создать массив 'int', отсчитывающий от 0, называемый 'questionMapper', и перетасовать его. Затем вы делаете что-то вроде 'questions [questionMapper [idQuestion]]'. –

+0

Вы должны попробовать новую структуру данных, хотя вы все равно сможете поместить текст в редактор. Также см. Мое последнее обновление, я забыл поставить 'public' в члены класса' Question'. –

Смежные вопросы