fairly simple looping and variable question

This topic contains 6 replies, has 5 voices, and was last updated by  Olaf Soyk 4 months, 2 weeks ago.

  • Author
    Posts
  • #90593

    Jason Roberts
    Participant

    Hi guys,

    I'm fairly new to powershell scripting and am stuck right now trying to get something to work the way I want.
    I wrote a simple script that simulates a slot machine using get-random. When three variables $a,$b,$c are the same, such as 'bell' or 'cherry' the variable $total becomes a certain number. Each time the loop runs the value of $total is reset. I want powershell to save the value of $total and add it to the new value of $total each time the loop is run. What's a way that I can do this?

    Here is my script

    cls

    $l = 1
    Do
    { "starting loop $l"
    $l
    $l++

    $slots = ($a,$b,$c)
    $a = Get-Random -inputobject 'bell','cherry','bell'
    $b = get-random -inputobject '7','bell','cherry'
    $c = get-random -inputobject 'bell','bell','cherry'
    $slots
    write-host 'the total credits are' $total
    if ($a -eq 7 -and $b -eq 7 -and $c -eq 7) {$total = 50 }
    elseif ($a -eq 'cherry' -and $b -eq 'cherry' -and $c -eq 'cherry') {$total = 6 }
    elseif ($a -eq 'cherry' -and $b -eq 'cherry' -and $c -ne 'cherry') {$total = 4}
    elseif ($a -ne 'cherry' -and $b -eq 'cherry' -and $c -eq 'cherry') {$total = 4}
    elseif ($a -eq 'cherry' -and $b -ne 'cherry' -and $c -eq 'cherry') {$total = 4}
    elseif ($a -eq 'bell' -and $b -eq 'bell' -and $c -eq 'bell') { $total = 12 }
    else {$total = 0}
    Start-Sleep 2
    ""
    } while ($l -le 8)

  • #90595

    Don Jones
    Keymaster

    $total += 6

    will take whatever's in $total and add 6 to it.

  • #90596

    Michael Orellana
    Participant

    A way to do it would be to create an empty array then add the results of the script to that array.

    Cls
    
    $result = @()
    
    $l = 1
    Do
    { "starting loop $l"
    $l
    $l++
    $slots = ($a,$b,$c)
    $a = Get-Random -inputobject 'bell','cherry','bell'
    $b = get-random -inputobject '7','bell','cherry'
    $c = get-random -inputobject 'bell','bell','cherry'
    $slots
    write-host 'the total credits are' $total
    if ($a -eq 7 -and $b -eq 7 -and $c -eq 7) {$result = 50 }
    elseif ($a -eq 'cherry' -and $b -eq 'cherry' -and $c -eq 'cherry') {$result = 6 }
    elseif ($a -eq 'cherry' -and $b -eq 'cherry' -and $c -ne 'cherry') {$result = 4}
    elseif ($a -ne 'cherry' -and $b -eq 'cherry' -and $c -eq 'cherry') {$result = 4}
    elseif ($a -eq 'cherry' -and $b -ne 'cherry' -and $c -eq 'cherry') {$result = 4}
    elseif ($a -eq 'bell' -and $b -eq 'bell' -and $c -eq 'bell') { $result = 12 }
    else {$result = 0}
    
    $total += $result
    
    Start-Sleep 2
    ""
    } while ($l -le 8)
    
    $total
    
  • #90605

    Chris Bakker
    Participant

    Did something similar once.
    To overcome a lot of if statements, u can use the switch statement.
    Also it makes it much more readable and you can expand/adapt it easily.
    But as it takes the 1 match, you should be careful with the order of the switch items.

    $slots= '7 cherry cherry'
    switch -wildcard ($slots)
    {  '7 7 7'                {'50';break}
       'cherry cherry cherry' {"6" ;break}
       'cherry cherry *'      {"4" ;break}
       '* cherry cherry'      {"4" ;break}
       'cherry * cherry'      {"4" ;break}
       'cherry *'             {"2" ;break}
         Default {}
    }
  • #90608

    Olaf Soyk
    Participant

    I didn't get the thing with the variable $total but as your "gambling engine" you could start with something like this:

    Function gamble {
        $Pattern = '(\W+|^(\w+)\W+|$\1)\2'
        $Game = $(
            for ($index = 1; $index -lt 4; $index++) {
                Get-Random -InputObject '7','bell','cherry' #,'star'
            }
        ) -join ' '
        If($Game -match $Pattern){"You win with '$($Matches[2])'"}Else{'You loose'}
    }
  • #90962

    Jason Roberts
    Participant

    Hey guys, you've given me some good stuff to work with.

    Thanks Don, I just tested your solution and confirmed that it worked the way I wanted it to.

    Thanks Chris, I'm going to experiment with the Switch function soon,

    Thanks Michael, I'll try that option too.

    Thanks Olaf. Your idea has some elements I'm not yet familiar with, but I like trying "new to me" cmdlets and parameters

  • #90965

    Olaf Soyk
    Participant

    Thanks Olaf. Your idea has some elements I'm not yet familiar with, but I like trying "new to me" cmdlets and parameters

    If you have some more questions – go ahead – ask! 😉

You must be logged in to reply to this topic.