can anyone help me how to teach me to create, insert and select database and to retrieve it randomly one by one and cannot be repeated.
[lua]
------ data.lua -----
local data = {}
local q = {}
q[#q+ 1] = {question = “What is the capital of France?”, answer = “Paris”}
q[#q+ 1] = {question = “What is a female swan called?”, answer = “Pen”}
q[#q+ 1] = {question = “Who played Chandler Bing in Friends?”, answer = “Matthew Perry”}
q[#q+ 1] = {question = “What colour is the sky?”, answer = “Blue”}
q[#q+ 1] = {question = “If a dog has no nose, how does it smell?”, answer = “Terrible”}
data.q = q
return data
------ game.lua -----
local data = require (“data”)
local questions = data.q
local currentQuestion = 1
local function shuffle(t)
local rand = math.random; assert(t, “table.shuffle() expected a table, got nil”)
local iterations = #t; local j
for i = iterations, 2 , - 1 do
j = rand(i); t[i], t[j] = t[j], t[i]
end
end
shuffle(questions)
while currentQuestion <= #questions do
print (“Question " …c urrentQuestion. .”: " …q uestions[currentQuestion].question
currentQuestion = currentQuestion + 1
end
[/lua]
thanks for the code I have one problem…First if the user got the right answer it will generate another question like a typical multiple choice game and at start there are 3 buttons easy, average and difficult button. The average button is disabled it will only enabled if the user have 3 correct answers on from the easy round, the difficult will only enabled if they have 3 correct answers on average and it will not be disabled unless they pressed the reset button to reset the correct answers to 0 and disable again the average and difficult button. NOTE: there should be 4 choices in each question including the right answer and should be shuffled with the questions respectively
In my trivia game which uses an online MySQL database, I do a query to get all the ID’s of the questions from the database for the category I’m interested in:
SELECT id FROM questions WHERE category = “history”;
I then put all of these ID’s in a table and run a shuffle sort on them. You can google for “corona shuffle” and find many way’s to shuffle a table. When this is done I have a randomized list of ID’s for all the questions in the database. My game grabs 20 at a time, so I can just loop over the 1st 20 questions in the table, then call another SELECT * FROM questions WHERE id = id (where the second id, is the value from the table for the question).
A similar thing should work for you with SQLlite. On the other hand, for my trivia game, I maintain a local .json file that I simply load into the game’s memory and have it just be an in memory table, shuffle sort, grab the questions in order.
Rob
hehe I understand a little but didn’t get it :wacko:
I really need to learn this and finish my project. help please
How many questions are you dealing with?
How much data is with each question?
Do you plan to add more questions later? How do you plan to add them?
I’m wondering if you need this in a database at all and if a flat text file that you load them all into memory would work better.
Rob
-
6 each questions on easy, average and difficult round
-
It depends on our adviser
-
Nope
we really need to insert this in the database. Just to make clear there are 3 buttons and should be disabled except on easy button and can do the following:
- The questions along with the choices assigned to the questions should shuffle
1.1 if the answer is wrong it will print it … = display.newText (“Incorrect”,…) and generate another question
1.2 if it is correct print it … = display.newText(“correct”,…) and generate another question
1.3 if the user got 2 correct answer the average button will unlocked
1.4 On average button it is the same as 1.1 and 1.2 but the user should get 4 correct answer to unlock the difficult round
1.5 On difficult button, same as the condition on easy round but need 6 correct answer to unlock the reset button
- The user can reset the activity and the buttons (average and difficult) will be disabled again
Can anyone help me??? still stuck on my problem
We can’t write your program for you. I wonder if this can help get you started?
http://stackoverflow.com/questions/2279706/select-random-row-from-an-sqlite-table
You could always load in all the records into a table then shuffle the table too.
Rob
Thank you
Is there a way to the record (choices of the question) on a button, when it can be pressed and if it is correct it will display it on the simulator and go to another question with different choices and if it is incorrect it will display it on the simulator
[lua]
------ data.lua -----
local data = {}
local q = {}
q[#q+ 1] = {question = “What is the capital of France?”, answer = “Paris”}
q[#q+ 1] = {question = “What is a female swan called?”, answer = “Pen”}
q[#q+ 1] = {question = “Who played Chandler Bing in Friends?”, answer = “Matthew Perry”}
q[#q+ 1] = {question = “What colour is the sky?”, answer = “Blue”}
q[#q+ 1] = {question = “If a dog has no nose, how does it smell?”, answer = “Terrible”}
data.q = q
return data
------ game.lua -----
local data = require (“data”)
local questions = data.q
local currentQuestion = 1
local function shuffle(t)
local rand = math.random; assert(t, “table.shuffle() expected a table, got nil”)
local iterations = #t; local j
for i = iterations, 2 , - 1 do
j = rand(i); t[i], t[j] = t[j], t[i]
end
end
shuffle(questions)
while currentQuestion <= #questions do
print (“Question " …c urrentQuestion. .”: " …q uestions[currentQuestion].question
currentQuestion = currentQuestion + 1
end
[/lua]
thanks for the code I have one problem…First if the user got the right answer it will generate another question like a typical multiple choice game and at start there are 3 buttons easy, average and difficult button. The average button is disabled it will only enabled if the user have 3 correct answers on from the easy round, the difficult will only enabled if they have 3 correct answers on average and it will not be disabled unless they pressed the reset button to reset the correct answers to 0 and disable again the average and difficult button. NOTE: there should be 4 choices in each question including the right answer and should be shuffled with the questions respectively
In my trivia game which uses an online MySQL database, I do a query to get all the ID’s of the questions from the database for the category I’m interested in:
SELECT id FROM questions WHERE category = “history”;
I then put all of these ID’s in a table and run a shuffle sort on them. You can google for “corona shuffle” and find many way’s to shuffle a table. When this is done I have a randomized list of ID’s for all the questions in the database. My game grabs 20 at a time, so I can just loop over the 1st 20 questions in the table, then call another SELECT * FROM questions WHERE id = id (where the second id, is the value from the table for the question).
A similar thing should work for you with SQLlite. On the other hand, for my trivia game, I maintain a local .json file that I simply load into the game’s memory and have it just be an in memory table, shuffle sort, grab the questions in order.
Rob
hehe I understand a little but didn’t get it :wacko:
I really need to learn this and finish my project. help please
How many questions are you dealing with?
How much data is with each question?
Do you plan to add more questions later? How do you plan to add them?
I’m wondering if you need this in a database at all and if a flat text file that you load them all into memory would work better.
Rob
-
6 each questions on easy, average and difficult round
-
It depends on our adviser
-
Nope
we really need to insert this in the database. Just to make clear there are 3 buttons and should be disabled except on easy button and can do the following:
- The questions along with the choices assigned to the questions should shuffle
1.1 if the answer is wrong it will print it … = display.newText (“Incorrect”,…) and generate another question
1.2 if it is correct print it … = display.newText(“correct”,…) and generate another question
1.3 if the user got 2 correct answer the average button will unlocked
1.4 On average button it is the same as 1.1 and 1.2 but the user should get 4 correct answer to unlock the difficult round
1.5 On difficult button, same as the condition on easy round but need 6 correct answer to unlock the reset button
- The user can reset the activity and the buttons (average and difficult) will be disabled again
Can anyone help me??? still stuck on my problem