Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active July 31, 2020 22:08
Show Gist options
  • Save JFFail/b788a7a7918e1c87e96d to your computer and use it in GitHub Desktop.
Save JFFail/b788a7a7918e1c87e96d to your computer and use it in GitHub Desktop.
Intermediate Reddit Daily Programmer 238 - Fallout Hacking Minigame
#https://www.reddit.com/r/dailyprogrammer/comments/3qjnil/20151028_challenge_238_intermediate_fallout/
#Reddit Daily Programmer 238 - Intermediate
#Get the desired difficulty from the user.
$needDifficulty = $true
while($needDifficulty) {
Write-Host "How difficult would you like the game to be?"
$difficulty = Read-Host "Enter a value (1 - 5)"
#Validate the input by making sure it's a number.
if(($difficulty -match "\d") -and ($difficulty.Length -eq 1)) {
#Now make sure it's between 1 and 5.
if(-not(($difficulty -ge 1) -and ($difficulty -le 5))) {
Write-Host "Valid numbers are between 1 and 5, inclusive! Try again..."
} else {
$needDifficulty = $false
}
} else {
Write-Host "That's not a number! Try again..."
}
}
#Import the word file. It'll take a second or two, plus the calculations below.
Write-Host "Importing words, please wait..."
$wordArray = Get-Content -Path .\word.txt
#Populate some data based on the selected difficulty.
if($difficulty -eq 1) {
$wordLength = 3
$numWords = 4
} elseif($difficulty -eq 2) {
$wordLength = 4
$numWords = 7
} elseif($difficulty -eq 3) {
$wordLength = 6
$numWords = 10
} elseif($difficulty -eq 4) {
$wordLength = 7
$numWords = 13
} elseif($difficulty -eq 5) {
$wordLength = 9
$numWords = 15
}
#Randomly select the words we'll use.
$counter = 0
$answerList = @()
$needAnswer = $true
$haveZero = $false
while($counter -lt $numWords) {
#Generate a random number.
$index = Get-Random -Minimum 0 -Maximum ($wordArray.Length - 1)
#See if it matches our parameters.
if(($wordArray[$index]).Length -eq $wordLength) {
#Select it as the answer if it's first.
if($needAnswer) {
$theAnswer = $wordArray[$index]
$answerList += $wordArray[$index]
$needAnswer = $false
$counter++
} else {
#See how many characters match and act accordingly.
$matchingLetters = 0
for($i = 0; $i -lt $wordLength; $i++) {
if($theAnswer[$i] -eq ($wordArray[$index])[$i]) {
$matchingLetters++
}
}
#Add only a single option with no matches.
if($matchingLetters -eq 0 -and (-not($haveZero))) {
$answerList += $wordArray[$index]
$haveZero = $true
$counter++
} elseif($matchingLetters -gt 0) {
$answerList += $wordArray[$index]
$counter++
}
}
}
}
#Now shuffle them all up since otherwise index 0 will always be the answer.
$availableIndices = New-Object System.Collections.ArrayList
for($i = 0; $i -lt $numWords; $i++) {
$availableIndices.Add($i) | Out-Null
}
#Make a placeholder array and initialize all indicies we need.
$finalArray = @()
for($i = 0; $i -lt $numWords; $i++) {
$finalArray += ""
}
#Recalculate the position of each word.
foreach($word in $answerList) {
#Can't have a min and max of both 0.
if($availableIndices.Count -gt 1) {
$tempIndexIndex = Get-Random -Minimum 0 -Maximum ($availableIndices.Count - 1)
$indexValue = $availableIndices[$tempIndexIndex]
} else {
$indexValue = $availableIndices[0]
}
#Actually make the assignments.
$finalArray[$indexValue] = $word
$availableIndices.Remove($indexValue)
}
#Show the word list to the user.
Write-Host "`n=========="
foreach($word in $finalArray) {
Write-Host $word
}
Write-Host "==========`n"
#Let the guessing begin!
$guesses = 0
while($guesses -lt 4) {
$left = 4 - $guesses
$userGuess = Read-Host "Selection ($left left)"
#Make sure it's valid.
if($finalArray.Contains($userGuess)) {
#Increment the counter.
$guesses++
#See if it's the correct answer.
if($userGuess -eq $theAnswer) {
Write-Host "Successful hack after $guesses attempt(s)!" -ForegroundColor Green
exit
} else {
#See how many characters are correct.
$correctLetters = 0
for($i = 0; $i -lt $wordLength; $i++) {
if($theAnswer[$i] -eq $userGuess[$i]) {
$correctLetters++
}
}
#Let the user know.
Write-Host "$correctLetters of $wordLength correct."
}
} else {
Write-Host "That's not a valid choice..."
}
}
#If we made it this far without exiting, the user was not successful!
Write-Host "FAILURE! Correct word was: $theAnswer" -ForegroundColor Red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment