Jump to content

Generate different random number


Recommended Posts

Hi guys , I wanted to make simple game for guessing generated number . Everything is perfect , but when I guess the number , it doesn't change. I know it is because I saved it as a variable but don't know any other way to do so. I want it to change everytime it is guessed. Another thing , I want with every guess , there is something to show how many times you clicked yes before you really guess it. here is the script.

#include <GUIConstantsEx.au3>

GUICreate("Guess the Number" , 300, 200, @DesktopWidth/2 - 150, @DesktopHeight/2 - 100)
GUISetState(@SW_SHOW)
$number = GUICtrlCreateInput("" , 50, 50, 150, 20)
GUICtrlSetLimit(-1, 3, 1)
$guess = GUICtrlCreateButton("Guess" , 240, 50)

$sRandom = Random(0, 100, 1)

HotKeySet("{F2}" , "Terminate")

While 1
$msg = GUIGetMsg()
$readnumber = GUICtrlRead($number)
Select
Case $msg = $guess
If $sRandom > $readnumber Then
MsgBox(0, "Greater" , "The number is more than" &  $readnumber)
ElseIf $sRandom < $readnumber Then
MsgBox(0, "Less" , "The number is less than" &  $readnumber)
Else
MsgBox(0, "You win", "Congratulations, the number is" &  $readnumber)
EndIf
EndSelect
WEnd

Func Terminate()
Exit 0
EndFunc
Link to comment
Share on other sites

Re-create the random number in the else statement:

Else
MsgBox(0, "You win", "Congratulations, the number is" &  $readnumber)

$sRandom = Random(0, 100, 1)
EndIf
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

meh... re-wrote it

#include <GUIConstantsEx.au3>
Global $hGUI, $hNumber, $hGuess, $iRandom

$hGUI = GUICreate("Guess the Number" , 300, 200, @DesktopWidth/2 - 150, @DesktopHeight/2 - 100)
$hNumber = GUICtrlCreateInput("" , 50, 50, 150, 20)
    GUICtrlSetLimit(-1, 3, 1)
$hGuess = GUICtrlCreateButton("Guess" , 240, 50)

$iRandom = Random(0, 100, 1)

GUISetState(@SW_SHOW)

HotKeySet("{F2}" , "Terminate")

While 1
    $msg = GUIGetMsg()
    Switch $msg ; <<<<<<<<<<<<<<<<<<<<<<<<<<<< Select to Switch... just keeping it simple
        Case $GUI_EVENT_CLOSE
            Terminate()
        Case $hGuess
            If _GuessNumber(GUICtrlRead($hNumber)) Then $iRandom = Random(0, 100, 1)
    EndSwitch
WEnd

Func _GuessNumber(Const $iValue)
    If $iValue < $iRandom Then
        MsgBox(64 + 262144, "Greater" , "The number is more than " &amp; $iValue)
    ElseIf $iValue  > $iRandom Then
        MsgBox(64 + 262144, "Less" , "The number is less than " &amp; $iValue)
    Else
        MsgBox(64 + 262144, "You win", "Congratulations, the number is " &amp; $iValue)
        Return True
    EndIf
EndFunc

Func Terminate()
    Exit 0
EndFunc

When the guess button is hit, calls function _GuessNumber() and sends the input field's value as an argument. If the input value = the generated number, the function returns as true, else it return nothing (aka 0 or false). If it returns true, a new number is generated.

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

$Guess += 1 somewhere in the wrong guessing check section would be my suggestion.

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

Link to comment
Share on other sites

Here a fast hack:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>

Global $aNumbers[99]
For $i = 0 To UBound($aNumbers) - 1
    $aNumbers[$i] = $i + 1
Next
Shuffle_Array($aNumbers)

Global $iTries = 5, $iRandom = 0
Global $hGUI = GUICreate("Guess it", 239, 96)
GUISetFont(12, 400, 0, "Arial")
Global $iLabel1 = GUICtrlCreateLabel("My guess", 8, 16, 70, 22)
Global $iInput1 = GUICtrlCreateInput("", 80, 12, 57, 26, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
GUICtrlSetState(-1, $GUI_DISABLE)
Global $iLabel2 = GUICtrlCreateLabel("Tries", 40, 62, 39, 22)
Global $iInput2 = GUICtrlCreateInput($iTries, 80, 60, 57, 26, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY,$ES_NUMBER))
Global $iBtn_Enter = GUICtrlCreateButton("Check", 152, 8, 75, 33)
GUICtrlSetState(-1, $GUI_DISABLE)
Global $iBtn_Start = GUICtrlCreateButton("Start", 152, 56, 75, 33)
GUISetState(@SW_SHOW)


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $iBtn_Enter
            ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aNumbers[$iRandom] = ' & $aNumbers[$iRandom] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
            $iIn = Int(GUICtrlRead($iInput1))
            If $iIn < 1 Or $iIn > 100 Or $iIn = "" Then
                MsgBox(48, "Information", "You must enter a number from 1 - 100 only!", 60, $hGUI)
            Else
                If $iIn > $aNumbers[$iRandom] Then
                    MsgBox(64, "Information", "Then number is smaller", 60, $hGUI)
                    $iTries -= 1
                    GUICtrlSetData($iInput2, $iTries)
                ElseIf $iIn < $aNumbers[$iRandom] Then
                    MsgBox(64, "Information", "Then number is larger", 60, $hGUI)
                    $iTries -= 1
                    GUICtrlSetData($iInput2, $iTries)
                Else
                    MsgBox(64, "Information", "You have found the searched number!!!", 60, $hGUI)
                    GUICtrlSetState($iBtn_Start, $GUI_ENABLE)
                    GUICtrlSetState($iBtn_Enter, $GUI_DISABLE)
                    GUICtrlSetState($iInput1, $GUI_DISABLE)
                    $iTries = 5
                    GUICtrlSetData($iInput2, $iTries)
                    $iRandom += 1
                    $iRandom = Mod($iRandom, 100)
                EndIf
                If $iTries = 0 Then
                    MsgBox(64, "Information", "Game over! The searched number was " & $aNumbers[$iRandom], 60, $hGUI)
                    GUICtrlSetState($iBtn_Start, $GUI_ENABLE)
                    GUICtrlSetState($iBtn_Enter, $GUI_DISABLE)
                    GUICtrlSetState($iInput1, $GUI_DISABLE)
                    $iTries = 5
                    GUICtrlSetData($iInput2, $iTries)
                    $iRandom += 1
                    $iRandom = Mod($iRandom, 100)
                EndIf
            EndIf
        Case $iBtn_Start
            GUICtrlSetState($iBtn_Start, $GUI_DISABLE)
            GUICtrlSetState($iBtn_Enter, $GUI_ENABLE)
            GUICtrlSetState($iInput1, $GUI_ENABLE)
            MsgBox(0, "Your turn", "Enter your guess to the input box. Enter a number from 1-100 and press the 'Enter' button!", 60, $hGUI)
    EndSwitch
