#!/usr/bin/perl
require "cgi-lib.pl";
MAIN:
{
#$data_directory="/usr/local/www/docs-kidscape/games/BoogieMan/";
$data_directory="/var/www/docs-games-webmagic/games/";
$image_directory="/games/BoogieMan/images/";
$cgi_name="BoogieMan.cgi";
# Print the header
print "Content-type: text/html\n";
print "Pragma: no-cache\n\n";
print "
Beat the Boogie Man! -- WebMagic";
print "Beat the Boogie Man!
";
# Read in all the variables set by the form
if (&ReadParse(*input) && $input{'reset'} == 0)
{
&ProcessForm;
}
else
{
$tries_left=10;
$word=&GetRandomWord;
$stars = $word;
$stars =~ s/\w/\*/g;
$letters_left = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
&PrintForm;
}
}
sub GetRandomWord
{
$data_file=$data_directory . "BoogieMan.dat";
open (FILE, $data_file);
@LINES=; #Read in the entire data file
close(FILE);
$count=@LINES; #Get the number of entries
srand; #seed the random number generator with the current time
$word_number = int rand($count); #pick a random nuber between 0 and $count
$current_line = $LINES[$word_number]; # get the randomly chosen word
chop $current_line; # chop off the CR/LF
return $current_line;
}
sub RandomReset
{
# the following "reset=rand" bit is a hack to be sure the browser won't use
# a cached copy of the form
srand; #seed the random number generator with the current time
$random_number=int rand(9999999)+1;
return "Choose one:
Play again
or

"; }
sub YouWon
{
print "You made it!
\n";
print "The answer was: $word
\n";
print &RandomReset;
}
sub GetImage
{
local ($image_file, $image_number);
$image_number = 10 - $tries_left;
$image_file = $image_directory . $image_number . ".gif";
return "
";
}
sub ProcessForm {
$tries_left=$input{'tries_left'};
$letters_left=$input{'letters_left'};
$word= uc $input{'word'};
$entered = uc $input{'entered'};
$position = index($word, $entered);
$stars = uc $input{'stars'};
$star_position = index($stars, $entered);
$done=0;
local($letter_position);
$letter_position=index($letters_left, $entered);
if ($letter_position != -1)
{
substr($letters_left, $letter_position, 1) = "";
}
if ( $entered =~ /$word/i) # case insensitive pattern match
{
&YouWon;
$done=1;
}
elsif ($position != -1 && $entered ne "" && $star_position == -1)
{
# guessed a letter correctly
while ($position != -1)
{
substr($stars, $position, 1) = substr($word, $position, 1);
$position=index($word, $entered, $position+1);
}
if ($stars eq $word)
{
&YouWon;
$done=1;
}
}
elsif ($tries_left == 0)
{
print "Sorry, you lost!
\n";
print "The answer was: $word
\n";
print &RandomReset;
$done=1;
}
else
{
$tries_left = $tries_left - 1;
}
if ($done != 1)
{
&PrintForm;
}
}
sub PrintForm {
# Print out the body of the form from a single quoted here-document
# Note that if ENDOFTEXT weren't surrounded by single quotes,
# it would be necessary to be more careful with the other text
# For example, the @ sign (in the address) would need to be escaped as \@
local($image) = &GetImage;
print <
Guess the word or phrase: $stars
Choose from these letters:
$letters_left
Back to the main menu
WebMagic's Game Room is copyright © 2000 WebMagic, Inc. and is designed, maintained and sponsored by WebMagic, Inc. WebMagic is a trademark of WebMagic, Inc. All rights reserved. No portion of this site may be reproduced in any form without express written permission.
ENDOFTEXT
print &HtmlBot;
}