Hangman

W

weberw

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

axel

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 strict;
use warnings;
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
use constant MAX_MISSES => 8;
&display();

There is no need to use & when calling a subroutine unless you know why
you need it.
my $word;
$word = "cat";
my $misses_left = MAX_MISSES;
my %letters = map {$_ => 1} split //, $word;;
my %guesses = ();

At this point %guesses has no entries. So all previous 'guesses' have
been lost as that information has not been retained.

This information easily be retained by passing it back in the form as a
hidden element.
if (param()) {

my $guess = param('guess');
&processguess($guess);
sub processguess{
if (exists $guesses{$guess}) {
print qq(You\'ve already guessed "$guess".\n);
}

This code will never be executed as %guesses is empty.
$guesses{$guess}++;
if (exists $letters{$guess}) {
delete $letters{$guess};
keys %letters or last GUESSES;

You don't specify a label GUESSES.
} else {
--$misses_left;
print "another";

I'm afraid the purpose of this print escapes me.
}
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;

}

}
}
}

One too many right-braces here.
sub display {
my $the_cookie = cookie (
-name=>'gameid',
-value=>$id,

$id is never defined. Using warnings would have told you that.


Axel
 
W

weberw

Thanks for answering my quesiton. The correct syntax to populate
%guesses would be what?


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 strict;
use warnings;
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
use constant MAX_MISSES => 8;
&display();

There is no need to use & when calling a subroutine unless you know why
you need it.
my $word;
$word = "cat";
my $misses_left = MAX_MISSES;
my %letters = map {$_ => 1} split //, $word;;
my %guesses = ();

At this point %guesses has no entries. So all previous 'guesses' have
been lost as that information has not been retained.

This information easily be retained by passing it back in the form as a
hidden element.
if (param()) {

my $guess = param('guess');
&processguess($guess);
sub processguess{
if (exists $guesses{$guess}) {
print qq(You\'ve already guessed "$guess".\n);
}

This code will never be executed as %guesses is empty.
$guesses{$guess}++;
if (exists $letters{$guess}) {
delete $letters{$guess};
keys %letters or last GUESSES;

You don't specify a label GUESSES.
} else {
--$misses_left;
print "another";

I'm afraid the purpose of this print escapes me.
}
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;

}

}
}
}

One too many right-braces here.
sub display {
my $the_cookie = cookie (
-name=>'gameid',
-value=>$id,

$id is never defined. Using warnings would have told you that.


Axel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top