Page 1 of 2

[SOLVED] Trivia Game Engine

Posted: Tue Sep 08, 2015 3:32 am
by LeNitrous
...This is my first post. Please don't hate me...
I'm trying to work on a game similar to Jeopardy. Instead the game starts immediately with a random question is chosen from a table. I'm a bit newbish to Love but I do understand the basics of Lua.

Code: Select all

local question = {}

function question.load()
    q = {
        { id = 1, text = "Is LOVE 2D Cool?", ch1 = "Yes", ch2 = "No", ch3 = "Probably", ch4 = "Indefinitely", correct = ch1 },
        { id = 2, text = "Is Lua easy?", a = "No", b = "Yes", c = "Indefinitely", d = "Probably", correct = b }
        }
end

function question.get( id )
    return q[math.random(#q)]
end

function question.getcorrect( id, correct )
end

function question.draw( text, ch1, ch2, ch3, ch4, correct )
    print(question.get(id)) -- just here for debug
end

return question
This is my question.lua, I'm thinking that I'm doing it incorrectly. Probably do it with metatables? Because when I get the result... I get random variables such as...

Code: Select all

table: 0x0252b768
... in the debug console. Am I doing something wrong? If so, please help!

Re: Multiple Choice Question Engine

Posted: Tue Sep 08, 2015 5:16 am
by ivan
Hello and welcome to the forums.
You're printing a reference to a table - and the result is showing you the table's address in memory.
Try:

Code: Select all

local randomQ = question.get(id)
print(randomQ.text)
To print all table elements (without recursion) you can do:

Code: Select all

for k,v in pairs(randomQ) do
  print(tostring(k) .. '=>' .. tostring(v))
end

Re: Multiple Choice Question Engine

Posted: Tue Sep 08, 2015 11:33 pm
by LeNitrous
Thanks alot Ivan! It worked. With one major quirk though. Console is spammed so is the game screen. As if question.get() is getting called in a loop.
This is my question.lua as of now

Code: Select all

local question = {}

function question.load()
    q = {
        { id = 1, text = "Is LOVE 2D Cool?", ch1 = "Yes", ch2 = "No", ch3 = "Probably", ch4 = "Indefinitely", correct = ch1 },
        { id = 2, text = "Is Lua easy?", ch1 = "No", ch2 = "Yes", ch3 = "Indefinitely", ch4 = "Probably", correct = ch2 },
        { id = 3, text = "A more different question", ch1 = "It is?", ch2 = "It isn't?", ch3 = "wazitooya?", ch4 = "OBEY", correct = ch4  },
        { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = ch4  }
        }
end

function question.get()
    return q[math.random(#q)]
end

function question.getcorrect( id, correct )
end

function question.draw( text, ch1, ch2, ch3, ch4, correct )
    local randomQ = question.get()
    love.graphics.print( randomQ.text )
    for k,v in pairs(randomQ) do
        print(tostring(k) .. '=>' .. tostring(v))
    end
end

return question
Added in more elements in table to decrease the chance of it getting picked.
It looks like this...
Image

Re: Multiple Choice Question Engine

Posted: Fri Sep 11, 2015 12:09 am
by LeNitrous
Bump...

Re: Multiple Choice Question Engine

Posted: Fri Sep 11, 2015 6:12 am
by ivan

Code: Select all

 { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = ch4  }
There is a small bug in the code above, ch4 is nil so it should be "ch4":

Code: Select all

 { id = 4, text = "Questions differ right?", ch1 = "It doesn't", ch2 = "It does", ch3 = "Who cares", ch4 = "Sure...", correct = "ch4"  }
Or better yet, use numeric indexes for the choices:

Code: Select all

{
  -- key part (meta info)
  id = 4,
  text = "Questions differ right?",
  correct = 4,
  -- numeric part (choices)
  [1] = "It doesn't", 
  [2] = "It does", 
  [3] = "Who cares", 
  [4] = "Sure..."  }
}
If you don't want to spam the console you can print each question per 1 line:

Code: Select all

    local text = {}
    for k,v in pairs(randomQ) do
        text[#text + 1] = tostring(k) .. '=>' .. tostring(v)
    end
    print(table.concat(text, ","))

Re: Multiple Choice Question Engine

Posted: Tue Sep 15, 2015 12:03 am
by LeNitrous
I made a really sloppy workaround with the for-loop since I think that was the source of the spamming.

Code: Select all

inQuestion = true
in the beginning of my lua file with some if-then-elseif-end statements. If someone can make a better one, that will help me alot!

Re: Multiple Choice Question Engine

Posted: Tue Sep 15, 2015 11:36 pm
by DeltaF1
It's also spamming because you're calling it every frame, inside question.draw (which I assume gets called by love.draw)

Re: Multiple Choice Question Engine

Posted: Wed Sep 16, 2015 2:15 pm
by LeNitrous
Yet... A bit sloppy since it gets called atleast 10 times in a frame. If someone is willing to fix my code then I would be pleased.
I can't get to display one simple question with this, unfortunately...

Re: Multiple Choice Question Engine

Posted: Thu Sep 17, 2015 7:55 pm
by Karai17
I'd like to point out here that pairs is not JIT enabled, but ipairs is. ipairs looks through the sequential (1->n as long as no gaps are in the sequence) part of the table and ignores any named keys. You could replace your ch1, ch2, etc with simply adding them to the table and then loop through the table with ipairs. This will speed up your loops a whole bunch.

Code: Select all

local q = {
   id = 4,
   text = "Questions differ right?",
   correct = 4,

   "It doesn't",
   "It does",
   "Who cares",
   "Sure..."
 }

for i, ch in ipairs(q) do
   print(i, v)
end

Re: Trivia Game Engine

Posted: Fri Sep 18, 2015 1:30 pm
by LeNitrous
Sorry for being a pain in the butt but I decided to change it into a simple trivia game. A question and a textbox.
My current problem is still the same. I added in some changes to the lua file to accomodate my goal.
My problem is that I'm trying to use a table to handle the questions and answers:

Code: Select all

q = {
		{ id = 1, text = "What is the game?", correct = "Untitled" },
		{ id = 2, text = "What language does this game run on?", correct = "Lua" },
		{ id = 3, text = "What framework does this game run on?", correct = "Love 2D" }
        }
The "text" string handles the display of questions while the "correct" string handles the comparing of answers, returning true if it is equal. The "id" integer is just there for something...

Code: Select all

function question.get()
	return q[math.random(#q)]
end
I did this method of getting a random element from the table, the problem is that... It keeps looping

Code: Select all

1
1
1
2
2
2
3
3
3
Spamming this to the console ( note that it is printing print( question.get().id )
Sorry for the wall of text but tl;dr I need help, please read it.