Sign in to follow this  
Followers 0
Damein

Problem with _ArraySearch & If..Else

3 posts in this topic

#1 ·  Posted (edited)

So I'm creating a trivia game that reads questions and answers from a INI and then outputs them. I have two problems.

[ Ignore the randomization part, I solved it via adding the first Random digit in the _CreateGUI function. Neglected to do that ]

  • I setup a randomization to not allow duplicate questions. And then I added the random ed number to an array, and did an if statement. This works, except for when I use it too often, it starts repeating them. Not sure if I am doing something wrong, but everything seems to be setup right.
  • Next I have the answers setup in buttons, and then the answer is saved to a var. Once they press the button it will check the answers together and depending on the conclusion will present the appropriate MsgBox. This works sometimes, but not other times for some reason. I assumed there were whitespaces in one of the vars so I stripped those to double check. But it didn't help.

Here is the full code

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <Array.au3>
HotKeySet("{`}", "_Random")
Opt("GUIOnEventMode", 1)
Global $FirstRun, $Answer1, $Answer2, $Answer3, $Answer4, $QuestionButton, $String, $Answer
Dim $UsedQuestions[20000]
$Questions = IniReadSection("QuestionDatabase.ini", "Level1")
If $FirstRun = 0 Then
$Money = "1000.00"
Else
$Money = "100.00"
EndIf
_CreateGUI()
Func _CreateGUI()
$Gui = GuiCreate("Trivia!", 800,600)
GuiCtrlCreateGroup("",10,5,780,80)
$Money = GuiCtrlCreateLabel("$ " & $Money, 15,30,700,40)
GuiCtrlSetFont($Money, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreatePic("Blank.bmp", 20, 100, 755, 200)
$Random = Random(1,$Questions[0][0],1)
; _ArrayAdd($UsedQuestions,$Random)
$QuestionButton = GuiCtrlCreateButton($Questions[$Random][0],20,100,755,200,BitOr($BS_CENTER, $BS_MULTILINE))
$SplitPossibleAnswers = StringSplit($Questions[$Random][1],",")
$String = StringSplit($SplitPossibleAnswers[4],":")
$Answer = StringStripWS($String[2],1)
StringStripWS($String[2],2)
GuiCtrlCreateGroup("",20,310,755,200)
$Answer1 = GuiCtrlCreateButton($SplitPossibleAnswers[1],30,330,365,75,BitOr($BS_CENTER, $BS_MULTILINE))
GUICtrlSetOnEvent($Answer1, "_Check1")
$Answer2 = GuiCtrlCreateButton($SplitPossibleAnswers[2],397,330,365,75,BitOr($BS_CENTER, $BS_MULTILINE))
GUICtrlSetOnEvent($Answer2, "_Check2")
$Answer3 = GuiCtrlCreateButton($SplitPossibleAnswers[3],30,420,365,75,BitOr($BS_CENTER, $BS_MULTILINE))
GUICtrlSetOnEvent($Answer3, "_Check3")
$Answer4 = GuiCtrlCreateButton($String[1],397,420,365,75,BitOr($BS_CENTER, $BS_MULTILINE))
GUICtrlSetOnEvent($Answer4, "_Check4")
GuiSetState()
EndFunc
Func _Random()
$Random = Random(1,$Questions[0][0],1)
_ArraySearch($UsedQuestions,$Random)
If @Error Then
_ArrayAdd($UsedQuestions,$Random)
GuiCtrlSetData($QuestionButton, $Questions[$Random][0])
$SplitPossibleAnswers = StringSplit($Questions[$Random][1],",")
$String = StringSplit($SplitPossibleAnswers[4],":")
$Answer = StringStripWS($String[2],1)
StringStripWS($String[2],2)
GuiCtrlCreateGroup("",20,310,755,200)
GuiCtrlSetData($Answer1, $SplitPossibleAnswers[1])
GuiCtrlSetData($Answer2, $SplitPossibleAnswers[2])
GuiCtrlSetData($Answer3, $SplitPossibleAnswers[3])
GuiCtrlSetData($Answer4, $String[1])
Else
MsgBox(0, "Error", "Error")
_Random()
EndIf
EndFunc
Func _Check1()
$Data = GuiCtrlRead($Answer1)
$TestAnswer = StringStripWS($Data,1)
StringStripWS($TestAnswer,2)
MsgBox(0, "Test", $TestAnswer & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc
Func _Check2()
$Data = GuiCtrlRead($Answer2)
$TestAnswer = StringStripWS($Data,1)
StringStripWS($TestAnswer,2)
MsgBox(0, "Test", $TestAnswer & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc
Func _Check3()
$Data = GuiCtrlRead($Answer3)
$TestAnswer = StringStripWS($Data,1)
StringStripWS($TestAnswer,2)
MsgBox(0, "Test", $TestAnswer & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc
Func _Check4()
$Data = GuiCtrlRead($Answer4)
$TestAnswer = StringStripWS($Data,1)
StringStripWS($TestAnswer,2)
MsgBox(0, "Test", $TestAnswer & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc

While 1
$Msg = GuiGetMsg()
If $Msg = $GUI_EVENT_CLOSE Then
  Exit
EndIf
Sleep(10)
WEnd

And here is the INI

[Level1]
Red Square in Moscow is neither red nor square. What does the word 'red' refer to? = Beautiful, Stoney, Occupied, Stage : Beautiful
Where was Yul Brynner born? = Iceland, Russia, Tahiti, Madagascar : Russia
In which American state would you find the city of El Paso? = Texas, Arizona, New Mexico, Nevada : Texas
Where are demersal creatures located? = Trees, On the sea bed, Inside caves, Hot deserts : On the sea bed
My Indian name was Goyathlay which literally means 'One who Yawns'. Who am I? = Geronimo, Mother Teresa, Mahatma Gandhi, Sir Cliff Richard : Geronimo
Name the rare tree or praying plants named by Mormons because of their upstretched arms. = Samson tree, Matthew Tree, Disciple tree, Joshua tree : Joshua Tree
The prestigious annual art award called The Turner Prize is named after whom? = Tina Turner, Ted Turner, Kathleen Turner, Joseph Mallord William Turner : Joseph Mallord William Turner
Times Square in New York was named in honour of which publication that moved its office there in 1904? = The News Of The World, TV Times, The Observer, New York Times : New York Times
Who said, "Basically my wife was immature. I'd be at home in my bath and she'd come in and sink my boats"? = Peter Sellers, Sir Francis Chickester, Richard Branson, Woody Allen : Woody Allen
What was the first name of bumbling Inspector Clouseau played in the movies by both Peter Sellers and Steve Martin? = Henri, Phillipe, Claude, Jacques : Jacques
Who said, "A dolphin will jump out of the water for a piece of fish, imagine what he'd do for some chips"? = Ben Elton, Harry Hill, Billy Connolly, David Attenborough : Harry Hill
Mount Kosciusko is Australia's highest mountain. What was the nationality of the explorer who discovered this mountain? = Polish, French, Irish, Spanish : Polish
What is the country of Brazil said to have derived its name from? = Amazon female warriors, A type of tree, A prehistoric river, A dinosaur : A type of tree
Who said, "Since we cannot know all there is to be known about everything, we ought to know a little about everything"? = George Walker Bush, Blaise Pascal, Sylvester Stallone, Ronald Reagan : Blaise Pascal

So just press the ` key to random the question.

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites



So.. after making the post I realized I should try doing a StringLen and checking if they are the same. They were not on some, but on some they were. The problem was I had the StringStrip setup as:

Func _Check4()
$Data = GuiCtrlRead($Answer4)
$TestAnswer = StringStripWS($Data,1)
StringStripWS($TestAnswer,2)
MsgBox(0, "Test", $TestAnswer & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc

And kind of assumed I didn't need to add a var to the second one, dunno why. But changing the function to

Func _Check4()
$Data = GuiCtrlRead($Answer4)
$TestAnswer = StringStripWS($Data,1)
$TestAnswer2 = StringStripWS($TestAnswer,2)
  $Len = StringLen($TestAnswer2)
$Len2 = StringLen($Answer)
MsgBox(0, "Test", $TestAnswer2 & " = " & $Len & @CRLF & @CRLF & $Answer & " = " & $Len2)
MsgBox(0, "Test", $TestAnswer2 & @CRLF & @CRLF & $Answer)
$FinalAnswer = MsgBox(4,"Final Answer", "Is " & $TestAnswer2 & " your final answer?")
If $FinalAnswer = 6 Then
  If $TestAnswer2 = $Answer Then
   MsgBox(0, "test", "You are right!")
  Else
   MsgBox(0, "Test", "You are wrong!")
  EndIf
EndIf
EndFunc

Seems to of done the trick! Sorry for wasting the space :D


MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites

For your second problem don't use GUICtrlRead of the buttons, just use the array $SplitPossibleAnswers[x], where X is the button number


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0