in reply to Re^3: hangman question
in thread hangman question

I think now I am understanding the script. However, I ended up commenting out the @guesses portion (mainly because I think @guesses is useless part and script still works)..
Was author wrong to put that code there(since it does nothing?).
#!/usr/bin/perl -w @words = qw( hi no ); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman="0-|--<"; @letters=split(//, $choice); print "\@letters is @letters\n"; @hangman=split(//, $hangman); @blankword=(0) x scalar(@letters); print "\@blankword is @blankword\n"; OUTER: while ($wrong<@hangman) { print "whielloop\n"; foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1] } print "\n\n\n\n Your Guess: "; $guess=<STDIN>; chomp $guess; # foreach(@guesses) { # print "foreach loop\n"; # print "\$guess is $guess\n"; # print "\$\_ is $_\n"; # next OUTER if ($_ eq $guess); # } # print "\@guesses is @guesses\n"; # $guess_scalar = "@guesses"; # print "\$guess_scalar is $guess_scalar\n"; #$guesses[@guesses]=$guess; # push @guesses, $guess; $right=0; for ($i=0; $i<@letters; $i++) { print "\$i is $i\n"; print "in the forloop\n"; if ($letters[$i] eq $guess) { $blankword[$i]=$guess; print "\$blankword\[\$i\] is $blankword[$i] +\n"; $right=1; } } $wrong++ if (not $right); print "\$wrong is $wrong\n"; if (join('', @blankword) eq $choice) { print "IF loop\n"; print "You got it right!\n"; exit;

Replies are listed 'Best First'.
Re^5: hangman question
by ysth (Canon) on Jul 16, 2007 at 02:46 UTC
    @guesses and the next OUTER were making it ignore guesses that had already been given instead of counting them as right or wrong. It would have been better to use a %guesses hash for that:
    ... $guess=<STDIN>; chomp $guess; next if $guesses{$guess}++; $right=0; ...
    but this program seems blithely ignorant of the existence of hashes.

    If you feel daring, take a look at the severely undercommented word twist for a more hash-ridden implementation of a word game.

      word twist.. is crazy.. well for me. too much at this stage.. but I will try to understand it.. I just ran the program and I am not even sure what it's doing.. but looks very interesting.