Word Find
$name = $_REQUEST["name"];
$height = $_REQUEST["height"];
$width = $_REQUEST["width"];
$wordList = $_REQUEST["wordList"];
//print "Name: {$name}\tHeight: {$height}\tWidth: {$width}\tWords: {$wordList}";
if ($wordList == NULL){
//make default puzzle
$wordList = "Bolton\nWander\nDebbie\nMatthew\nDavid\n";
print $wordList;
$boardData = array(
width => 10,
height => 10,
name => "Generic Puzzle"
);
} else {
//get puzzle data from HTML form
$boardData = array(
width => $width,
height => $height,
name => $name
);
}
//try to get a word list from user input
if (parseList() == TRUE){
$legalBoard = FALSE;
//keep trying to build a board until you get a legal result
while ($legalBoard == FALSE){
clearBoard();
$legalBoard = fillBoard();
} //end while
//make the answer key
$key = $board;
$keyPuzzle = makeBoard($key);
//make the final puzzle
addFoils();
$puzzle = makeBoard($board);
//print out the result page
printPuzzle();
} //end if parseList
function parseList(){
//gets word list, create array of words from it
//or return false if imposible
global $word, $wordList, $boardData;
$itWorked = TRUE;
//convert word list entirely to upper case
$wordList = strtoupper($wordList);
//split word list into array
$word = split("\n", $wordList);
foreach ($word as $currentWord){
//take out trailing newline characters
$currentWord = rtrim($currentWord);
//stop if any words are too long to fit in a puzzle
if ((strLen($currentWord) > $boardData["width"]) && (strLen($currentWord) > $boardData["height"])){
print "$currentWord is too long for the puzzle";
$itWorked = FALSE;
} //end if
} //end foreach
return $itWorked;
} //end parseList
function clearBoard(){
//initialise board with a "." in each cell
global $board, $boardData;
for ($row=0; $row < $boardData["height"]; $row++){
for ($col=0; $col < $boardData["width"]; $col++){
$board[$row][$col] = ".";
} //end col for
} //end row for
} //end clearBoard
function fillBoard(){
//fill board with list by calling addword() for each word
//or return false if failed
global $word;
$direction = array("N", "S", "E", "W");
$itWorked = TRUE;
$counter = 0;
$keepGoing = TRUE;
while($keepGoing){
$dir = rand(0,3);
$result = addWord($word[$counter], $direction[$dir]);
if ($result == FALSE){
//print "failed to place $word[$counter$]";
$keepGoing = FALSE;
$itWorked = FALSE;
} //end if
$counter++;
if ($counter >= count($word)){
$keepGoing = FALSE;
} //end if
} //end while
return $itWorked;
} //end fillBoard
function addWord($theWord, $dir){
//attempt to add a word to the board or return false if failed
global $board, $boardData;
//remove trailing characters if necessary
$theWord = rtrim($theWord);
$itWorked = TRUE;
switch ($dir){
case "E":
//col from 0 to board width - word width
//row from 0 to board height
$newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
$newRow = rand(0, $boardData["height"] - 1);
for ($i=0; $i < strlen($theWord); $i++){
//new character same row, initial column
$boardLetter = $board[$newRow][$newCol + $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) || ($boardLetter == ".")){
$board[$newRow][$newCol + $i] = $wordLetter;
} else {
$itWorked = FALSE;
} //end if else
} //end for
break;
case "W":
//col from word width to board width
//row from 0 to board height
$newCol = rand(strlen($theWord), $boardData["width"] - 1);
$newRow = rand(0, $boardData["height"] - 1);
//print "west:\tRow: $newRow\tCol: $newCol
\n";
for ($i=0; $i < strlen($theWord); $i++){
//check for a legal move
$boardLetter = $board[$newRow][$newCol - $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) || ($boardLetter == ".")){
$board[$newRow][$newCol - $i] = $wordLetter;
} else {
$itWorked = FALSE;
} //end if else
} //end for
break;
case "S":
//col from 0 to board width
//row from 0 to board height - word length
$newCol = rand(0, $boardData["width"] - 1);
$newRow = rand(0, $boardData["height"] - 1 -strlen($theWord));
//print "south:\tRow: $newRow\tCol: $newCol
\n";
for ($i=0; $i < strlen($theWord); $i++){
//check for a legal move
$boardLetter = $board[$newRow + $i][$newCol];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) || ($boardLetter == ".")){
$board[$newRow + $i][$newCol] = $wordLetter;
} else {
$itWorked = FALSE;
} //end if else
} //end for
break;
case "N":
//col from 0 to board width
//row from word length to board height
$newCol = rand(0, $boardData["width"] - 1);
$newRow = rand(strlen($theWord), $boardData["height"] - 1);
for ($i=0; $i < strlen($theWord); $i++){
//check for a legal move
$boardLetter = $board[$newRow - $i][$newCol];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) || ($boardLetter == ".")){
$board[$newRow - $i][$newCol] = $wordLetter;
} else {
$itWorked = FALSE;
} //end if else
} //end for
break;
} //end switch
return $itWorked;
} //end addWord
function makeBoard($theBoard){
//given a board array, return a HTML table based on the array
global $boardData;
$puzzle = "";
$puzzle .= "
\n";
for ($row = 0; $row < $boardData["height"]; $row++){
$puzzle .= "\n";
for ($col = 0; $col < $boardData["width"]; $col++){
$puzzle .= "\t| {$theBoard[$row][$col]} | \n";
} //end for col
$puzzle .= "
\n";
} //end for row
$puzzle .= "
\n";
return $puzzle;
} //end makeBoard
function addFoils(){
//add random dummy characters to board
global $board, $boardData;
for ($row = 0; $row < $boardData["height"]; $row++){
for ($col = 0; $col < $boardData["width"]; $col++){
if ($board[$row][$col] == "."){
$newLetter = rand(65, 90);
$board[$row][$col] = chr($newLetter);
} //end if
} //end for col
} //end for row
} //end addFoils
function printPuzzle(){
//print out page to user with Puzzle on it
global $puzzle, $word, $keyPuzzle, $boardData;
//print puzzle itself
print <<
{$boardData["name"]}
{$puzzle}
Word List
HERE;
//print word list
foreach ($word as $theWord){
print "| {$theWord} |
\n";
} //end foreach
print "
\n";
$puzzleName = $boardData["name"];
//print form for requesting answer key
//send answer key to that form (sneaky!)
print <<
HERE;
} //end printPuzzle
?>