#!/usr/bin/perl ########################################### # Guess Game v1.0 by Allen C. Huffman # # Copyright (C) 2001 by Sub-Etha Software # # FREEWARE! # # subetha@pobox.com # # http://disneyfans.com/subetha # ########################################### # # You may do whatever you want with this code, provided you put a comment in that # says "Baseed on" (or similar) and my original copyright notice above. # # Revision History: # # 2001/08/03 1.0 Initial (working) version. # # # A number between 1 and ... ? # $max = 100; # # Where do we send the user when they are done playing? # $done_playing_URL = "http://yourdomain.com/yourpage.htm"; # # If you want to count how many play the game, specify # a path to the count data file here, else leave it empty. # $counter_path = "seguessgame.dat"; # # Location/name of this script... Hopefully you won't have to change # this, but if you do it should be something like "/cgi-bin/seguessgame.cgi" # or wherver your script is and whatever it is called... # $this_script = "$ENV{'SCRIPT_NAME'}"; #################################################################################### # # Begin processing... Nothing below this line needs to be modified. # &ReadParse; # if no input, this must be the first time the script has ran... if (!$in) { $tries = 0; $magic = int(rand($max-1)); $magic++; &do_top; print "

Welcome to Sub-Etha Software's GUESS THE NUMBER game!

\n"; # if we have specified a counter file, let's use it. if ( $counter_path ) { unless ( open(FILE, "+<$counter_path") ) { open( FILE,"+>$counter_path") || die ("Unable to create file.\n" ); } $count = ; chomp($count); $count++; seek(FILE,0,0); truncate(FILE,0); print FILE "$count"; close(FILE); print "

This game has been played $count time"; if ( $count!=1 ) { print "s"; } print " so far.

\n"; } &do_form; &do_end; exit(0); } $magic = $in{'magic'}; $tries = $in{'tries'}; # first, are we done? $playagain = $in{'playagain'}; if ( $playagain eq 'Yes' ) { print "Location: $this_script\n\n"; } elsif ( $playagain eq 'No' ) { print "Location: $done_playing_URL\n\n"; } elsif ( $playagain eq 'Give Up' ) { &do_top; print "

The magic number was $magic..." . "You gave up after $tries "; if ( $tries==1 ) { print "try"; } else { print "tries"; } print ".

\n"; &play_again; &do_end; exit(0); } $guess = $in{'guess'}; $tries++; &do_top; if ( $guess < $magic ) { print "

That guess was too low!

\n"; } elsif ( $guess > $magic ) { print "

That guess was too high!

\n"; } else { print "

You gueesed it, and it only took you $tries "; if ( $tries==1 ) { print "try"; } else { print "tries"; } print "!

\n"; &play_again; &do_end; exit(0); } &do_form; &do_end; exit(0); sub do_top { print "Content-Type: text/html\n\n"; print "Sub-Etha's GUESS THE NUMBER...\n"; } sub do_end { print "\n"; } sub do_form { print <

I am thinking of a number between 1 and $max.

What is your guess? (1-$max)
EOF } sub play_again { print <
Would you like to play again?
EOF } #################################################################################### ### ### Adapted from cgi-lib.pl by S.E.Brenner@bioc.cam.ac.uk ### Copyright 1994 Steven E. Brenner sub ReadParse { local (*in) = @_ if @_; local ($i, $key, $val); ### replaced his MethGet function if ( $ENV{'REQUEST_METHOD'} eq "GET" ) { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); } else { # Added for command line debugging # Supply name/value form data as a command line argument # Format: name1=value1\&name2=value2\&... # (need to escape & for shell) # Find the first argument that's not a switch (-) $in = ( grep( !/^-/, @ARGV )) [0]; $in =~ s/\\&/&/g; } @in = split(/&/,$in); foreach $i (0 .. $#in) { # Convert plus's to spaces $in[$i] =~ s/\+/ /g; # Split into key and value. ($key, $val) = split(/=/,$in[$i],2); # splits on the first =. # Convert %XX from hex numbers to alphanumeric $key =~ s/%(..)/pack("c",hex($1))/ge; $val =~ s/%(..)/pack("c",hex($1))/ge; # Associate key and value. \0 is the multiple separator $in{$key} .= "\0" if (defined($in{$key})); $in{$key} .= $val; } return length($in); }