我似乎找不到我的 Perl Hangman 代码的原因:
#!\usr\bin\perl
use 5.016;
&hangman;
sub hangman{
our(@graphics,@words,$word,@chars,$hidden,$incorrect,$guess,);
@graphics=("=========",
" |\n |\n |\n |\n |\n=========",
" ======\n |\n |\n |\n |\n |\n=========",
" ======\n | |\n |\n |\n |\n |\n=========",
" ======\n | |\n O |\n |\n |\n |\n=========",
" ======\n | |\n O |\n | |\n |\n |\n=========",
" ======\n | |\n O |\n /| |\n |\n |\n=========",
" ======\n | |\n O |\n /|\\ |\n |\n |\n=========",
" ======\n | |\n O |\n /|\\ |\n / |\n |\n=========",
" ======\n | |\n O |\n /|\\ |\n / \\ |\n |\n=========");
@words=qw(ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra);
$word=$words[rand scalar@words];
for(0..length$word){
@chars=split("",$word);
}
$hidden="-"x length($word);
while($incorrect<0+@graphics){
print"Word: $hidden\nGuess a letter: ";
chomp($guess=<STDIN>);
foreach(@chars){
if($guess==$_){
substr($hidden,$_)
}
}
}
}
始终if
在第 25 行运行该语句。我正在尝试创建一个if
语句,以便它仅在存在时运行guess
任何word
人都知道为什么会发生这种情况以及如何阻止它?
谢谢。