Jump to content

Input box suggestions


Recommended Posts

Hi All,

I'm new to AutoIT programming language, and decided to build a lotto game to practice using some of it's features. Could someone please suggest a better way to script this program? Should I be using an array?

Thank you,

Gary

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt") 
EndIf

$bLoop = 1
While $bLoop = 1
    $no1 = InputBox ("Game 1", "Enter a number from 1 - 47")
    $no2 = InputBox ("Game 2", "Enter a number from 1 - 47")
    $no3 = InputBox ("Game 3", "Enter a number from 1 - 47")
    $no4 = InputBox ("Game 4", "Enter a number from 1 - 47")
    $no5 = InputBox ("Game 5", "Enter a number from 1 - 47")
    $no6 = InputBox ("Game 6", "Enter a number from 1 - 47")
    $no7 = InputBox ("Game 7", "Enter a number from 1 - 47")
    $no8 = InputBox ("Game 8", "Enter a number from 1 - 47")
    $no9 = InputBox ("Game 9", "Enter a number from 1 - 47")
    $no10 = InputBox ("Game 10", "Enter a number from 1 - 47")
    $no11 = InputBox ("Game 11", "Enter a number from 1 - 47")
    $no12 = InputBox ("Game 12", "Enter a number from 1 - 47")
    If $no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12 > 47 Then
        $answer = MsgBox(5, "Error", "Number Exceeds 47")
        If $answer = 2 Then
            Exit
        EndIf   
        Else
        If Number($no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12) > 0 Then            
            $bLoop = 0          
        Else
         $answer1 = MsgBox(5, "Error", "A valid number is required")
         If $answer1 = 2 Then
             Exit
         EndIf       
        EndIf
    EndIf   
WEnd
    
    If @error = 1 Then
        Exit
    EndIf

FileWrite($file, $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12 & @CRLF)
FileClose($file)
Edited by gfunk999
Link to comment
Share on other sites

I just keep getting the answer is greater then 47

Why do you ask for numbers 12 times and then only use $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12

I think this bit If $no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12 > 47 Then needs to use + not & but I still don't understand what type of lottery format it is

Link to comment
Share on other sites

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf

$bLoop = 1
While $bLoop = 1
    $no1 = InputBox ("Game 1", "Enter a number from 1 - 47")
    $no2 = InputBox ("Game 2", "Enter a number from 1 - 47")
    $no3 = InputBox ("Game 3", "Enter a number from 1 - 47")
    $no4 = InputBox ("Game 4", "Enter a number from 1 - 47")
    $no5 = InputBox ("Game 5", "Enter a number from 1 - 47")
    $no6 = InputBox ("Game 6", "Enter a number from 1 - 47")
    $no7 = InputBox ("Game 7", "Enter a number from 1 - 47")
    $no8 = InputBox ("Game 8", "Enter a number from 1 - 47")
    $no9 = InputBox ("Game 9", "Enter a number from 1 - 47")
    $no10 = InputBox ("Game 10", "Enter a number from 1 - 47")
    $no11 = InputBox ("Game 11", "Enter a number from 1 - 47")
    $no12 = InputBox ("Game 12", "Enter a number from 1 - 47")
    If $no1 or $no2 or $no3 or $no4 or $no5 or $no6 or $no7 or $no8 or $no9 or $no10 or $no11 or $no12 > 47 Then ;You weren't comparing a single ;string, instead you compared all 12 combined.
        $answer = MsgBox(5, "Error", "Number Exceeds 47")
        If $answer = 2 Then
            Exit
        EndIf   
        Else
        If Number($no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12) > 0 Then           
            $bLoop = 0     
        Else
         $answer1 = MsgBox(5, "Error", "A valid number is required")
         If $answer1 = 2 Then
             Exit
         EndIf    
        EndIf
    EndIf   
WEnd
   
    If @error = 1 Then
        Exit
    EndIf

