Hello monks!
I'm new to perl and wanted to ask the people here if they could help me fix a simple
hangman program that i cant get to work.
I'm trying to get this program to open the file "dictionary.txt"
Then i want to assign that file to @words. However, immediately there's a problem with that, two question marks pop up at the beginning which were not in the original file (I've tried using the chomp function).
I then want to choose a word at random from that file and split it so that each letter is a different element in @letters so that i can turn the number of elements into a single number. The array @blankword is used to mark which letter the player has guessed successfully. (0) x scalar(@letters) creates a list that is as long as the number of elements in @letters, which is stored in @blankword. As letters are guessed, these 0s are changed to letters which will mark the positions of the correctly guessed letters.But the number of 0s are always too many.
This is the code for my program.
#!/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";
The following code works ok but i cant seem to change it to get it to choose the words from the dictionary file.
#!/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";
and here is the contents of the dictionary file which i think should be readable from perls perspective.
Printer
Internet
Mouse
Keyboard
Monitor
Thank you for reading this and i would appreciate it very much if you could help me.
elle45purple
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.