Wordsearch c# – Word class

I am just doing a test/comparsion between the different ways of getting a similar code syntax to work in different languages for a similar project idea. The main project idea is wordsearch grid with words inserted onto the grid, I have already done a basic php version and here is the word class in php word. Below there is a full class version of whole class, but also some parts that I have expanded to try and explain in more detail.

This code will return a String array from a ArrayList type. The ToArray changes the ArrayList to type of string then the “as string[]” returns the list of string as a string array.

			return (arrayL.ToArray(typeof(string)) as string[]);

Here is the word class in c#, I have added allot of code comments to give details of what is happening.

using System;
using System.Xml;
using System.Collections;
 
namespace wordsearchcsharp
{
	class Word 
	{
		private String[] _words;
 
	 	public  Word() : this("words.xml", 5) {}
 
		public Word(string wordsOrFilename, int maxSearchNum)
		{
			this._words = new String[maxSearchNum];
			// just load from a file the words to search, instead of input as well.
			String[] loadedWords = loadWords(wordsOrFilename);
 
			// create the searchable words from the loadedwords array.
			this._words = this.searchableWords(loadedWords, maxSearchNum);
		}
 
		private String[] loadWords(string filename)
		{
			XmlTextReader fileReader = new XmlTextReader(filename);
 
			ArrayList fileArray = new ArrayList();
 
			while (fileReader.Read())
			{
				// get the NodeType from the XML reader, different types
				// Element = name of element
				// Text = the actual text/word
				// End Element = end of the element name
			    switch (fileReader.NodeType) 
			    {
					// if there is a word in the file e.g. not a end/element
					  case XmlNodeType.Text: 
							fileArray.Add(fileReader.Value);
				            break;
			    }
			}
			// change the ArrayList into a String[] array using the ToArray method
			return (fileArray.ToArray(typeof(string)) as string[]);
		}
 
		// create a list of words from the words passed in and also 
		// the maximum number is the maxSearchNum.
		private String[] searchableWords(String[] words, int maxSearchNum)
		{
			ArrayList returnWords = new ArrayList();
 
			// if the maxsearch value is greater or equal to the number of words to search for 
			// just return the words string[].
			if (words.Length <= maxSearchNum)
			{
				return words;
			}
			else
			{
				// create a good seed for the random generator.
				Random randGen = new Random((int)DateTime.Now.Ticks);
				int randomNum;
				// randomly pick out words from the array
				for (int i = 0; i < maxSearchNum; i++)
				{
					// pick a random number
					randomNum = randGen.Next(0, words.Length);
					// add to the array list to return
					returnWords.Add(words[randomNum]);
					// rebuild the array with removing the random number generated.
					words = rebuildArray(words, randomNum);
				}
			}
			// convert back to the String[] 
			return (returnWords.ToArray(typeof(string)) as string[]);
		}
 
		private String[] rebuildArray(String[] rebuildSt, int numberToTakeOut)
		{
			ArrayList arrayL = new ArrayList();
			// out of range error.
			if (rebuildSt.Length < numberToTakeOut)
			{
				return rebuildSt;
			}
			else
			{
				for (int i =0; i < rebuildSt.Length; i++)
				{
					// only add in the words that are not the
					// numberToTakeOut word from the array
					if (i != numberToTakeOut)
					{
						arrayL.Add(rebuildSt[i]);
					}
				}
			}
			return (arrayL.ToArray(typeof(string)) as string[]);
		}
 
		public void printOutWords()
		{
			Console.Write("Words : (");
			for (int i = 0; i < this._words.Length; i++)
			{
				Console.Write("  " + i + " = " + this._words[i]);
			}
			Console.Write(")\n");
		}
 
		public String[] returnWords()
		{
			return this._words;
		}
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			Word word = new Word("words.xml",3);
                        word.printOutWords();
		}
	}
}

and a output would be

Words : (  0 = he  1 = old  2 = sole)

Wordsearch – c# Grid class

When coding in c#/csharp, I use monodevelop. Here is the wordsearch base Grid class in c#, I am coding in different languages just for fun as such, but here is the main idea for the wordsearch. I have already done a basic version in php which is at the present state in creating a grid (but without any intersection of words with others). But here is the Grid class in php, and below is the similar setup in csharp.

