Re: Data

BlitzMax Forums/BlitzMax Beginners Area/Re: Data

Joe90bigrat90(Posted 2012) [#1]
Hi there im writing a simple program. Basically its a quiz game. I want the program to simply ask a question and you are given 4 answers to choose from. I guess its like who wants to be a millionaire type game. I would like to know whats the best way of doing this. i.e how do i store all of the questions and answers.
in basic i know you can store things using data and read but im not sure what would be the bets way in blitz max. Would someone provide some code just asking a question and storing the 4 possible answers. Many thanks guys
Kind Regards
Stants


Midimaster(Posted 2012) [#2]
Write your questions and answers in an INI-file. This makes game-coding indepentend from question-creating. This is an easy and comfortable way and there are a lot of tutorials about reading those INI-files.

a INI-File is a TXT-file written with a simple text-Editor and looks like this:
[Question 1]
Question=Who was Tarzan?
Answer 1=Hey, I am Tarzan!
Answer 2=A monkey.
Answer 3=The guy, who got Jane!
Solution=3


You can add as many questions as you want and you can also add new properties without need of using them in all questions:
[Question 2]
Question=Where is the Eiffel-tower?
Answer 1=Paris
Answer 2=London
Answer 3=Kualalumpur
Solution=1
Picture=Eiffel.png


I did this some years ago with music related questions and with this system the user was able to add additional questions and send them to me. Now we have 900 questions in this game. The need of writting the questions is the biggest and most important part of such games. And doing this outside of the game code is the best way to get a lot of questions very comfortable.

For the game you only need the "reading" of a INI-File. I wrote a tutorial in the german forum about this. This tutorial contains a minimalistic sample of INI-Reading-code, it was written for BB, but it may help you also in BMax:

SuperStrict

Graphics 800,600

' att: first create a file "test.txt" with minimum 1 question

' example of reading one value:
Print ReadValue("Question 1", "Question")
WaitKey()
End


Function ReadValue$(Section$, Entry$)
   Local File$, FileHandle:TStream, Value$, Temp$
   File="Test.txt" ' or also: "test.ini"
   Section="[" + Section + "]"
   If FileExists(File) Then
      FileHandle = ReadFile(File)
         While Eof(FileHandle) = False
            Temp = ReadLine(FileHandle)
            If Temp = Section Then
                Value = SearchForEntry(FileHandle,Entry)
                Exit
            EndIf
         Wend
      CloseFile FileHandle
      Return Value
   EndIf
End Function


Function SearchForEntry$(FileHandle:TStream, Entry$)
   Local Temp$, Value$
   While Eof(FileHandle) = False
      Temp = ReadLine(FileHandle)
      If LeftPart(Temp) = Entry Then
         Value = SeparateValue(Temp)
	 Return Value
      EndIf
   Wend
End Function


Function LeftPart$(Line$)
   Local Value$, Here%
   Here=Instr(Line,"=")
   If Here>0
      Value=Left( Line, Here-1)
   EndIf
   Return Value$
End Function


Function SeparateValue$(Line$)
   Local Value$, Here%
   Here=Instr(Line,"=")
   Value=Mid( Line, Here+1, -1)
   Return Value$
End Function


Function FileExists%(File$)
   If FileType(File) =1 Then
      Return True
   EndIf
End Function


Last edited 2012

Last edited 2012


Joe90bigrat90(Posted 2012) [#3]
cool thanks a lot. I understand the ini file which sounds pretty good to me.
Shame the code is in german. Is there any way of putting this into english.
Also in the ini file if i have say 2 questions do i leave a space after the first question like this:

[Question 1]
Question=Who was Tarzan?
Answer 1=Hey, I am Tarzan!
Answer 2=A monkey.
Answer 3=The guy, who got Jane!
Solution=3

[Question 2]
Question=Where is the Eiffel-tower?
Answer 1=Paris
Answer 2=London
Answer 3=Kualalumpur
Solution=2
Picture=Eiffel.png


Midimaster(Posted 2012) [#4]
it does not matter! you can, but you need not! The only thing, which is important is, that each expression has its own line. And all the entries belonging to same same question are below the headline in brackets.

Now I will edit the code above to "english" and bmax. Done!

Last edited 2012


xlsior(Posted 2012) [#5]
Note: You have the wrong answer for question 2 in your example. :-?