- Home /
Creating Q&A Quiz game using Dictionaries as the database
I am making a quiz game and I have decided to use Dictionaries to store Q&A. I am making some progress but I have some issues which I will now outline .
you just need to attach this script to a gameobject .
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class testQuestions : MonoBehaviour {
I start be declaring some global variables and creating the dictionary and adding content to the dictionary in my start function .
Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
string []vl ;
string ky;
//int Qnum;
string A;
int indx;
// Use this for initialization
void Start () {
dictionary.Add("ups", new string[] {"updegree", "popup"});
dictionary.Add("down", new string[] {"sun", "bun"});
dictionary.Add("left", new string[] {"higi", "migi"});
dictionary.Add("right", new string[] {"een", "yyo"});
}
I then use up my update function to check if a button is pressed and to run a function which generates a random key from the dictionary
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("q"))
{
GenerateRandoms();
}
}
then i create the function where a random key is selected
strng[] keys = new string[dictionary.Count];//get the dictionary count and store in array
dictionary.Keys.CopyTo(keys, 0);
var index = Random.Range(0, keys.Length);
var key = keys[index];
var value = dictionary[key];
ky= key;
vl = value;
foreach (string ku in value)
{
// create buttons with the ku text as the button text
indx = index;
//Qnum +=1;
A = ku;
}
//---------- remove each after answer button click
//dictionary.Remove(key); // remove key after it is already displayed. this must only be exicuted at the end of the function
}
then i create the GUI function where i should have buttons dynamically created to match the number of values in the randomly selected key with each button text being a valur from that dictionary key
void OnGUI ()
{
// here dynamically added butons must be added
GUI.TextArea(new Rect (0,0,500,20),"Key is " + ky + " value is " + string.Join(",", vl) + " " +int.Parse(indx.ToString()));
if (GUI.Button (new Rect(0, 40, 100, 20),A))
{
if (dictionary.Keys.ElementAt(dictionary.Count - indx).Contains(A))
{
print ("correct");
}
}
}
}
how can i dynamically create the buttons and just get this thing to work
If your problem was solved by the answer provided, please mark it as an answer, if not, feel free to comment on it with problems
Answer by floAr · Oct 16, 2013 at 11:19 AM
To be honest I am not 100% about your question. If I got everything right you store your Question with possible answers in the Dictionary and now want to draw a procedural button menue. I see a problem there, as you would need another field, indicating which answer is the correct one. Regarding your problem think some of you functions can be simplified to fit into the following approach:
On ButtonPress pick a random question
If question is picked show answer menue
If no question is picket show something else
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; public class testQuestions : MonoBehaviour { //Question array Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>(); int pickedQuestion = -1; void Start() { dictionary.Add("ups", new string[] { "updegree", "popup" }); dictionary.Add("down", new string[] { "sun", "bun" }); dictionary.Add("left", new string[] { "higi", "migi" }); dictionary.Add("right", new string[] { "een", "yyo" }); } void Update() { if (Input.GetKeyDown("q")) { GenerateRandoms(); } } void GenerateRandoms() { pickedQuestion = (int)Random.Range(0, dictionary.Count);//pick the index of one question } void OnGUI() { if (pickedQuestion != -1) { // here dynamically added butons must be added GUI.TextArea(new Rect(0, 0, 500, 20), "Key is " + dictionary.Keys[pickedQuestion] + " value is " + string.Join(",", dictionary.Values[pickedQuestion]) + " " + pickedQuestion); for (int i = 0; i < dictionary.Values[pickedQuestion].Length; i++) { if (GUI.Button(new Rect(0, 40 * i, 100, 20), dictionary.Values[pickedQuestion][i])) { //check if value is correct } } } else { //handle alternative gui here } } }
Hope this helps you getting started.
PS: I think it would be easier to encapsulate the question into an extra class like this:
public class Question
{
string Text{get;set;}
string[] Answers{get;set;}
int CorrectAnswerId{get;set;}
}
Then you could use a simple ArrayList in you main class and you will get all the information for one question into one single place.
i tried your method but then a bunch of errors got thrown up which shouldn't . i managed to get the whole thing working with just under 100 lines of code :)
this is by far the easiest method of getting a Quiz done :)
the fix is in my comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers
Putting dictionary items in GUI ScrollView 1 Answer
Distribute terrain in zones 3 Answers