The main class, is where the application executes from, it is very similar to c++ in that there is a main function

public static void Main(string[] args)

which takes the string[] args as a parameter, which is what is passed in on the command line.

Anyway, here is the code.. I have added in some comments to what the code is doing.

using System;
 
namespace wordsearchcsharp
{
	class Grid {
		protected int _size = 0;
		protected char[][] _grid;
 
		// the ": this(11)" means call the Grid(int size) constructor with 11 as a parameter
		public Grid() : this(11)
		{
			// default size of a grid = 11
		}
 
		public Grid(int size)
		{
			this._size = size;
			// create a new row (Y direction)
			this._grid = new char[this._size][];
			for (int i =0; i < this._size; i++)
			{
				// create new X direction row.
				this._grid[i] = createGridRow();
			}
		}
 
		// createGridRow, this can be inherited and thus altered to create any type of character in the grid 
		public virtual char[] createGridRow()
		{
			// need to insert a good seed into the random generator.. else same characters
			Random random = new Random((int)DateTime.Now.Ticks);
 
			// create a new row (X direction) and place characters into it.
			char[] grid_insert = new char[this._size];
			for (int i = 0; i < this._size; i++)
			{
				// 0 - 25 = alphabet, and also +97 is the ascii start for the character 'a'
				grid_insert[i] = (char)(random.Next(0,25)+97);
			}
			return grid_insert;
		}
 
		// printOutGrid, will print out the grid created above.
		// i = Y direction, and j = X direction.
		public void printOutGrid()
		{
			Console.WriteLine("The Grid");
			for (int i = 0; i < this._size; i++)
			{
				Console.Write(i + " ..");
				for (int j = 0; j < this._size; j++)
				{
					Console.Write(j + " = " + this._grid[i][j]);
				}
				Console.WriteLine("");
			}
		}
 
		public int returnSize()
		{
			return this._size;
		}
 
		// insert a character into the grid at position X, Y and the character 
		public void insertCharIntoGrid(int pX, int pY, char character)
		{
			this._grid[pY][pX] = character;
		}
 
		//  return the grid reference 
		public char[][] returnGrid()
		{
			return this._grid;
		}
 
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			Grid grid = new Grid();
			grid.printOutGrid();
		}
	}
}

And here is the output.

The Grid
0 ..0 = m1 = y2 = p3 = d4 = d5 = y6 = g7 = p8 = m9 = i10 = t
1 ..0 = k1 = c2 = y3 = n4 = i5 = m6 = q7 = f8 = g9 = p10 = j
2 ..0 = o1 = r2 = v3 = q4 = a5 = i6 = y7 = h8 = k9 = y10 = c
3 ..0 = h1 = u2 = f3 = s4 = v5 = r6 = p7 = g8 = h9 = j10 = b
4 ..0 = n1 = j2 = y3 = u4 = v5 = q6 = n7 = b8 = y9 = w10 = g
5 ..0 = g1 = m2 = i3 = x4 = r5 = a6 = e7 = a8 = w9 = i10 = f
6 ..0 = m1 = c2 = c3 = a4 = r5 = y6 = c7 = v8 = o9 = u10 = k
7 ..0 = f1 = e2 = k3 = c4 = n5 = j6 = s7 = t8 = m9 = g10 = j
8 ..0 = l1 = t2 = f3 = e4 = n5 = i6 = q7 = p8 = e9 = t10 = p
9 ..0 = e1 = w2 = n3 = h4 = j5 = r6 = h7 = o8 = b9 = e10 = o
10 ..0 = y1 = x2 = u3 = j4 = n5 = g6 = n7 = h8 = m9 = t10 = a

I use linux and thus mono is my csharp virtual machine setup, but here is a output of the file command in linux which tells me that the wordsearchcsharp.exe if a mono assembly and thus if you ./wordsearchcsharp.exe will goto the mono virtual machine.. it will link/expand to mono wordsearchcsharp.exe on the command line.

file wordsearchcsharp.exe
wordsearchcsharp.exe: PE32 executable for MS Windows (console) Intel 80386 32-bit Mono/.Net assembly

Wordsearch php – The words inserted into the grid

The WordToSearchGrid class, it extends the grid class to add in the words to search for class.