FileWrite($file, $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12 & @CRLF)
FileClose($file)

Link to comment
Share on other sites

It's California lottery format; however, what i'm doing is picking 12 numbers, and at the end I only choose six numbers to write to the lotto.txt file. Eventually I will add more six number outputs out of that 12 number pool.

I just keep getting the answer is greater then 47

Why do you ask for numbers 12 times and then only use $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12

I think this bit If $no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12 > 47 Then needs to use + not & but I still don't understand what type of lottery format it is

Edited by gfunk999
Link to comment
Share on other sites

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf

$bLoop = 1
While $bLoop = 1
    $no1 = InputBox ("Game 1", "Enter a number from 1 - 47")
    $no2 = InputBox ("Game 2", "Enter a number from 1 - 47")
    $no3 = InputBox ("Game 3", "Enter a number from 1 - 47")
    $no4 = InputBox ("Game 4", "Enter a number from 1 - 47")
    $no5 = InputBox ("Game 5", "Enter a number from 1 - 47")
    $no6 = InputBox ("Game 6", "Enter a number from 1 - 47")
    $no7 = InputBox ("Game 7", "Enter a number from 1 - 47")
    $no8 = InputBox ("Game 8", "Enter a number from 1 - 47")
    $no9 = InputBox ("Game 9", "Enter a number from 1 - 47")
    $no10 = InputBox ("Game 10", "Enter a number from 1 - 47")
    $no11 = InputBox ("Game 11", "Enter a number from 1 - 47")
    $no12 = InputBox ("Game 12", "Enter a number from 1 - 47")
    If $no1 or $no2 or $no3 or $no4 or $no5 or $no6 or $no7 or $no8 or $no9 or $no10 or $no11 or $no12 > 47 Then;You weren't comparing a single;string, instead you compared all 12 combined.
        $answer = MsgBox(5, "Error", "Number Exceeds 47")
        If $answer = 2 Then
            Exit
        EndIf   
        Else
        If Number($no1 & $no2 & $no3 & $no4 & $no5 & $no6 & $no7 & $no8 & $no9 & $no10 & $no11 & $no12) > 0 Then           
            $bLoop = 0   
        Else
         $answer1 = MsgBox(5, "Error", "A valid number is required")
         If $answer1 = 2 Then
             Exit
         EndIf  
        EndIf
    EndIf   
WEnd
   
    If @error = 1 Then
        Exit
    EndIf

FileWrite($file, $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12 & @CRLF)
FileClose($file)[/autoit]

Tut tut Generator! Try this

if 1 or 2 > 99 then msgbox(0,'yes','99 is less than 1 or 2')

@gfunk999

What's need is

If $No1 > 47 or $No2 > 47 ... etc

In answer to your question about arrays, then I think they would make things a lot simpler.

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf


Dim $no[13];<---replaces $No1, $No2 etc
For $n = 1 To 12
    $bLoop = 1
    While $bLoop = 1
        $no[$n] = InputBox ("Game " & $n, "Enter a number from 1 - 47")

        If $no[$n]> 47 Then
            $answer = MsgBox(5, "Error", "Number Exceeds 47")
            If $answer = 2 Then
                Exit
            EndIf
        Else
            If Number($no[$n]) > 0 Then
                $bLoop = 0
            Else
                $answer1 = MsgBox(5, "Error", "A valid number is required")
                If $answer1 = 2 Then
                    Exit
                EndIf
            EndIf
        EndIf
    WEnd
Next

;not sure what your intention was with this if bit below, but @error is only relevant to the last function called
If @error = 1 Then
    Exit
EndIf

FileWrite($file, $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12 & @CRLF)
FileClose($file)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hi Martin,

Thank you for the revision. Very nice! Question though :whistle: ; If this line (Dim $no[13];<---replaces $No1, $No2 etc) replaces my $No1, $No2...variables how would I be able to seperate them if $no will always stay the same? At the end of the code It will choose and write six random numbers from the pool of 12 to c:\lotto.txt file.

