Last active
May 17, 2021 13:21
-
-
Save 0ryant/f01b6f150a28fa0922e6f19ac00d1443 to your computer and use it in GitHub Desktop.
ironscripter pwsh summit / devops collective box game thingy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region funcs | |
function checkSum { | |
param ( | |
$upnums, | |
[int]$diceroll | |
) | |
$sum = ($upnums | Measure-Object -sum).Sum | |
for ($i=0; $i -lt $upnums.count; $i++){ | |
for ($j=0; $j -lt $upnums.count; $j++){ | |
if (($upnums[$i] + $upnums[$j] -eq $diceroll) -and ($i -ne $j) -or ($upnums[$i] -eq $diceroll) -or ($sum -eq $diceroll)){ | |
return $true | |
} | |
} | |
} | |
return $false | |
} | |
function displayBoxes { | |
param ( | |
[int]$min, | |
[int]$max, | |
[array]$downnums | |
) | |
$disp = ($min..$max| % {Write-Output "|$_|"}) | |
foreach ($d in $downnums){ | |
$disp = $disp -replace $d,'X' | |
} | |
return (Write-Host $disp -NoNewline) | |
} | |
function checkWin { | |
param ( | |
[array]$upnums | |
) | |
if ($upnums.Count -eq 0){ | |
return $true | |
} | |
return $false | |
} | |
function rollDice{ | |
$d = get-random -min 1 -max 7 | |
return $d | |
} | |
#endregion | |
#region settings | |
cls | |
$downnums = New-Object Collections.Generic.List[Int] | |
$upnums = New-Object Collections.Generic.List[Int] | |
$min = 1 | |
$max = 9 | |
$min..$max | % {$upnums.Add($_)} | |
#endregion | |
#region game | |
while ($true){ | |
#display the open/closed boxes | |
displayBoxes -min $min -max $max -downnums $downnums | |
#check if play has won | |
if (checkWin -upnums $upnums){ | |
return 'YOU WIN' | |
} | |
#roll the dice | |
$dice1 = rollDice | |
$dice2 = rollDice | |
$diceroll = $dice1 + $dice2 | |
Write-Output "`nyou rolled $diceroll `ndice one = $dice1, dice two = $dice2" | |
#check if a move is possible | |
if(!(checkSum -upnums $upnums -diceroll $diceroll)){ | |
"lose, can't finish turn with this roll" | |
break | |
} | |
#box selection cycle | |
[int]$door = $null | |
$accum = New-Object Collections.Generic.List[Int] | |
$sum = 0 | |
while ($sum -lt $diceroll){ | |
do{ | |
$door = Read-Host -Prompt "which door numbers to close? $sum of $diceroll used" | |
if ( | |
($door -gt $diceroll) -or ` | |
($door -le 0) -or ` | |
($door -eq'[a-z]') -or ` | |
(($accum | ? {$door -eq $_}) -eq $door) -or ` | |
(($downnums | ? {$door -eq $_}) -eq $door) -or ` | |
($door -gt $max) | |
){ | |
'invalid, try again' | |
$accum = New-Object Collections.Generic.List[Int] | |
$sum = 0 | |
}else { | |
$accum.add($door) | |
$sum = ($accum | Measure-Object -Sum).sum | |
if ($sum -gt $diceroll){ | |
'invalid, try again' | |
$accum = New-Object Collections.Generic.List[Int] | |
$sum = 0 | |
} | |
} | |
}until($sum-eq $diceroll) | |
for ($i=0;$i-lt$accum.count;$i++){ | |
$downnums.Add($accum[$i]) | Out-Null | |
$upnums.Remove($accum[$i]) | Out-Null | |
} | |
} | |
} | |
#endregion |
Seven sides dice. What would that look like in 3 dimensions? ;-)
funny to think about but actually the 7 is impossible to roll, not sure if it's difference between ps / ps core....
Seven sides dice. What would that look like in 3 dimensions? ;-)
funny to think about but actually the 7 is impossible to roll, not sure if it's difference between ps / ps core....
You are wright, I didn't know that. Never used Get-Random before. A bit suprised they did it that way...
"... Get-Random
returns a value that is less than the maximum (not equal). ..."
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seven sides dice. What would that look like in 3 dimensions? ;-)