Jump to content

master mind


Recommended Posts

Working script translated from Dutch, but with one simple question (at least for those who know...)

At the end of the script I want to run the script again, without leaving.

Formerly (autoit 2.64). one could use a MsgBox and as a result of the answer, jump to the corresponding label (goto start: )

It is said to be easy, but please, help me out. Where did the Goto - Label function go?

Just a clou will do and I will work my way out...

mmindUK.au3

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

There is no "GoTo" anymore.

Use a loop and define an exit condition. Something like:

While 1
   _MasterMind()
    If condition is met then Exit
Wend
Func _MasterMind()
  ; Your code goes here
EndFunc
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

have to try this one. Thanks anyway for fast reply.

Two problems: One: English, as common language in ICT is not my native language (being Dutch) and two: I'm not a programmer.

But I'm willing to learn.

First: your solution to work out...

Tnx

Link to comment
Share on other sites

  • Developers

Two problems: One: English, as common language in ICT is not my native language (being Dutch) and two: I'm not a programmer.

2 leuke uitdagingen om iets te leren. :huh2:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

See if you can follow this version of your example that uses loops and arrays.

#cs
    MasterMind, played with two numbers, each of them 4 digits, are to be compared and evaluated.
    A digit present, but not correctly placed score a 0,  placed correctly brings up an X and not present at all makes =
    Results showing like = = 0 X
#ce

Const $titel = ".               Forum MasterMind"
Local $antw = 6, $aVG, $aRG

