PX : code

WordSearch by Park Wiker
Download this code


<? 

// for an example of the submit form, look at http://www.wiker.net/games/wordsearch.phps

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=ucfirst(strtolower(trim($HTTP_POST_VARS[title]))); 
    $gridsize=$HTTP_POST_VARS[gridsize]; 
    $wordlist=trim($HTTP_POST_VARS[wordlist]); 
    $difficulty=$HTTP_POST_VARS[difficulty]; 
    $listorder=$HTTP_POST_VARS[listorder]; 
    $studentname=$HTTP_POST_VARS[studentname]; 
    $classname=$HTTP_POST_VARS[classname]; 
    $assignment=$HTTP_POST_VARS[assignment]; 
    $period=$HTTP_POST_VARS[period]; 
         // if the loser enters a blank list, put in the word "loser" 
    if ($wordlist== "") 
        $wordlist= "loser"; 


// ------------------------------------------------------------------------------------------ 
         // 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  "<tr>\n"; 
        for ($j=0;$j!=$gridsize;$j++){ 
            echo  "    <td width=\"20\" height=\"15\" align=\"CENTER\" valign=\"MIDDLE\">" . $board[$i][$j] .  "</td>\n"; 
            } 
        echo  "</tr>\n"; 
        } 

// ----------------------------------------------------------------- 
function printlist(){ 
    global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
     
    sort ($wordlist); 
    reset ($wordlist); 
    while (list ($key,$val)=current( $wordlist) ) {  
        echo  "<tr>\n"; 
        for ($i=0;$i<3;$i++){ 
            $val=current($wordlist); 
            echo  "    <td width=\"150\" align=\"LEFT\" valign=\"MIDDLE\">" . $val .  "</td>\n"; 
            $val=next($wordlist);         
            } 
        echo  "</TR>\n"; 
        } 
    }     

// ----------------------------------------------------------------- 
function printinfo(){ 
    global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 

    echo  "<tr>\n    <td align=\"LEFT\" valign=\"MIDDLE\">Class:  " . $classname .  "<br>\n"; 
    echo  "Student:  " . $studentname .  "</td>\n"; 
    echo  "    <td align=\"LEFT\" valign=\"MIDDLE\">Assignment:  " . $assignment .  "<br>\n"; 
    echo  "Period:  " . $period .  "</td></tr>\n"; 
    echo  "<tr>\n<td><i><font size=\"-1\"><b>Provided by Wiker Business Systems</b></font></i><td>\n<i><font size=\"-1\"><b>http://www.wiker.net</b></font></i></td></tr>\n"; 

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<html> 
<head> 
    <title>Puzzle Result</title> 
</head> 

<body bgcolor="White" topmargin=0> 
<? 
setvars(); 
seplist(); 
boardset(); 
placewords(); 
echo  "<div align=\"center\"><h2>$title</h2></div>"; 
?> 
<table border="0" cellspacing="0" cellpadding="0" align="CENTER" valign="TOP" nowrap> 
<? 
printgrid(); 
?> 
</table> 
<br><br> 
<table width="80%" border="0" cellspacing="0" cellpadding="0" align="CENTER" valign="MIDDLE" nowrap> 
<? 
printlist(); 
?> 
</table> 
<br> 
<table width="80%" border="1" cellspacing="0" cellpadding="0" align="CENTER" valign="MIDDLE" nowrap> 
<? 
printinfo(); 
?> 
</table> 


</body> 
</html> 


Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.