Could someone please take a look a this piece of my script ? (full script in the attachment).
As some of you might know I'm working on some educational script for my kids. Below code does what I want, but I am
duplicating quite a bit of code, but I cannot figure out how to compact it.
It's not a great big deal, but it feels a bit like spaghetti code
Thanks in advance for your help.
relevant snippet:
Code: Select all
OkButton:
GuiControl, enable, Answer
GuiControl, focus, Answer
GuiControl, hide, Woord
GuiControl, hide, GoodOrBad
GuiControl, show, Answer
GuiControl, disable, OkButton
Gui, 2: Submit, NoHide
if(Answer = "")
{
GuiControl, enable, OKButton
return
}
CheckAnswer := Instr(Answer, Woord, CaseSensitive := true)
if(CheckAnswer = 1) && (Answer = Woord) ; if answer is correct execute below
{
GuiControl, , GoodOrBad, Goed !
GuiControl, show, GoodOrBad
Sleep, 1500
GuiControl, hide, GoodOrBad
FileReadLine, Woord, %Dictee%, % ++LineNum ; Read the next line from the file and store it in Woord
if ErrorLevel = 1 ; end of file reached
{
WrongAnswers := WrongAnswersArray.MaxIndex()
if (WrongAnswersArray.MaxIndex() = "") ; if no mistakes were made, replace empty MaxIndex string with the number 0
{
WrongAnswers := 0 ; reset variable
FileDelete, %A_ScriptDir%\Dictees\temp_fouten.txt
WrongAnswersArray := []
CorrectAnswersArray := []
}
MsgBox, 4096,, % "Klaar je had " WrongAnswers " fouten" ; get the number of mistakes
Concat := ""
For Each, WrongAnswer in WrongAnswersArray
{
If (Concat <> "") ; if concat is not empty
Concat .= "`n" ; add a line feed
Concat .= CorrectAnswersArray[A_Index] . " jij antwoorde: " . WrongAnswer
}
If (Concat <> "")
MsgBox, 4096,, % Concat ; show all wrong answers
WrongAnswersArray := [] ; clear Array
CorrectAnswersArray := [] ; clear Array
Gui, 2: Destroy
Gui, 1: -Disabled ; enable gui 1
Gui, 1: Show
if FileExist("Dictees\fouten.txt") ; re-run the questions that have been answered wrongly
{
sleep, 500
SetControlDelay -1 ; should improve reliabity
ControlClick,, EduFun, Dictee,,, NA
}
return
}
GuiControl, show, Woord
GuiControl, hide, Answer
GuiControl, enable, OkButton
}
else ; if answer is wrong execute below
{
WrongAnswersArray.Push(Answer)
CorrectAnswersArray.Push(Woord)
GuiControl, enable, OKButton
GuiControl, show, Woord
GuiControl, hide, Answer
FileRead, CheckForDuplicates, %A_ScriptDir%\Dictees\fouten.txt
SkipDuplicates := Instr(CheckForDuplicates, Woord)
if(SkipDuplicates = 0) ; only append wrong answer if it isn't already in fouten.txt
{
FileAppend, %Woord%`n, %A_ScriptDir%\Dictees\fouten.txt
}
FileReadLine, Woord, %Dictee%, % ++LineNum ; Read the next line from the file and store it in Woord
if ErrorLevel = 1 ; end of file reached
{
WrongAnswers := WrongAnswersArray.MaxIndex()
if (WrongAnswersArray.MaxIndex() = "") ; if no mistakes were made, replace empty MaxIndex string with the number 0
{
WrongAnswers := 0 ; reset variable
FileDelete, %A_ScriptDir%\Dictees\temp_fouten.txt
WrongAnswersArray := []
CorrectAnswersArray := []
}
MsgBox, 4096,, % "Klaar je had " WrongAnswers " fouten" ; get the number of mistakes
Concat := ""
For Each, WrongAnswer in WrongAnswersArray
{
If (Concat <> "") ; if concat is not empty
Concat .= "`n" ; add a line feed
Concat .= CorrectAnswersArray[A_Index] . " jij antwoorde: " . WrongAnswer
}
If (Concat <> "")
MsgBox, 4096,, % Concat ;
WrongAnswersArray := [] ; clear Array
CorrectAnswersArray := [] ; clear Array
Gui, 2: Destroy
Gui, 1: -Disabled ; enable gui 1
Gui, 1: Show
if FileExist("Dictees\fouten.txt") ; re-run the questions that have been answered wrongly
{
sleep, 500
SetControlDelay -1 ; should improve reliabity
ControlClick,, EduFun, Dictee,,, NA
}
return
}
GuiControl, , GoodOrBad, Fout !
GuiControl, show, GoodOrBad
}
GuiControl, 2:, Woord, %Woord% ; Update the word above the Edit box
GuiControl, 2:, Answer, ; Erase the last answer from the Edit box
GuiControl, Disable, Answer ; Disable control to prevent typing in the hidden edit box
return