The constructor from base class Grid creates the basic grid, I have added in a testing createGridRow() in this class to build just a empty grid (this is a great thing with class inheritance / extends etc, so that you can just re-write one aspect of the base class if you want it done a different way and it still works), and the output below is of the testing createGridRow() function so it makes it easier to see where the words was placed.

The direction of the words are like a compass directions, north, south, east and west and the north east, south east, north west and south west. Their are 8 directions in total that a word can take.

The getWords function gets the words to search for.

Here is a break down of the createSearchGrid function, which is the make aspect of placement of words.

    public function createSearchGrid()
    {
	// get the size of the grid
	$size = parent::returnSize();
 
	// loop thought the words to insert
	for ($i = 0; $i < count($this->_words); $i++)
	{

Loop thought the amount of words that are going to be inserted (tried to be)

	  $wordLen = strlen($this->_words[$i]);
	  // if the word is larger than the size of the grid, it cannot be inserted.
 
	  // NOTE : at present there is no intersection (crossing search words) shall do in the next version
 
	  if ($wordLen < $size) 
	  {

As NOTED in the code, there is no intersection at present, shall do this at a later stage, just the basics of insertion at present.

Also if the word to be inserted is longer in length than the actual grid then there is no point trying to insert it, because it will not fit.

	      //need to pick a direction and also a point on the grid.
	      // also need to try and place the word onto the grid x amount of times else break out.
	      $direction = rand(1,8);
	      $pointX = rand(1, $size);
	      $pointY = rand(1, $size);
 
	      // the remainder of the subtracting the points from the size of grid will basically say how much space is required
	      $space = $size - $wordLen;

Get a randomize direction, there are 8 directions and also randomized points to insert into the grid from 1 to the maximum size of the grid

	      // check against the direction and the size of the word against where it is on the poistion in the grid
	      // 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	      // from the points point of view.
 
	      $spaceY = $size - $pointY;
	      $spaceX = $size - $pointX;

Here is when the fun starts, the space X/Y left is basically how much space is left from the size of the grid to where the point X/Y are placed on the grid.

	      // if the direction is not east or west, and there is not enought space, move the insertion pointY difference
	      // north and south
	      if (!(($direction == 7) || ($direction == 3)))
	      {
		  if ($spaceY > $space) 
		      $pointY+= ($spaceY - $space);
	      }

If the direction is not going west or east then we are going north or south. The next question is, how much space is there to insert the word (we are always working in a north/west direction, but can move the word points around for south and east) and is the space left to insert the word greater than the space left required to insert the word.

E.g.. the size of the grid is 11 and the pointY = 2 and the word to insert is “merry” which has a word length of 5.
The space required ($space) = 11 – 5 which equals 6
The space left ($spaceY) = 11 – 2 which equals 9
and since we are always heading in a north/west direction as a default (if we are heading in any direction, e.g. sometimes it would be north or west or both).
then if the space left (9) is greater than the space required (6) which it is then increment the pointY (space left) the difference between the space left ($spaceY) and the space required ($space) which equals 3.

So from the equation this would be

if ( $spaceY (9) > $space (6) )
$pointY (2) increment by 3 which makes $pointY = 5 which is the length of the word (could just make it equal the length of the word, but I prefer the equation 🙂 )

The below is the same as the north/south test, apart from east and west.

	      // if the direction is not north or south and there is not enought space, move the insertion pointX difference
	      // east and west.
	      if (!(($direction == 1) || ($direction == 5)))
	      {
		  if ($spaceX > $space)
		      $pointX+= ($spaceX - $space);
	      }
 
	      $this->insertIntoGrid($pointX, $pointY, $this->_words[$i], $direction);
	  }
	  else
	  {
	      // no need to tell the user to search for something that was not inserted
	      echo "Word ". $this->_words[$i] . " was too long for the wordsearch";
	      unset($this->_words[$i]);
	  }
	}
    }

