Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
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);
$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;
  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";
    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.
Replies are listed 'Best First'.
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 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 Anonymous Monk on Mar 25, 2008 at 18:44 UTC
    And I shall comment since no one has in Eight Years! Berko
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://41969]
[Mr. Muskrat]: (considered) Re^3: Please help with Regexp::Common needs to be reparented to Re^2: Please help with Regexp::Common
[Mr. Muskrat]: Is it just me or does that timeout issue seems to be happening more often lately?
[Corion]: Mr. Muskrat: I'm not sure if it really happens more often, but I don't exactly know either
[LanX]: yep
[LanX]: more often for some weeks now
[Corion]: I think I'll have to manually (as god) intervene with that node, as the simple reparenting didn't seem to fix the parent/child relationship of the nodes
[Corion]: I think I have an idea but I'll have to open a ticket with on that - hopefully I get to that on the weekend
LanX imagines a burning thorn bush

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-01-19 16:22 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.