Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl Hangman

by Nimster (Sexton)
on Nov 16, 2000 at 19:04 UTC ( #41969=sourcecode: print w/ replies, xml ) Need Help??

Category: Fun Stuff
Author/Contact Info Nimster nimster@netvision.net.il
Description: A newbie's hangman in perl. Not the most effective, but works. Features are score, completion of the word from user input, and a slowly dying hangman. CGI version soon.
#!/usr/bin/perl -w
use strict;

my ($miss, $guesses, $guess, @build, $tries, @guessed, @wordbank);

@wordbank=qw(cubicle scramble deduction envelope century rediculous); 
+#as many words as you like.
my $selected=rand(scalar(@wordbank));
my (@word)=split(//, $wordbank[$selected]);
@build=("_") x scalar(@word);
$_=join('',@word);
$miss=0; $guesses="0-<--<"; $tries=0;



print "Welcome to HangPerl Hangman ver 0.56... mentained by: Nimster\n
+";
print "Word has "; print scalar(@word); print " letters\n";
print "Your hangman: "; print $guesses; print "\n";
print "Good luck! Start guessin`!\n";

while (1) 
  {
  last if $miss==length($guesses);
  last if join('',@build) eq join('',@word);
  $guess=<STDIN>; chomp $guess;
  $guess=lc(substr($guess,0,1));
  $tries+=1;
  if (m/$guess/) 
    {
    print "right!\n";
    for(my $index=0; $index<scalar(@word); $index++) 
      {
        $build[$index]=$word[$index] if ($word[$index] eq $guess);  
      }
    
    foreach my $letter (@build) 
      {
      print "$letter ";
      }
    print "\n";
    }
  else 
    {
    $miss++;
    print substr($guesses, $miss, length($guesses)); print "\n";
    }
  }
print "It took you $tries tries ($miss misses) and ";
if ($miss < length($guesses)) { print "You made it!\n"; }
else { print "You failed.\n"; }

#it can be made much more effective easily (elimination of @word/$curr
+word which are basically the same, same with some more variables, so 
+changes in the algorythm, etc. However, this is my first try at a rea
+l, kinda full perl program. Soon to be ported to CGI, then maybe I'll
+ add DBM highscores, etc.

Comment on Perl Hangman
Download Code
Re: Perl Hangman
by japhy (Canon) on Nov 16, 2000 at 23:10 UTC
    I'd suggest a modification by using hashes instead of arrays wherever sensible. Code will be upcoming in a node called hangmonk (this node will be updated upon existence of the hangmonk node).

    UPDATE hangmonk is now up.

    $monks{japhy}++ while $posting;
Re: Perl Hangman
by Dominus (Parson) on Nov 17, 2000 at 00:01 UTC
    You're really going to piss off the first person who gets stuck with 'rediculous'.

      Well the code is 'mentained' so that can be fexed, I thenk.

      ;-)

        I'll piss off the first one who'll have to spell
Re: Perl Hangman
by Anonymous Monk on Mar 25, 2008 at 18:44 UTC
    And I shall comment since no one has in Eight Years! Berko

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://41969]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-09-23 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (234 votes), past polls