The insertIntoGrid function, inserts the word that has a pointX and pointY coordinates and the direction.

    // insert into the grid at the positions x, y, the word to insert with the direction
    public function insertIntoGrid($pX, $pY, $word, $direction)
    {
	$wordLen = strlen($word);

Since we are based in north/west direction if the direction is south/east then correct the starting point to reflect the word direction.

	// move the starting point of the word to the correct place within the grid
	// the default is north and west, so will need to correct for south and east directions.
	if ($direction >=4 && $direction <=6) $pY-= ($wordLen-1);
	if ($direction >=2 && $direction <=4) $pX-= ($wordLen-1);
 
	// 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	// process each letter of the word and move the position to insert in the correct direction
	for ($i = 0; $i < $wordLen; $i++)
	{
	    parent::insertCharIntoGrid($pX, $pY, $word[$i]);
	    // move to the next grid position to insert the character
	    switch($direction)
	    {
	      case 1 : $pY--; break;
	      case 2 : $pY--; $pX++; break;
	      case 3 : $pX++; break;
	      case 4 : $pY++; $pX++; break;
	      case 5 : $pY++; break;
	      case 6 : $pY++; $pX--; break;
	      case 7 : $pX--; break;
	      case 8 : $pY--; $pX--; break;
	      default : break;
	    }
	}
    }

Here is the full class structure, and also a small demo. You will need to have the other classes in the same php file, the classes grid and the words.

class WordToSearchGrid extends Grid {
 
    private $_words;
 
    /* create a blank grid  - testing -- just need to comment out this function to create the real grid*/
    public function createGridRow()
    {
	$retArray = array();
	for ($i = 1; $i <= parent::returnSize(); $i++)
	{
	    $retArray[$i] = " ";
	}
	return array();
    }
 
    /* Gets the words to search for */
    public function getWords($wordsArray)
    {
	if (isset($wordsArray) && is_array($wordsArray))
	{
	  $this->_words = $wordsArray;
	}
    }
 
    // createSearchGrid, will insert the words onto a blank grid (for testing).
    // in random places and directions.
    public function createSearchGrid()
    {
	// get the size of the grid
	$size = parent::returnSize();
 
	// loop thought the words to insert
	for ($i = 0; $i < count($this->_words); $i++)
	{
	  $wordLen = strlen($this->_words[$i]);
	  // if the word is larger than the size of the grid, it cannot be inserted.
 
	  // NOTE : at present there is no intersection (crossing search words) shall do in the next version
 
	  if ($wordLen < $size) 
	  {
	      //need to pick a direction and also a point on the grid.
	      // also need to try and place the word onto the grid x amount of times else break out.
	      $direction = rand(1,8);
	      $pointX = rand(1, $size);
	      $pointY = rand(1, $size);
 
	      // the remainder of the subtracting the points from the size of grid will basically say how much space is required
	      $space = $size - $wordLen;
 
	      // check against the direction and the size of the word against where it is on the poistion in the grid
	      // 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	      // from the points point of view.
 
	      $spaceY = $size - $pointY;
	      $spaceX = $size - $pointX;
 
	      // if the direction is not east or west, and there is not enought space, move the insertion pointY difference
	      // north and south
	      if (!(($direction == 7) || ($direction == 3)))
	      {
		  if ($spaceY > $space) 
		      $pointY+= ($spaceY - $space);
	      }
 
	      // if the direction is not north or south and there is not enought space, move the insertion pointX difference
	      // east and west.
	      if (!(($direction == 1) || ($direction == 5)))
	      {
		  if ($spaceX > $space)
		      $pointX+= ($spaceX - $space);
	      }
 
	      $this->insertIntoGrid($pointX, $pointY, $this->_words[$i], $direction);
	  }
	  else
	  {
	      // no need to tell the user to search for something that was not inserted
	      echo "Word ". $this->_words[$i] . " was too long for the wordsearch";
	      unset($this->_words[$i]);
	  }
	}
    }
 
    // print out the words to search for that have been inserted into the grid
    public function printTheWordsToSearch()
    {
	foreach ($this->_words as $key => $value)
	{
	    echo "<br/>$key = $value";
	}
    }
 
