Hunting the Whale Wordsearch
From the HistoryShelf web site: <www.historyshelf.org>
Your name:
Date:
//************************************** // Name: WordSearch // Original code from: Park Wiker http://www.wiker.net/ // Modified for HistoryShelf by: Pete Gray //************************************** ?> reset ($HTTP_POST_VARS); // ------------------------------------------------------------------------------------------ function setvars() { // make all the vars globally available global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; // set all variables from post to discrete vars $title="Hunting the Whale Wordsearch"; $gridsize=25; $wordlist=array('Armed','Dundee','Logbook','Shetland','Autumn','Engine','Melville','Ship','Auxiliary','Fall','Navy','Small','Baleen','Flensed','Nest','Spirits','Bay','Greenland','Oil','Spring','Blubber','Greenmen','Orkney','Voyage','Boat','Harpoon','Protection','Wage','Bounty','Lance','Row','Whale','Coffee','Leith','Sang','Whalebone','Lighting','Seals','Wind'); $difficulty="easy"; //$listorder=$HTTP_POST_VARS[listorder]; $studentname=""; $classname=""; $assignment=""; $period=""; } // ------------------------------------------------------------------------------------------ // separate word list into separate vars for each word function seplist() { global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; // make wordlist an array by linefeed //$wordlist=explode( "\n",$wordlist); //reset($wordlist); // take wordlist and put separate words into a multi-dim array // of course make them upper case and trim all whitespace from front and back while ( list( $key, $val ) = each( $wordlist) ) { $val=trim ($val); $val=strtoupper($val); // after assigning everything to upper case, replace the array val with the new val $wordlist[$key]=$val; // get the string length so that we can parse through the word and make a // multidim array each letter being an array position $strlength=strlen($val); for ($i=0;$i!=($strlength);$i++){ $letter=substr($val,$i,1); $wordarray[$key][$i]=$letter; } } } // ------------------------------------------------------------------------------------------ function boardset() { global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; // establish direction array relative directions $dirtn[0]=array(x=>0,y=>1);// up | $dirtn[1]=array(x=>1,y=>0);// right - $dirtn[2]=array(x=>0,y=>-1); // down | $dirtn[3]=array(x=>-1,y=>0); // left - $dirtn[4]=array(x=>1,y=>1);// up and right / $dirtn[5]=array(x=>1,y=>-1); // down and right \ $dirtn[6]=array(x=>-1,y=>-1); //down and left / $dirtn[7]=array(x=>-1,y=>1); //up and left \ //establish board with blank spaces for ($i=0;$i!=$gridsize;$i++){ for ($j=0;$j!=$gridsize;$j++){ $board[$i][$j]= " "; } } } // ------------------------------------------------------------------------------------------ function placewords(){ global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; // count the number of letters in the first word $wordnumber=count($wordarray); // main for loop one time through for each word for($i=0;$i!=$wordnumber;$i++) { // count the letters in the current word $letternumber=(count($wordarray[$i])); // toggle wordfit for verification of word fitting on the grid without overwriteing any other letters $wordfit=0; while ($wordfit <= 0){ // toggle wordfit again in case the while loop runs a few times $wordfit=0; // toggle length ok to ensure proper checking of word length within constraints of grid $lengthok=0; // test for fit of word while ($lengthok!=1){ // seed random with the clock srand((double)microtime()*1000000); // get the random direction based on difficulty selected switch ($difficulty){ case hard: $direction=rand()%8; break; case easy: $direction=rand()%4; break; default: $direction=rand()%8; } // pick a random position within the scope of the grid srand((double)microtime()*1000000); $position=array(x=>(rand()%$gridsize),y=>(rand()%$gridsize)); // calculate where the word will end $endposition[x]=$position[x]+($letternumber*$dirtn[$direction][x]); $endposition[y]=$position[y]+($letternumber*$dirtn[$direction][y]); // make our endposition calculation have to be between 0 and 1 $result[x]=(abs($endposition[x]/$gridsize)); $result[y]=(abs($endposition[y]/$gridsize)); // if the end position is inside the grid set the length ok if($result[x]>1) { $lengthok=0; } elseif($result[y]>1) { $lengthok=0; } else{ $lengthok=1; } } // set a temp var to verify every position a letter will occupy $tposition=$position; // now check for what is in the positions selected reset($wordarray[$i]); while ( list ($key,$val)=each($wordarray[$i])){ // what is currently in the space switch ($board[$tposition[x]][$tposition[y]]){ // if it is a space, move in the direction one space and toggle wordfit +1 case " ": $tposition[x]=$tposition[x]+$dirtn[$direction][x]; $tposition[y]=$tposition[y]+$dirtn[$direction][y]; $wordfit=$wordfit+1; break; // if it is the same as the value we want to place there, move and toggle wordfit +1 case $val: $tposition[x]=$tposition[x]+$dirtn[$direction][x]; $tposition[y]=$tposition[y]+$dirtn[$direction][y]; $wordfit=$wordfit+1; break; // if the letter is not compatible, toggle wordfit -50 so that we do not place the word // we will have to go back and find a new place default: $wordfit=(-50); } } } reset ($wordarray[$i]); // we have successfully gotten out of the first while loop and the word can be placed while (list ($key,$val)=each($wordarray[$i])){ $board[$position[x]][$position[y]]=$val; $position[x]=$position[x]+$dirtn[$direction][x]; $position[y]=$position[y]+$dirtn[$direction][y]; } } // end of main for loop } //------------------------------------------------------------------------------------ function printgrid(){ global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; //fill blanks with random characters reset($board); // while in each row x while (list($key,$val)=each($board)){ // go to each column y while (list($key2,$val2)=each($board[$key])){ // if the character there is blank go ahead and fill it in in the array switch ($board[$key][$key2]){ case " ": srand((double)microtime()*1000000); $board[$key][$key2]=chr(rand()%26+65); break; default: break; } } } // now print those values to the page inside of a table for ($i=0;$i!=$gridsize;$i++){ echo "" . $val . "
Original WordSearch script by Park Wiker <www.wiker.net>