# Puzzle Formula Problem

Hello! I am trying to add the diagonals of a puzzle. I got SE, and NW directions working so far. I am currently working on SW direction. It displays the right direction of the word however, it showed the following errors Notice: Undefined offset: -1 in C:\xampp\htdocs… Notice: Undefined offset: -2 in C:\xampp\htdocs… Notice: Undefined offset: -3 in C:\xampp\htdocs… Can anybody please help me figure out what I did wrong? Here is my code for direction “SW”:

case “SW”:

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

for (\$i = 0; \$i &lt; 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;
``````

Did you mean to do “\$newCol - \$i”? Because it looks like \$i is getting bigger than \$newCol, which is resulting in a negative index for the array, which is causing the error.

You are not testing for if the word goes off the edge of the board for this direction. Are you performing that test for the other directions or did you just get lucky there and not come across a situation where the word went off the board?

This is the code for my SE direction. I did not do any if conditions. I test it many times and it didn’t show any error. I guess it is luck.
case “SE”:

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

for (\$i = 0; \$i &lt; 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;
``````