    // insert into the grid at the positions x, y, the word to insert with the direction
    public function insertIntoGrid($pX, $pY, $word, $direction)
    {
	$wordLen = strlen($word);
 
	// move the starting point of the word to the correct place within the grid
	// the default is north and west, so will need to correct for south and east directions.
	if ($direction >=4 && $direction <=6) $pY-= ($wordLen-1);
	if ($direction >=2 && $direction <=4) $pX-= ($wordLen-1);
 
	// 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west
	// process each letter of the word and move the position to insert in the correct direction
	for ($i = 0; $i < $wordLen; $i++)
	{
	    parent::insertCharIntoGrid($pX, $pY, $word[$i]);
	    // move to the next grid position to insert the character
	    switch($direction)
	    {
	      case 1 : $pY--; break;
	      case 2 : $pY--; $pX++; break;
	      case 3 : $pX++; break;
	      case 4 : $pY++; $pX++; break;
	      case 5 : $pY++; break;
	      case 6 : $pY++; $pX--; break;
	      case 7 : $pX--; break;
	      case 8 : $pY--; $pX--; break;
	      default : break;
	    }
	}
    }
}
 
 
$GridWords = new WordToSearchGrid(11);
 
$WordsToSearchFor = new Word("words.xml",3);
 
$GridWords->getWords($WordsToSearchFor->wordsArray());
 
$GridWords->createSearchGrid();
 
$GridWords->printOut();
 
$GridWords->printTheWordsToSearch();

Here is the output from the above, you may get something different since it is based on randomized numbers.

The grid
1 .. 1 = 2 = 3 = 4 = 5 = s 6 = 7 = 8 = 9 = 10 = 11 =
2 .. 1 = 2 = 3 = 4 = 5 = a 6 = 7 = 8 = 9 = 10 = 11 =
3 .. 1 = 2 = 3 = y 4 = 5 = w 6 = 7 = 8 = 9 = o 10 = 11 =
4 .. 1 = 2 = 3 = r 4 = 5 = 6 = 7 = 8 = 9 = 10 = l 11 =
5 .. 1 = 2 = 3 = r 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = d
6 .. 1 = 2 = 3 = e 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
7 .. 1 = 2 = 3 = m 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
8 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
9 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
10 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
11 .. 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 =
 
0 = merry
1 = was
2 = old

Wordsearch php – Words to search for

As from the main project, wordsearch, here is the part that will either load the words from a xml file or from a input string on a web page. The grid class can be found here.

The basics of the word class, is to load a xml file (or via a user input string/s) and then generate words to search from the words that have either been loaded or inputted.

The word class can again be inherited so that each part can be re-done differently if there is another way to open a file etc.

I have included comments in the code, the __construct and __destruct are what happens when the class is created (via the “new” command) and destroyed respectively. Just a nice way to create the class/object and also clean up after itself.

