views:

105

answers:

1

[Disclaimer: I am new to PHP, and I am just learning, so please no flamers, it really hinders the learning process when one is trying to find solutions or information, thank you, and the code works fine in terms of the crossword puzzle, it's just really baffling me how one gets a diagonal orientation with the given information, or what I am doing wrong and not seeing?]

Given a switch:

  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;

AND:

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;

I should be able to combine the two to get NE (or diagonal text being outputted onto the screen)

HOWEVER, when I try this, it's not working, and I have been trying different combinations of N and E to get NE or a diagonal NE orientation with no luck, what gives?

#Tried multiple different combination's of N & E, I am out of ideas 

switch ($dir){
case "E":

  $newCol = rand(0, $boardData["width"] - 1 - strlen($theWord)); 
  $newRow = rand(strlen($theWord), $boardData["height"]-1);


  for ($i = 0; $i < strlen($theWord); $i++){
  #Combined but no luck, WTF:

    $boardLetter = $board[$newRow][$newRow - $i];
     $boardLetter  = $board[$newCol][$newCol + $i];
 $wordLetter = substr($theWord, $i, 1);

    //check for legal values in current space on board
    if (($boardLetter == $wordLetter) ||
        ($boardLetter == ".")){

      $board[$newRow][$newRow - $i] = $wordLetter;
   $board[$newCol][$newCol + $i] = $wordLetter;
    } else {
      $itWorked = FALSE;
    } // end if
  } // end for loop
  break;
+2  A: 

The break; statement will break out of the switch clause after running the code for case "E". You'll need to set up new, explicit cases for the combinations to make it work that way.

Kaivosukeltaja