php row and col problem

  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 23

Post 3+ Months Ago

Im making a word search pgm or modifying an existing one but im haveing trouble with one diagonal part but getting the opposite done has got stuck. Im only posting the switch that has the moves i have made already the problem im having is the case "NW" and case "SE"
Code: [ Select ]
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 + $i
    $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
   } // end for loop
   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<br>\n";
 
   for ($i = 0; $i < strlen($theWord); $i++){
    //check for a legal move
    $boardLetter = $board[$newRow][$newCol - $i];
    $wordLetter = substr($theWord, $i, 1);
    if (($boardLetter == $wordLetter) ||
      ($boardLetter == ".")){
     $board[$newRow][$newCol - $i] = $wordLetter;
    } else {
     $itWorked = FALSE;
    } // end if
   } // end for loop
   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<br>\n";

   for ($i = 0; $i < strlen($theWord); $i++){
    //check for a legal move
    $boardLetter = $board[$newRow + $i][$newCol];
    $wordLetter = substr($theWord, $i, 1);
    if (($boardLetter == $wordLetter) ||
      ($boardLetter == ".")){
     $board[$newRow + $i][$newCol] = $wordLetter;
       } else {
     $itWorked = FALSE;
       } // end if
   } // end for loop
   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);
    if (($boardLetter == $wordLetter) ||
      ($boardLetter == ".")){
     $board[$newRow - $i][$newCol] = $wordLetter;
       } else {
     $itWorked = FALSE;
       } // end if
   } // end for loop
   break;
    case "NE":
      //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 + $i
    $boardLetter = $board[$newRow - $i][$newCol + $i];//modded $newRow to $newRow - $i
    $wordLetter = substr($theWord, $i, 1);
    
    //check for legal values in current space on board
    if (($boardLetter == $wordLetter) ||
      ($boardLetter == ".")){
     $board[$newRow-$i][$newCol + $i] = $wordLetter;//modded [newRow] to [newRow - $i]
    } else {
     $itWorked = FALSE;
    } // end if
   } // end for loop
   break;
  case "SW":
      //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 "northwest:\tRow: $newRow\tCol: $newCol<br>\n";
 
   for ($i = 0; $i < strlen($theWord); $i++){
    //check for a legal move
    $boardLetter = $board[$newRow + $i][$newCol - $i];
    $wordLetter = substr($theWord, $i, 1);
    if (($boardLetter == $wordLetter) ||
      ($boardLetter == ".")){
     $board[$newRow+$i][$newCol-$i] = $wordLetter;
    }else{
     $itWorked = FALSE;
    } // end if
   } // end for loop
   break;
    case "NW"://<--this is for forward NW words
        //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 "northwest:\tRow: $newRow\tCol: $newCol<br>\";
        
        for($i = 0; $i < strlen($theWord); $i++){
         //check for legal move
         $boardLetter = $board[$newRow - $i][$newCol + $i];//<--having trouble here getting row and col
         $wordLetter = substr($theWord, $i, 1);
         $wordLetter = substr($theWord, $i, 1);
         if(($boardLetter == $wordLetter)||
             ($boardLetter == ".")){
         $board[$newRow - $i][$newCol + $i] = $wordLetter;
         }else{
             $itWorked = FALSE;
         }
        }
        break;
    case "SE"://<--for backward NW word
        //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 "northwest:\tRow: $newRow\tCol: $newCol<br>\";
        
        for($i = 0; $i < strlen($theWord); $i++){
         //check for legal move
         $boardLetter = $board[$newRow - $i][$newCol + $i];//<--having trouble here getting row and col
         $wordLetter = substr($theWord, $i, 1);
         if(($boardLetter == $wordLetter)||
             ($boardLetter == ".")){
         $board[$newRow - $i][$newCol + $i] = $wordLetter;
         }else{
             $itWorked = FALSE;
         }
        }
        break;
 } // end switch
  1. switch ($dir){
  2.      case "E":
  3.       //col from 0 to board width - word width
  4.    //row from 0 to board height
  5.    $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
  6.       $newRow = rand(0, $boardData["height"]-1);
  7.    for ($i = 0; $i < strlen($theWord); $i++){
  8.     //new character same row, initial column + $i
  9.     $boardLetter = $board[$newRow][$newCol + $i];
  10.     $wordLetter = substr($theWord, $i, 1);
  11.     
  12.     //check for legal values in current space on board
  13.     if (($boardLetter == $wordLetter) ||
  14.       ($boardLetter == ".")){
  15.      $board[$newRow][$newCol + $i] = $wordLetter;
  16.     } else {
  17.      $itWorked = FALSE;
  18.     } // end if
  19.    } // end for loop
  20.    break;
  21.    
  22.      case "W":
  23.       //col from word width to board width
  24.       //row from 0 to board height
  25.    $newCol = rand(strlen($theWord), $boardData["width"] -1);
  26.    $newRow = rand(0, $boardData["height"]-1);
  27.    //print "west:\tRow: $newRow\tCol: $newCol<br>\n";
  28.  
  29.    for ($i = 0; $i < strlen($theWord); $i++){
  30.     //check for a legal move
  31.     $boardLetter = $board[$newRow][$newCol - $i];
  32.     $wordLetter = substr($theWord, $i, 1);
  33.     if (($boardLetter == $wordLetter) ||
  34.       ($boardLetter == ".")){
  35.      $board[$newRow][$newCol - $i] = $wordLetter;
  36.     } else {
  37.      $itWorked = FALSE;
  38.     } // end if
  39.    } // end for loop
  40.    break;
  41.    
  42.   case "S":
  43.       //col from 0 to board width
  44.       //row from 0 to board height - word length
  45.    $newCol = rand(0, $boardData["width"] -1);
  46.    $newRow = rand(0, $boardData["height"]-1 - strlen($theWord));
  47.    //print "south:\tRow: $newRow\tCol: $newCol<br>\n";
  48.    for ($i = 0; $i < strlen($theWord); $i++){
  49.     //check for a legal move
  50.     $boardLetter = $board[$newRow + $i][$newCol];
  51.     $wordLetter = substr($theWord, $i, 1);
  52.     if (($boardLetter == $wordLetter) ||
  53.       ($boardLetter == ".")){
  54.      $board[$newRow + $i][$newCol] = $wordLetter;
  55.        } else {
  56.      $itWorked = FALSE;
  57.        } // end if
  58.    } // end for loop
  59.    break;
  60.   case "N":
  61.       //col from 0 to board width
  62.       //row from word length to board height
  63.    $newCol = rand(0, $boardData["width"] -1);
  64.    $newRow = rand(strlen($theWord), $boardData["height"]-1);
  65.    for ($i = 0; $i < strlen($theWord); $i++){
  66.     //check for a legal move
  67.     $boardLetter = $board[$newRow - $i][$newCol];
  68.     $wordLetter = substr($theWord, $i, 1);
  69.     if (($boardLetter == $wordLetter) ||
  70.       ($boardLetter == ".")){
  71.      $board[$newRow - $i][$newCol] = $wordLetter;
  72.        } else {
  73.      $itWorked = FALSE;
  74.        } // end if
  75.    } // end for loop
  76.    break;
  77.     case "NE":
  78.       //col from 0 to board width - word width
  79.    //row from 0 to board height
  80.    $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
  81.       $newRow = rand(0, $boardData["height"]-1);
  82.    for ($i = 0; $i < strlen($theWord); $i++){
  83.     //new character same row, initial column + $i
  84.     $boardLetter = $board[$newRow - $i][$newCol + $i];//modded $newRow to $newRow - $i
  85.     $wordLetter = substr($theWord, $i, 1);
  86.     
  87.     //check for legal values in current space on board
  88.     if (($boardLetter == $wordLetter) ||
  89.       ($boardLetter == ".")){
  90.      $board[$newRow-$i][$newCol + $i] = $wordLetter;//modded [newRow] to [newRow - $i]
  91.     } else {
  92.      $itWorked = FALSE;
  93.     } // end if
  94.    } // end for loop
  95.    break;
  96.   case "SW":
  97.       //col from word width to board width
  98.       //row from 0 to board height
  99.    $newCol = rand(strlen($theWord), $boardData["width"] -1);
  100.    $newRow = rand(0, $boardData["height"]-1);
  101.    //print "northwest:\tRow: $newRow\tCol: $newCol<br>\n";
  102.  
  103.    for ($i = 0; $i < strlen($theWord); $i++){
  104.     //check for a legal move
  105.     $boardLetter = $board[$newRow + $i][$newCol - $i];
  106.     $wordLetter = substr($theWord, $i, 1);
  107.     if (($boardLetter == $wordLetter) ||
  108.       ($boardLetter == ".")){
  109.      $board[$newRow+$i][$newCol-$i] = $wordLetter;
  110.     }else{
  111.      $itWorked = FALSE;
  112.     } // end if
  113.    } // end for loop
  114.    break;
  115.     case "NW"://<--this is for forward NW words
  116.         //col from word width to board width
  117.         //row from 0 to board height
  118.         $newCol = rand(strlen($theWord), $boardData["width"]-1);
  119.         $newRow = rand(0, $boardData["height"]-1);
  120.         //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
  121.         
  122.         for($i = 0; $i < strlen($theWord); $i++){
  123.          //check for legal move
  124.          $boardLetter = $board[$newRow - $i][$newCol + $i];//<--having trouble here getting row and col
  125.          $wordLetter = substr($theWord, $i, 1);
  126.          $wordLetter = substr($theWord, $i, 1);
  127.          if(($boardLetter == $wordLetter)||
  128.              ($boardLetter == ".")){
  129.          $board[$newRow - $i][$newCol + $i] = $wordLetter;
  130.          }else{
  131.              $itWorked = FALSE;
  132.          }
  133.         }
  134.         break;
  135.     case "SE"://<--for backward NW word
  136.         //col from word width to board width
  137.         //row from 0 to board height
  138.         $newCol = rand(strlen($theWord), $boardData["width"]-1)
  139.         $newRow = rand(0, $boardData["height"]-1);
  140.         //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
  141.         
  142.         for($i = 0; $i < strlen($theWord); $i++){
  143.          //check for legal move
  144.          $boardLetter = $board[$newRow - $i][$newCol + $i];//<--having trouble here getting row and col
  145.          $wordLetter = substr($theWord, $i, 1);
  146.          if(($boardLetter == $wordLetter)||
  147.              ($boardLetter == ".")){
  148.          $board[$newRow - $i][$newCol + $i] = $wordLetter;
  149.          }else{
  150.              $itWorked = FALSE;
  151.          }
  152.         }
  153.         break;
  154.  } // end switch
  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 23