The parameters that are passed to the __construct have default values, words.xml and 5, these can be over written when creating the class as shown in the code (instead of the maxSearchNum being 5 it is 3. But this gives more control over the construction of the class.

<?php
 
class Word {
    /* private section of the class word */
 
    private $_words;
 
    /* constructor for the word class 
       default action to load a xml file to gain 5 words
    */
 
    public function __construct($wordsOrFilename = "words.xml", $maxSearchNum = 5)
    {
	$this->_words = array();
 
	// if the filename is actually words from the input screen then just add in them instead
	if (is_array($wordsOrFilename)) 
	{
	  $_loadedWords = $wordsOrFilename;
	}
	else
	{
	  // load the file of words into a array
	  $_loadedWords = $this->loadWords($wordsOrFilename);
	}
	// create a array of words to be searched for, max number as above
	$this->_words = $this->searchableWords($_loadedWords, $maxSearchNum);
    }
 
    // unset the class words variable.
    public function __destruct()
    {
	unset($this->_words);
    }
 
    // load in the words file 
    public function loadWords($filename)
    {
	$xmlFileLoad = simplexml_load_file($filename);
	$returnWords = array();
	$i=0;
	foreach ($xmlFileLoad->word as $theword)
	{
	  // only the string, because otherwise it would be a simpleXMLObject
	  $returnWords[$i++] = (string)$theword;
	}
	return $returnWords;
    }
 
    // searchableWords will create a array of words to search for from the searchablewords parameter, with maximum number of searchs (maxsearch)
    public function searchableWords($searchableWords, $maxSearch)
    {
	$returnSearchWords = array();
	$numberSearchWords = count($searchableWords);
	// if the maxsearch value is greater or equal to the number of searchable words just fill in the return will all of the words
	if ($numberSearchWords <= $maxSearch)
	{
	    for ($i = 0; $i < $numberSearchWords; $i++)
	    {
	      $returnSearchWords[$i] = $searchableWords[$i];
	    }
	}
	else
	{
	  // randomly pick out words from the array of searchable words
	  for ($i = 0; $i < $maxSearch; $i++)
	  {
	    // re-index the array
	    $searchableWords = array_values($searchableWords);
	    // obtain a random index value
	    $searchWordNum = rand(0, count($searchableWords)-1);
	    // insert into the return value
	    $returnSearchWords[$i] = $searchableWords[$searchWordNum];
	    // delete the word from the array
	    unset($searchableWords[$searchWordNum]);
	  }
	}
	return $returnSearchWords;
    }
 
    public function wordsArray()
    {
	return $this->_words;
    }
 
    public function printOutWords()
    {
	prit_r($this->_words);
    }
};
 
 
$wordsToSearch = new Word("words.xml",3);
$wordsToSearch->printOutWords();
?>

and the output would be something like, of course since it is random generated, then could be the same words or different ones!!.

Array ( [0] => was [1] => sole [2] => old )

Wordsearch php – grid class

From the post on the projects page, wordsearch, here is a basic design of a grid class that can be used to link together with the other aspects of a wordsearch game. This will be able to create a basic grid and populate it with characters.

The reason why I created the createGridRow function was because this class could be inherited and thus alter any aspects of the class to suit the future needs.

The

$this->

within the code references the local variables of the class and not any other variables e.g. local to that function.

The

chr(rand(0,25) + 97);

means create a chr (character) from a random generated value within a range of 0 to 25 (26 letters in the English alphabet) and then add the value 97 to it, because the integer value of 97 – 125 are the ascii codes for a-z characters.

<?php
class Grid {
 
  /* private section of the class grid */
 
  // store the size and the grid
    private $_size = 0;
    private $_grid;
 
 
  /* the public section of the class grid */
 
    // construct the grid with a given size
    public function __construct($size=0) 
    { 
      // set the private variables
      $this->_size = $size; 
      $this->_grid = array();
      for ($i =1; $i <= $this->_size; $i++)
      {
	// create each row of the grid with the class function createGridRow
	$this->_grid[$i] = $this->createGridRow();
      }
    }
 
    // delete the grid 
    public function __destruct()
    {
      // clear up the grid, each line and then the whole object.
      for ($i = 1; $i <= $this->_size; $i++)
      {
	unset($this->_grid[$i]);
      }
      unset($this->_grid);
    }
 
    /* createGridRow, this can be inherited and thus altered to create any type of character in the grid */
    public function createGridRow()
    {
	$_grid_insert = array();
	for ($j = 1; $j <= $this->_size; $j++)
	{
	  $_grid_insert[$j] = chr(rand(0,25) + 97);
	}
	return $_grid_insert;
    }
 
    /* basic print out of a grid */
    public function printOut()
    {
      echo "The grid<br/>";
      for ($i = 1; $i <= $this->_size; $i++)
      {
	echo "$i .. ";
	for ($j = 1; $j <= $this->_size; $j++)
	{
	    echo " $j = ".$this->_grid[$i][$j];
	}
	echo "<br/>";
      }
    }
 
    /* return the size of the grid */
    public function returnSize()
    {
      return $this->_size;
    }
 
    /* insert a character into the grid at position X, Y and the character */
    public function insertCharIntoGrid($pX, $pY, $char)
    {
	$this->_grid[$pY][$pX] = $char;
    }
 
    /* return the grid reference */
    public function returnGrid()
    {
      return $this->_grid;
    }
}
$wordsearch_grid = new Grid(6);
$wordsearch_grid->printOut();
?>

The output of the code as above is

The grid
1 .. 1 = l 2 = l 3 = z 4 = m 5 = h 6 = m
2 .. 1 = u 2 = r 3 = z 4 = s 5 = k 6 = y
3 .. 1 = f 2 = k 3 = j 4 = n 5 = k 6 = p
4 .. 1 = h 2 = p 3 = x 4 = o 5 = c 6 = q
5 .. 1 = s 2 = s 3 = x 4 = r 5 = u 6 = v
6 .. 1 = r 2 = f 3 = g 4 = q 5 = s 6 = o