Skip to content

Instantly share code, notes, and snippets.

@0ryant
Last active May 17, 2021 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0ryant/f01b6f150a28fa0922e6f19ac00d1443 to your computer and use it in GitHub Desktop.
Save 0ryant/f01b6f150a28fa0922e6f19ac00d1443 to your computer and use it in GitHub Desktop.
ironscripter pwsh summit / devops collective box game thingy
#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
@B4Art
Copy link

B4Art commented May 12, 2021

Seven sides dice. What would that look like in 3 dimensions? ;-)

@B4Art
Copy link

B4Art commented May 12, 2021

image

@0ryant
Copy link
Author

0ryant commented May 17, 2021

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

@B4Art
Copy link

B4Art commented May 17, 2021

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