Currently I'm making a small game of questions and answers in which, to find out what is the correct answer I have thought about using a dictionary, to check with a key and a 'if' statement the correct answer. The code is the following, but basically I take the answers from a SQLite database, then I put them in a dictionary, which is converted into a list, I shuffled it and put each component of the list in a text to show them in differents UI buttons. Here it is:
string A_p = reader.GetString(3);
string B_p = reader.GetString(4);
string C_p = reader.GetString(5);
string D_p = reader.GetString(6);
Dictionary<string, string> dictionaryAnswers = new Dictionary<string, string>();
dictionaryAnswers[A_p] = "C";
dictionaryAnswers[B_p] = "W";
dictionaryAnswers[C_p] = "W";
dictionaryAnswers[D_p] = "W";
listAnswers = dictionaryAnswers.Keys.ToList();
var randomizer = new System.Random();
for (int i = 0; i < dictionaryAnswers.Count / 2; i++)
{
var randNum = randomizer.Next(i, listAnswers.Count);
var temp = listAnswers[randNum];
listAnswers[randNum] = listAnswers[i];
listAnswers[i] = temp;
}
string A = listAnswers.First();
listAnswers.Remove(A);
string B = listAnswers.First();
listAnswers.Remove(B);
string C = listAnswers.First();
listAnswers.Remove(C);
string D = listAnswers.First();
listAnswers.Remove(D);
QuestionText.text = question;
AnswerAText.text = A;
AnswerBText.text = B;
AnswerCText.text = C;
AnswerDText.text = D;
If the code is not undestandable, please let me know. The thing is, I don't know how to check what of this strings (A, B, C, D) contains the key that I am looking for. Can someone, please, guide me on how to do what I intend. Thank you.