Jump to content

[SOLVED] Help with @error


Recommended Posts

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:

Global $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 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

  • Moderators

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:

Global $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

WEnd

I hope this is useful. Ask if you do not understand anything.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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.

M23

Yes I think I do understand it. Thank you - this also fixes the plays not adding up correctly. :) Edited 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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...