Post 3+ Months Ago

i fixed it now so the program will place the words any way back and forward as soon as i comment it ill post the results to get feed back im trying to learn the proper technique for php coding
  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 23

Post 3+ Months Ago

ok im just posting the php part (sorry it took so long work has been busy) I added the diagnal lines in the switch statement but now i need to use random letters from the users list of words to fill the puzzle up instead of just random letters. fyi im useing the book php6/mysql to learn and this is one of the challenges in chaptor five. Please just point me in the right direction do not solve it. its the addfoils function im trying to change. I tried making a array from the $wordList var and then useing that to make the fill letters but it didnt work and ive working on this for two days and cant get it to work. if you look at the top i added the $_REQUEST array to see if they printing and they are but i cant figure out how to get them to print in the $foil function. do i need to split the array ? or trim ? any help would be appriciated
thank you

PHP Code: [ Select ]
<?php
 
foreach ($_REQUEST as $key => $value) {//added this just to see if words were printing
print <<<HERE
$value<br>
$key<br>
HERE;
   
}
//modded for diagnals 10/28/11 by samuel mckenna
// word Find
// by Andy Harris, 2003
// for PHP/MySQL programming for the Absolute Beginner
// Generates a word search puzzle based on a word list
// entered by user.  User can also specify the size of
// the puzzle and print out an answer key if desired
 
