home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / php.exe / WordSearch.php < prev   
Encoding:
Text File  |  2001-07-02  |  10.6 KB  |  299 lines

  1.     //**************************************
  2.     //     
  3.     // Name: WordSearch
  4.     // Description:The user provides a list 
  5.     //     of words to be placed into a word search
  6.     //     puzzle suitable for printing. by Park Wi
  7.     //     ker
  8.     // By: PHP Code Exchange
  9.     //**************************************
  10.     //     
  11.     
  12.     <? 
  13.     // for an example of the submit form, lo
  14.     //     ok at http://www.wiker.net/games/wordsea
  15.     //     rch.phps
  16.     reset ($HTTP_POST_VARS); 
  17.     // -------------------------------------
  18.     //     ----------------------------------------
  19.     //     ------------- 
  20.     function setvars() { 
  21.     // make all the vars globally available 
  22.     //     
  23.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  24.     // set all variables from post to discre
  25.     //     te vars 
  26.     $title=ucfirst(strtolower(trim($HTTP_POST_VARS[title]))); 
  27.     $gridsize=$HTTP_POST_VARS[gridsize]; 
  28.     $wordlist=trim($HTTP_POST_VARS[wordlist]); 
  29.     $difficulty=$HTTP_POST_VARS[difficulty]; 
  30.     $listorder=$HTTP_POST_VARS[listorder]; 
  31.     $studentname=$HTTP_POST_VARS[studentname]; 
  32.     $classname=$HTTP_POST_VARS[classname]; 
  33.     $assignment=$HTTP_POST_VARS[assignment]; 
  34.     $period=$HTTP_POST_VARS[period]; 
  35.     // if the losser enters a blank list, pu
  36.     //     t in the word "losser" 
  37.     if ($wordlist== "") 
  38.     $wordlist= "losser"; 
  39.     } 
  40.     // -------------------------------------
  41.     //     ----------------------------------------
  42.     //     ------------- 
  43.     // separate word list into separate vars
  44.     //     for each word 
  45.     function seplist() { 
  46.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  47.     // make wordlist an array by linefeed 
  48.     $wordlist=explode( "\n",$wordlist); 
  49.     reset($wordlist); 
  50.     // take wordlist and put separate words 
  51.     //     into a multi-dim array 
  52.     // of course make them upper case and tr
  53.     //     im all whitespace from front and back 
  54.     while ( list( $key, $val ) = each( $wordlist) ) { 
  55.     $val=trim ($val); 
  56.     $val=strtoupper($val); 
  57.     // after assigning everything to upper c
  58.     //     ase, replace the array val with the new 
  59.     //     val 
  60.     $wordlist[$key]=$val; 
  61.     // get the string length so that we can 
  62.     //     parse through the word and make a 
  63.     // multidim array each letter being an a
  64.     //     rray position 
  65.     $strlength=strlen($val); 
  66.     for ($i=0;$i!=($strlength);$i++){ 
  67.     $letter=substr($val,$i,1); 
  68.     $wordarray[$key][$i]=$letter; 
  69.     } 
  70.     } 
  71.     } 
  72.     // -------------------------------------
  73.     //     ----------------------------------------
  74.     //     ------------- 
  75.     function boardset() { 
  76.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  77.     // establish direction array relative di
  78.     //     rections 
  79.     $dirtn[0]=array(x=>0,y=>1);// up | 
  80.     $dirtn[1]=array(x=>1,y=>0);// right - 
  81.     $dirtn[2]=array(x=>0,y=>-1); // down | 
  82.     $dirtn[3]=array(x=>-1,y=>0); // left - 
  83.     $dirtn[4]=array(x=>1,y=>1);// up and right / 
  84.     $dirtn[5]=array(x=>1,y=>-1); // down and right \ 
  85.     $dirtn[6]=array(x=>-1,y=>-1); //down and left / 
  86.     $dirtn[7]=array(x=>-1,y=>1); //up and left \ 
  87.     //establish board with blank spaces 
  88.     for ($i=0;$i!=$gridsize;$i++){ 
  89.     for ($j=0;$j!=$gridsize;$j++){ 
  90.     $board[$i][$j]= " "; 
  91.     } 
  92.     } 
  93.     } 
  94.     // -------------------------------------
  95.     //     ----------------------------------------
  96.     //     ------------- 
  97.     function placewords(){ 
  98.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  99.     // count the number of letters in the fi
  100.     //     rst word 
  101.     $wordnumber=count($wordarray); 
  102.     // main for loop one time through for ea
  103.     //     ch word 
  104.     for($i=0;$i!=$wordnumber;$i++) { 
  105.     // count the letters in the current word
  106.     //     
  107.     $letternumber=(count($wordarray[$i])); 
  108.     // toggle wordfit for verification of wo
  109.     //     rd fitting on the grid without overwrite
  110.     //     ing any other letters 
  111.     $wordfit=0; 
  112.     while ($wordfit <= 0){ 
  113.     // toggle wordfit again in case the whil
  114.     //     e loop runs a few times 
  115.     $wordfit=0; 
  116.     // toggle length ok to ensure proper che
  117.     //     cking of word length within constraints 
  118.     //     of grid 
  119.     $lengthok=0; 
  120.     // test for fit of word 
  121.     while ($lengthok!=1){ 
  122.     // seed random with the clock 
  123.     srand((double)microtime()*1000000); 
  124.     // get the random direction based on dif
  125.     //     ficulty selected 
  126.     switch ($difficulty){ 
  127.     case hard: 
  128.     $direction=rand()%8; 
  129.     break; 
  130.     case easy: 
  131.     $direction=rand()%4; 
  132.     break; 
  133.     default: 
  134.     $direction=rand()%8; 
  135.     } 
  136.     // pick a random position within the sco
  137.     //     pe of the grid 
  138.     srand((double)microtime()*1000000); 
  139.     $position=array(x=>(rand()%$gridsize),y=>(rand()%$gridsize)); 
  140.     // calculate where the word will end 
  141.     $endposition[x]=$position[x]+($letternumber*$dirtn[$direction][x]); 
  142.     $endposition[y]=$position[y]+($letternumber*$dirtn[$direction][y]); 
  143.     // make our endposition calculation have
  144.     //     to be between 0 and 1 
  145.     $result[x]=(abs($endposition[x]/$gridsize)); 
  146.     $result[y]=(abs($endposition[y]/$gridsize)); 
  147.     // if the end position is inside the gri
  148.     //     d set the length ok 
  149.     if($result[x]>1) { 
  150.     $lengthok=0; 
  151.     } 
  152.     elseif($result[y]>1) { 
  153.     $lengthok=0; 
  154.     } 
  155.     else{ 
  156.     $lengthok=1; 
  157.     } 
  158.     } 
  159.     // set a temp var to verify every positi
  160.     //     on a letter will occupy 
  161.     $tposition=$position; 
  162.     // now check for what is in the position
  163.     //     s selected 
  164.     reset($wordarray[$i]); 
  165.     while ( list ($key,$val)=each($wordarray[$i])){ 
  166.     // what is currently in the space 
  167.     switch ($board[$tposition[x]][$tposition[y]]){ 
  168.     // if it is a space, move in the directi
  169.     //     on one space and toggle wordfit +1 
  170.     case " ": 
  171.     $tposition[x]=$tposition[x]+$dirtn[$direction][x]; 
  172.     $tposition[y]=$tposition[y]+$dirtn[$direction][y]; 
  173.     $wordfit=$wordfit+1; 
  174.     break; 
  175.     // if it is the same as the value we wan
  176.     //     t to place there, move and toggle wordfi
  177.     //     t +1 
  178.     case $val: 
  179.     $tposition[x]=$tposition[x]+$dirtn[$direction][x]; 
  180.     $tposition[y]=$tposition[y]+$dirtn[$direction][y]; 
  181.     $wordfit=$wordfit+1; 
  182.     break; 
  183.     // if the letter is not compatible, togg
  184.     //     le wordfit -50 so that we do not place t
  185.     //     he word 
  186.     // we will have to go back and find a ne
  187.     //     w place 
  188.     default: 
  189.     $wordfit=(-50); 
  190.     } 
  191.     } 
  192.     } 
  193.     reset ($wordarray[$i]); 
  194.     // we have successfully gotten out of th
  195.     //     e first while loop and the word can be p
  196.     //     laced 
  197.     while (list ($key,$val)=each($wordarray[$i])){ 
  198.     $board[$position[x]][$position[y]]=$val; 
  199.     $position[x]=$position[x]+$dirtn[$direction][x]; 
  200.     $position[y]=$position[y]+$dirtn[$direction][y]; 
  201.     } 
  202.     } // end of main for loop 
  203.     } 
  204.     //--------------------------------------
  205.     //     ----------------------------------------
  206.     //     ------ 
  207.     function printgrid(){ 
  208.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  209.     
  210.     //fill blanks with random characters 
  211.     reset($board); 
  212.     // while in each row x 
  213.     while (list($key,$val)=each($board)){ 
  214.     // go to each column y 
  215.     while (list($key2,$val2)=each($board[$key])){ 
  216.     // if the character there is blank go ah
  217.     //     ead and fill it in in the array 
  218.     switch ($board[$key][$key2]){ 
  219.     case " ": 
  220.     srand((double)microtime()*1000000); 
  221.     $board[$key][$key2]=chr(rand()%26+65); 
  222.     break; 
  223.     default: 
  224.     break; 
  225.     } 
  226.     } 
  227.     } 
  228.     // now print those values to the page in
  229.     //     side of a table 
  230.     for ($i=0;$i!=$gridsize;$i++){ 
  231.     echo "<tr>\n"; 
  232.     for ($j=0;$j!=$gridsize;$j++){ 
  233.     echo "<td width=\"20\" height=\"15\" align=\"CENTER\" valign=\"MIDDLE\">" . $board[$i][$j] . "</td>\n"; 
  234.     } 
  235.     echo "</tr>\n"; 
  236.     } 
  237.     } 
  238.     // -------------------------------------
  239.     //     ---------------------------- 
  240.     function printlist(){ 
  241.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  242.     
  243.     sort ($wordlist); 
  244.     reset ($wordlist); 
  245.     while (list ($key,$val)=current( $wordlist) ) { 
  246.     echo "<tr>\n"; 
  247.     for ($i=0;$i<3;$i++){ 
  248.     $val=current($wordlist); 
  249.     echo "<td width=\"150\" align=\"LEFT\" valign=\"MIDDLE\">" . $val . "</td>\n"; 
  250.     $val=next($wordlist); 
  251.     } 
  252.     echo "</TR>\n"; 
  253.     } 
  254.     } 
  255.     // -------------------------------------
  256.     //     ---------------------------- 
  257.     function printinfo(){ 
  258.     global $studentname,$classname,$assignment,$period,$board,$wordarray,$dirtn,$title,$gridsize,$wordlist,$difficulty,$listorder,$HTTP_POST_VARS; 
  259.     echo "<tr>\n<td align=\"LEFT\" valign=\"MIDDLE\">Class: " . $classname . "<br>\n"; 
  260.     echo "Student: " . $studentname . "</td>\n"; 
  261.     echo "<td align=\"LEFT\" valign=\"MIDDLE\">Assignment: " . $assignment . "<br>\n"; 
  262.     echo "Period: " . $period . "</td></tr>\n"; 
  263.     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"; 
  264.     } 
  265.     ?> 
  266.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  267.     <html> 
  268.     <head> 
  269.     <title>Puzzle Result</title> 
  270.     </head> 
  271.     <body bgcolor="White" topmargin=0> 
  272.     <? 
  273.     setvars(); 
  274.     seplist(); 
  275.     boardset(); 
  276.     placewords(); 
  277.     echo "<div align=\"center\"><h2>$title</h2></div>"; 
  278.     ?> 
  279.     <table border="0" cellspacing="0" cellpadding="0" align="CENTER" valign="TOP" nowrap> 
  280.     <? 
  281.     printgrid(); 
  282.     ?> 
  283.     </table> 
  284.     <br><br> 
  285.     <table width="80%" border="0" cellspacing="0" cellpadding="0" align="CENTER" valign="MIDDLE" nowrap> 
  286.     <? 
  287.     printlist(); 
  288.     ?> 
  289.     </table> 
  290.     <br> 
  291.     <table width="80%" border="1" cellspacing="0" cellpadding="0" align="CENTER" valign="MIDDLE" nowrap> 
  292.     <? 
  293.     printinfo(); 
  294.     ?> 
  295.     </table> 
  296.     </body> 
  297.     </html> 
  298.  
  299.