While $antw = 6
    Local $vg = "", $rg = ""

    ; Input fixed number    fx translated for Dutch use  vg=vast getal
    While StringLen($vg) <> 4
        $vg = InputBox($titel, @TAB & "fixed number of 4 digits" & @CRLF & @TAB & "without spaces!")
        If StringLen($vg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
    WEnd

    ; splitting number into 4 separate variables
    $aVG = StringSplit($vg, "", 2)

    ; Input guessed number   gn translated to   rg=raad getal
    While StringLen($rg) <> 4
        $rg = InputBox($titel, @TAB & "4 digits of guessed number" & @CRLF & @TAB & "without spaces!")
        If StringLen($rg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
    WEnd
    $aRG = StringSplit($rg, "", 2)

    ; evaluation: comparing with no match, makes value for $s as set in start of section:  so   "= "
    Local $aRes[UBound($aRG)]
    For $i = 0 To UBound($aRG) - 1
        If $aVG[$i] = $aRG[$i] Then
            $aRes[$i] = "X"
        Else
            $aRes[$i] = "0"
        EndIf
    Next

    SoundPlay(@WindowsDir & "\media\notify.wav", 0)

    $antw = MsgBox(4, $titel, @CRLF & @TAB & $aVG[0] & @TAB & $aVG[1] & @TAB & $aVG[2] & @TAB & $aVG[3] & @CRLF & @TAB & _
            $aRG[0] & @TAB & $aRG[1] & @TAB & $aRG[2] & @TAB & $aRG[3] & @CRLF & @CRLF & @TAB & @TAB & @CRLF & "=" & @TAB & _
            $aRes[0] & @TAB & $aRes[1] & @TAB & $aRes[2] & @TAB & $aRes[3] & @CRLF & @CRLF & 'To start again press "Yes" button')
WEnd
Link to comment
Share on other sites

2 leuke uitdagingen om iets te leren. :huh2:

Zeker weten, Jos, alleen toen ik leerde lopen, hoefde dat niet meteen een marathon te zijn.

Dat kon pasje voor pasje en iedereen om je heen deed dat.

Jij draait al wat langer mee, geef eens een tip waar je het best kunt beginnen, want veel voorbeelden gaan uit van geoefende lopers.

Sure thing, Jos. Only thing is, when I learned my first steps to walk, it was not immediately ment to be a marathon. You could learn step by step and everyone around you, did it...

You, being around quite a bit longer, please give me a hint where to start, because many script-examples assume trained runners.

Link to comment
Share on other sites

See if you can follow this version of your example that uses loops and arrays.

Copied the script and going to analyse it, step by step. Need a bit time before I can answer about results, but it looks prommissing

Tnx

Link to comment
Share on other sites

@Malkey:

Reduced your script to essential lines, where it goes wrong. Still anylising. See attatchmentreply.rtf

Local $vg = "", $rg = ""

$vg = InputBox("", @TAB & "fixed number of 4 digits" & @CRLF & @TAB & "without spaces!")

$aVG = StringSplit($vg, "", 2)

;

$rg = InputBox("", @TAB & "4 digits of guessed number" & @CRLF & @TAB & "without spaces!")

$aRG = StringSplit($rg, "", 2)

;

Local $aRes[uBound($aRG)]

For $i = 0 To UBound($aRG) - 1

If $aVG[$i] = $aRG[$i] Then

$aRes[$i] = "X"

Else

$aRes[$i] = "0"

EndIf

Next

$antw = MsgBox(4, "", @CRLF & @TAB & $aVG[0] & @TAB & $aVG[1] & @TAB & $aVG[2] & @TAB & $aVG[3] & @CRLF & @TAB & _

$aRG[0] & @TAB & $aRG[1] & @TAB & $aRG[2] & @TAB & $aRG[3] & @CRLF & @CRLF & @TAB & @TAB & @CRLF & "=" & @TAB & _

$aRes[0] & @TAB & $aRes[1] & @TAB & $aRes[2] & @TAB & $aRes[3] & @CRLF & @CRLF & 'To start again press "Yes" button')

/quote]

I found out where this section goes wrong, causing the outcome not being correct.

In my script you could see that each comparision results in THREE options.

Each digit is valuated as: = not present // 0 present but wrong position // or X correct and proper position)

Your routine only has two options, either X, or 0.

Edited by Eller
Link to comment
Share on other sites

I have added a "ElseIf" to the evaluation routine to allow for the missing "=" result.

And change the "Input Boxes" to accept "Cancel" button to exit script.

About the title "Master Mind", maybe "Test for Very Short Term Memory Loss" might be a better title.

#cs
    MasterMind, played with two numbers, each of them 4 digits, are to be compared and evaluated.
    A digit present, but not correctly placed score a 0,  placed correctly brings up an X and not present at all makes =
    Results showing like = = 0 X
#ce

Const $titel = ".               Forum MasterMind"
Local $antw = 6, $aVG, $aRG

While $antw = 6
    Local $vg = "", $rg = ""

    ; Input fixed number    fx translated for Dutch use  vg=vast getal
    While StringLen($vg) <> 4
        $vg = InputBox($titel, 'Fixed number of 4 digits without spaces!' & @CRLF & 'Then press "OK" button.' & @CRLF & _
                @CRLF & "or" & @CRLF & @CRLF & 'Press "Cancel" button to exit')
        If @error = 1 Then Exit
        If StringLen($vg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
    WEnd

    ; splitting number into 4 separate variables
    $aVG = StringSplit($vg, "", 2)

    ; Input guessed number   gn translated to   rg=raad getal
    While StringLen($rg) <> 4
        $rg = InputBox($titel, 'Enter 4 digits of guessed number' & @CRLF & 'Then press "OK" button.' & @CRLF & _
                @CRLF & "or" & @CRLF & @CRLF & 'Press "Cancel" button to exit')
        If @error = 1 Then Exit
        If StringLen($rg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
    WEnd
    $aRG = StringSplit($rg, "", 2)

    ; evaluation: comparing with no match, makes value for $s as set in start of section:  so   "= "
    Local $aRes[UBound($aRG)]
    For $i = 0 To UBound($aRG) - 1
        If $aVG[$i] = $aRG[$i] Then
            $aRes[$i] = "X"
        ElseIf StringInStr($vg, $aRG[$i]) Then
            $aRes[$i] = "0"
        Else
            $aRes[$i] = "="
        EndIf
    Next

    SoundPlay(@WindowsDir & "\media\notify.wav", 0)

    $antw = MsgBox(4, $titel, @CRLF & @TAB & $aVG[0] & @TAB & $aVG[1] & @TAB & $aVG[2] & @TAB & $aVG[3] & @CRLF & @TAB & _
            $aRG[0] & @TAB & $aRG[1] & @TAB & $aRG[2] & @TAB & $aRG[3] & @CRLF & @CRLF & @TAB & @TAB & @CRLF & @TAB & _
            $aRes[0] & @TAB & $aRes[1] & @TAB & $aRes[2] & @TAB & $aRes[3] & @CRLF & @CRLF & 'To start again press "Yes" button')
WEnd
Link to comment
Share on other sites

offtopic, im only wondering

@ Eller

I had similar question for c++ code that OP geaved me 10-15 dayes ago, 4 number guessing game, problems with program restarting from the loop...

are you the one?

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

offtopic, im only wondering

@ Eller

I had similar question for c++ code that OP geaved me 10-15 dayes ago, 4 number guessing game, problems with program restarting from the loop...

are you the one?

Can't help you. I joined this forum only a few days ago. As the profile states 22 May.

I have still to learn my ways around here, but already I'm gratefull for the hints I received.

I wish, I had more programming-background, because the possibilties in autoit seem endless.

Link to comment
Share on other sites

Can't help you. I joined this forum only a few days ago. As the profile states 22 May.

I have still to learn my ways around here, but already I'm gratefull for the hints I received.

I wish, I had more programming-background, because the possibilties in autoit seem endless.

Thats strange, i even have the OP-s mail with attachment c++ code and the gamename is identical :huh2:

Newer the less, heare is some small present for you ;)

it shud work correctly, only problem is my english and that autoit or scite don`t have google translator for comments and spell check :alien:

;including needed files
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

;including files for fun :)
#include <IE.au3>

;declare variable that we will use
Global $Input_Data, $NumberCount
Global $Input_Arr, $Rando_Arr

;declare variable that we will use to store compared data
Global $Compa_Data
Global $MsgBox_Data
;declare our 4 digit random number
$Rando_Numb = Random(1000, 9999, 1)

;Creating gui and its controls (we will add random number in the Gui title for the purpos of testing)
$gui = GUICreate("My GUI " & $Rando_Numb, 200, 100)
$Input_Cont = GUICtrlCreateInput("", 10, 10, 50, 20, $ES_NUMBER);$ES_NUMBER variable is for only numeric input its from EditConstants.au3 include
$Check_Butt = GUICtrlCreateButton("Check?", 90, 10, 50, 20)
$Lable_Data = GUICtrlCreateLabel("0000", 10, 50, 50, 20)
$LableCount = GUICtrlCreateLabel("1", 10, 70, 50, 20)
GUICtrlSetLimit($Input_Data, 4);limit the Input to 4 chars max

;Gui is hiden we need to show him
GUISetState(@SW_SHOW)

;this is our main loop that is keeping the script alive
While 1
    $msg = GUIGetMsg();if something happen in a gui, some events like click on button, its stored in a $msg variable
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    If $msg = $Check_Butt Then;when someone click on a button

        ;lets clean our $Compa_Data every time someone click the button, in it we will store our end result
        $Compa_Data = ""

        ;so on button click we will read data from Input controll and store them to variable named $Input_Data that we declared earlyer on top of the script
        $Input_Data = GUICtrlRead($Input_Cont)

        ;we will now see if it have 4 digits, and if not msgbox wirh error will alert user
        If StringLen($Input_Data) = 4 Then

;~          ;we will blank out gui input control every tome when user put some number with 4 digits
;~          GUICtrlSetData($Input_Cont,"")

            ;Number count will go +1 every time someone click the button if it have 4 digits
            $NumberCount += 1
            ;we will change $LableCount every time someone click on it if it have 4 digits
            GUICtrlSetData($LableCount, $NumberCount)


            ;we will need to split the user entered string and $Rando_numb string so that we can compare random number with user input and store them to Array variable
            ;we are spliting on every nothing "" and we will get every part of string in separate part of array variable

            $Input_Arr = StringSplit($Input_Data, "");if you enter 8596 $Input_Arr[0] will hold number of splits, $Input_Arr[1] will hold first number (8) $Input_Arr[2] will hold second number (5)...
            $Rando_Arr = StringSplit($Rando_Numb, "")

            ;lets compare them to get some results
            For $x = 1 To $Input_Arr[0] Step 1;$Input_Arr[0] hold the number of splits, so this will lop 4 times because we have 4 numbers and ewery time $x will go +1
                If $Input_Arr[$x] = $Rando_Arr[$x] Then;we are comparing every part of array to random array data, $x will increase every time for 1 so we will use it :)
                    ;so lets store the result in our $Compa_Data if the numbers are ok
                    $Compa_Data = $Compa_Data & "X";x is if its on correct place
                Else;it isnt on correct place, lets see it its strin in another string
                    If StringInStr($Rando_Numb, $Input_Arr[$x]) Then;we are comparing one number of user input to the original random string
                        $Compa_Data = $Compa_Data & "0";0 is the wrong position
                    Else
                        $Compa_Data = $Compa_Data & "=";= no number like that
                    EndIf
                EndIf
            Next

            ;now we will write the end result to the lable,    X0=X...
            GUICtrlSetData($Lable_Data, $Compa_Data)

            If $Compa_Data = "XXXX" Then
                $MsgBox_Data = MsgBox(64 + 1, "You WON!!!", "Wona go for another try"); (64+1 = Information-sign icon+OK and Cancel buttons; we can write 65 its the identical result

                ;we stored user action from msgbox to $MsgBox_Data variable
                If $MsgBox_Data = 1 Then ; if user clicked on OK button of MsgBox

                    ;so user wona go for another try?
                    ;lets random new number for him
                    $Rando_Numb = Random(1000, 9999, 1)
                    ;change the title to have new random number
                    WinSetTitle($gui, "", "My GUI " & $Rando_Numb)
                    ;and reset Number count that will go +1 every time someone click the button if it have 4 digits
                    $NumberCount = 0
                    GUICtrlSetData($LableCount, $NumberCount)

                Else
                    ExitLoop
                EndIf
            ElseIf $NumberCount = 10 Then;limit the user to 10 times
                $MsgBox_Data = MsgBox(48 + 1, "You Lose!!!", "Wona go for another try or your gona go some other game and buy-resell arrows?"); (48+1 = Exclamation-point icon+OK and Cancel buttons; we can write 49 its the identical result
                ;we stored user action from msgbox to $MsgBox_Data variable
                If $MsgBox_Data = 1 Then ; if user clicked on OK button of MsgBox
                    ;so user wona go for another try?
                    ;lets random new number for him
                    $Rando_Numb = Random(1000, 9999, 1)
                    ;change the title to have new random number
                    WinSetTitle($gui, "", "My GUI " & $Rando_Numb)
                    ;and reset Number count that will go +1 every time someone click the button if it have 4 digits
                    $NumberCount = 0
                    GUICtrlSetData($LableCount, $NumberCount)
                Else
                    _IECreate("http://www.inailed.it/", 0, 1, 0);lets have some un if the user dont want to play the game anymore :)
                    ExitLoop
                EndIf
            EndIf
        Else
            MsgBox(16, "Error", "You need to enter 4 digits before using the button");msgbox with (16, = Stop-sign icon
        EndIf
    EndIf
WEnd
GUIDelete()
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Thats strange, i even have the OP-s mail with attachment c++ code and the gamename is identical :huh2:

MasterMind is a pretty old game in different variaties. Played with coloured pins, with coloured circles a.s.o.

I remember having seen questions for a game like the one you mentiond.

Your script is working fine, but to high for me to analyse.

As a game is does not met my needs, cause it's a solo-game, while the game we play, runs on a Dutch senior forum.

I made something alike, years ago in visual basic, but not using these languages periodicly, you forget all those magic formulas.

I'm was glad with Malkey's help, which gives me the oppertunity to read and learn step by step.

Tnx anyway for your present.

Link to comment
Share on other sites

@Malkey

Great job. And instructiv as well. I have made a selection of the analysing routine, because I have seen that it works, but cannot figure out how exactly.

The helpfile is not enough for me, but with the selection I saved, I can experiment to find out.

.

I made a tiny change, which makes the script more suitable for me.

I asked for a command to make that one could start again without leaving the script and you touhgt me how to get that done.

Now I moved the "while ans" to another line, causing that once the fixed number is set, one can compare more than only one "guessed number".

When the user clicks Yes ($antw=6) for more input, the script now moves back to the input window of guessed number, leaving fixed number untouched. This way you can compare lots of numbers with an already fixed one.

Tnx

Link to comment
Share on other sites

@Malkey

I studied your script, line by line, and I do believe I understand how it works.

I'm working on another problem in this script.

The outcom(result) now, is given in sequence of digits in both $VG and $RG. Not hierarchical or in random sequence, thus making it easy to make your new guess in MasterMind.

Of course, you can input the respons yourself, making your own line, but autoit will certainly have the tools to solve this.

I tried to imitate the comparing section of your script, but for now I keep on getting errors.

I joined the four $aRes[n] together.

After that, I tried to make a new comparison with the StringInString function to get =, 0, and X precisely in that ascii-sequence .

And, being the story of my computing live, it doesn't work this way.

Can you find the time to help me again?

Here is what I tried as a addtition to your script.

; ==========================================================================================================================

$tot = ($aRes[0] & $aRes[1] & $aRes[2] & $aRes[3]) ; putting results together

; for $n = 0 to 4 ; counter (counts to, or including 4?)

; if StringInString($tot) = "-" Then

; $p[$n] = "-" ; putting lowest value first

; ElseIf StringInString($tot) = "0" Then

; $p[$n] = "0" ; putting middle value next

; Else $p[$n] = "X" ; or putting hihgest score

; next

SoundPlay(@WindowsDir & "\media\notify.wav", 0) ; just dressing up

MsgBox(0, $titel, "gevonden waarden, ongesorteerd:" & @TAB & @TAB & @TAB & $tot)

; MsgBox(0, $titel, "Gesorteerde uitkomst: " & @TAB & @TAB & @TAB & $p[0] & @TAB & $p[1] & @TAB & $p[2] & @TAB & $p[3])

; ===========================================================================================================================

Edited by Eller
Link to comment
Share on other sites

Solitaire Mastermind.

A playable version.

#cs
    MasterMind -  Logically deduce a hidden, 4 digit, computer generated, number.
    To help with the deduction,"X 0 = " are used.
    "X" - A digit is present and placed correctly.
    "0" - A digit is present, but not correctly placed.
    "=" - A digit is not present at all.
#ce

Const $titel = "  Forum MasterMind"
Local $antw = 6, $aVG, $aRG, $vg, $rg, $sDisp, $sPreDis, $sPostDis, $bWin = False
Local $iNumTurns = 12 ; Number of guesses

While $antw = 6
    $iLoop = 0
    $vg = ""
    $sPreDis = @TAB & "? ? ? ?" & @TAB & " Results" & @CRLF
    $sDisp = ""
    $bWin = False
    $sPostDis = '"X" - Match number and position,' & @CRLF & _
            '"0" - Match number, not position,' & @CRLF & _
            '"=" - Number not present' & @CRLF
    Local $aArray[$iNumTurns + 1][6] = [[Random(0, 9, 1) & " " & Random(0, 9, 1) & " " & Random(0, 9, 1) & " " & Random(0, 9, 1)]]
    Local $aTemp = StringSplit($aArray[0][0], " ", 2); splitting number into 4 separate variables
    For $i = 0 To UBound($aTemp) - 1
        $aArray[0][1] = $aTemp[0]
        $aArray[0][2] = $aTemp[1]
        $aArray[0][3] = $aTemp[2]
        $aArray[0][4] = $aTemp[3]
    Next

    ;ConsoleWrite($aArray[0][0] & @CRLF) ; The hidden numbers (uncomment for testing purposes

    ; ---------------- Loop for guesses ---------------------
    While 1
        $iLoop += 1
        $rg = ""
        $sDisp = ""
        For $i = 1 To $iNumTurns
            $sDisp &= $i & ":" & @TAB & $aArray[$i][0] & @TAB & $aArray[$i][5] & @CRLF
        Next

        ; Exit loop for guesses .
        If $bWin = True Or $iLoop = $iNumTurns + 1 Then ExitLoop

        ; Input guess number
        While StringLen($rg) <> 4
            $rg = InputBox($titel, $sPreDis & $sDisp & @CRLF & $sPostDis & @CRLF & 'Logically deduce the hidden 4 digit number, no spaces.' & _
                    @CRLF & 'Then press "OK" button.' & @CRLF & _
                    @CRLF & "or" & @CRLF & @CRLF & 'Press "Cancel" button to exit', "", "", 100, 430)
            If @error = 1 Then ExitLoop 2 ; Exit two loops.
            If StringLen($rg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
        WEnd
        $aRG = StringSplit($rg, "")
        $aArray[$iLoop][0] = StringTrimRight(StringRegExpReplace($rg, "(.)", "\1 "), 1) ; Add space between digits & remove trailing space.

        ; evaluation: comparing with no match, makes value for $s as set in start of section:  so   "= "
        Local $aRes[UBound($aRG)]
        For $i = 1 To UBound($aRG) - 1
            If $aArray[0][$i] = $aRG[$i] Then
                $aRes[1] &= "X" & " "
            ElseIf StringInStr($aArray[0][0], $aRG[$i]) Then
                $aRes[2] &= "0" & " "
            Else
                $aRes[3] &= "=" & " "
            EndIf
        Next
        $aArray[$iLoop][5] = $aRes[1] & $aRes[2] & $aRes[3]
        If $aArray[0][0] == $aArray[$iLoop][0] Then $bWin = True
    WEnd
    ; ------------> End of  Loop for guesses ---------------------

    SoundPlay(@WindowsDir & "\media\notify.wav", 0) ; just dressing up
    If $bWin = True Then
        MsgBox(0, $titel, @TAB & $aArray[0][0] & @TAB & " Results" & @CRLF & $sDisp & @CRLF & @TAB & "Guessed  correctly!")
    Else
        MsgBox(0, $titel, @TAB & $aArray[0][0] & @TAB & " Results" & @CRLF & $sDisp & @CRLF & @TAB & "You lose!")
    EndIf
    $antw = MsgBox(4, $titel, 'To start game again press "Yes" button')
WEnd

Edit: Changed deduct to deduce.

Edited by Malkey
Link to comment
Share on other sites

copied your script and played the sologame, which brought up one hudge difference, with the game we play over here.

First I thought your game didn't work, but after losing my game, it appeared that in your version it is allowed to use one digit more than once in a number. We don't do that. Making an average of 7 turns to guess right.

Doesn't matter, let me study the script to sort out how the section works where the evaluation gets "ascii-sorted".

Tnx so far.

post-64994-0-84743400-1306680427_thumb.p

Link to comment
Share on other sites

Another slightly varied version for your perusal.

Alterations:

- Changed number of quesses from 12 to 8.

- Added $aVG, an array of the hidden numbers. And removed this array of digits from $aArray[1][0] to $aArray[4][0]. So that now the array, $aArray, contains only the hidden numbers in $aArray[0][0], the guessed numbers in $aArray[>0][0], and the evaluation of those guessed numbers in $aArray[>0][1].

- Added a routine that generates unique digits for the hidden number.

#cs
    MasterMind -  Logically deduce a hidden, 4 digit, computer generated, number.
    To help with the deduction,"X 0 = " are used.
    "X" - A digit is present and placed correctly.
    "0" - A digit is present, but not correctly placed.
    "=" - A digit is not present at all.
    
    Note: This version has no repeated digits in the hidden number.
#ce 

Const $titel = "  Forum MasterMind"
Local $antw = 6, $aVG, $aRG, $rg, $sDisp, $sPreDis, $sPostDis, $bWin = False
Local $iNumTurns = 8 ; Number of guesses

While $antw = 6
    $iLoop = 0
    $sPreDis = @TAB & "? ? ? ?" & @TAB & " Results" & @CRLF
    $sDisp = ""
    $bWin = False
    $sPostDis = '"X" - Match number and position,' & @CRLF & _
            '"0" - Match number, not position,' & @CRLF & _
            '"=" - Number not present' & @CRLF

    ; Generate unique 4 digit number separated with spaces
    Local $sHidNumber = Random(0, 9, 1), $iRand
    Do
        $iRand = Random(0, 9, 1)
        If StringInStr($sHidNumber, $iRand) = 0 Then $sHidNumber &= " " & $iRand
    Until StringLen($sHidNumber) = 7 ; Example result is "1 2 3 4" So, 4 digits plus 3 spaces = 7 characters.

    Local $aArray[$iNumTurns + 1][2] = [[$sHidNumber]]
    ;Local $aArray[$iNumTurns + 1][6] = [[Random(0, 9, 1) & " " & Random(0, 9, 1) & " " & Random(0, 9, 1) & " " & Random(0, 9, 1)]]
    $aVG = StringSplit($aArray[0][0], " "); splitting number into 4 separate variables

    ;ConsoleWrite($aArray[0][0] & @CRLF) ; The hidden numbers (uncomment for testing purposes)

    ; ---------------- Loop for guesses ---------------------
    While 1
        $iLoop += 1
        $rg = ""
        $sDisp = ""
        For $i = 1 To $iNumTurns
            $sDisp &= $i & ":" & @TAB & $aArray[$i][0] & @TAB & $aArray[$i][1] & @CRLF
        Next

        ; Exit loop for guesses .
        If $bWin = True Or $iLoop = $iNumTurns + 1 Then ExitLoop

        ; Input guess number
        While StringLen($rg) <> 4
            $rg = InputBox($titel, $sPreDis & $sDisp & @CRLF & $sPostDis & @CRLF & 'Logically deduce the hidden 4 digit number, no spaces.' & _
                    @CRLF & 'Then press "OK" button.' & @CRLF & _
                    @CRLF & "or" & @CRLF & @CRLF & 'Press "Cancel" button to exit', "", "", 100, 380)
            If @error = 1 Then ExitLoop 2 ; Exit two loops.
            If StringLen($rg) <> 4 Then MsgBox(48, $titel, @TAB & "Wrong input: Input again", 2)
        WEnd
        $aRG = StringSplit($rg, "")
        $aArray[$iLoop][0] = StringTrimRight(StringRegExpReplace($rg, "(.)", "\1 "), 1) ; Add space between digits & remove trailing space.

        ; evaluation: comparing with no match, makes value for $s as set in start of section:  so   "= "
        Local $aRes[UBound($aRG)]
        For $i = 1 To UBound($aRG) - 1
            If $aVG[$i] = $aRG[$i] Then
                $aRes[1] &= "X" & " "
            ElseIf StringInStr($aArray[0][0], $aRG[$i]) Then
                $aRes[2] &= "0" & " "
            Else
                $aRes[3] &= "=" & " "
            EndIf
        Next
        $aArray[$iLoop][1] = $aRes[1] & $aRes[2] & $aRes[3]
        If $aArray[0][0] == $aArray[$iLoop][0] Then $bWin = True
    WEnd
    ; ------------> End of  Loop for guesses ---------------------

    SoundPlay(@WindowsDir & "\media\notify.wav", 0) ; just dressing up
    
    If $bWin = True Then
        MsgBox(0, $titel, @TAB & $aArray[0][0] & @TAB & " Results" & @CRLF & $sDisp & @CRLF & @TAB & "Guessed  correctly!")
    Else
        MsgBox(0, $titel, @TAB & $aArray[0][0] & @TAB & " Results" & @CRLF & $sDisp & @CRLF & @TAB & "You lose!")
    EndIf
    
    $antw = MsgBox(4, $titel, 'To start game again press "Yes" button')
WEnd
Link to comment
Share on other sites

I'll try to analyse, but my attempts yesterday didn't light me up.

I was trying to isolate the section which places the result in sequence of ascii value, using minus (-) instead of equal (=)

That way you get ascii values 45. 48 and 88. But that was as far as I could get.

Still studiing. I will copy this script and look if I can distilize the sorting routine right here.

But, new week coming up, (9.44 monday here in Haarlem, near Amsterdam) I first have to step on my vacuum cleaner and work my way around our houseboat.

See you and tnx.

Link to comment
Share on other sites

Did a few things, over here. Studied the script, the most of it is understood.

A few lines I have questions about.

line 32, where 4 digits were supposted to be generated, separated by spaces, I understand this line perfectly, BUT you made that line a remark line....

later on (line 57, you use a quite different way to achief that goal. And I don't quite understand what happens there.

If I may ask, can you discribe what happens on line 57?

Last tiny question:

Line 43, where it says: $sDisp &= $i etcetera.

I noticed the ampersand and equalsign, without space in between. Purpose of that notation? Is it to make the value of $i hidden /invisible?

I made a few tiny changes, mosty to get a nicer layout, but may be, this is only a matter of taste.

I attach the addapted script, so when you discuss the lines, I had questions about, we are synchronized in linenumbers.

Thanx again. solommUK.au3

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