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)

donj
January 1, 2018, 4:38pm
2
$total += 6

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

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

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 {}
}

Olaf
January 2, 2018, 7:15am
5
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’}
}

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

Olaf
January 6, 2018, 9:45pm
7
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! ;-)