Tut tut Generator! Try this

if 1 or 2 > 99 then msgbox(0,'yes','99 is less than 1 or 2')

@gfunk999

What's need is

If $No1 > 47 or $No2 > 47 ... etc

In answer to your question about arrays, then I think they would make things a lot simpler.

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf
s
For $n = 1 To 12
    $bLoop = 1
    While $bLoop = 1
        $no[$n] = InputBox ("Game " & $n, "Enter a number from 1 - 47")

        If $no[$n]> 47 Then
            $answer = MsgBox(5, "Error", "Number Exceeds 47")
            If $answer = 2 Then
                Exit
            EndIf
        Else
            If Number($no[$n]) > 0 Then
                $bLoop = 0
            Else
                $answer1 = MsgBox(5, "Error", "A valid number is required")
                If $answer1 = 2 Then
                    Exit
                EndIf
            EndIf
        EndIf
    WEnd
Next

;not sure what your intention was with this if bit below, but @error is only relevant to the last function called
If @error = 1 Then
    Exit
EndIf

FileWrite($file, $no1 & "-" & $no2 & "-" & $no3 & "-" & $no5& "-" & $no8 & "-" & $no12 & @CRLF)
FileClose($file)
Link to comment
Share on other sites

Try this:

After the next at the end, is a loop that chooses numbers at random (as random as a computer can be i suspect) and outputs them.

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf

Dim $no[13]
For $n = 1 To 12
    $bLoop = 1
    While $bLoop = 1
        $no[$n] = InputBox ("Game " & $n, "Enter a number from 1 - 47")

        If $no[$n]> 47 Then
            $answer = MsgBox(5, "Error", "Number Exceeds 47")
            If $answer = 2 Then
                Exit
            EndIf
        Else
            If Number($no[$n]) > 0 Then
                $bLoop = 0
            Else
                $answer1 = MsgBox(5, "Error", "A valid number is required")
                If $answer1 = 2 Then
                    Exit
                EndIf
            EndIf
        EndIf
    WEnd
Next

For $i = 1 to Ubound($no) -1
    $LottoNumber = Random(1, 13)
    FileWrite($file, $no[$LottoNumber]&",")
Next
FileClose($file)
Link to comment
Share on other sites

Hi tAKTelapis,

I already a mathematical way to randomize my outputs if I can get all 12 numbers in separate variables . What I need is to find a way to collect all 12 numbers inside variables then base on my equation I will randomize the output to a text file using filewrite. Thus random(1, 13) wont work unless I can use something else other than random function.

For example:

FileWrite($file, $no1 & "-" & $no2 & "-" & $no4 & "-" & $no5 & "-" & $no8 & "-" & $no9 & @CRLF)

FileWrite($file, $no1 & "-" & $no4 & "-" & $no7 & "-" & $no9 & "-" & $no11 & "-" & $no12 & @CRLF)

FileWrite($file, $no4 & "-" & $no5 & "-" & $no8 & "-" & $no10 & "-" & $no11 & "-" & $no12 & @CRLF)

Thanks for your help!

Gary

Try this:

After the next at the end, is a loop that chooses numbers at random (as random as a computer can be i suspect) and outputs them.

#include <File.au3>

$file = FileOpen("c:\lotto.txt", 1)
If $file = -1 Then
_FileCreate("c:\lotto.txt")
EndIf

Dim $no[13]
For $n = 1 To 12
    $bLoop = 1
    While $bLoop = 1
        $no[$n] = InputBox ("Game " & $n, "Enter a number from 1 - 47")

        If $no[$n]> 47 Then
            $answer = MsgBox(5, "Error", "Number Exceeds 47")
            If $answer = 2 Then
                Exit
            EndIf
        Else
            If Number($no[$n]) > 0 Then
                $bLoop = 0
            Else
                $answer1 = MsgBox(5, "Error", "A valid number is required")
                If $answer1 = 2 Then
                    Exit
                EndIf
            EndIf
        EndIf
    WEnd
