Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Hangman

    Can someone help with my code? When the user enters "c" in the text
    box it fills in c_ _, which is great. But when they enter the next
    letter, like "a". It erases the c_ _ and produces _ a _. I need to
    keep it to be ca_. Also how do you get the number of wrong guesses in
    a string? Example, j,l,u,v etc.so the user knows the letters they have
    entered?
    #!C://Perl/bin/perl


    use CGI ':standard';

    use CGI::Carp 'fatalsToBrowser';

    use constant MAX_MISSES => 8;


    &display();

    my $word;
    $word = "cat";
    my $misses_left = MAX_MISSES;
    my %letters = map {$_ => 1} split //, $word;;
    my %guesses = ();

    if (param()) {

    my $guess = param('guess');
    &processguess($guess);



    sub processguess{


    if (exists $guesses{$guess}) {
    print qq(You\'ve already guessed "$guess".\n);

    }

    $guesses{$guess}++;

    if (exists $letters{$guess}) {
    delete $letters{$guess};
    keys %letters or last GUESSES;

    } else {
    --$misses_left;
    print "another";

    }
    if ($misses_left > 0)
    {
    print "Your guess is: ", join(' ', sort keys %guesses), "\n";
    print "You have $misses_left misses left.\n\n";
    print join(' ', map {exists $guesses{$_} ? $_ : '_'} split //,
    $word), "\n";
    my $guess;

    }

    }
    }
    }
    sub display {


    my $the_cookie = cookie (
    -name=>'gameid',
    -value=>$id,
    -expires=>'+1h');

    print header(-cookie=>$the_cookie),
    start_html(
    -title=>"Hangman!",
    -onLoad=>"document.forms[0].guess.select()"),

    h1("Hangman!"),


    "Tries Remaining: ",
    "The word: ",
    br,
    "Letters guessed so far: ",
    br,

    start_form,
    "Next Guess: ",
    textfield (
    -name=>"guess",
    -size=>5,
    -maxlength=>1,
    -default=>''),
    br,
    br,


    br,
    submit (-name=>"submit", -label=>"Guess"),
    submit (-name=>"new_game", -label=>"New Game"),

    end_form,
    end_html;

    }
    end_html;

    weberw@adelphia.net Guest

  2. Similar Questions and Discussions

    1. hangman help
      Hi I am creating a hangman game and wish to have a 'play again' button. How do i script this so that it starts again choosing a new word and...
  3. #2

    Default Re: Hangman

    [email]weberw@adelphia.net[/email] wrote:
    > Can someone help with my code? When the user enters "c" in the text
    > box it fills in c_ _, which is great.
    [...]

    Didn't you like the responses that you got in CLPMisc?
    Anyway, as you are asking in CLPModules the answer is
    [url]http://search.cpan.org/search?query=hangman&mode=all[/url], of course.

    jue


    Jürgen Exner Guest

  4. #3

    Default Re: Hangman


    Jürgen Exner wrote:
    > [email]weberw@adelphia.net[/email] wrote:
    > > Can someone help with my code? When the user enters "c" in the text
    > > box it fills in c_ _, which is great.
    > [...]
    >
    > Didn't you like the responses that you got in CLPMisc?
    or perl.beginners... this guy was really hedging his bets...

    -jp

    DJ Stunks Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139