Hello, I am working on a script for school and I have most of it working I have one error I cannot figure out the debug for - I keep getting an array out of range error on my array. I have checked my code against the example and it is fine. does anyone have any ideas?
$playgame = "false"
$response - ""
$randomno = get-random
# I wish he would just use this cmdlt instead of the system one which does not work!
$number = 0
$secretword = ""
$attempts = 0
$status = "true"
$guesses = ""
$reply = ""
$tempstring = ""
$vaildreply = ""
$rejectlist = '~!@#$%^&_=|\;".,<>/?:'
$guessesremaining = ""
$words = @()
$words[0] = @("","","","","","","","","","","","","","","")
$words[1] = @("c","o","m","m","a","n","d","e","r")
$words[2] = @("f","l","a","g")
$words[3] = @("t","o","a","s","t","e","r")
$words[4] = @("m","a","r","k","e","r")
$words[5] = @("P","i","c","t","u","r","e")
$words[6] = @("d", "e","s","k")
$words[7] = @("g","o","b","l","e")
$words[8] = @("s","p","e","a","k","e","r")
$words[9] = @("b","a","c","k","y","a","r","d")
$words[10] = @("p","e","n","c","i","l")
function check-answer
{param($reply)
$script:guesses = $script:guesses + "" + $reply
for ($i = 0; $i -le $secrectword.length -1; $i++)
{
if($secretword[$i] -ne $reply)
{ if ($words[0][$i] -eq "")
{$words[0][$i] = "_"}
}
else {
$words[0][$i] = $reply
}
}
}
# I am sorry but I am not artisticly inclined enough to draw a hangman.
write-host "`n`n`n`n welcome to the "
write-host "`n`n`n`n PowerShell Hangman game"
$response = read-host "`n`n`n`n`n`n`n`n`n`n Would you like to play? (y/n)?"
if($response -eq "y")
{$playgame = "true"
}
elseif($response -eq "n")
{clear-host
"`n`n please visit again soon"
read-host
exit
}
else{
clear-host
write-host "`n`n I am sorry, I did not uderstand your answer, please use `"y`" or `"n`" "
read-host
}
While($status -eq "true")
{$tempstring = ""
$words[0] = @("","","","","","","","","","","","","","","")
$attempts = 0
$guesses = ""
$reply = ""
$randomno = get-random -minimum 1 -maximum 11
$secretword = $words[$number]
while($reply -eq "")
{clear-host
$reply= read-host "`n`n`n Please enter a guess"
if($reply -eq "")
{ continue
}
if($reply.length -gt 1)
{clear-host
write-host "`n`n I am sorry I can only process one character at a time"
$reply = ""
continue
}
if((123456789 -match $reply) -or ($rejectlist -match $reply))
{ clear-host
write-host "`n`n I am sorry I can only process Alphabetic characters "
read-host "`n`n`n`n`n`n`n`n`n`n press enter to continue"
$reply = ""
}
clear-host
$attempts++
check-answer $reply
$tempstring = ""
for ($i = 0; $i -le $word[0].length -1; $i++)
{$tempstring = $temstring + "" + $word[0][$i]
}
write-host "`n`n Results"
write-host "----------------------`n"
write-host "$tempstring `n"
write-host "---------------------- `n"
write-host "Letters that have been guessed $guesses `n"
$guessesremaining = (12- $attempts)
write-host "number of guesses remaining: $guessesremaining"
read-host "`n`n`n`n`n`n Press enter to continue "
if($tempstring -notmatch "_")
{ write-host "`n Game Over. You have guessed the secret word!"`
"in $attempts guesses. `n`n"
write-host "The secret word was $secretword `n`n"
write-host "`n`n`n`n`n`n`n`n`n`n`n`n`n"
read-host
$reply = "done"
continue
}
if($attempts -eq 12)
{clear-host
write-host "`n Game Over You have exceeded the maximum number of guesses allowed"
write-host " The secret word was $secretword `n`n"
write-host "The best you could do was $tempstring `n`n`n`n`n`n"
read-host
$reply = "done"
continue
}
$reply = ""
}
$response = ""
$validreply = "false"
while($validreply -ne "true")
{clear-host
$response = read-host "`n`n Would you like to play again (y/n)?"
if($response -eq "y")
{$vaildreply = "true"
$status = "true"
}
elseif($response -eq "n")
{write-host "Please play again soon"
read-host
$validreply = "true"
$status = "false"
}
elseif(($response -ne "y") -and ($response -ne "n"))
{
clear-host
write-host "I am sorry, I can only accept y or n as an answer. Press enter to try again"
$validreply = "false"
read-host
}
}
}