Next

For $i = 1 to Ubound($no) -1
    $LottoNumber = Random(1, 13)
    FileWrite($file, $no[$LottoNumber]&",")
Next
FileClose($file)
Edited by gfunk999
Link to comment
Share on other sites

You already have them each one stored into its own variable.

The code posted by tAKTelapis is doing exactly what you want.

$no[1] - holds 1st number

$no[2] - holds 2nd number

.....

$no[12] - holds 12th number

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hi Enaiman,

Ahhh, my apologies. I wasn't sure how to use the variables once they were passed to $no. Everything makes sense now. I just have to incorporate my random numbers to this script. I'm not sure if my logic will even work, but I think it would be fun seeing the outcome.

Thanks again all!

Gary

You already have them each one stored into its own variable.

The code posted by tAKTelapis is doing exactly what you want.

$no[1] - holds 1st number

$no[2] - holds 2nd number

.....

$no[12] - holds 12th number

Link to comment
Share on other sites

Glad we could help.

My code might have been a bit confusing, i rushed it to get the brownie points (i haven't been doing a lot of helping lately.. there hasn't really been much worth helping with)

Either way, for the sake of others finding this via a search, ill explain further:

I entered a loop which will run for as many iterations as there are values in the array. UBound counts the total size, and seeing as $array[0] always contains the number of items in the array, i have simply removed 1 number from the over all size, so it will only run for each value actually inserted (I could have also just used "For $i = 1 to $no[0]" but this could be in-accurate, ubound actually counts the total items in an array of its own accord)

For $i = 1 to Ubound($no) -1

I have then set the variable: $LottoNumber to a Random() number, between 1 and 13 (the number of items in our array).

$LottoNumber = Random(1, 13)

And outputed each number followed by a , into the designated text file. the comma makes it easier to separate the string (StringSplit()) if read back in via another part of the script.

FileWrite($file, $no[$LottoNumber]&",")

Next <= obligatory Next for the For loop

It does however occur to me now, that you wanted to only output 6 of the 12 numbers. To do that, simply shorten the size of the loop, only running 6 iterations:

For $i = UBound($no) -7

; code for random + output to file here

Next

Will mean you get an output of 6 random numbers from the 12 available.

Cheers

/tAK

Edited by tAKTelapis
Link to comment
Share on other sites

Not confusing at all, but I appreciate the thorough explanation; very cool. This makes easy for newbies like me. THanks again!

Glad we could help.

My code might have been a bit confusing, i rushed it to get the brownie points (i haven't been doing a lot of helping lately.. there hasn't really been much worth helping with)

Either way, for the sake of others finding this via a search, ill explain further:

I entered a loop which will run for as many iterations as there are values in the array. UBound counts the total size, and seeing as $array[0] always contains the number of items in the array, i have simply removed 1 number from the over all size, so it will only run for each value actually inserted (I could have also just used "For $i = 1 to $no[0]" but this could be in-accurate, ubound actually counts the total items in an array of its own accord)

For $i = 1 to Ubound($no) -1

I have then set the variable: $LottoNumber to a Random() number, between 1 and 13 (the number of items in our array).

$LottoNumber = Random(1, 13)

And outputed each number followed by a , into the designated text file. the comma makes it easier to separate the string (StringSplit()) if read back in via another part of the script.

FileWrite($file, $no[$LottoNumber]&",")

Next <= obligatory Next for the For loop

It does however occur to me now, that you wanted to only output 6 of the 12 numbers. To do that, simply shorten the size of the loop, only running 6 iterations:

For $i = UBound($no) -7

; code for random + output to file here

Next

Will mean you get an output of 6 random numbers from the 12 available.

Cheers

/tAK

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...