#!/usr/bin/perl
# NOTE: You must change the line above to point to the path to Perl
# on your system.
#########################################################################
##### SFEWordSearch v1.0 #
##### Copyright 2002, Kristina Pfaff-Harris, http://tesol.net/scripts/ #
# #
# SFEWordSearch is a little program to generate printable wordsearch #
# puzzles. You will be walked through a series of steps to create a #
# grid, then fill in words in the grid and clues for the puzzle. When #
# you have finished, the program will fill in the blank squares with #
# random letters, and generate a printable wordsearch puzzle for you #
# to print and hand out to students. #
# #
#########################################################################
##### Licensing: #
##### #
##### This program may be used free of charge under the following #
##### conditions: #
##### #
##### 1. All instructions and Copyright lines must remain unchanged. #
##### 2. All pages generated by the program must contain one of the #
##### following pieces of HTML code: #
##### #
##### SFEWordSearch is Copyright 2002 by #
##### Kristina Pfaff-Harris and can be found at: #
##### #
##### http://www.tesol.net/scripts #
##### OR: #
##### #
##### #
##### #
##### 3. You may not sell or redistribute this program. This isn't #
##### really arbitrary: I just like to make sure that when people #
##### get the script, they get the latest version. You may charge #
##### a reasonable fee for installing it for a client as long as #
##### you make it clear that you are not the author, and you are #
##### not selling the program to them: only charging for installing #
##### it. #
##### 4. You agree that this program is offered without warranty of #
##### any kind, including warranty of fitness for a particular #
##### purpose. You further agree that the author and all sites #
##### associated in any way with this program are not liable for #
##### any damage or loss incurred as a result of using this program.#
#########################################################################
##### #
##### IMPORTANT INSTRUCTIONS: #
##### #
##### In this program, I have put **CHANGE** in all the places where #
##### you will need to modify the program to run on your server, so #
##### that you can easily find all the places where changes are #
##### necessary. This program must be chmod 755 or 775 in order to #
##### work. #
##### #
##### Please read the README.sfewordsearch file for more instructions #
##### on how to set this up and on what to do about problems. #
#########################################################################
#########################################################################
##### #
##### BEGIN SECTION WHERE YOU WILL NEED TO CHANGE THINGS: #
##### #
##### In this section, there are several places where you'll need #
##### to make changes. Please read all instructions carefully #
##### before you make the changes. #
##### #
#########################################################################
# Okay, first we need to tell the program how to find itself. :-) Since
# the program goes through a series of steps, each time you hit a button,
# it will be looking for itself to run the next step. On many systems,
# what I have below will work. However, if you are getting "File not found"
# errors when trying to run the script, **CHANGE** the line below to reflect
# the full URL of where the script is on your website. For example:
# $cgi_url = "http://www.your_site.com/cgi-bin/sfewordsearch.cgi";
$cgi_url = "$ENV{'SCRIPT_NAME'}";
# Next, $header and $footer are some HTML that will appear at the top and
# the bottom of each page generated by the program. You'll use these to
# make the program pages look like the rest of your site. **CHANGE**
# this to whatever HTML code you like, HOWEVER -- (and this is important!)
# all of the following characters must have backslashes in front of them:
# " @ $
# like this:
# \" \@ \$
# If you forget to put backslashes in front of EVEN ONE " or @, you will
# get "Internal Server Error" and the program will not run. If you forget
# to put a backslash in front of a $, then the $ will be invisible. Please
# only make changes in between the and
# lines to make sure that you don't accidentally remove the " and ; that
# are needed by the program. :-)
$header = "
Scripts for Educators WordSearch Maker
WordSearch Powered by Scripts for Educators
"; # <-- Leave this one " alone. :-)
# $footer is for the HTML at the bottom of pages generated by the program.
# You may **CHANGE** this to any HTML you want, BUT you must follow the
# same instructions as for $header above. For more information about
# $header and $footer, please check the FAQ at http://tesol.net/scripts/FAQ/
# and look for "How do I make the pages in a script look like the rest of my
# site? What is this $header or $htmlheader?"
$footer = "
SFEWordSearch wordsearch puzzle generator software
v1.0 can be found at
http://tesol.net/scripts/ : CGI Scripts for Educators
"; # <-- Leave this one " alone too. :-)
#########################################################################
##### #
##### END SECTION WHERE THINGS NEED TO BE CHANGED. #
##### #
##### You should not HAVE to change anything beyond this point, #
##### although you might want to read through it to see what it does. #
##### If you're interested in how CGI programs work, then this may #
##### act as its own tutorial of sorts. You definitely shouldn't #
##### change anything beyond this point unless you know what you're #
##### doing, though. :) #
##### #
#########################################################################
print "Content-type: text/html\n\n";
my %data = &get_data();
# If you remove this link, without replacing it with a similar
# HTML comment (as above in the License section) you are in violation
# of the license for this program. No, I don't have the resources to
# enforce this, and I know that some people will remove it anyway.
# But honestly, since you didn't have to pay for this, is the link
# really too much to ask? :)
$footer = "
SFEWordSearch v1.0 is Copyright © 2002
Kristina Pfaff-Harris and can be found at
http://www.tesol.net/scripts
$footer" if $footer !~ /http:\/\/tesol.net\/scripts/is;
$fa = $data{'FA'};
if($fa eq "Step2"){
&create_grid;
exit();
}
elsif($fa eq "Step3"){
&add_clues;
exit();
}
elsif($fa eq "CreateFinal"){
&create_puzzle;
exit();
}
else {
&get_grid_info();
exit();
}
sub get_grid_info {
if($_[0]){ $errmsg = "$_[0]"; }
print "$header
$footer";
}
sub create_grid {
if($data{'NumDown'} eq "" || $data{'NumDown'} =~ /\D/){
$error = "Error: \"$data{'NumDown'}\" is not a valid number of squares down.
";
}
if($data{'NumAcross'} eq "" || $data{'NumAcross'} =~ /\D/){
$error .= "Error: \"$data{'NumAcross'}\" is not a valid number of squares across.
";
}
if($error){ &get_grid_info($error); exit(); }
print "$header
In this screen, type the words (one letter per square) into the grid. Then,
count up the number of words you've put in, and type that number into
\"Number of words in the puzzle.\"
$footer";
}
sub add_clues {
print "$header
To the left, you'll see your grid with only your words filled in. Type the
clues for those words into the slots on the right. (The blank spaces in the
grid will be filled in automatically in the next screen.)
$footer";
}
sub get_random_character {
my(@chars,$num,$char);
@chars = split(//, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$num = int(rand(@chars));
$char = $chars[$num];
$char;
}
sub create_puzzle {
print "$header
";
for ($i = 1; $i <= $data{'NumDown'}; $i++){
print "\n";
for($j = 1; $j <= $data{'NumAcross'}; $j++){
$gridno = "$i-$j";
if($data{$gridno} eq ""){
$data{$gridno} = &get_random_character();
}
$data{$gridno} = uc($data{$gridno});
print "| $data{$gridno} | \n";
}
print " \n";
}
print " |
|
| Word List |
";
for($i = 0; $i < $data{'NumWords'}; $i++){
$num = $i + 1;
$wordlist[$i] = $data{"Word$num"};
}
@wordlist = sort(@wordlist);
$i = 0;
foreach $word (@wordlist){
if($i / 3 == int($i / 3)){ print ""; }
$num = $i + 1;
print "| $word | ";
if($num / 3 == int($num / 3)){ print " "; }
$i++;
}
print " |
$footer";
}
sub get_data {
local($string);
# get data
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$string = $ENV{'QUERY_STRING'};
}
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }
# split data into name=value pairs
@data = split(/&/, $string);
# split into name=value pairs in associative array
foreach (@data) {
split(/=/, $_);
$_[0] =~ s/\+/ /g; # plus to space
$_[0] =~ s/%00//g; # We don' need no steenking nulls :)
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
if(defined($data{$_[0]})){
$data{$_[0]} .= "\0";
$data{$_[0]} .= "$_[1]";
}
else {
$data{"$_[0]"} = $_[1];
}
}
# translate special characters
foreach (keys %data) {
$data{"$_"} =~ s/\+/ /g; # plus to space
$data{"$_"} =~ s/%00//g; # We don' need no steenking nulls :)
$data{"$_"} =~ s/%0a/newline/g;
$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
}
%data; # return associative array of name=value
}