Wordseach c# – WordToSearchGrid

The WordToSearchGrid is basically the last part of the beginning part of the wordsearch project. It brings together the grid class (which it in inheriting from) and the word class.

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.

For testing, I have overloaded the createRowGrid method in the grid class. So just need to comment out the method in the class below.

I have already done a version of this code in PHP and more of a detailed information about the createSearchGrid method is on this PHP WordToSearchGrid webpage. But hopefully the comments are good enough in the code below to explain why and what is happening.

To compile up the whole thing, the Word and Grid classes will need to be included in the source code.

usingSystem;usingSystem.Xml;usingSystem.Collections;namespace wordsearchcsharp
{/*
* WordToSearchGrid extends / Inheritance from the base Grid class
* so that the basics of the grid class can be used e.g. creation of a random grid etc.
*/class WordToSearchGrid : Grid
{// I like to use privates instead of protected variables so that each class// has access to there private variablesprivateString[] _words;// the constructor to call the base constructor would use public WordToSearchGrid():base(){}public WordToSearchGrid(int size):base(size){}// create a empty grid - override ('new') the base class method.publicoverridechar[] createGridRow(){char[] retArray =newchar[returnSize()];for(int i =0; i < returnSize(); i++){
retArray[i]=' ';}return retArray;}// get the words from the words class into this class.publicvoid getWords(String[] wordsArray){this._words = wordsArray;}// print out the words to search for that have been inserted into the gridpublicvoid printTheWordsToSearch(){
Console.WriteLine("Words to search for : ");foreach(string st inthis._words){
Console.WriteLine(st);}}// insertIntoGrid at point position X (pX) and Y (pY) with the word and directionpublicvoid insertIntoGrid(int pX, int pY, String word, int direction){int wordLen = word.Length;// 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 directionfor(int i =0; i < wordLen; i++){
insertCharIntoGrid(pX, pY, word[i]);switch(direction){case1: pY--;break;case2: pY--; pX++;break;case3: pX++;break;case4: pY++; pX++;break;case5: pY++;break;case6: pY++; pX--;break;case7: pX--;break;case8: pY--; pX--;break;default:break;}}}// createSearchGrid, will insert the words onto a blank grid (for testing).// in random places and directions.publicvoid createSearchGrid(){
Random rand =new Random((int)DateTime.Now.Ticks);int wordLen, direction, pointX, pointY, space, spaceY, spaceX;// loop thought the words to insertfor(int i =0; i < _words.Length; i++){
wordLen = _words[i].Length;// 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 versionif(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.Next(1, _size);
pointX = rand.Next(1, _size);
pointY = rand.Next(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 southif(!((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, _words[i], direction);}else{
Console.WriteLine("Word : "+ _words[i]+" is too long for the grid of size "+ _size);}}}}class MainClass
{publicstaticvoid Main(string[] args){
Word word =new Word("words.xml",3);
WordToSearchGrid gridWithWords =new WordToSearchGrid();// place the words into the WordToSearchGrid
gridWithWords.getWords(word.returnWords());// create a search grid with the words passed above.
gridWithWords.createSearchGrid();// print out the grid
gridWithWords.printOutGrid();// print out the words to search
gridWithWords.printTheWordsToSearch();// of course could have used the Word class// but since this is the WordToSearchGrid, so just placed one method in there as well.
word.printOutWords();}}}

using System;
using System.Xml;
using System.Collections;
namespace wordsearchcsharp
{
/*
* WordToSearchGrid extends / Inheritance from the base Grid class
* so that the basics of the grid class can be used e.g. creation of a random grid etc.
*/
class WordToSearchGrid : Grid
{
// I like to use privates instead of protected variables so that each class
// has access to there private variables
private String[] _words;
// the constructor to call the base constructor would use
public WordToSearchGrid() : base() {}
public WordToSearchGrid(int size) : base (size) {}
// create a empty grid - override ('new') the base class method.
public override char[] createGridRow()
{
char[] retArray = new char[returnSize()];
for (int i =0; i < returnSize(); i++)
{
retArray[i] = ' ';
}
return retArray;
}
// get the words from the words class into this class.
public void getWords(String[] wordsArray)
{
this._words = wordsArray;
}
// print out the words to search for that have been inserted into the grid
public void printTheWordsToSearch()
{
Console.WriteLine("Words to search for : ");
foreach (string st in this._words)
{
Console.WriteLine(st);
}
}
// insertIntoGrid at point position X (pX) and Y (pY) with the word and direction
public void insertIntoGrid(int pX, int pY, String word, int direction)
{
int wordLen = word.Length;
// 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 (int i =0; i < wordLen; i++)
{
insertCharIntoGrid(pX, pY, word[i]);
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;
}
}
}
// createSearchGrid, will insert the words onto a blank grid (for testing).
// in random places and directions.
public void createSearchGrid()
{
Random rand = new Random((int)DateTime.Now.Ticks);
int wordLen, direction, pointX, pointY, space, spaceY, spaceX;
// loop thought the words to insert
for (int i =0; i < _words.Length; i++)
{
wordLen = _words[i].Length;
// 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.Next(1, _size);
pointX = rand.Next(1, _size);
pointY = rand.Next(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, _words[i], direction);
}
else
{
Console.WriteLine("Word : " + _words[i] + " is too long for the grid of size " + _size);
}
}
}
}
class MainClass
{
public static void Main(string[] args)
{
Word word = new Word("words.xml",3);
WordToSearchGrid gridWithWords = new WordToSearchGrid();
// place the words into the WordToSearchGrid
gridWithWords.getWords(word.returnWords());
// create a search grid with the words passed above.
gridWithWords.createSearchGrid();
// print out the grid
gridWithWords.printOutGrid();
// print out the words to search
gridWithWords.printTheWordsToSearch();
// of course could have used the Word class
// but since this is the WordToSearchGrid, so just placed one method in there as well.
word.printOutWords();
}
}
}