#!/usr/bin/perl # You may need to change the above line to #!/usr/local/bin/perl ############################################################### # Hangman 1.3 # By J.R. Greer jrg@albedo.net # ftp://ftp.albedo.net/pub/games/html/ # http://www.albedo.net/pub/cgi-games.html # # game based on code by Nik Swoboda nswoboda@cs.indiana.edu # http://blackcat.brynmawr.edu/~nswoboda/prog-html.html # # cgi_receive routine loosely based on code developed at the # National Center for Supercomputing Applications (NCSA) at # the University of Illinois at Urbana-Champaign. # # (c) 1997,1998 all rights reserved # # The program is placed under the GNU Public License (GPL) # You may copy freely or modify the code in anyway as long # as the above information stays intact. # ################################################################# # Configuration # The Directory where the .gif files are ie: /game/hangman/image/ $GIF_DIR = "/game/image/"; # The cgi-bin dir and the name of the cgi ie: /cgi-bin/hangman.pl $CGI_BIN = "/cgi-bin/hangman.1.3.pl"; # the URL to go back to leave the game ie: http://www.albedo.net/ $HOMEP = "http://www.albedo.net/pub/cgi-games.html"; # The location of the word file $DATAFILE = "/usr/local/etc/httpd/cgi-bin/data/hangman.words"; # If the Clue stays on the screen $CLUE=1; $bgcolour="white"; $textcolour="#000010"; $linkcolour="blue"; $vlinkcolour="#FF00FF"; $alinkcolour="#FFFF00"; # use background $BG=0; $BGGIF="/game/image/bg.gif"; ################################################################## ################################################################### # Do not edit below this line ################################################################### &cgi_receive; if ($FINPUT{'word'}) { &p_head; $type = $FINPUT{'clue'}; $FINPUT{'word'} =~ y/a-x,y,z/c-z,a,b/; ### un-encrypt word if ($FINPUT{'done'} eq 'done') ### guessed the word { if ($FINPUT{'word'} eq $FINPUT{'new'}) ### get it right? { print "
"; print "You were right.
"; print ""; print "You Won!!!
"; print "\"\"
"; } else { print "
"; print "You were WRONG!
"; print "\"\n
"; print "It was really ",$FINPUT{'word'},"."; } print "
Play another game of Hangman"; print " | go back to the homepage"; print "
\n"; exit; return; } chop($FINPUT{'st'}); # kill newline $FINPUT{'st'} =~ s/\s//g; # Kill spaces $FINPUT{'st'} =~ s/^(.)(.*)/$1/; # strip all but 1st char $guess = $FINPUT{'st'}.' '.$FINPUT{'guess'}; $guess =~ y/A-Z/a-z/; # lower case @sofar = split(/\s+/,$guess); # make guess into an array $gue = @sofar; # get number of letters $num = length($FINPUT{'word'}); ### make the real word into an array buy splitting on space or nil @rword = split(/ */,$FINPUT{'word'}); print "
"; $gotten = 0; $TEST = ""; LET: for ($i=0;$i<$num;$i++) { # match letters $temp = $rword[$i]; for ($g=0;$g<$gue;$g++) { if ($sofar[$g] eq $temp) { print $temp; if ($TEST !~ /$temp/) { # is there dupe letters $gotten = $gotten +1; # no up count } $TEST= "$TEST$temp"; # build autotest string next LET; } } print "_ "; # if no match print this $TEST = "$TEST _"; } print "

"; ############################################ ###### Test if they got it right if ($TEST eq $FINPUT{'word'}) { print "
"; print "You Won!!!
"; print "
"; print "Play another game of Hangman"; print " | go back to the homepage
"; print ""; exit; } ############################################ ##### Print new page print "
"; print "\"\"
"; if (($gue-$gotten) < 6) { # still guesses left $FINPUT{'word'} =~ y/a,b,c-z/y,z,a-y/; # encrypt word if ((6-($gue-$gotten)) == 1) { # lets make it print "You have 1 guess left!"; # look nice } else { print "You have ",(6-($gue-$gotten))," guesses left!"; } ######## Print clue if ($CLUE==1){ print "
hint : $type"; } print "

