elle45purple has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w open (DICT, "/Users/programming/dictionary.txt"); @words=<DICT>; @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @letters=split(//, $choice); @hangman=split(/,/, $hangman); @blankword=(0) x scalar(@letters); Outer: while ($wrong<@hangman) { foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman [0..$wrong-1] } print "\n Your Guess: "; $guess=<STDIN>; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $guesses[@guesses]=$guess; $right=0; for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ if (not $right); if (join('', @blankword) eq $choice) { print "you got it right!\n"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n";
#!/usr/bin/perl -w @words=qw(printer internet); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @letters=split(//, $choice); @hangman=split(/,/, $hangman); @blankword=(0) x scalar(@letters); Outer: while ($wrong<@hangman) { foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman [0..$wrong-1] } print "\n Your Guess: "; $guess=<STDIN>; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $guesses[@guesses]=$guess; $right=0; for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ if (not $right); if (join('', @blankword) eq $choice) { print "you got it right!\n"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Assigning a file to an Array.
by kyle (Abbot) on Feb 12, 2009 at 16:02 UTC | |
|
Re: Assigning a file to an Array.
by olus (Curate) on Feb 12, 2009 at 15:43 UTC | |
|
Re: Assigning a file to an Array.
by ikegami (Patriarch) on Feb 12, 2009 at 15:15 UTC | |
|
Re: Assigning a file to an Array.
by JavaFan (Canon) on Feb 12, 2009 at 15:20 UTC | |
|
Re: Assigning a file to an Array.
by Zen (Deacon) on Feb 12, 2009 at 15:28 UTC | |
|
Re: Assigning a file to an Array.
by Ciclamino (Sexton) on Feb 12, 2009 at 15:47 UTC | |
by Bloodnok (Vicar) on Feb 12, 2009 at 16:58 UTC | |
by elle45purple (Initiate) on Feb 12, 2009 at 16:05 UTC |