Help

Hello!

I am coding a hangman game on Perl.
I provide three options for the difficulty level:

1. randomly choose a word from a file containing up to 4 letters only ( my file is called easy_list);
2.randomly choose a word from a file containing up to 7 but more than 4 letters only ( my file is called medium_list);
3.a word from a file containing up to 10 but more than 7 letters only ( my file is called hard_list);

The files I get by grepping the /usr/share/dict/words library like this:

[rpy020@linux hangman]$ cat list
#!/bin/bash
cat /usr/share/dict/words | awk ' length <=4 {print $0}'> easy_list
Ctrl -d

[rpy020@linux hangman]$ cat list01
#!/bin/bash
cat /usr/share/dict/words | awk ' length >4 && length <=7 {print $0}'> medium_list
Ctrl -d

[rpy020@linux hangman]$ cat list02
#!/bin/bash
cat /usr/share/dict/words | awk ' length >7 && length <=10 {print $0}'> hard_list
Ctrl -d

So that I now have these three files (easy_list, medium_list, hard_list) the lenght I want them to have.

I have alll these in my plus my maim hangman.pl file in my ~/hangman/
directory, I invoke my program like this:

./hangman.pl ~/hangman


And this is the code I've written so far:




#!/usr/bin/perl -w

use strict;
use diagnostics;

use constant MAX_MISSES => 10;



sub challenge;
sub FetchWordEasy;
sub FetchWordMedium;
sub FetchWordHard;




my ($name, $mode, $continue, $level, $word);

system ('clear');

print "
Welcome to Hangman!!!
";
print "
Now tell us your name first>> ";
$name=;
print "
We will try to hang you now,$name
";

sleep(1);

system ('clear');

print "Brrrrr.....
";

sleep(3);

system('clear');

print "
Well, unless you break free, you will be hanged right after your 10th guess!!! Ha - Ha - Ha!
";

sleep(3);

system('clear');

print "
****************************************************";
print "
* *";
print "
* --| Remember!!! You only have |-- *";
print "
* O 10 guesses to make until O *";
print "
* _^_ you be hanged!!! _^_ *";
print "
* | | *";
print "
* / \ / \ *";
print "
****************************************************
";

sleep(6);

#mei this is to display the menu for single and double player mode, for
#quit mode and maybe for giving clues

system('clear');

print "
*---------------------------------------------------*
";
print "
* Choose your mode and difficulty level !!! *
";
print "
*---------------------------------------------------*
";
print "
* Press 1 for Single Player *
";
print "
* Press 2 for Challenge mode *
";
print "
* Press 3 to Quit *
";


print "
*And now your choice is: ";
$mode=;
chomp($mode);

if ($mode ne "1" and $mode ne "2")
{exit;}






if($mode eq "2") {
&challenge;
}



else {
print"
________________________________________________________________________________";
print"
|* This is really cool you want us to hang you but please choose your level first!*|";
print"
|* Choose a number as indicated below: *|";
print"
|* -- *|";
print"
|* | *|";
print"
|* ------O-------> 1.Press 1 for short words up to 4 letters. *|";
print"
|* -^- *|";
print"
|* ------|-------> 2.Press 2 for longer words up to 7 letters. *|";
print"
|* / \ *|";
print"
|* --------------> 3.Press 3 or die hard , for words over 7 letters of length. *|";
print"
|*________________________________________________________________________________*|";

print"

So, which level do you choose? >> ";
$level=;

chomp($level);


if ($level eq "1"){


&FetchWordEasy;



}

if ($level eq "2"){

&FetchWordMedium;


}
else {

&FetchWordHard;

}



sub challenge {
print"

----------------------------------------------------------------------------------------";
print"
Please enter a word or phrase:
";
}

sub FetchWordEasy {
#my $word = "superman";
my @lines;
my @words;
my $line_count = 0;
my $word_count = 0;
my $rand_line = 0;
my $rand_word = 0;
open( DICTIONARY, "easy_list" ) or die "Cannot open file: $!
";
@lines = ;
close( DICTIONARY );
$line_count = @lines;
$rand_line = int(rand $line_count);
@words = split(/,/, $lines[$rand_line] );
$word_count = @words;
$rand_word = int(rand $word_count);
my $word = $words[$rand_word];
chomp($word);
return $word;

}

sub FetchWordMedium {
#my $word = "superman";
my @lines;
my @words;
my $line_count = 0;
my $word_count = 0;
my $rand_line = 0;
my $rand_word = 0;
open( DICTIONARY, "medium_list" ) or die "Cannot open file: $!
";
@lines = ;
close( DICTIONARY );
$line_count = @lines;
$rand_line = int(rand $line_count);
@words = split(/,/, $lines[$rand_line] );
$word_count = @words;
$rand_word = int(rand $word_count);
my $word = $words[$rand_word];
chomp($word);
return($word);


}


sub FetchWordHard {
#my $word = "superman";
my @lines;
my @words;
my $line_count = 0;
my $word_count = 0;
my $rand_line = 0;
my $rand_word = 0;
open( DICTIONARY, "hard_list" ) or die "Cannot open file: $!
";
@lines = ;
close( DICTIONARY );
$line_count = @lines;
$rand_line = int(rand $line_count);
@words = split(/,/, $lines[$rand_line] );
$word_count = @words;
$rand_word = int(rand $word_count);
my $word = $words[$rand_word];
chomp($word);
return($word);




}
}


So, it just welcomes the user and chooses a random word so far.
The problem is, when I choose an up to 4 letters mode and I put a print ($word) it returns a word even longer than 10 words, what's wrong with my subs? Please help me!

Thanx a lot!








Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories