How can I put these random questions?

Asked

Viewed 396 times

1

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

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;
    public string[] alternativaA;
    public string[] alternativaB;
    public string[] alternativaC;
    public string[] alternativaD;
    public string[] 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 = Mathf.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);
            Application.LoadLevel ("nota");
        }

    }
}
  • 5

    Where do you want to do this? Do you have any criteria? What are the questions? The question is not at all clear.

  • Hello David. Edit the question to explain your code. Explain what the questions are, what you want to do, and most importantly, where exactly is your difficulty (for example, don’t you know how to generate a random number in C#? or don’t know how to ensure that it is between 0 and the maximum of an array/list of questions? etc).

1 answer

2

I don’t know if this will be useful to you. But I show below an example of how to leave a random collection (List,Array).

Call of the Method

public void RandomizarMinhaLista()
{
    List<string> foo = new List<string>();
    foo.Add("foo1");
    foo.Add("foo2");
    foo.Add("foo3");

    var fooRandomizado = foo.Randomize();
    //Ou
    var fooRandomizado2 = RandomizeArray.Randomize(foo);
}

Static class

public static class RandomizeArray
{
    private static Random _random = new Random();
    public static IEnumerable<TModel> Randomize<TModel>(this IEnumerable<TModel> collection)
    {
        List<KeyValuePair<int, TModel>> list = new List<KeyValuePair<int, TModel>>();

        // Insere todos os items da coleção em uma nova lista.
        foreach (TModel s in collection)
        {
            list.Add(new KeyValuePair<int, TModel>(_random.Next(), s));
        }
        // Randomiza a lista
        var sorted = from item in list
                        orderby item.Key
                        select item;

        // Aloca um array de TModel
        TModel[] result = new TModel[collection.ToArray().Length];
        // Copia os valores para o array.
        int index = 0;
        foreach (KeyValuePair<int, TModel> pair in sorted)
        {
            result[index] = pair.Value;
            index++;
        }
        // Retorna array copiado.
        return result;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.