//check for a word list
if (!filter_has_var(INPUT_POST, "wordList")){
  print <<< HERE
    <p>
      This program is meant to be run from the
      <a href = "battleShip.html">wordFind</a>
      page.
    </p>
HERE;
} else {
  //get puzzle data from HTML form
  $boardData = array(
    "width" => filter_input(INPUT_POST, "width"),
    "height" => filter_input(INPUT_POST, "height"),
    "name" => filter_input(INPUT_POST, "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 parsed list if
} // end word list exists if
 
function parseList(){
  //gets word list, creates array of words from it
  //or return false if impossible
 
  global $word, $wordList, $boardData;
 
  $wordList = filter_input(INPUT_POST, "wordList");
  $itWorked = TRUE;
 
  //convert word list entirely to upper case
  $wordList = strtoupper($wordList);
 
  //split word list into array
  $word = split("\n", $wordList);
 
//$currentWord = split(NULL, $wordList);//////////////////
 
  foreach ($word as $currentWord){
    //take out trailing newline characters
    $currentWord = rtrim($currentWord);////
 
    //stop if any words are too long to fit in puzzle
    if ((strLen($currentWord) > $boardData["width"]) &&
        (strLen($currentWord) > $boardData["height"])){
      print "$currentWord is too long for puzzle";
      $itWorked = FALSE;
    } // end if
 
  } // end foreach
  return $itWorked;
} // end parseList
 
function clearBoard(){
  //initialize 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 loop
  } // end row for loop
} // end clearBoard
 
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 + $i
        $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
      } // end for loop
      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<br>\n";
 
      for ($i = 0; $i < strlen($theWord); $i++){
        //check for a legal move
        $boardLetter = $board[$newRow][$newCol - $i];
        $wordLetter = substr($theWord, $i, 1);
        if (($boardLetter == $wordLetter) ||
            ($boardLetter == ".")){
          $board[$newRow][$newCol - $i] = $wordLetter;
        } else {
          $itWorked = FALSE;
        } // end if
      } // end for loop
      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<br>\n";
 
      for ($i = 0; $i < strlen($theWord); $i++){
        //check for a legal move
        $boardLetter = $board[$newRow + $i][$newCol];
        $wordLetter = substr($theWord, $i, 1);
        if (($boardLetter == $wordLetter) ||
            ($boardLetter == ".")){
          $board[$newRow + $i][$newCol] = $wordLetter;
         } else {
         $itWorked = FALSE;
         } // end if
      } // end for loop
      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);
        if (($boardLetter == $wordLetter) ||
            ($boardLetter == ".")){
          $board[$newRow - $i][$newCol] = $wordLetter;
         } else {
         $itWorked = FALSE;
         } // end if
      } // end for loop
      break;
   case "NE":
     //col from 0 to board width - word width
      //row from 0 to board height
      $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));//modded sam
     $newRow = rand(0, $boardData["height"]-1);
 
      for ($i = 0; $i < strlen($theWord); $i++){
        //new character same row, initial column + $i
        $boardLetter = $board[$newRow - $i][$newCol + $i];//modded $newRow to $newRow - $i sam
        $wordLetter = substr($theWord, $i, 1);
       
        //check for legal values in current space on board
        if (($boardLetter == $wordLetter) ||
            ($boardLetter == ".")){
          $board[$newRow-$i][$newCol + $i] = $wordLetter;//modded [newRow] to [newRow - $i] sam
        } else {
          $itWorked = FALSE;
        } // end if
      } // end for loop
      break;
    case "SW":
     //col from word width to board width
     //row from 0 to board height
      $newCol = rand(strlen($theWord), $boardData["width"] -1);// modded sam
      $newRow = rand(0, $boardData["height"]-1);//modded sam
      //print "northwest:\tRow: $newRow\tCol: $newCol<br>\n";
 
      for ($i = 0; $i < strlen($theWord); $i++){
        //check for a legal move
        $boardLetter = $board[$newRow + $i][$newCol - $i];//modded to + and -
        $wordLetter = substr($theWord, $i, 1);
        if (($boardLetter == $wordLetter) ||
            ($boardLetter == ".")){
          $board[$newRow + $i][$newCol - $i] = $wordLetter;//modded to + and -
        }else{
          $itWorked = FALSE;
        } // end if
      } // end for loop
      break;
   case "NW":
      //col from word width to board width
      //row from 0 to board height
      $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));//modded sam
       $newRow = rand(0, $boardData["height"]-1);//modded sam
      //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
     
      for($i = 0; $i < strlen($theWord); $i++){
        //check for legal move
        $boardLetter = $board[$newRow - $i][$newCol - $i];//modded to - and -
        $wordLetter = substr($theWord, $i, 1);
        if(($boardLetter == $wordLetter)||
         ($boardLetter == ".")){
        $board[$newRow - $i][$newCol - $i] = $wordLetter;// modded to - and -
        }else{
         $itWorked = FALSE;
        }
      }
      break;
   case "SE":
      //col from word width to board width
      //row from 0 to board height
         
      $newCol = rand(0, $boardData["height"]-1 - strlen($theWord));//modded
      $newRow =rand(strlen($theWord), $boardData["width"]-1); //modded
      //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
     
      for($i = 0; $i < strlen($theWord); $i++){
        //check for legal move
        $boardLetter = $board[$newRow + $i][$newCol + $i];//changed to + and +
        $wordLetter = substr($theWord, $i, 1);
        if(($boardLetter == $wordLetter)||
         ($boardLetter == ".")){
        $board[$newRow + $i][$newCol + $i] = $wordLetter;// changed to + and +
        }else{
         $itWorked = FALSE;
        }
      }
      break;
  } // end switch
  return $itWorked;
} // end addWord
 
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","NE","SW","NW","SE");//added diagnals sam
  $itWorked = TRUE;
  $counter = 0;
  $keepGoing = TRUE;
  while($keepGoing){
    $dir = rand(0, 7);//add one for each dir
    $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 makeBoard($theBoard){
  //given a board array, return an HTML table based on the array
  global $boardData;
  $puzzle = "";
  $puzzle .= "<table>\n";
  //check logic here
  for ($row = 0; $row < $boardData["height"]; $row++){
   $puzzle .= "<tr>\n";
    for ($col = 0; $col < $boardData["width"]; $col++){
      $puzzle .= "  <td>{$theBoard[$row][$col]}</td>\n";
    } // end col for loop
    $puzzle .= "</tr>\n";
  } // end row for loop
  $puzzle .= "</table>\n";
  return $puzzle;
} // end printBoard;
 
//////////////////
 
 
 
//$samWord = filter_input(INPUT_POST, "wordList");
$value = strtoupper($value);
$value = rtrim($value);
/////////////////
 
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);//xxxxxxxxxxxxxxxxxxxxxxxxx
        $board[$row][$col] = chr($newLetter);
      } // end if
    } // end col for loop
  } // end row for loop
} // end addFoils
 
