{"id":507,"date":"2010-01-14T22:26:33","date_gmt":"2010-01-14T22:26:33","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=507"},"modified":"2010-01-15T11:43:27","modified_gmt":"2010-01-15T11:43:27","slug":"wordseach-c-wordtosearchgrid","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/wordseach-c-wordtosearchgrid\/","title":{"rendered":"Wordseach c# &#8211; WordToSearchGrid"},"content":{"rendered":"<p>The WordToSearchGrid is basically the last part of the beginning part of the wordsearch project.  It brings together the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/12\/wordsearch-c-word-class\/\">grid<\/a> class (which it in inheriting from) and the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/13\/wordsearch-c-word-class-2\/\">word<\/a> class.<\/p>\n<p>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.<\/p>\n<p>The getWords function gets the words to search for. <\/p>\n<p>For testing, I have overloaded the createRowGrid method in the grid class. So just need to comment out the method in the class below.<\/p>\n<p>I have already done a version of this code in PHP and more of a detailed information about the createSearchGrid method is on this <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/11\/wordsearch-php-the-words-inserted-into-the-grid\/\">PHP WordToSearchGrid<\/a> webpage.  But hopefully the comments are good enough in the code below to explain why and what is happening.<\/p>\n<p>To compile up the whole thing, the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/13\/wordsearch-c-word-class-2\/\">Word<\/a> and <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/12\/wordsearch-c-word-class\/\">Grid<\/a> classes will need to be included in the source code.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Xml;\r\nusing System.Collections;\r\n\r\nnamespace wordsearchcsharp\r\n{\r\n\t\/*\r\n\t * WordToSearchGrid extends \/ Inheritance from the base Grid class\r\n\t * so that the basics of the grid class can be used e.g. creation of a random grid etc.\r\n\t *\/\r\n\t\r\n\tclass WordToSearchGrid : Grid\r\n\t{\r\n\t\t\/\/ I like to use privates instead of protected variables so that each class\r\n\t\t\/\/ has access to there private variables\r\n\t\tprivate String[] _words;\r\n\t\t\r\n\t\t\/\/ the constructor to call the base constructor would use \r\n\t\tpublic WordToSearchGrid() : base() {}\r\n\t\tpublic WordToSearchGrid(int size) : base (size) {}\r\n\t\t\r\n                \/\/ create a empty grid - override ('new') the base class method.\r\n\t\tpublic override char[] createGridRow()\r\n\t\t{\r\n\t\t\tchar[] retArray = new char[returnSize()];\r\n\t\t\tfor (int i =0; i < returnSize(); i++)\r\n\t\t\t{\r\n\t\t\t\tretArray[i] = ' ';\r\n\t\t\t}\r\n\t\t\treturn retArray;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ get the words from the words class into this class.\r\n\t\tpublic void getWords(String[] wordsArray)\r\n\t\t{\r\n\t\t\tthis._words = wordsArray;\r\n\t\t}\r\n\t\t\r\n\t    \/\/ print out the words to search for that have been inserted into the grid\r\n\t\tpublic void printTheWordsToSearch()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Words to search for : \");\r\n\t\t\tforeach (string st in this._words)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(st);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ insertIntoGrid at point position X (pX) and Y (pY) with the word and direction\r\n\t\tpublic void insertIntoGrid(int pX, int pY, String word, int direction)\r\n\t\t{\r\n\t\t\tint wordLen = word.Length;\r\n\t\t\t\r\n\t\t\t\/\/ move the starting point of the word to the correct place within the grid\r\n\t\t\t\/\/ the default is north and west, so will need to correct for south and east directions.\r\n\t\t\tif (direction >= 4 && direction <=6) pY-=(wordLen-1);\r\n\t\t\tif (direction >= 2 && direction <=4) pX-=(wordLen-1);\r\n\t\t\t\r\n\t\t\t\/\/ 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west\r\n\t\t\t\/\/ process each letter of the word and move the position to insert in the correct direction\r\n\t\t\tfor (int i =0; i < wordLen; i++)\r\n\t\t\t{\r\n\t\t\t\tinsertCharIntoGrid(pX, pY, word[i]);\r\n\t\t\t\tswitch (direction)\r\n\t\t\t\t{\r\n\t\t\t      case 1 : pY--; break;\r\n\t\t\t      case 2 : pY--; pX++; break;\r\n\t\t\t      case 3 : pX++; break;\r\n\t\t\t      case 4 : pY++; pX++; break;\r\n\t\t\t      case 5 : pY++; break;\r\n\t\t\t      case 6 : pY++; pX--; break;\r\n\t\t\t      case 7 : pX--; break;\r\n\t\t\t      case 8 : pY--; pX--; break;\r\n\t\t\t      default : break;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\t    \/\/ createSearchGrid, will insert the words onto a blank grid (for testing).\r\n\t    \/\/ in random places and directions.\r\n\t\tpublic void createSearchGrid()\r\n\t\t{\r\n\t\t\tRandom rand = new Random((int)DateTime.Now.Ticks);\r\n\t\t\tint wordLen, direction, pointX, pointY, space, spaceY, spaceX;\r\n\t\t\t\/\/ loop thought the words to insert\r\n\t\t\tfor (int i =0; i < _words.Length; i++)\r\n\t\t\t{\r\n\t\t\t\twordLen = _words[i].Length;\r\n\t\t\t\t\/\/ if the word is larger than the size of the grid, it cannot be inserted.\r\n\t\t\t  \r\n\t\t\t    \/\/ NOTE : at present there is no intersection (crossing search words) shall do in the next version\r\n\t\t\t\tif (wordLen < _size)\r\n\t\t\t\t{\r\n\t\t\t        \/\/need to pick a direction and also a point on the grid.\r\n\t\t\t        \/\/ also need to try and place the word onto the grid x amount of times else break out.\r\n\t\t\t\t\tdirection = rand.Next(1, _size);\r\n\t\t\t\t\tpointX = rand.Next(1, _size);\r\n\t\t\t\t\tpointY = rand.Next(1,_size);\r\n\t\t\t\t\t\r\n\t\t\t\t    \/\/ the remainder of the subtracting the points from the size of grid will basically say how much space is required\r\n\t\t\t\t\tspace = _size - wordLen;\r\n\t      \r\n\t\t\t\t    \/\/ check against the direction and the size of the word against where it is on the poistion in the grid\r\n\t\t\t\t    \/\/ 1 = north , 2 = north east, 3 = east, 4 = south east, 5 = south, 6 = south west, 7 = west, 8= north west\r\n\t\t\t\t    \/\/ from the points point of view.\r\n\t\t\t\t\tspaceY = _size - pointY;\r\n\t\t\t\t\tspaceX = _size - pointX;\r\n\r\n\t\t\t\t    \/\/ if the direction is not east or west, and there is not enought space, move the insertion pointY difference\r\n\t\t\t\t\t\/\/ north and south\r\n\t\t\t\t\tif (!((direction == 7) || (direction == 3)))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (spaceY > space)\r\n\t\t\t\t\t\t\tpointY+= (spaceY - space);\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\r\n\t\t\t        \/\/ if the direction is not north or south and there is not enought space, move the insertion pointX difference\r\n\t\t\t        \/\/ east and west.\r\n\t\t\t\t\tif (!((direction == 1) || (direction == 5)))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (spaceX > space)\r\n\t\t\t\t\t\t\tpointX+= (spaceX - space);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tthis.insertIntoGrid(pointX, pointY, _words[i], direction);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tConsole.WriteLine(\"Word : \" + _words[i] + \" is too long for the grid of size \" + _size);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main(string[] args)\r\n\t\t{\r\n\t\t\tWord word = new Word(\"words.xml\",3);\r\n\t\t\tWordToSearchGrid gridWithWords = new WordToSearchGrid();\r\n\r\n\t\t\t\/\/ place the words into the WordToSearchGrid \r\n\t\t\tgridWithWords.getWords(word.returnWords());\r\n\t\t\t\/\/ create a search grid with the words passed above.\r\n\t\t\tgridWithWords.createSearchGrid();\r\n\t\t\t\/\/ print out the grid\t\t\r\n\t\t\tgridWithWords.printOutGrid();\r\n\t\t\t\/\/ print out the words to search\r\n\t\t\tgridWithWords.printTheWordsToSearch();\r\n\t\t\t\/\/ of course could have used the Word class\r\n\t\t\t\/\/ but since this is the WordToSearchGrid, so just placed one method in there as well.\r\n\t\t\tword.printOutWords();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and a example output would be with the testing blank grid created.<\/p>\n<pre lang=\"bash\">\r\nThe Grid\r\n0 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 =  10 =  \r\n1 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 = o9 =  10 =  \r\n2 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 = l10 =  \r\n3 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 =  10 = d\r\n4 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 =  10 =  \r\n5 ..0 =  1 =  2 =  3 =  4 = s5 =  6 =  7 =  8 =  9 =  10 =  \r\n6 ..0 =  1 =  2 =  3 =  4 =  5 = o6 =  7 =  8 =  9 =  10 =  \r\n7 ..0 =  1 =  2 =  3 =  4 =  5 =  6 = l7 =  8 =  9 =  10 = w \r\n8 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 = e8 =  9 =  10 = a \r\n9 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 =  10 = s\r\n10 ..0 =  1 =  2 =  3 =  4 =  5 =  6 =  7 =  8 =  9 =  10 =  \r\nWords to search for : \r\nold\r\nwas\r\nsole\r\nWords : (  0 = old  1 = was  2 = sole)\r\n<\/pre>\n<p>and output without the testing blank grid<\/p>\n<pre lang=\"bash\">\r\nThe Grid\r\n0 ..0 = u1 = r2 = s3 = f4 = x5 = b6 = a7 = o8 = m9 = j10 = m\r\n1 ..0 = s1 = s2 = o3 = l4 = e5 = k6 = h7 = g8 = v9 = e10 = h\r\n2 ..0 = a1 = y2 = l3 = a4 = q5 = a6 = q7 = i8 = x9 = s10 = i\r\n3 ..0 = t1 = j2 = q3 = x4 = r5 = b6 = s7 = m8 = g9 = f10 = d\r\n4 ..0 = m1 = t2 = v3 = u4 = r5 = c6 = t7 = q8 = o9 = s10 = w\r\n5 ..0 = g1 = f2 = c3 = s4 = r5 = d6 = v7 = u8 = x9 = f10 = r\r\n6 ..0 = n1 = c2 = s3 = q4 = v5 = t6 = f7 = w8 = a9 = t10 = s\r\n7 ..0 = h1 = m2 = y3 = o4 = v5 = u6 = h7 = b8 = i9 = h10 = n\r\n8 ..0 = b1 = w2 = e3 = m4 = e5 = h6 = j7 = f8 = d9 = t10 = i\r\n9 ..0 = u1 = i2 = j3 = k4 = w5 = w6 = l7 = k8 = l9 = h10 = c\r\n10 ..0 = o1 = s2 = o3 = i4 = w5 = x6 = n7 = o8 = o9 = t10 = w\r\nWords to search for : \r\nhe\r\nsole\r\nold\r\nWords : (  0 = he  1 = sole  2 = old)\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/wordseach-c-wordtosearchgrid\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wordseach c# &#8211; WordToSearchGrid<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[57],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-word-search"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":6,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/507\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}