[英]Quiz Game - How to ask 6 random questions without repeating?
Good morning, I'm doing a quiz game, has 6 questions each topic. 早上好,我在做问答游戏,每个主题有6个问题。 I wanted to make the questions random (without repeater as well) and just give the final grade after I've done the 6 questions. 我想将问题随机化(也不需要中继器),并在完成6个问题后才给出最终成绩。
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. 我尝试使用Random.Range,但是它没有用,因为有时我只跑了大约2、3或1个问题,很快便进入了笔记屏幕,我看到了一些有关制作列表的主题,Shuffle也曾尝试过,但是Shuffle并未出现在我的脚本和.Count命令减去很多,所以我不知道它是否有效。
can anybody help me? 有谁能够帮助我? Well, I have little knowledge and I do not know how to use Shuffle correctly. 好吧,我所掌握的知识很少,我也不知道如何正确使用Shuffle。 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");
}
}
}
}
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: 让我们问N
问题:
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.