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!

actually, I have my files both the grep and awk ways, but decided I should use awk because of the string length func with grep I just wrote four times [A-Za-z] and used ? anyway, the word lists are OK, just i don't know why it prints an over 10 lettered word if I choose up to 4 letter option, and does it really use this word it prints, i use the print just for debugging and to make sure it chooses a correct value, but how am I to know, if it, say, chooses the right word, but just the print is printing sth else (I still hope, this might be so haha :)

And Happy Easter! 

Comments

  • The only thing I can think of would be to check that your files have the words in the right format - if it can't find any commas, it won't splt on them. I'd personally have one word per line, which would also reduce the code you'd need - you wouldn't have to split at commas.

    A couple of things I'd suggest:
    Instead of storing the whole file to memory at once, read it, but don't store it, and use the $. var to check how many lines there are. Then rand that number, set $. back to 0, and set a loop going through however many times, setting a var to that current line. At the end of the loop, it should hopefully still store the line you wanted, and you won't have stored an entire file. At the very least, you should undef @ines as soon as it's no longer needed.

    Also, instead of having three almost identical subs, just have one to get the file. When the user says what difficuly they want, set $filename to easy, hard, or medium_list and use that in the open. Reduces maintenance and file space.
  • Thanks a lot for the help and ideas, actually I figured the thing out, but the whole of my program's just working until a certain time. I need it to get working before i engage in any changes to save memory and time :( I just want it to get it work first, but I get this error message at runtime and it all stops:


    Welcome to Hangman!!!

    Now tell us your name first>> Ralica

    We will try to hang you now,Ralica
    Brrrrr.....

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

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

    *---------------------------------------------------*

    * Choose your mode and difficulty level !!! *

    *---------------------------------------------------*

    * Press 1 for Single Player *

    * Press 2 for Challenge mode *

    * Press 3 to Quit *

    *And now your choice is: 1

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

    So, which level do you choose? >> 1
    Use of uninitialized value in split at ./pro.pl line 141 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl tells you what operation
    you used the undefined value in. Note, however, that perl optimizes your
    program and the operation displayed in the warning may not necessarily
    appear literally in your program. For example, "that $foo" is
    usually optimized into "that " . $foo, and the warning will refer to
    the concatenation (.) operator, even though there is no . in your
    program.

    Use of uninitialized value in substitution (s///) at ./pro.pl line 143 (#1)
    Use of uninitialized value in split at ./pro.pl line 144 (#1)


    Use of uninitialized value in concatenation (.) or string at ./pro.pl line 201 (#1)
    --------------------------------------------------------
    Use of uninitialized value in concatenation (.) or string at ./pro.pl line 202 (#1)
    Clue:
    Word:
    Guess list: []

    --------------------------------------------------------------


    Guess or just hit enter.
    Guess:w

    Guess:w

    Letter, please.
    Letter:i


    Use of uninitialized value in concatenation (.) or string at ./pro.pl line 201,
    line 5 (#1)
    --------------------------------------------------------
    Clue:
    Word:
    Guess list: []

    --------------------------------------------------------------


    Guess or just hit enter.
    Guess:r

    Guess:r
    [rpy020@linux hangman]$







    Something's not quite OK around my $index :(
    This is the code so far, of course it's to be improved once it starts working at all :(:





    [rpy020@linux project]$ vi hangman.pl
    #!/usr/bin/perl -w

    use strict;
    use diagnostics;

    use constant MAX_MISSES => 10;



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


    my ($name, $mode, $continue,$level, $clue ,$word,$guess, $wrong_letters, @letters, @word, @display,$display,$correct_guess);

    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 "
    * *";
    "hangman.pl" 357L, 7866C 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



    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);

    system("clear");

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






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

    "hangman.pl" 357L, 7866C 83,0-1
    86 else {
    87 print"
    ________________________________________________________________________________";
    88 print"
    |* This is really cool you want us to hang you but please choose your level first!*|";
    89 print"
    |* Choose a number as indicated below: *|";
    90 print"
    |* -- *|";
    91 print"
    |* | *|";
    92 print"
    |* ------O-------> 1.Press 1 for short words up to 4 letters. *|";
    93 print"
    |* -^- *|";
    94 print"
    |* ------|-------> 2.Press 2 for longer words up to 7 letters. *|";
    95 print"
    |* / \ *|";
    96 print"
    |* --------------> 3.Press 3 or die hard , for words over 7 letters of length. *|";
    97 print"
    |*________________________________________________________________________________*|";
    98
    99 sleep(2);
    100
    101 print"

    So, which level do you choose? >> ";
    102 $level=;
    103
    104 chomp($level);
    105
    106
    107 system("clear");
    108
    109
    110 if ($level eq "1"){
    111
    112
    113
    114
    115
    116 &FetchWordEasy;
    117
    118
    119
    120 }
    121
    122 if ($level eq "2"){
    123
    124
    125
    126 &FetchWordMedium;
    127
    128
    :set number 131 }
    132 else {
    133
    134
    135
    136 &FetchWordHard;
    137
    138
    139
    140 }
    141
    142 $wrong_letters=0;
    143 @letters=();
    144 @word=split(//,$word);
    145 $display=$word;
    146 $display=~s/[a-zA-Z]/-/g;
    147 @display = split(//, $display);
    148 $correct_guess=0;
    149
    150 &printResult;
    151
    152
    153 while($wrong_letters < 10 and $correct_guess == 0) {
    154
    155 my ($index, $correct_letter) = (0,0);
    156
    157 print "
    Letter, please:";
    158 my ($letter)=;
    159 chomp($letter);
    160
    161 $letter=lowercase($letter);
    162
    163 if(join("", @letters)=~$letter) {
    164 print"-------------------------------------------------";
    165 print"
    You have already guessed that letter.
    ";
    166 print"-------------------------------------------------";
    167 next;
    168
    169 }
    :set number

    169 }
    170
    171 push(@letters, $letter);
    172
    173 foreach (@word) {
    174
    175 if (lowercase($_) eq $letter) {
    176 $display[$index] = $_;
    177 $correct_letter = 1;
    178 }
    179 $index+=1;
    180 }
    181
    182 $display = join("", @display);
    183 if ($display!~ m/-/) {
    184 &printResult;
    185 last;
    186 }
    187
    188 if ($correct_letter==0) {
    189 $wrong_letters+=1;
    190 print "
    You guessed wrong! Death is getting closer! Ha-Ha-Ha!!!";
    191 }
    192 else {print "
    Damn it! You guessed right!
    ";
    193
    194 &printResult;
    195
    196 }
    197
    198 print "
    Game over.
    ";
    199 if($wrong_letters != 10) {
    200 print"Congratulations. You won!!!";
    201 }
    202 else{
    203 print "Sorry. You are dead!";
    204 }
    205 print "
    How about another game? :)
    ";
    206
    207 $continue = ;
    208 chomp($continue);
    209
    210
    211 }
    :set number sub FetchWordEasy {
    214 #my $word = "superman";
    215 my @lines;
    216 my @words;
    217 my $line_count = 0;
    218 my $word_count = 0;
    219 my $rand_line = 0;
    220 my $rand_word = 0;
    221 open( DICTIONARY, "easy_list" ) or die "Cannot open file: $!
    ";
    222 @lines = ;
    223 close( DICTIONARY );
    224 $line_count = @lines;
    225 $rand_line = int(rand $line_count);
    226 @words = split(/,/, $lines[$rand_line] );
    227 $word_count = @words;
    228 $rand_word = int(rand $word_count);
    229 my $word = $words[$rand_word];
    230 # print "$word";
    231 chomp($word);
    232 return $word;
    233
    234 }
    235
    236
    237
    238
    239
    240 sub FetchWordMedium {
    241 #my $word = "superman";
    242 my @lines;
    243 my @words;
    244 my $line_count = 0;
    245 my $word_count = 0;
    246 my $rand_line = 0;
    247 my $rand_word = 0;
    248 open( DICTIONARY, "medium_list" ) or die "Cannot open file: $!
    ";
    249 @lines = ;
    250 close( DICTIONARY );
    251 $line_count = @lines;
    252 $rand_line = int(rand $line_count);
    253 @words = split(/,/, $lines[$rand_line] );
    :set number
    254 $word_count = @words;
    255 $rand_word = int(rand $word_count);
    256 my $word = $words[$rand_word];
    257 # print"$word";
    258 chomp($word);
    259 return $word;
    260
    261 }
    262
    263
    264
    265
    266
    267
    268
    269 sub FetchWordHard {
    270 #my $word = "superman";
    271 my @lines;
    272 my @words;
    273 my $line_count = 0;
    274 my $word_count = 0;
    275 my $rand_line = 0;
    276 my $rand_word = 0;
    277 open( DICTIONARY, "hard_list" ) or die "Cannot open file: $!
    ";
    278 @lines = ;
    279 close( DICTIONARY );
    280 $line_count = @lines;
    281 $rand_line = int(rand $line_count);
    282 @words = split(/,/, $lines[$rand_line] );
    283 $word_count = @words;
    284 $rand_word = int(rand $word_count);
    285 my $word = $words[$rand_word];
    286 #print "$word";
    287 chomp($word);
    288 return $word;
    289
    290 }
    291
    292
    293
    294 sub challenge {
    295
    296
    :set number
    297 print"

    So you got all to scared and decided to hang someone else, huh?";
    298 print"
    ---------------------------------------------------------------";
    299 print"
    Please enter a word!

    ";
    300 my ($ownword);
    301 $ownword=;
    302 chomp($ownword);
    303 print "
    Please, be nice and enter a clue!

    ";
    304 my ($clue);
    305 $clue=;
    306 chomp($clue);
    307 print"
    ----------------------------------------------------------------
    ";
    308 }
    309
    310
    311
    312
    313
    314 sub printResult {
    315
    316 print "

    ------------------------------------------------------------------
    ";
    317 print"
    Clue:$clue";
    318 print"
    Word:$display";
    319 print"
    Guess list: [".join(",",@letters)."]";
    320
    321
    322 print"

    ";
    323
    324 if($wrong_letters == 1) {print"
    - ";}
    325 if($wrong_letters == 2) {print"-";}
    326 if($wrong_letters == 3) {print"|";}
    327 else {print"
    .....whatever....";}
    328
    329 print"
    ......whatever ....."; 330 }
    331
    332 print"-------------------------------------------------------------------------
    ";
    333
    334 if ($correct_guess != 1 and $wrong_letters == 10) {
    335
    336 print"

    Come on, guess!!! Just hit enter if you have no idea.";
    337
    338 $guess=;
    339 chomp($guess);
    340 print"
    Guess: $guess
    ";
    341
    342 }
    343
    344 if (lowercase($guess) eq lowercase($word)) {
    345 $correct_guess = 1;
    346 }
    347 else { print "
    Incorrect guess. You will lose if you keep up the pattern :)
    ";
    348
    349 }
    350
    351 sub lowercase {
    352
    353 my $temp=$_[0];
    354 $temp =~ tr/[A-Z]/[a-z]/;
    355 return $temp;
    356 }
    357 }









  • : 141
    : 142 $wrong_letters=0;
    : 143 @letters=();
    : 144 @word=split(//,$word);
    : 145 $display=$word;
    : 146 $display=~s/[a-zA-Z]/-/g;
    : 147 @display = split(//, $display);
    : 148 $correct_guess=0;



    In the code example you have to declare $word before line 144

    the first place it actually gets assigned anything is
    : 229 my $word = $words[$rand_word];

  • ...which is in a subroutine which gets called before $word is actually used. ;-)

    However, you're close. $word is declared in each fetchword subroutine, but with a my construct - in other words, it's local. As soon as the subroutine ends, $word becomes undef. The solution is to either declare $word outside of the subroutine (and before it gets called), or to inititialise it with our $word to make it global. I don't know if that's going to fix all the errors, but it might do - errors breed fast, but they're often dependant on their parents.
  • wow, your last comment real helped, this $word wasn't just going the same everywhere, so what I did was just delete all those FetchWord subs and just open different files as you had indeed proposed :)
    but now again it is not printing the word with the right lenghth, while just the subs by themselves did, now why is that do you know?
    here's what I have now, for the clue I'll think of some way to analyse the word and tell the user how many vowels there are or sth , anyway, I'll do that letter, i have the word printed any time u call for a whatever level and it always comes sth word longer than four now again if i choose level one. Thanx for any ideas! :)

    this is the code:

    use strict;
    use diagnostics;


    use constant MAX_MISSES => 10;




    sub lowercase;


    my ($letter, @letters, $display, @display, @word, $word_letters, $clue,$name, $mode, $continue,$level ,
    $wrong_letters, $guess, $correct_guess, $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



    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);

    system("clear");

    if ($mode ne "1" and $mode ne "2")
    {exit;}
    else {$continue = "y";}
    while($continue eq "y") {
    if ($mode eq "2") {
    print"So you got all to scared and decided to hang someone else, huh?";
    print"
    ---------------------------------------------------------------";
    print"
    Please enter a word!

    ";
    my ($ownword);
    ($ownword)=;
    chomp($ownword);
    print "
    Please, be nice and enter a clue!

    ";
    my ($clue);
    $clue=;
    chomp($clue);
    print"
    ----------------------------------------------------------------
    ";
    }

    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"

    ";

    print"

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

    chomp($level);

    system("clear");


    if ($level eq "1"){





    open( DICTIONARY, "easy_list" ) or die "Cannot open file: $!
    ";





    }

    if ($level eq "2"){

    open( DICTIONARY, "medium_list" ) or die "Cannot open file: $!
    ";

    }
    else {

    open( DICTIONARY, "hard_list" ) or die "Cannot open file: $!
    ";


    }





    my @lines = "";
    my @words = "";
    my $line_count = 0;
    my $word_count = 0;
    my $rand_line = 0;
    my $rand_word = 0;
    @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);
    our $word ; $word = $words[$rand_word];
    print "$word";
    chomp($word);


    $wrong_letters = 0;
    @letters = ();
    @word = split(//, $word);
    $display = $word;
    $display =~s/[a-zA-Z]/-/g;
    @display = split(//, $display);
    $correct_guess = 0;

    while($wrong_letters < 10 and $correct_guess == 0) {
    my ($index, $correct_letter) = (0,0);

    print "
    Letter, please.";
    print"
    Letter:";
    $letter = ;
    chomp($letter);

    $letter = lowercase($letter);

    if (join("", @letters)=~$letter){
    print"
    You already guessed that letter.
    ";
    next;
    }
    push(@letters, $letter);

    foreach(@word) {
    if (lowercase($_) eq $letter){
    $display[$index]=$_;
    $correct_letter = 1;
    }
    $index +=1;
    }
    $display = join("", @display);
    if ($display !~ m/-/) {
    &print_results;
    last;
    }

    if ($correct_letter == 0) {
    $wrong_letters += 1;
    print "
    You guessed wrong!";
    }
    else {print"
    You guessed right!";}



    @letters = "";
    print "

    --------------------------------------------------------";
    my $clue = "some clue here";
    print"
    Clue: $clue";
    print"
    Word: $display";
    print"
    Guess list: [".join(",",@letters)."]";
    print"

    ";
    print"--------------------------------------------------------------";
    print "
    ";
    if ($wrong_letters > 0 && $wrong_letters < 10) { print " do something";}
    elsif ($wrong_letters == 10) {print "do something else";}
    if($correct_guess != 1 and $wrong_letters < 6){
    print "

    Guess or just hit enter.";
    print"
    Guess:";
    $guess=;
    chomp($guess);
    print"
    Guess:$guess
    ";
    }
    else {print "
    Incorrect guess. You'll soon be mien.
    ";}


    print"
    Game over.
    ";
    if ($wrong_letters != 10) {
    print"Congrats!You survived!";
    }
    else{
    print"Sorry, you lose.";
    }
    print "
    Do u want to play again?
    ";
    $continue=;
    chomp($continue);
    }


    sub lowercase {
    my $temp = $_[0];
    $temp =~ tr/[A-Z]/[a-z]/;
    return $temp;
    }
    }}




    And sample run if you might wanna look at it:
    [rpy020@linux project]$ ./hangman.cp project

    Welcome to Hangman!!!

    Now tell us your name first>> Ralica

    We will try to hang you now,Ralica
    Brrrrr.....

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

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

    *---------------------------------------------------*

    * Choose your mode and difficulty level !!! *

    *---------------------------------------------------*

    * Press 1 for Single Player *

    * Press 2 for Challenge mode *

    * Press 3 to Quit *

    *And now your choice is: 1

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



    So, which level do you choose? >> 1
    intubated

    Letter, please.
    Letter:a

    You guessed right!

    --------------------------------------------------------
    Clue: some clue here
    Word: -----a---
    Guess list: []

    --------------------------------------------------------------


    Guess or just hit enter.
    Guess:a

    Guess:a

    Game over.
    Congrats!You survived!
    Do u want to play again?


    etc. i still have to fix the logic of print messages here haha :))

    Thanx a lot for reading this, u've been a lot of help so far anyway.











  • [b][red]This message was edited by gorilka at 2004-4-13 3:55:51[/red][/b][hr]
    it seems like my input for $level doesn't really get input and it always goes to the else condition, and it always chooses an up to 10- leter- word because my last else opens the hard list, so i deleted this system("clear") I had (why was I having it at all?:) put right after receiving user's input for level and now it seems to be working fine.

    Question:

    Does system("clear") really clears user's input from being prcessed?! I thought it'd just clear the screen!

  • just a thought:

    print "
    Do u want to play again?
    ";
    $continue=;
    chomp($continue);
    --> if ($continue eq 'n') { exit; }

  • No, I didn't avoid this thread on purpose... :-)

    : Does system("clear") really clears user's input from being
    : prcessed?! I thought it'd just clear the screen!
    :
    It's possible (and my best guess would be) that it clears the keyboard input buffer, so when Perl comes to read it there is nothing to read. Or something of the like...

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • system("clear") is just supposed to clear the screen, like system("cls") on a DOS system
  • yeah, it is just to clear the screen but it seems like it's preventing the data to be processed sometimes, at least when I changed the place of my system("clear") my if statements finally got to work the way they should be according to the user input, while otherwise it just skipped them and executed the else, like here i wrote this to test it,
    it doesn't display the prompt and even if you write some input it doesn't take it into account, at least it seems like that to me, and like Jonathan says :)


    #!/usr/bin/perl -w

    use strict;
    use diagnostics;


    print"Type 50 please! >>";

    system("clear");

    my $num=;



    if ( $num eq "50") {
    print"Yes, I got it! You typed, $num";

    }


    else {print"No, I didn't get anything :(";}

    {exit;}




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