#!/usr/bin/perl
use Math::Random;
print "Content-Type: text/html\n\n";
# Parse Form Contents
# Determine the form's REQUEST_METHOD (GET or POST) and split the form #
# fields up into their name-value pairs. If the REQUEST_METHOD was #
# not GET or POST, send an error. #
$price=0;
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
# user typed it from the terminal mode
print "bad form method used ($ENV{'REQUEST_METHOD'})\n";
# exit 0;
};
# Version history
# 1.0.0 Original program written, debugged, and tested.
# 1.1.0 Changed the input routine to allow spaces and paragraph style word arrays. Added
# unique word detector
# 2.0.0 added the ability to add words to a database of puzzles and then be capable of
# pulling from that list to generate a random puzzle.
$version = "2.0.0";
$title = join(" ", "Wordsearch",$version);
print "\n";
print "\n";
print "
\n";
print "\n";
$min = 10;
$max = 99;
$defaultx = 15;
$defaulty = 15;
$width = $defaultx;
$height = $defaulty;
@goodwords = ();
$wordsearchname = "Test Puzzle";
# database file name
$mywordsearchdatabase = "/library/webserver/cgi-executables/private/wordsearch.database";
$printsolution = 1;
$printlinenumbers = 0;
$dosort = 0;
$addwords = 0;
$randompuzzle = 0;
$debug=0;
$fillspaces = 1;
@chars = (".", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
$words = "robert,dell,wordsearch,puzzle,generator,mix,codewarrior,apprentice,duke,nukem,aesopolis,zork";
# these directions are only for internal reference. the array is never used.
@directions = ("", "up", "right", "down", "left", "upright", "downright", "downleft", "upleft");
# 1 2 3 4 5 6 7 8
foreach $pair (@pairs) {
# Split the pair up into individual variables. #
local($name, $value) = split(/=/, $pair);
# Decode the form encoding on the name and value variables. #
# v1.92: remove null bytes #
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/\0//d;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/\0//d;
$item = 0;
if ($name eq 'width') {
$width = $value;
if (($width < $min) or ($width > $max)) {
print "Ranges for width and height must be between 10 and 99 inclusive";
exit 0;
};
}
elsif ($name eq 'height') {
$height = $value;
if (($height < $min) or ($height > $max)) {
print "Ranges for width and height must be between 10 and 99 inclusive";
exit 0;
};
}
elsif ($name eq 'solution') {
$printsolution = $value;
if (($printsolution != 0) and ($printsolution != 1)) {
$printsolution = 1;
};
}
elsif ($name eq 'linenumber') {
$printlinenumbers = $value;
if (($printlinenumbers != 0) and ($printlinenumbers != 1)) {
$printlinenumbers = 0;
};
}
elsif ($name eq 'dosort') {
$dosort = $value;
if (($dosort != 0) and ($dosort != 1)) {
$dosort = 1;
};
}
elsif ($name eq 'addwords') {
$addwords = $value;
if (($addwords != 0) and ($addwords != 1)) {
$addwords = 0;
};
}
elsif ($name eq 'randompuzzle') {
$randompuzzle = $value;
if (($randompuzzle != 0) and ($randompuzzle != 1)) {
$randompuzzle = 0;
};
}
elsif ($name eq 'name') {
$wordsearchname = $value;
$wordsearchname =~ s/,//g;
$wordsearchname = lc $wordsearchname;
@temp = split(" ", $wordsearchname);
$wordsearchname = "";
foreach $w (@temp) {
$w = ucfirst $w;
$l = length $wordsearchname;
if ($l == 0) {
$wordsearchname = $w;
}
else {
$wordsearchname = join (" ", $wordsearchname, $w);
};
};
}
elsif ($name eq 'words') {
$words = $value;
};
};
$words =~ tr/ /,/;
$words =~ tr/\n/,/;
$words =~ tr/\r/,/;
$words =~ tr/\t/,/;
$words =~ s/\./,/g;
$words =~ s/\'//g;
$words =~ s/\"//g;
$words =~ s/\!//g;
$words =~ s/\_//g;
$words =~ s/\(//g;
$words =~ s/\)//g;
$words =~ s/\://g;
$words =~ s/\-//g;
$words =~ s/\///g;
$words =~ s/\/g;
$words =~ s/\>//g;
$words =~ s/\=//g;
$words =~ s/\%//g;
$words =~ s/\[//g;
$words =~ s/\]//g;
$words =~ s/\{//g;
$words =~ s/\}//g;
$words =~ s/\#//g;
$words =~ s/\@/at/g;
$words =~ s/\$/,/g;
$words =~ s/0//g;
$words =~ s/1//g;
$words =~ s/2//g;
$words =~ s/3//g;
$words =~ s/4//g;
$words =~ s/5//g;
$words =~ s/6//g;
$words =~ s/7//g;
$words =~ s/8//g;
$words =~ s/9//g;
$words =~ s/\,\,/,/g;
$words =~ s/\,\,/,/g;
$words =~ s/\,\,/,/g;
$words = uc $words;
@wordarray = split(/,/,$words);
@validwords = ();
foreach $w (@wordarray) {
# print $w,"/",length $w," \n";
$alreadyadded = 0;
for ($i=0; $i<=$#validwords; $i++) {
if ($w eq $validwords[$i]) {
$alreadyadded = 1;
$i = $#validwords;
};
};
if (($alreadyadded == 0) and (length $w > 2)) {
$validwords[$#validwords + 1] = $w;
};
};
@wordarray = @validwords;
$words = "";
foreach $w (@wordarray) {
$l = length $words;
if ($l == 0) {
$words = $w;
}
else {
$words = join(",",$words,$w);
};
$l = length $w;
if ($l > $height) {
$height = $l + 1;
};
if ($l > $width) {
$width = $l + 1;
};
};
$words = join(",", $height, $width, $wordsearchname, $words);
if ($randompuzzle == 1) {
# first thing we do is kill adding words. no need to re-add the words to the list.
$addwords = 0;
# now we prepare the arrays to handle the new list
@wordarray = ();
# read in the data file completely and then decide on what line to display
open ($datafile, $mywordsearchdatabase);
@data = <$datafile>;
close ($datafile);
$rnd = random_uniform_integer(1, 0, $#data);
$words = $data[$rnd];
# strip out the return character
$words =~ s/\n//g;
$words =~ s/\r//g;
# Now separate the various values from the incoming line
# @wordarray = split(/,/,$words);
@temparray = split(/,/,$words);
$height = $temparray[0];
$width = $temparray[1];
$wordsearchname = $temparray[2];
for ($i=3; $i<=$#temparray; $i++) {
$wordarray[$#wordarray + 1] = $temparray[$i];
};
@temparray = ();
};
$matrixsize = $width * $height;
@searcharray = ();
for ($i = 1; $i <= $matrixsize; $i++) {
$searcharray[$i] = ".";
};
if ($addwords == 1) {
open ($datafile, $mywordsearchdatabase);
@data = <$datafile>;
close ($datafile);
$data[$#data + 1] = join("", $words, "\n");
$mywordsearchdatabase = join("", ">", $mywordsearchdatabase);
open ($datafile, $mywordsearchdatabase);
foreach $line (@data) {
print $datafile $line;
};
close ($datafile);
print "
Successfully added the following line to the database.
\n";
print "\n";
print $words," \n";
print "";
}
else {
# Put the words on the matrix
$wordcount = 0;
foreach $w (@wordarray) {
@where = (1,1,1,0);
&scanboard($w);
if ($where[3]<0) {
}
else {
$goodwords[$wordcount] = $w;
&putword ($w);
$wordcount = $wordcount + 1;
};
};
@wordarray = @goodwords;
# sort out the word list
if ($dosort == 1) {
for ($i=0; $i<$#wordarray; $i++) {
$testword = $wordarray[$i];
for ($j=$i; $j<=$#wordarray; $j++) {
if ($wordarray[$j] lt $testword) {
$testword = $wordarray[$j];
$wordarray[$j] = $wordarray[$i];
$wordarray[$i] = $testword;
};
};
};
};
# fill a matrixx of random numbers for the random letters
@xtest=();
@xtest = random_uniform_integer($matrixsize+1, 1, 26);
# backup the matrix to the solution
@solution = @searcharray;
# fill the letters not used with a random number.
if ($fillspaces == 1) {
for ($i=0; $i<=$matrixsize; $i++) {
if ($searcharray[$i] eq ".") {
$searcharray[$i] = $chars[$xtest[$i]];
};
};
};
print "