"; print "\n
"; print ""; print ""; ### add clue to form if ($CLUE == 1){ print ""; } &buttons; print "
\n"; print "Or if you are daring type in what you think that the word is.

"; print "

"; print "   "; print ""; print ""; print "\n"; print "
\n"; } ### You are Hung!! else{ print "
"; print "Game over.
"; print "The correct answer is $FINPUT{'word'}.
"; print "Play another game of Hangman"; print " | go back to the homepage
"; print ""; exit; } } else { ######################################################## # First time through the program generate the data ######################################################## &p_head; # read word file open (WF, $DATAFILE) || die "file not found"; @word_list=; close (WF); srand(time ^ $$);$num = rand(@word_list); ($word, $type) = split(/,/, $word_list[$num]); print "
"; $num = length($word); for ($i=1;$i<=$num;$i++) { print '_ '; } $word =~ y/a,b,c-z/y,z,a-y/; # encrypt word print "
\n

"; print "\"\"
"; print "Guess your first letter of the above unknown word.
"; print "hint : ",$type,""; print "
"; print ""; if ($CLUE==1){ print ""; } print ""; &buttons; print "
\n"; print "Or if you are daring type in what you think that the word is.
"; print "
"; print "   "; print ""; print ""; print ""; print "\n"; print "
\n"; } ##################################################### # Print a nice colourful header as well as HTML head sub p_head { print "Content-type: text/html\n\n"; print ""; print "\n"; print " Hangman \n"; print ""; print "
"; print "H"; print "a"; print "n"; print "g"; print "m"; print "a"; print "n"; print "   ver 1.3
\n"; } ##################################################### ## print the A-Z buttons sub buttons { print "
"; if ($guess !~ /a/) { print "";} if ($guess !~ /b/) { print "";} if ($guess !~ /c/) { print "";} if ($guess !~ /d/) { print "";} if ($guess !~ /e/) { print "";} if ($guess !~ /f/) { print "";} if ($guess !~ /g/) { print "";} if ($guess !~ /h/) { print "";} if ($guess !~ /i/) { print "";} if ($guess !~ /j/) { print "";} if ($guess !~ /k/) { print "";} if ($guess !~ /l/) { print "";} if ($guess !~ /m/) { print "";} if ($guess !~ /n/) { print "";} if ($guess !~ /o/) { print "";} if ($guess !~ /p/) { print "";} if ($guess !~ /q/) { print "";} if ($guess !~ /r/) { print "";} if ($guess !~ /s/) { print "";} if ($guess !~ /t/) { print "";} if ($guess !~ /u/) { print "";} if ($guess !~ /v/) { print "";} if ($guess !~ /w/) { print "";} if ($guess !~ /x/) { print "";} if ($guess !~ /y/) { print "";} if ($guess !~ /z/) { print "";} } ##################################################################### ## receive & Decode the html data sub cgi_receive { if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'}); } else { $incoming = $ENV{'QUERY_STRING'}; # this should never happen } @pairs = split(/&/, $incoming); foreach (@pairs) { ($name, $value) = split(/=/, $_); $name =~ tr/+/ /; $value =~ tr/+/ /; $name =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie; $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie; $value =~ s/;/$$/g; # Strip out semicolons $value =~ s/&(\S{1,6})$$/&\1;/g; $value =~ s/$$/ /g; $value =~ s/\|/ /g; $value =~ s/^!/ /g; # $value =~ s/[\/;\[\]<\>&\t]/_/g; #Sanitize! next if ($value eq ""); if ($name =~ /^assign-dynamic/) { $name = $value; $value = "on"; } $FINPUT{$name} .= ", " if ($FINPUT{$name}); $FINPUT{$name} .= $value; } }