function printPuzzle(){
  //print out page to user with puzzle on it
 
  global $puzzle, $word, $keyPuzzle, $boardData;
  //print puzzle itself
 
  print <<<HERE
  <h1>{$boardData["name"]}</h1>
  $puzzle
  <h3>Word List</h3>
  <ul>
 
HERE;
  //print word list
  foreach ($word as $theWord){
    $theWord = rtrim($theWord);
    print "<li>$theWord</li>\n";
  } // end foreach
  print "</ul>\n";
  $puzzleName = $boardData["name"];
 
  //print form for requesting answer key.
  //save answer key as session var
 
  $_SESSION["key"] = $keyPuzzle;
  $_SESSION["puzzleName"] = $puzzleName;
 
 
  print <<<HERE
 
  <form action = "wordP_Key.php"
        method = "post"
            id = "keyForm">
  <div>
  <input type = "submit"
         value = "show answer key" />
  </div>
  </form>
 
HERE;
 
} // end printPuzzle
 
 
?>
 
 
 
  1. <?php
  2.  
  3. foreach ($_REQUEST as $key => $value) {//added this just to see if words were printing
  4. print <<<HERE
  5. $value<br>
  6. $key<br>
  7. HERE;
  8.    
  9. }
  10. //modded for diagnals 10/28/11 by samuel mckenna
  11. // word Find
  12. // by Andy Harris, 2003
  13. // for PHP/MySQL programming for the Absolute Beginner
  14. // Generates a word search puzzle based on a word list
  15. // entered by user.  User can also specify the size of
  16. // the puzzle and print out an answer key if desired
  17.  
  18. //check for a word list
  19. if (!filter_has_var(INPUT_POST, "wordList")){
  20.   print <<< HERE
  21.     <p>
  22.       This program is meant to be run from the
  23.       <a href = "battleShip.html">wordFind</a>
  24.       page.
  25.     </p>
  26. HERE;
  27. } else {
  28.   //get puzzle data from HTML form
  29.   $boardData = array(
  30.     "width" => filter_input(INPUT_POST, "width"),
  31.     "height" => filter_input(INPUT_POST, "height"),
  32.     "name" => filter_input(INPUT_POST, "name")
  33.   );
  34.  
  35.   //try to get a word list from user input
  36.   if (parseList() == TRUE){
  37.     $legalBoard = FALSE;
  38.  
  39.     //keep trying to build a board until you get a legal result
  40.     while ($legalBoard == FALSE){
  41.       clearBoard();
  42.       $legalBoard = fillBoard();
  43.     } // end while
  44.  
  45.     //make the answer key
  46.     $key = $board;
  47.     $keyPuzzle = makeBoard($key);
  48.  
  49.     //make the final puzzle
  50.     addFoils();
  51.     $puzzle = makeBoard($board);
  52.  
  53.     //print out the result page
  54.     printPuzzle();
  55.    
  56.   } // end parsed list if
  57. } // end word list exists if
  58.  
  59. function parseList(){
  60.   //gets word list, creates array of words from it
  61.   //or return false if impossible
  62.  
  63.   global $word, $wordList, $boardData;
  64.  
  65.   $wordList = filter_input(INPUT_POST, "wordList");
  66.   $itWorked = TRUE;
  67.  
  68.   //convert word list entirely to upper case
  69.   $wordList = strtoupper($wordList);
  70.  
  71.   //split word list into array
  72.   $word = split("\n", $wordList);
  73.  
  74. //$currentWord = split(NULL, $wordList);//////////////////
  75.  
  76.   foreach ($word as $currentWord){
  77.     //take out trailing newline characters
  78.     $currentWord = rtrim($currentWord);////
  79.  
  80.     //stop if any words are too long to fit in puzzle
  81.     if ((strLen($currentWord) > $boardData["width"]) &&
  82.         (strLen($currentWord) > $boardData["height"])){
  83.       print "$currentWord is too long for puzzle";
  84.       $itWorked = FALSE;
  85.     } // end if
  86.  
  87.   } // end foreach
  88.   return $itWorked;
  89. } // end parseList
  90.  
  91. function clearBoard(){
  92.   //initialize board with a . in each cell
  93.   global $board, $boardData;
  94.  
  95.   for ($row = 0; $row < $boardData["height"]; $row++){
  96.     for ($col = 0; $col < $boardData["width"]; $col++){
  97.       $board[$row][$col] = ".";
  98.     } // end col for loop
  99.   } // end row for loop
  100. } // end clearBoard
  101.  
  102. function addWord($theWord, $dir){
  103.   //attempt to add a word to the board or return false if failed
  104.   global $board, $boardData;
  105.  
  106.   //remove trailing characters if necessary
  107.   $theWord = rtrim($theWord);
  108.  
  109.   $itWorked = TRUE;
  110.  
  111.   switch ($dir){
  112.    case "E":
  113.      //col from 0 to board width - word width
  114.       //row from 0 to board height
  115.       $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
  116.      $newRow = rand(0, $boardData["height"]-1);
  117.  
  118.       for ($i = 0; $i < strlen($theWord); $i++){
  119.         //new character same row, initial column + $i
  120.         $boardLetter = $board[$newRow][$newCol + $i];
  121.         $wordLetter = substr($theWord, $i, 1);
  122.        
  123.         //check for legal values in current space on board
  124.         if (($boardLetter == $wordLetter) ||
  125.             ($boardLetter == ".")){
  126.           $board[$newRow][$newCol + $i] = $wordLetter;
  127.         } else {
  128.           $itWorked = FALSE;
  129.         } // end if
  130.       } // end for loop
  131.       break;
  132.      
  133.    case "W":
  134.      //col from word width to board width
  135.      //row from 0 to board height
  136.       $newCol = rand(strlen($theWord), $boardData["width"] -1);
  137.       $newRow = rand(0, $boardData["height"]-1);
  138.       //print "west:\tRow: $newRow\tCol: $newCol<br>\n";
  139.  
  140.       for ($i = 0; $i < strlen($theWord); $i++){
  141.         //check for a legal move
  142.         $boardLetter = $board[$newRow][$newCol - $i];
  143.         $wordLetter = substr($theWord, $i, 1);
  144.         if (($boardLetter == $wordLetter) ||
  145.             ($boardLetter == ".")){
  146.           $board[$newRow][$newCol - $i] = $wordLetter;
  147.         } else {
  148.           $itWorked = FALSE;
  149.         } // end if
  150.       } // end for loop
  151.       break;
  152.      
  153.     case "S":
  154.      //col from 0 to board width
  155.      //row from 0 to board height - word length
  156.       $newCol = rand(0, $boardData["width"] -1);
  157.       $newRow = rand(0, $boardData["height"]-1 - strlen($theWord));
  158.       //print "south:\tRow: $newRow\tCol: $newCol<br>\n";
  159.  
  160.       for ($i = 0; $i < strlen($theWord); $i++){
  161.         //check for a legal move
  162.         $boardLetter = $board[$newRow + $i][$newCol];
  163.         $wordLetter = substr($theWord, $i, 1);
  164.         if (($boardLetter == $wordLetter) ||
  165.             ($boardLetter == ".")){
  166.           $board[$newRow + $i][$newCol] = $wordLetter;
  167.          } else {
  168.          $itWorked = FALSE;
  169.          } // end if
  170.       } // end for loop
  171.       break;
  172.  
  173.     case "N":
  174.      //col from 0 to board width
  175.      //row from word length to board height
  176.       $newCol = rand(0, $boardData["width"] -1);
  177.       $newRow = rand(strlen($theWord), $boardData["height"]-1);
  178.  
  179.       for ($i = 0; $i < strlen($theWord); $i++){
  180.         //check for a legal move
  181.         $boardLetter = $board[$newRow - $i][$newCol];
  182.         $wordLetter = substr($theWord, $i, 1);
  183.         if (($boardLetter == $wordLetter) ||
  184.             ($boardLetter == ".")){
  185.           $board[$newRow - $i][$newCol] = $wordLetter;
  186.          } else {
  187.          $itWorked = FALSE;
  188.          } // end if
  189.       } // end for loop
  190.       break;
  191.    case "NE":
  192.      //col from 0 to board width - word width
  193.       //row from 0 to board height
  194.       $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));//modded sam
  195.      $newRow = rand(0, $boardData["height"]-1);
  196.  
  197.       for ($i = 0; $i < strlen($theWord); $i++){
  198.         //new character same row, initial column + $i
  199.         $boardLetter = $board[$newRow - $i][$newCol + $i];//modded $newRow to $newRow - $i sam
  200.         $wordLetter = substr($theWord, $i, 1);
  201.        
  202.         //check for legal values in current space on board
  203.         if (($boardLetter == $wordLetter) ||
  204.             ($boardLetter == ".")){
  205.           $board[$newRow-$i][$newCol + $i] = $wordLetter;//modded [newRow] to [newRow - $i] sam
  206.         } else {
  207.           $itWorked = FALSE;
  208.         } // end if
  209.       } // end for loop
  210.       break;
  211.     case "SW":
  212.      //col from word width to board width
  213.      //row from 0 to board height
  214.       $newCol = rand(strlen($theWord), $boardData["width"] -1);// modded sam
  215.       $newRow = rand(0, $boardData["height"]-1);//modded sam
  216.       //print "northwest:\tRow: $newRow\tCol: $newCol<br>\n";
  217.  
  218.       for ($i = 0; $i < strlen($theWord); $i++){
  219.         //check for a legal move
  220.         $boardLetter = $board[$newRow + $i][$newCol - $i];//modded to + and -
  221.         $wordLetter = substr($theWord, $i, 1);
  222.         if (($boardLetter == $wordLetter) ||
  223.             ($boardLetter == ".")){
  224.           $board[$newRow + $i][$newCol - $i] = $wordLetter;//modded to + and -
  225.         }else{
  226.           $itWorked = FALSE;
  227.         } // end if
  228.       } // end for loop
  229.       break;
  230.    case "NW":
  231.       //col from word width to board width
  232.       //row from 0 to board height
  233.       $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));//modded sam
  234.        $newRow = rand(0, $boardData["height"]-1);//modded sam
  235.       //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
  236.      
  237.       for($i = 0; $i < strlen($theWord); $i++){
  238.         //check for legal move
  239.         $boardLetter = $board[$newRow - $i][$newCol - $i];//modded to - and -
  240.         $wordLetter = substr($theWord, $i, 1);
  241.         if(($boardLetter == $wordLetter)||
  242.          ($boardLetter == ".")){
  243.         $board[$newRow - $i][$newCol - $i] = $wordLetter;// modded to - and -
  244.         }else{
  245.          $itWorked = FALSE;
  246.         }
  247.       }
  248.       break;
  249.    case "SE":
  250.       //col from word width to board width
  251.       //row from 0 to board height
  252.          
  253.       $newCol = rand(0, $boardData["height"]-1 - strlen($theWord));//modded
  254.       $newRow =rand(strlen($theWord), $boardData["width"]-1); //modded
  255.       //print "northwest:\tRow: $newRow\tCol: $newCol<br>\";
  256.      
  257.       for($i = 0; $i < strlen($theWord); $i++){
  258.         //check for legal move
  259.         $boardLetter = $board[$newRow + $i][$newCol + $i];//changed to + and +
  260.         $wordLetter = substr($theWord, $i, 1);
  261.         if(($boardLetter == $wordLetter)||
  262.          ($boardLetter == ".")){
  263.         $board[$newRow + $i][$newCol + $i] = $wordLetter;// changed to + and +
  264.         }else{
  265.          $itWorked = FALSE;
  266.         }
  267.       }
  268.       break;
  269.   } // end switch
  270.   return $itWorked;
  271. } // end addWord
  272.  
  273. function fillBoard(){
  274.   //fill board with list by calling addWord() for each word
  275.   //or return false if failed
  276.  
  277.   global $word;
  278.   $direction = array("N", "S", "E", "W","NE","SW","NW","SE");//added diagnals sam
  279.   $itWorked = TRUE;
  280.   $counter = 0;
  281.   $keepGoing = TRUE;
  282.   while($keepGoing){
  283.     $dir = rand(0, 7);//add one for each dir
  284.     $result = addWord($word[$counter], $direction[$dir]);
  285.     if ($result == FALSE){
  286.       //print "failed to place $word[$counter]";
  287.       $keepGoing = FALSE;
  288.       $itWorked = FALSE;
  289.     } // end if
  290.     $counter++;
  291.     if ($counter >= count($word)){
  292.       $keepGoing = FALSE;
  293.     } // end if
  294.   } // end while
  295.   return $itWorked;
  296.  
  297. } // end fillBoard
  298.  
  299. function makeBoard($theBoard){
  300.   //given a board array, return an HTML table based on the array
  301.   global $boardData;
  302.   $puzzle = "";
  303.   $puzzle .= "<table>\n";
  304.   //check logic here
  305.   for ($row = 0; $row < $boardData["height"]; $row++){
  306.    $puzzle .= "<tr>\n";
  307.     for ($col = 0; $col < $boardData["width"]; $col++){
  308.       $puzzle .= "  <td>{$theBoard[$row][$col]}</td>\n";
  309.     } // end col for loop
  310.     $puzzle .= "</tr>\n";
  311.   } // end row for loop
  312.   $puzzle .= "</table>\n";
  313.   return $puzzle;
  314. } // end printBoard;
  315.  
  316. //////////////////
  317.  
  318.  
  319.  
  320. //$samWord = filter_input(INPUT_POST, "wordList");
  321. $value = strtoupper($value);
  322. $value = rtrim($value);
  323. /////////////////
  324.  
  325. function addFoils(){
  326.   //add random dummy characters to board
  327.   global $board, $boardData;
  328.   for ($row = 0; $row < $boardData["height"]; $row++){
  329.     for ($col = 0; $col < $boardData["width"]; $col++){
  330.       if ($board[$row][$col] == "."){
  331.           $newLetter = rand(65,90);//xxxxxxxxxxxxxxxxxxxxxxxxx
  332.         $board[$row][$col] = chr($newLetter);
  333.       } // end if
  334.     } // end col for loop
  335.   } // end row for loop
  336. } // end addFoils
  337.  
  338. function printPuzzle(){
  339.   //print out page to user with puzzle on it
  340.  
  341.   global $puzzle, $word, $keyPuzzle, $boardData;
  342.   //print puzzle itself
  343.  
  344.   print <<<HERE
  345.   <h1>{$boardData["name"]}</h1>
  346.   $puzzle
  347.   <h3>Word List</h3>
  348.   <ul>
  349.  
  350. HERE;
  351.   //print word list
  352.   foreach ($word as $theWord){
  353.     $theWord = rtrim($theWord);
  354.     print "<li>$theWord</li>\n";
  355.   } // end foreach
  356.   print "</ul>\n";
  357.   $puzzleName = $boardData["name"];
  358.  
  359.   //print form for requesting answer key.
  360.   //save answer key as session var
  361.  
  362.   $_SESSION["key"] = $keyPuzzle;
  363.   $_SESSION["puzzleName"] = $puzzleName;
  364.  
  365.  
  366.   print <<<HERE
  367.  
  368.   <form action = "wordP_Key.php"
  369.         method = "post"
  370.             id = "keyForm">
  371.   <div>
  372.   <input type = "submit"
  373.          value = "show answer key" />
  374.   </div>
  375.   </form>
  376.  
  377. HERE;
  378.  
  379. } // end printPuzzle
  380.  
  381.  
  382. ?>
  383.  
  384.  
  385.  

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 42 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.