{"id":497,"date":"2010-01-13T23:22:12","date_gmt":"2010-01-13T23:22:12","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=497"},"modified":"2010-01-14T11:56:26","modified_gmt":"2010-01-14T11:56:26","slug":"wordsearch-c-word-class-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/13\/wordsearch-c-word-class-2\/","title":{"rendered":"Wordsearch c# &#8211; Word class"},"content":{"rendered":"<p>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 <a href=\"http:\/\/www.codingfriends.com\/index.php\/projects\/word-search\">wordsearch<\/a> grid with words inserted onto the grid, I have already done a basic php version and here is the word class in php <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/05\/wordsearch-php-words-to-search-for\/\">word<\/a>.  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.<\/p>\n<p>This code will return a String array from a ArrayList type.  The ToArray changes the ArrayList to type of string then the &#8220;as string[]&#8221; returns the list of string as a string array.<\/p>\n<pre lang=\"csharp\">\r\n\t\t\treturn (arrayL.ToArray(typeof(string)) as string[]);\r\n<\/pre>\n<p>Here is the word class in c#, I have added allot of code comments to give details of what is happening.  <\/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\tclass Word \r\n\t{\r\n\t\tprivate String[] _words;\r\n\t\t\r\n\t \tpublic  Word() : this(\"words.xml\", 5) {}\r\n\t\t \r\n\t\tpublic Word(string wordsOrFilename, int maxSearchNum)\r\n\t\t{\r\n\t\t\tthis._words = new String[maxSearchNum];\r\n\t\t\t\/\/ just load from a file the words to search, instead of input as well.\r\n\t\t\tString[] loadedWords = loadWords(wordsOrFilename);\r\n\t\t\t\r\n\t\t\t\/\/ create the searchable words from the loadedwords array.\r\n\t\t\tthis._words = this.searchableWords(loadedWords, maxSearchNum);\r\n\t\t}\r\n\t\t\r\n\t\tprivate String[] loadWords(string filename)\r\n\t\t{\r\n\t\t\tXmlTextReader fileReader = new XmlTextReader(filename);\r\n\t\t\t\t\t\t\r\n\t\t\tArrayList fileArray = new ArrayList();\r\n\t\t\t\r\n\t\t\twhile (fileReader.Read())\r\n\t\t\t{\r\n\t\t\t\t\/\/ get the NodeType from the XML reader, different types\r\n\t\t\t\t\/\/ Element = name of element\r\n\t\t\t\t\/\/ Text = the actual text\/word\r\n\t\t\t\t\/\/ End Element = end of the element name\r\n\t\t\t    switch (fileReader.NodeType) \r\n\t\t\t    {\r\n\t\t\t\t\t\/\/ if there is a word in the file e.g. not a end\/element\r\n\t\t\t\t\t  case XmlNodeType.Text: \r\n\t\t\t\t\t\t\tfileArray.Add(fileReader.Value);\r\n\t\t\t\t            break;\r\n\t\t\t    }\r\n\t\t\t}\r\n\t\t\t\/\/ change the ArrayList into a String[] array using the ToArray method\r\n\t\t\treturn (fileArray.ToArray(typeof(string)) as string[]);\r\n\t\t}\r\n\r\n\t\t\/\/ create a list of words from the words passed in and also \r\n\t\t\/\/ the maximum number is the maxSearchNum.\r\n\t\tprivate String[] searchableWords(String[] words, int maxSearchNum)\r\n\t\t{\r\n\t\t\tArrayList returnWords = new ArrayList();\r\n\t\t\t\r\n\t\t\t\/\/ if the maxsearch value is greater or equal to the number of words to search for \r\n\t\t\t\/\/ just return the words string[].\r\n\t\t\tif (words.Length <= maxSearchNum)\r\n\t\t\t{\r\n\t\t\t\treturn words;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t\/\/ create a good seed for the random generator.\r\n\t\t\t\tRandom randGen = new Random((int)DateTime.Now.Ticks);\r\n\t\t\t\tint randomNum;\r\n\t\t\t\t\/\/ randomly pick out words from the array\r\n\t\t\t\tfor (int i = 0; i < maxSearchNum; i++)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/ pick a random number\r\n\t\t\t\t\trandomNum = randGen.Next(0, words.Length);\r\n\t\t\t\t\t\/\/ add to the array list to return\r\n\t\t\t\t\treturnWords.Add(words[randomNum]);\r\n\t\t\t\t\t\/\/ rebuild the array with removing the random number generated.\r\n\t\t\t\t\twords = rebuildArray(words, randomNum);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\/\/ convert back to the String[] \r\n\t\t\treturn (returnWords.ToArray(typeof(string)) as string[]);\r\n\t\t}\r\n \r\n\t\tprivate String[] rebuildArray(String[] rebuildSt, int numberToTakeOut)\r\n\t\t{\r\n\t\t\tArrayList arrayL = new ArrayList();\r\n\t\t\t\/\/ out of range error.\r\n\t\t\tif (rebuildSt.Length < numberToTakeOut)\r\n\t\t\t{\r\n\t\t\t\treturn rebuildSt;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tfor (int i =0; i < rebuildSt.Length; i++)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/ only add in the words that are not the\r\n\t\t\t\t\t\/\/ numberToTakeOut word from the array\r\n\t\t\t\t\tif (i != numberToTakeOut)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tarrayL.Add(rebuildSt[i]);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn (arrayL.ToArray(typeof(string)) as string[]);\r\n\t\t}\r\n\t\t\r\n\t\tpublic void printOutWords()\r\n\t\t{\r\n\t\t\tConsole.Write(\"Words : (\");\r\n\t\t\tfor (int i = 0; i < this._words.Length; i++)\r\n\t\t\t{\r\n\t\t\t\tConsole.Write(\"  \" + i + \" = \" + this._words[i]);\r\n\t\t\t}\r\n\t\t\tConsole.Write(\")\\n\");\r\n\t\t}\r\n\r\n\t\tpublic String[] returnWords()\r\n\t\t{\r\n\t\t\treturn this._words;\r\n\t\t}\r\n\t}\r\n\t\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                        word.printOutWords();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and a output would be<\/p>\n<pre lang=\"bash\">\r\nWords : (  0 = he  1 = old  2 = sole)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/13\/wordsearch-c-word-class-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wordsearch c# &#8211; Word class<\/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":[45],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-wordsearch"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/497","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=497"}],"version-history":[{"count":5,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}