Quote:
|
Originally Posted by Arowana
Ok... Ugh, I have tried a million things to no avail regarding the problem when first visiting the site. I either get errors, or I get the welcome message I create in ADDITION to the "Sorry, etc... This is driving me crazy. Can you help further? Give me an example, please? Thanks! My brain is fried...
Chris
|
LOL... the old fried brain
actually I was having fun with the script so I added a couple of things this afternoon. I think you can figure out what I did for the default condition, plus I added a routine that gives a score if you win, its not anything very well thought out but hopefully it adds to the fun. Anyways, here it is (not real well tested but looks like it should work OK):
Code:
#!/usr/bin/perl
use CGI ':standard';
use Fcntl;
use SDBM_File;
use strict;
my $max_tries = 10;
my $id;
my %current = ('tries_left' => 10, 'letters' => '', 'revealed' => '');
tie(my %hash, 'SDBM_File', '/tmp/game_file.sdbm', O_CREAT|O_RDWR, 0666) || die $!;
&PrintHeader();
my $tries_left = $current{'tries_left'};
my @guessed_letters = split(//, $current{'letters'} );
my $secret_word = lc $current{'word'};
my $this_guess = 0;
my $guessed_string = "";
my $found_letter = 0;
my $playing = 0;
$playing = 1 if param('new_game') || param('start_game') || param('submit');
$this_guess = lc param('guess');
undef($this_guess) if param('new_game') || param('start_game');
$found_letter = &GuessLetter($this_guess) if $this_guess;
if ($playing) {
&win() if (($current{'word'} eq $current{'revealed'}) && ($tries_left <= $max_tries));
$tries_left-- if ($found_letter == 0 && $this_guess);
&lose() if ($tries_left < 1 && $this_guess);
push(@guessed_letters, $this_guess) if $found_letter == 0;
$guessed_string = join( "", @guessed_letters);
$current{'tries_left'} = $tries_left;
$current{'letters'} = $guessed_string;
&PrintContent();
$hash{$id} = $current{'word'}."~".$current{'tries_left'}."~".$current{'letters'}."~".$current{'revealed'};
}
else {&enter;}
untie(%hash);
exit(0);
sub enter {
print start_html( -title=>"Hangman!"),
h1("Lets Play Hangman!"),
"The rules: guess letters to reveal the secret word.",
br,
"You win if you guess the secret word before your tries remaining run out.",
start_form,
submit (-name=>'start_game', -label=>'Start Game'),
end_form,
end_html;
}
sub generate_gameid {
$id .= int(rand 9) for (0..8);
return $id;
}
sub win {
my ($score,$max_score) = &score($current{'revealed'},$current{'word'});
print start_html("Hangman - Congratulations!"),
"Congratulations, your word was:",
h2($current{'word'}),
"Your score was <b>$score</b> of a possible <b>$max_score</b> points.",
start_form,
submit (-name=>"new_game", -label=>"Play Again"),
end_form,
end_html;
untie(%hash);
exit(0);
}
sub lose {
my ($score,$max_score) = &score($current{'revealed'},$current{'word'});
print start_html("Hangman - Sorry!"),
"Sorry, your word was:",
h2($current{'word'}),
"Your score was <b>$score</b> of a possible <b>$max_score</b> points.",
start_form, submit (-name=>"new_game", -label=>"Play Again"),
end_form,
end_html;
untie(%hash);
exit(0);
}
sub PrintHeader {
my $the_cookie;
my $saved_game;
if(param('new_game')) {
$id = &generate_gameid();
$the_cookie = cookie ( -name=>'gameid',
-value=>$id,
-expires=>'+1h');
&InitGame();
}
else {
$id = cookie('gameid');
$saved_game = $hash{$id};
&LoadGame($saved_game);
}
print header(-cookie=>$the_cookie);
}
sub PrintContent {
print start_html( -title=>"Hangman!", -onLoad=>"document.forms[0].guess.select()"),
span({-style=>'font-size:30px;font-weight:bold;'},'Secret Word: '),
span({-style=>'letter-spacing:5px;font-size:30px;font-weight:bold;'},$current{'revealed'}),
br,br,
"Tries Remaining: ",
b($current{'tries_left'}),
br,
"Incorrect guesses: ",
b(em(join " ", split //, $current{'letters'})),
br,
start_form,
"Your Guess: ",
textfield ( -name=>'guess', -size=>5, -maxlength=>1, -default=>'', override=>1),
submit (-name=>'submit', -label=>'Guess'),br,br,
submit (-name=>'new_game', -label=>'New Game'),
end_form,
end_html;
}
sub InitGame {
my $word = &FetchWord();
$current{'word'} = $word;
$current{'tries_left'} = $max_tries;
$current{'letters'} = "";
$current{'revealed'} = &MakeLines($word);
}
sub LoadGame {
my $foo = $_[0];
my @saved_game = split(/~/,$foo);
$current{'word'} = $saved_game[0];
$current{'tries_left'} = $saved_game[1];
$current{'letters'} = $saved_game[2];
$current{'revealed'} = $saved_game[3];
}
sub FetchWord {
#my $word = "superman";
my @lines;
my @words;
my $line_count = 0;
my $word_count = 0;
my $rand_line = 0;
my $rand_word = 0;
open( DICTIONARY, "words.txt" ) or die "Cannot open file: $!\n";
@lines = <DICTIONARY>;
close( DICTIONARY );
$line_count = @lines;
$rand_line = int(rand $line_count);
@words = split(/,/, $lines[$rand_line] );
$word_count = @words;
$rand_word = int(rand $word_count);
my $word = $words[$rand_word];
chomp($word);
return $word;
}
sub MakeLines {
my $blanks;
$blanks .= "_" for (1..length($_[0]));
return $blanks;
}
sub GuessLetter {
my $guess = $_[0];
my @word = split(//,$current{'word'});
my $letter = "";
my @old_revealed = split(//,$current{'revealed'});
my @new_revealed = @old_revealed;
my $new_revealed_string = "";
my $got_one = 0;
my $count = 0;
foreach $letter(@word) {
if(lc($guess) eq lc($letter)) {
$new_revealed[$count] = $guess;
$got_one = 1;
}
$count++;
}
$new_revealed_string = join("",@new_revealed);
$current{'revealed'} = $new_revealed_string;
return $got_one;
}
sub score {
$tries_left = 1 if $tries_left < 1;
my ($score,$score2,$max_score) = (0,0,0);
my @guess = split(//,shift);
my @word = split(//,shift);
my @points = qw(aeiou ytrnlchds bfgkmp jqvwxz);
foreach (@guess) {
$score+=3 if (/[$points[0]]/i);
$score+=7 if (/[$points[1]]/i);
$score+=10 if (/[$points[2]]/i);
$score+=15 if (/[$points[3]]/i);
}
foreach (@word) {
$score2+=3 if (/[$points[0]]/i);
$score2+=7 if (/[$points[1]]/i);
$score2+=10 if (/[$points[2]]/i);
$score2+=15 if (/[$points[3]]/i);
}
$max_score = $score2*$max_tries;
$score = $score*$tries_left;
return ($score,$max_score);
}
Last edited by Millennium; 11-12-03 at 11:41 PM.
|