Lingo Coding—Chem_Module
I am using Gary Rosenzweig’s Books to “cut and paste” my basic code and supplementing as needed. I also use various “Lingo Forums” to read/post bugs and strategies.
global
gQuestionNum, gPossiblePoints, gScore, gCorrectAnswer,gDatabase,\
gCurrentQuestion, gTempQuestions, gQuestionOrder,
gAnswerOrder, gDifficultyLevel,\
gNumCorrect, gNumPossible, gResultsDb,
on
startGame
openDatabase
gDifficultyLevel
= 1
gScore = 0
showScore
initializeResults
newLevel
askQuestion
go to frame "Play"
end
--The
openDatabase handler opens a file selected by the user from a popup menu.
--It is currently
set to automatically open module_1, but can be expanded.
on
openDatabase
fileObj = new(Xtra "FileIO")
filename = member("read
popup").text
if filename =
"module_2.txt" then
alert "This module is not available yet"
abort
end if
if filename = "" then
alert "You must choose a module to begin"
abort
end if
if filename =
"module_1.txt" then
openFile(fileObj, filename, 1)
text = readFile(fileObj)
closeFile(fileObj)
database = value(text) --value function takes a string and evaluates it as a Lingo
expression
if not listP(database) then --listP evaluates if the string is a
list (True/False)
alert "Not a valid database file"--
else
gDatabase =
database
end if
end if
member("read popup").text = ""--updates the user selection for next
time
end
--The handler
"on initializeResults" creates the database for all user results.
on
initializeResults
gResultsDb =
[newResults()]
end
--The handler
"on nextResults" creates a new blank prop list for results.
--The
"N/A" is necesary to show user problems they have not seen yet.
--This is called
every time the user goes to a new level.
on
newResults
newList = [:]
addProp newList, #Diff_Level, "N/A"
addProp newList, #Question_1, "N/A"
addProp newList, #Question_2, "N/A"
addProp newList, #Question_3, "N/A"
addProp newList, #Number_Correct, "N/A"
addProp newList, #Number_Possible, "N/A"
return newList
end
--The NewLevel
handler is called whenever the user starts a new level of 5 questions.
--The user will
get 5 new questions in random order from the main database of all questions in
module.
--On newLevel
looks for all database entries with the property Difficulty_Level of the global
of the same name.
--It then
randomizes the order of the questions and the answers.
on
newLevel
gTempQuestions =
[]
tempCount = 1
repeat with i = 1 to gDatabase.count
if gDifficultyLevel = value(gDatabase[i].Difficulty_Level) then
add gTempQuestions, gDatabase[i]
end if
end repeat
if gTempQuestions = [] then--Blank records mean end of DB, so finished.
showResults
go to frame "Results"
exit
end if
gQuestionNum = 1
gNumCorrect = 0--Number of correct answers in each level
gNumPossible = 1--Number of possible question in each level
randomizeQuestions
askQuestion
end
--This handler
creates a random order of questions for each new difficulty level.
--The questions
are placed into the global var gQuestionOrder.
on
randomizeQuestions
gQuestionOrder =
[]
tempList = [1,2,3]
repeat while tempList.count > 0
r = random(tempList.count)
add gQuestionOrder, getAt (tempList,r)
deleteAt tempList,r
end repeat
end
--This handler
gets called from askQuestion to populate the answer fields in random order.
--randomizeAnswers
will randomize the order of the answers in a similar way to questions.
--The global
variable of gCorrectAnswer is set to be first in the list for convenience.
--The answer is
the first element in gAnswerOrder.
on
randomizeAnswers theNumberOfTheQuestion
gAnswerOrder = []
tempList = [1,2,3,4]
repeat while tempList.count > 0
r = random(tempList.count)
add gAnswerOrder, getAt (tempList,r)
deleteAt tempList,r
end repeat
tempNum =
theNumberOfTheQuestion
member("Answer"&&gAnswerOrder[1]).text =
gTempQuestions[tempNum].Correct_Answer
member("Answer"&&gAnswerOrder[2]).text =
gTempQuestions[tempNum].Alternate_Answer_1
member("Answer"&&gAnswerOrder[3]).text = gTempQuestions[tempNum].Alternate_Answer_2
member("Answer"&&gAnswerOrder[4]).text =
gTempQuestions[tempNum].Alternate_Answer_3
gCorrectAnswer = value(gAnswerOrder[1])
end
--AskQuestion is
called for each question. It populates the question/answer fields.
--gQuestionOrder
is a random list of question numbers (from on RandomizeQuestions)
--gQuestionNum is
the first question for the user (could be any question from db)
on
askQuestion
tempNum =
gQuestionOrder[gQuestionNum]
member("Question").text = gTempQuestions[tempNum].Question_Text
randomizeAnswers
tempNum
gPossiblePoints = 0
showPossiblePoints
end
on
gameTimer
gPossiblePoints =
gPossiblePoints + 1
showPossiblePoints
end
on
showPossiblePoints
member("Possible Points").text =
"Points:"&&gPossiblePoints
end
on
showScore
member("Score").text = "Score:"&&gScore
end
--ClickAnswer is
called by the complex button behavior attached to each answer.
--n is a number
(1-4) called by the button as "on clickAnswer n"
--This handler
checks if the answer is correct/incorrect, updates the points,\
alerts the user,
and either moves up another level, or asks a similar question.
on
clickAnswer n
if n = gCorrectAnswer then
gScore = gScore
+ gPossiblePoints
showScore
alert "Good Job! You are
moving to the next level!"
gNumCorrect =
gNumCorrect + 1
recordResults TRUE--send the correct answer to the resultsDB
--gDifficultyLevel tracks user level
for new questions and for results prop list.
gDifficultyLevel = gDifficultyLevel +1
gResultsDB[gDifficultyLevel] = newResults()
newLevel
else
gPossiblePoints
= gPossiblePoints + 100
showPossiblePoints
alert "Sorry, Incorrect--Try again on this question"
gScore = gScore
+ gPossiblePoints
showScore
recordResults FALSE--send the incorrect answer to the resultsDB
gNumPossible =
gNumPossible + 1
nextQuestion
end if
end
--this handler
will check to see if the current level is beyond the last entry.
--If a question
has the current level, nothing is done.
Otherwise, we go to the results page.
on
checkFinished currentLevel
repeat with i = 1 to gDatabase.count
if not (currentLevel = value(gDatabase[i].Difficulty_Level)) then
showResults
go to frame "Results"
end if
end repeat
end
--This handler
adds the user response to a global list that tracks their correct
--and incorrect
responses for review at the end.
on
recordResults answer
theQuestionNumber
= gQuestionOrder[gQuestionNum]
newList =
gResultsDB[gDifficultyLevel]
newList.Diff_Level = gDifficultyLevel
newList.Number_Correct = gNumCorrect
newList.Number_Possible = gNumPossible
if answer = TRUE then
case theQuestionNumber of
1:
newList.Question_1 =
"Correct"
2:
newList.Question_2 =
"Correct"
3:
newList.Question_3 =
"Correct"
end case
else
case theQuestionNumber of
1:
newList.Question_1 =
"Incorrect"
2:
newList.Question_2 =
"Incorrect"
3:
newList.Question_3 =
"Incorrect"
end case
end if
gResultsDB[gDifficultyLevel] = newList--updates the global results list
end
--NextQuestion
checks to see if the user has finished all of the questions on the
--current level
before showing the results page
on
nextQuestion
gQuestionNum =
gQuestionNum + 1
if gQuestionNum >
gQuestionOrder.count then
showResults
go to frame "Results"
else
askQuestion
end if
end
--ShowResults
creates an html table on the results page at the end of the test.
on
showResults
htext = "<HTML><BODY
BGCOLOR=#FFFFFF>"&RETURN
--put the table headings into htext.
put
"<TABLE><TR><TH>Level</TH><TH>Question
1</TH><TH>Question 2</TH><TH>\
Question
3</TH><TH>Num Correct</TH><TH>Num
Possible</TH></TR>"&RETURN after htext
--loop through the results DB and
create the records.
repeat with i = 1 to gResultsDB.count
put "<TR>" after htext
put "<TD>"
&gResultsDB[i].Diff_Level&"</TD>" after htext
put "<TD>"
&gResultsDB[i].Question_1&"</TD>" after htext
put "<TD>"
&gResultsDB[i].Question_2&"</TD>" after htext
put "<TD>"
&gResultsDB[i].Question_3&"</TD>" after htext
put "<TD>"
&gResultsDB[i].Number_Correct&"</TD>" after htext
put "<TD>"
&gResultsDB[i].Number_Possible&"</TD>" after htext
end repeat
--Close out the table and HTML
put "</TABLE></BODY></HTML>" after htext
--Place HTML into text member
"Results_Table"
member("Results Table").html = htext
end