- Home /
How do I find if a string from list was used and exclude it from further use.
I am making a quiz game in Unity 2d and it's almost complete (excluding all the questions, but that's not important). All my questions and answers are in a List, they are in "questions" List and "correctAnswer" List. The questions are randomized and i want to make it that if a question is picked by random at least once, then it won't be used again, meaning it won't be used by the randomizer.
Sorry if i used something incorrectly, i'm a beginner. What is in questions is not important it's in another language. P.S. I am looking for how to make random range not to repeat.
List<string> questions = new List<string>() { "Pasaules augstākais kalns?", "Kas ir Vācijas galvaspilsēta?", "Kur atrodas ASV?", "Geo jautājums 4", "jt", "Geo jautājums 5", "Cik ir 2+2?", "Cik ir 12+1?", "Cik ir 9+10?", "Kvadrātsakne no 49", "2+2=5?" };
List<string> correctAnswer = new List<string>() { "3", "1", "2", "3", "1", "2", "2", "3", "1", "1" };
public static string selectedAnswer;
public Transform resultObj;
public static string choiceSelected = "n";
public static int randQuestion = -1;
public int catMod = 0;
public Transform auraObj;
public float totalCorrect = 0;
public float totalQuestions = 0;
public Transform scoreObj;
public float scorePer;
public Transform nextButton;
public Transform firstText;
public Transform secondText;
public Transform thirdText;
public Transform firstChild;
public Transform secondChild;
public Transform thirdChild;
void Start()
{
if (selectCat.catTopic == "Matemātika")
{
catMod = 5;
}
if (selectCat.catTopic == "Ģeogrāfija")
{
catMod = 0;
}
}
void FixedUpdate()
{
//scoreObj.GetComponent<TextMesh>().text = "Score : " +totalCorrect; nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
if (totalQuestions > 0)
{
scorePer = (totalCorrect / totalQuestions) * 100;
}
scoreObj.GetComponent<TextMesh>().text = "Score : " + scorePer+ "%";
if (randQuestion == -1)
{
randQuestion = Random.Range(0 + catMod, 5 + catMod);
nextButton.GetComponent<Transform>().position = new Vector3(49.1f, 8.62f, 0);
firstText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
secondText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
}
if (randQuestion > -1)
{
GetComponent<TextMesh>().text = questions[randQuestion];
}
//Debug.Log(questions[randQuestion]);
if (choiceSelected == "y")
{
choiceSelected = "n";
totalQuestions += 1;
if (correctAnswer[randQuestion] == selectedAnswer)
{
resultObj.GetComponent<TextMesh>().text = "Pareizi! Ejiet pa labi lai turpinātu";
totalCorrect += 1;
nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
firstText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
secondText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
}
else
{
NextButton.resetAura = "n";
resultObj.GetComponent<TextMesh>().text = "Nepareizi!";
if (correctAnswer[randQuestion] == "1")
{
auraObj.GetComponent<Transform> ().position = new Vector3(-3.4f, 24.2f, 0);
}
if (correctAnswer[randQuestion] == "2")
{
auraObj.GetComponent<Transform>().position = new Vector3(9.9f, 24.2f, 0);
}
if (correctAnswer[randQuestion] == "3")
{
auraObj.GetComponent<Transform>().position = new Vector3(22.9f, 24.2f, 0);
}
nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
firstText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
secondText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
}
}
}
}
Answer by elenzil · Jan 31, 2017 at 09:07 PM
i'd suggest making a copy of the list, call it "unusedQuestions" or something, and when you choose an item from it at random, remove that item from the list.
You could do the above mention and that would work or if you really care about memory (which you probably won't need to worry about..) you could do the following:
You could keep track of how many questions you have with an int that would be used in the random number generation. Everytime a question is picked, swap that one with the one in the last position of the list and decrease the limit of the int used in the random number generator by one.
If you have further questions about this, reply back and I can go more into detail.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Update List<> in editor 0 Answers
Is it possible to name a list by a string variable? 2 Answers
Randomize a spawn position without repition 3 Answers
How do I make a list of lists? 2 Answers