-1

Good morning, I'm doing a quiz game, has 6 questions each topic. I wanted to make the questions random (without repeater as well) and just give the final grade after I've done the 6 questions.

I tried with Random.Range, but it did not work because sometimes I only ran about 2, 3 or 1 questions and went to note screen soon, I saw some topics about making a list Shuffle also tried but the Shuffle did not appear in my script and much minus the .Count command so I do not know if it would work.

can anybody help me? Well, I have little knowledge and I do not know how to use Shuffle correctly. If they could talk in detail or send the corrected script I would be very grateful. Thanks in advance for your attention and understanding, and I apologize for any spelling mistakes I'm Brazilian and I used google translator to translate that message.

Here's my complete script that I use for questions and answers.

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.SceneManagement;

    public class responder : MonoBehaviour {

    private 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 alternativas A
    public string[] alternativaB;       //armazena todas as alternativas B
    public string[] alternativaC;       //armazena todas as alternativas C
    public string[] alternativaD;       //armazena todas as alternativas D
    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;  /// se fosse 20 questões aqui seria 19
        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);  //calcula a media com base no percentual de acerto
                notaFinal = Mathf.RoundToInt(media); //calcula a nota para o proximo inteiro, segundo a regra da matematica

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

                SceneManager.LoadScene ("notaFinal");


        }
            }

    }
}
Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Rayllander
  • 11
  • 1
  • 2
    Store your questions in a list, when you get the random question remove it from the list and store it in your "asking" list. – AresCaelum May 24 '18 at 13:30
  • Just scroll the list instead, one by one. No true need to remove. – Attersson May 24 '18 at 13:31
  • Please try to break your question down to the problem, nobody has the time to read your source code without English comments. – Tim Rasim May 24 '18 at 13:32

2 Answers2

5

Instead of string[] use List<String> and therefore you can shuffle. Quick and effective.

Attersson
  • 4,755
  • 1
  • 15
  • 29
3

Another possibility is to remove asked questions from the list of available ones:

   private static Random s_Random = new Random();

   // Question: Let's extract question into class
   public static IEnumerable<Question> AskQuestions(IEnumerable<Question> allQuestions) {
     if (null == allQuestions)
       throw new ArgumentNullException("allQuestions");  

     // Copy of all the questions
     List<Question> availableQuestions = allQuestions.ToList(); 

     while (availableQuestions.Any()) {
       int index = s_Random.Next(availableQuestions.Count);

       yield return availableQuestions[index]; // Ask Question

       availableQuestions.RemoveAt(index);     // And Remove It
     }
   }

So you can put

   // Any collection (let it be list) of all possible question
   List<Question> allQuestions = ....

Let's ask N questions:

   int N = 6; // 6 distinct (no repeating) questions

   foreach (Question question in AskQuestions(allQuestions).Take(N)) {
     //TODO: Ask question here, give response etc.
     Console.WriteLine(question.ToString());
   } 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215