I feel like I found the base of this code so on some Powershell tutorial, but I'm not sure. Anyways, I've modified how the game plays out because I thought going from one chamber to the next was a bit boring. Instead, you rotate the barrel (or whatever you call it) to land on a random chamber. For the sake of the demo ending quickly, I made it so that it would always be a new random chamber that you did not previously land on.
I'll probably modify this a little bit to make it interactive and have multiple players playing. Then I'll post it on Code Review Stack Exchange.
Tested on Powershell v2
$seed = Get-Date
[int]$bullet_chamber = Get-Random -Minimum 1 -Maximum 6 -SetSeed $seed.Millisecond
[int]$trigger_pulls = 0
$vecChambers = New-Object System.Collections.ArrayList
for ($i = 0; $i -lt 6; $i++) {
[void]$vecChambers.Add($i + 1)
}
Write-Host "Powershell Russian Roulette. The bullet is in chamber #$bullet_chamber."
Sleep -Seconds 2
while (1) {
$trigger_pulls++
[int]$vec_index = 0
if ($vecChambers.Count -gt 1) {
$vec_index = Get-Random -Maximum ($vecChambers.Count - 1)
}
[int]$nChamber = $vecChambers[$vec_index]
Write-Host "Pulled the trigger on chamber #$nChamber!"
if ($nChamber -eq $bullet_chamber) {
Write-Host "Bang! You died after $trigger_pulls shots."
break
}
else {
Write-Host "You survived this time."
$vecChambers.RemoveRange($vec_index, 1)
}
Sleep -Seconds 2
}
No comments:
Post a Comment