Tekki Posted July 15, 2009 Share Posted July 15, 2009 (edited) I am making this game and experimenting with things. It is a basic "guess my number" game. My problem is that when you press cancel on the input box for this game - it brings up the error message of an incorrect character. I think it is something to do with @error. If anyone knows why or knows a fix please post. This is the script of the game: expandcollapse popupGlobal $win = IniRead("Game.ini", "Win!", "Won:", "0"), $lose =IniRead("Game.ini", "Lost!", "Lost:", "0"), $plays = $win + $lose $start = MsgBox(4, "Hey there!", "Would you like to play my Random Guess Game?") If $start = 6 Then Main() ElseIf $start = 7 Then Exit EndIf Func Main() $plays = $win + $lose ;increases plays by adding losses and wins togethor $number = Random(1,10,1) ; Generates the number that you must guess. $guess = Int(InputBox("Guess my number","Guess my number. It is 1 - 10" & @CRLF & " Type it below.")) ; Ask's for your guess If @error = 1 Then Exit ElseIf $guess > 10 Or $guess < 1 Then MsgBox(0,"Oops", "You MUST only type in the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... Try again") ; invalid character or a number greater then 10 or less then 1. (also for some reason the Cancel button) Main() EndIf If $guess = $number Then $win = $win + 1 $msg = "You win!" IniWrite("Game.ini", "Win!", "Won:", $win) ; updates ini file Else $lose = $lose + 1 $msg = "Sorry, You lost." IniWrite("Game.ini", "Lost!", "Lost:", $lose) ;u pdates ini file EndIf MsgBox(0,"Results", $msg & " Your number was " & $guess & " and my number was " & $number) ; shows your results of this game $iniwin = IniRead("Game.ini", "Win!", "Won:", "0") ; finds the value for the wins $inilost = IniRead("Game.ini", "Lost!", "Lost:", "0") ; finds the value for losses $stat = MsgBox(4, "Statistics", "Would like you see your statistics?") If $stat = 6 Then ; 6 = Yes button MsgBox(0, "Stats!","Number of plays: " & $plays & @CRLF & "Wins: " & $iniwin & @CRLF & "Losses: " & $inilost) ; using the ini file created - It shows you stats ElseIf $stat = 7 Then ; 7 = No button EndIf $again = MsgBox(4, "Play Again", "Would you like to Play again?") ;4 = Yes/No button If $again = 6 Then ; 6 = Yes button Main() ElseIf $again = 7 Then ; 7 = No button Exit EndIf EndFunc This part here is were the bug occurs: $guess = Int(InputBox("Guess my number","Guess my number. It is 1 - 10" & @CRLF & " Type it below.")) ; Ask's for your guess If @error = 1 Then Exit ElseIf $guess > 10 Or $guess < 1 Then MsgBox(0,"Oops", "You MUST only type in the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... Try again") ; invalid character or a number greater then 10 or less then 1. (also for some reason the Cancel button) Anyone with any ideas? EDIT: Changed a part of a script EDIT2: Found a bug that doesn't make sense. the $plays value is off by one. but if the $plays is $win + $lose then it shouldn't be off. Anyone know why? Edited July 15, 2009 by Tekki If you intend to use Sarcasm you must this sticker!!![size="1"][sub]pic made by manadar[/sub][/size] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 15, 2009 Moderators Share Posted July 15, 2009 Tekki,The problem arises because you are using Int(InputBox(....)). The @error you are checking is coming from the Int function - and not the InputBox!Recode like this and all will be well:$guess = InputBox("Guess my number","Guess my number. It is 1 - 10" & @CRLF & " Type it below.") ; Ask's for your guess If @error = 1 Then Exit $guess = Int ($guess) If $guess > 10 Or $guess < 1 Then MsgBox(0,"Oops", "You MUST only type in the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... Try again")However, I also noticed that you are running function Func Main() recursively - a really bad idea as you will end up crashing the system in the end! recursion has its place in programming - but this is not it!Here is your code rewritten slightly to avoid recursion:expandcollapse popupGlobal $win = IniRead("Game.ini", "Win!", "Won:", "0"), $lose =IniRead("Game.ini", "Lost!", "Lost:", "0"), $plays = $win + $lose If MsgBox(4, "Hey there!", "Would you like to play my Random Guess Game?") = 7 Then Exit While 1 $plays = $plays + 1 ;increases plays by one every time you start the game $number = Random(1,10,1) ; Generates the number that you must guess. While 1 $guess = InputBox("Guess my number","Guess my number. It is 1 - 10" & @CRLF & " Type it below.") ; Ask's for your guess If @error = 1 Then Exit $guess = Int ($guess) If $guess > 10 Or $guess < 1 Then MsgBox(0,"Oops", "You MUST only type in the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... Try again") ; invalid character or a number greater then 10 or less then 1. (also for some reason the Cancel button) Else ExitLoop EndIf WEnd If $guess = $number Then $win = $win + 1 $msg = "You win!" IniWrite("Game.ini", "Win!", "Won:", $win) ; updates ini file Else $lose = $lose + 1 $msg = "Sorry, You lost." IniWrite("Game.ini", "Lost!", "Lost:", $lose) ;u pdates ini file EndIf MsgBox(0,"Results", $msg & " Your number was " & $guess & " and my number was " & $number) ; shows your results of this game $iniwin = IniRead("Game.ini", "Win!", "Won:", "0") ; finds the value for the wins $inilost = IniRead("Game.ini", "Lost!", "Lost:", "0") ; finds the value for losses $stat = MsgBox(4, "Statistics", "Would like you see your statistics?") If $stat = 6 Then ; 6 = Yes button MsgBox(0, "Stats!","Number of plays: " & $plays & @CRLF & "Wins: " & $iniwin & @CRLF & "Losses: " & $inilost) ; using the ini file created - It shows you stats ElseIf $stat = 7 Then ; 7 = No button EndIf If MsgBox(4, "Play Again", "Would you like to Play again?") = 7 Then Exit ;4 = Yes/No button WEndI hope this is useful. Ask if you do not understand anything.M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Tekki Posted July 15, 2009 Author Share Posted July 15, 2009 (edited) Tekki,The problem arises because you are using Int(InputBox(....)). The @error you are checking is coming from the Int function - and not the InputBox!Recode like this and all will be well:>>CODE WAS HERE<<However, I also noticed that you are running function Func Main() recursively - a really bad idea as you will end up crashing the system in the end! recursion has its place in programming - but this is not it!Here is your code rewritten slightly to avoid recursion:>>CODE WAS HERE<<I hope this is useful. Ask if you do not understand anything.M23Yes I think I do understand it. Thank you - this also fixes the plays not adding up correctly. Edited July 15, 2009 by Tekki If you intend to use Sarcasm you must this sticker!!![size="1"][sub]pic made by manadar[/sub][/size] Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now