I'm doing a quiz game I need help. How can I make an array (called random questions) to exit and return to the same are not repeated.
Why didnt you post your code normal? What should the screenshots add?
Sou novo primeira vez que posto uma perguntas de ajuda, como faço o procedimento, por favor.
I dont speak portugees, I'm from germany. English is the worlds language for a reason.
When posting code, do not post screen shots people can not help much that way. Please format ALL you code properly and paste the code directly into the body of the question. To do that copy the code to your clipboard and press the "101010" button in the toolbar of the UA text editor. This will format the code so it can be easily read in the forum.
using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; using UnityEngine.UI; using System.Collections;
public class responder : $$anonymous$$onoBehaviour {
public int idTema;
public Text pergunta;
public Text respostaA;
public Text respostaB;
public Text respostaC;
public Text respostaD;
public Text infoRespostas;
public string[] perguntas; // armazena todas as perguntas.
public string[] alternativaA; // armazena todas as perguntasA.
public string[] alternativaB; // armazena todas as perguntasB.
public string[] alternativaC; // armazena todas as perguntasC.
public string[] alternativaD; // armazena todas as perguntasD.
public string[] corretas; // armazena todas as alternativas corretas.
private int idPergunta;
private float acertos;
private float questoes;
private float media;
private int notaFinal;
// Use this for initialization
void Start () {
idTema = PlayerPrefs.GetInt ("idTema");
idPergunta = 0;
questoes = perguntas.Length;
pergunta.text = perguntas[idPergunta];
respostaA.text = alternativaA[idPergunta];
respostaB.text = alternativaB[idPergunta];
respostaC.text = alternativaC[idPergunta];
respostaD.text = alternativaD[idPergunta];
infoRespostas.text = " Respondendo " + (idPergunta + 1).ToString () + " de " + questoes.ToString () + " perguntas! ";
}
public void resposta(string alternativa)
{
if(alternativa == "A")
{
if (alternativaA[idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if(alternativa == "B")
{
if (alternativaB [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if(alternativa == "C")
{
if (alternativaC [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if(alternativa == "D")
{
if (alternativaD [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
proximaPergunta();
}
void proximaPergunta()
{
idPergunta += 1;
if (idPergunta <= (questoes - 1))
{
pergunta.text = perguntas [idPergunta];
respostaA.text = alternativaA [idPergunta];
respostaB.text = alternativaB [idPergunta];
respostaC.text = alternativaC [idPergunta];
respostaD.text = alternativaD [idPergunta];
infoRespostas.text = " respondendo " + (idPergunta + 1).ToString () + " de " + questoes.ToString () + " perguntas! ";
}
else
{
media = 10 * (acertos / questoes);
notaFinal = $$anonymous$$athf.RoundToInt (media);
if (notaFinal > PlayerPrefs.GetInt ("notaFinal" + idTema.ToString ())) {
PlayerPrefs.SetInt ("notaFinal" + idTema.ToString (), notaFinal);
PlayerPrefs.SetInt ("acertos" + idTema.ToString (), (int)acertos);
}
PlayerPrefs.SetInt ("notaFinalTemp" + idTema.ToString (), notaFinal);
PlayerPrefs.SetInt ("acertosTemp" + idTema.ToString (), (int)acertos);
Scene$$anonymous$$anager.LoadScene ("notaFinal" + idTema.ToString ());
}
}
}
Answer by Oribow · May 08, 2016 at 01:11 AM
You could shuffel your array before hand. Just take you array and randomly swap two elements for a while. So that the elements in the array are already in a random order to begin with. Then just take the first, second, third ... until you reach the end of the array. In that way you wont get duplicats and dont have to remember which elements you already used.
example:
{1,2,3,4}
swap [2] and [0]
swap [3] and [2] ... // as often as you want, with random indcies
{4,1,3,2} //result of swaping x times
return [0] -> 4
return [1] -> 1
return [2] -> 3
return [3] -> 2
completly random and without duplicats
Your answer
Follow this Question
Related Questions
Keep track of last audio clip played from multiple arrays 1 Answer
Problems with arrays (or how to rewind objects' states) 0 Answers
C# ArrayList Accessing and RemoveAt? 0 Answers
Comparing an ArrayList to the current object in an Arraylist being checked through a forEach Loop? 0 Answers
Why can't I assign a value to the array in the class "Player"? 0 Answers