WEnd



; #FUNCTION# ======================================================================================
; Name ................:    Shuffle_Array()
; Version .............:    v0.50 build 2011-05-24 beta
; Description .......:  Shuffles an array - support 1D and 2D arrays only
; Syntax ..............:    Shuffle_Array(ByRef $array, $startindex = 0, $endindex = 0)
; Parameters ........:  $array - the array to shuffle
;                               $startindex = from which index to start the shuffling
;                               $endindex = to which index to start the shuffling; 0 means last index of the array
; Return values ....:   True
;                               Failure 1 - $array is not an array
;                               Failure 2 - array has more than 2 dimensions
;                               Failure 3 - array is empty
;                               Failure 4 -  $startindex / $endindex are set wrongly
; Author ..............:    UEZ
; Modified ............:
; Remarks ............:
; Related ..............:   Array
; =================================================================================================
Func Shuffle_Array(ByRef $array, $startindex = 0, $endindex = 0)
    If Not IsArray($array) Then Return SetError(1, 0, 0)
    If UBound($array, 0) > 2 Then Return SetError(2, 0, 0)
    If UBound($array) = 1 Then Return SetError(3, 0, 0)
    Local $u1
    If Not $endindex Then
        $u1 = UBound($array) - 1
    Else
        If $endindex > $startindex And $endindex < UBound($array) Then
            $u1 = $endindex
        Else
            Return SetError(4, 0, 0)
        EndIf
    EndIf
    If UBound($array, 2) Then
        Local $aSwap[1][UBound($array, 2)], $u2 = UBound($array, 2) - 1
        Local $i, $j, $r
        For $i = $startindex To $u1
            $r = Random($startindex, $u1, 1)
            For $j = 0 To $u2
                $aSwap[0][$j] = $array[$i][$j]
                $array[$i][$j] = $array[$r][$j]
                $array[$r][$j] = $aSwap[0][$j]
            Next
        Next
    Else
        Local $aSwap[1]
        For $i = $startindex To $u1
            $r = Random($startindex, $u1, 1)
            $aSwap[0] = $array[$i]
            $array[$i] = $array[$r]
            $array[$r] = $aSwap[0]
        Next
    EndIf
    Return 1
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

... now you're just asking bad questions...

green arrows = changes

#include <GUIConstantsEx.au3>
Global $hGUI, $hNumber, $hGuess, $iRandom, $iGuess = 0 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

$hGUI = GUICreate("Guess the Number" , 300, 200, @DesktopWidth/2 - 150, @DesktopHeight/2 - 100)
$hNumber = GUICtrlCreateInput("" , 50, 50, 150, 20)
    GUICtrlSetLimit(-1, 3, 1)
$hGuess = GUICtrlCreateButton("Guess" , 240, 50)

$iRandom = Random(0, 100, 1)

GUISetState(@SW_SHOW)

HotKeySet("{F2}" , "Terminate")

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Terminate()
        Case $hGuess
            $iGuess+=1 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            If _GuessNumber(GUICtrlRead($hNumber)) Then $iRandom = Random(0, 100, 1)
    EndSwitch
WEnd

Func _GuessNumber(Const $iValue)
    If $iValue < $iRandom Then
        MsgBox(64 + 262144, "Greater" , "The number is more than " & $iValue)
    ElseIf $iValue  > $iRandom Then
        MsgBox(64 + 262144, "Less" , "The number is less than " & $iValue)
    Else
        MsgBox(64 + 262144, "You win", "Congratulations, the number is " & $iValue & @LF & _
                                        "It took you " & $iGuess & " tries to guess the correct number.") ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $iGuess = 0 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Return True
    EndIf
EndFunc

Func Terminate()
    Exit 0
EndFunc
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Shouldn't there be a change in random seed somewhere ? Otherwise the program may generate same row of random numbers at each run.

Or does the AutoIT random() auto-seed ?

I am just a hobby programmer, and nothing great to publish right now.

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