Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:50:11+00:00 2026-06-11T17:50:11+00:00

I wrote a program that creates a puzzle based on user’s inputs. There is

  • 0

I wrote a program that creates a puzzle based on user’s inputs. There is a html form that accepts the user’s words and posts them to the php program. Then the php program creates the puzzle and prints it.

There is a live demo here. You can type you own words.

It looks good but when I run it in my local server with php error prompt turned on, I see the error msg saying Undefined offset: 10 at line 147 and 148. The error is generated from the php code line starting from if ($board[$curr_row][$curr_col] == '.'… You can use Ctrl+F to find the code. I don’t understand how could I get 10 in $curr_col or $curr_row since the loop should stop after they reach 9.

Please help me understand how could the loop run after they have reached 10, thanks a lot!

The zipped version of the program is here.

Here is the code of the php:

<html>
    <body>
        <?php

        /* word Find
         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
         */
        //If there is no data from the form, prompt the user to go back
        if (!filter_has_var(INPUT_POST, "puzzle_name")) {
            print <<<MUL_LINE
    <!DOCTYPE html>
<html >
    <head>
        <title>Oops!</title>
    </head>

    <body>
        <p>This page should not be called directly, please visit 
        the <a href="./form.html">puzzle form</a> to continue.</p>
    </body>
</html>

MUL_LINE;
        } else {
            $boardData = array("name" => filter_input(INPUT_POST, "puzzle_name"), "width" => filter_input(INPUT_POST, "grid_width"), "height" => filter_input(INPUT_POST, "grid_height"));

            if (parseList() == TRUE) {//parse the word list in textarea to an array of words
                //keep trying to fill the board untill a valid puzzle is made
                do {
                    clearBoard();
                    //reset the board
                    $pass = fillBoard();
                } while($pass == FALSE);
                printBoard();
                //if the board if successfully filled, print the puzzle
            }
        }//end word list exists if

        //parse the word list in textarea to an array of words
        function parseList() {
            //get 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 = explode("\n", $wordList);
            //an array of words

            foreach ($word as $key => $currentWord) {
                //trim all the beginning and trailer spaces
                $currentWord = rtrim(ltrim($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
                $word[$key] = $currentWord;
            }//end foreach
            return $itWorked;
        }//end parseList

        //reset the board by filling each cell with "."
        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

        //fill the board
        function fillBoard() {
            global $word;
            $pass = FALSE;
            //control the loop of filling words, false will stop the loop
            //control the loop, if all the words are filled, the counter will be as equal to the number
            //of elements in array $words, thus the loop stops successfully
            $counter = 0;

            do {

                $pass = fillWord($word[$counter]);
                //if a word is filled, $pass is set to true
                $counter++;

            }
            //if a word can't be filled, pass==FALSE and loop stops
            //or if all words are through, loop stops
            while($pass==TRUE && $counter<count($word));

            //return TRUE if all filled successfully, FALSE if not
            if ($pass == TRUE && $counter == count($word)) {
                return TRUE;
            } else {
                return FALSE;
            }
        }

        //function used to fill  a single word
        function fillWord($single_word) {
            global $board, $boardData;
            //the direction of how the word will be filled, 50% chance to be H, and 50% chance to be V
            $dir = (rand(0, 1) == 0 ? "H" : "V");
            //H(horizontal) means fill the word from left to right
            //V(vertical) means fill the word from up to down
            //loop control. if a letter is not filled, $pass is set to false and loop stops
            $pass = TRUE;
            //loop control. if all letters are filled successfully, loop stops too.
            $counter = 0;

            //decide the cell to fill the first word. the cell is located at $board[$curr_row][$curr_col]
            if ($dir == "H") {//if the word will be fileld from left to right
                $curr_row = rand(0, $boardData["height"] - 1);
                //pick up a random row of the 10 rows ( rand(0,9) )
                $curr_col = rand(0, ($boardData["width"] - ( strlen($single_word - 1)) - 1));
                //pick up a random column of fillable columns
                //if the word is "banana" and the board's width
                //is 10, the starting column can only be rand(0, 4)
            } else if ($dir == "V") {//if the word will be fileld from up to down
                $curr_row = rand(0, ($boardData["height"] - ( strlen($single_word - 1)) - 1));
                $curr_col = rand(0, $boardData["width"] - 1);
            } else {
                print "invalid direction";
            }

            //the loop that keeps trying to fill letters of the word
            while ($pass && ($counter < strlen($single_word))) {//while the $pass is true AND there are still letters

                //to fill, keep the loop going

                //the next line and the line after generate the msg "Undefined offset: 10",
                //$curr_row and $curr_col should never be 10 because the loop should be stopped
                //if the last letter of the word is filled
                if ($board[$curr_row][$curr_col] == '.' || //if the cell is not filled, reset fillboard() to "."
                $board[$curr_row][$curr_col] == substr($single_word, $counter, 1))//or it has already been filled with the same letter
                {
                    $board[$curr_row][$curr_col] = substr($single_word, $counter, 1);
                    // write/fill the letter in the cell
                    $counter++;
                    if ($dir == "H") {
                        $curr_col++;
                        //next column, move to the next right cell
                    } else if ($dir == "V") {
                        $curr_row++;
                        //next row, move to the next lower cell
                    } else {
                        print "\nHuge direction error!";
                    }

                } else {
                    $pass = FALSE;
                    // failed to fill a letter, stop the loop
                }
            }
            //if all the letters are filled successfully, the single word is filled successfully
            //return true, let $fillBoard go filling next single word
            if ($pass && ($counter == strlen($single_word))) {
                /* for debug purpose
                 print "<hr />";print "<p>TRUE</p>";print "<hr />";
                 print $single_word;
                 print $curr_row . "," . $curr_col . "<br />";
                 print "<hr />";*/

                return TRUE;
            } else {
                //failed to fill the word, reset the board and start all over again
                return FALSE;
            }

        }//end function fillWord

        //print the successful filled puzzle
        function printBoard() {
            global $board;
            print <<<MULLINE
    <style type="text/css">
    table, td{
        border: 1px solid black;
    }

    </style>
MULLINE;
            print '<table >';
            foreach ($board as $row) {
                print '<tr>';
                foreach ($row as $cell) {
                    print '<td>';
                    print $cell;
                    print '</td>';

                }
                print("<br />");
                print '</tr>';
            }
            print "</table>";
        }
        ?>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:50:12+00:00Added an answer on June 11, 2026 at 5:50 pm

    I don’t think the following fragment of code is right.

    strlen($single_word - 1)
    

    located in the lines:

    $curr_col = rand(0, ($boardData["width"] - ( strlen($single_word - 1)) - 1));
    

    and

    $curr_row = rand(0, ($boardData["height"] - ( strlen($single_word - 1)) - 1));
    

    It will convert the word to an integer. Subtract one from that number. Then convert that back to a string and take the length. So you have a rubbish value for the length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small program, that creates files at an interval of 1 minute.
I wrote a program that accepts and outputs Hebrew (i.e. right-to-left) text. In lieu
I've wrote an adequate little program in C that creates a window. However, I'm
I wrote a program that creates a TCP and UDP socket in C and
I wrote a program that forks some processes with fork(). I want to kill
I wrote a program that worked as a server. Knowing that accept was blocking
I've wrote a program that gets a string, and then calculate a total value
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
A while ago, I wrote a program that outputs any localhost or network database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.