/// <summary> /// This operation will ready the RedBlack dictionary to an array of strings, and the /// RedBlack wordsFound to an array of strings. This done so the serialization of the data /// objects become extremely easy for section 5 array encoding, and even easier for each data /// object's ultimate destination to the server side database. /// </summary> /// <param name="RedBlack_Dictionary">The RedBlack dictionary to convert.</param> /// <param name="puzzle">The char [,,] to convert.</param> /// <param name="RedBlack_wordsFound">The RedBlack wordsFound to convert.</param> /// <param name="solution">The solution object class reference.</param> private void SetDataForStorage( RedBlack RedBlack_Dictionary, char[,,] puzzle, RedBlack RedBlack_wordsFound, ref Solution solution ) { try { // Serialize the dictinary string path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\dictionary.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(string[]), "http://ns_DictionaryCreator" ); RedBlack_Dictionary.GetActualSize( RedBlack_Dictionary, ref index ); string [] dictionary = new string[index]; index = 0; VisitRedBlackTree( RedBlack_Dictionary, ref dictionary ); serializer.Serialize( writer.BaseStream, dictionary ); writer.Close(); reader = new StreamReader( path ); dictionaryXML = reader.ReadToEnd(); reader.Close(); // Serialize the puzzle path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\puzzle.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(char[][][]), "http://Puzzle_Creation" ); char[][][] jaggedPuzzle = ConvertTo3DJaggedArray( puzzle ); serializer.Serialize( writer.BaseStream, jaggedPuzzle ); writer.Close(); reader = new StreamReader( path ); puzzleXML = reader.ReadToEnd(); reader.Close(); // Serialize the wordsFound path = @"\\" + System.Net.Dns.GetHostName() + @"\thepuzzler_3dstyle\ManageDB\wordsFound.xml"; writer = new StreamWriter( path ); serializer = new XmlSerializer( typeof(string[]), "http://Puzzle_Solution" ); index = 0; RedBlack_wordsFound.GetActualSize(RedBlack_wordsFound, ref index); string [] wordsFound = new string[index]; index = 0; VisitRedBlackTree( RedBlack_wordsFound, ref wordsFound ); serializer.Serialize( writer.BaseStream, wordsFound ); writer.Close(); reader = new StreamReader( path ); wordsFoundXML = reader.ReadToEnd(); reader.Close(); } catch( Exception ex ) { new ManageDB_TestClass().HandleException( ref ex ); } finally { writer.Close(); reader.Close(); } }
/// <summary> /// This helper method will randomize the dictionary created in the last /// method call, and decrease it's size to that specified by dictionarySize /// from the last method call. /// </summary> /// <param name="dictionary">The dictionary to randomize.</param> /// <param name="size">The size of the dictionary specified by the user.</param> /// <returns>The user specified RedBlack Tree dictionary.</returns> private RedBlack RandomizeDictionary( RedBlack dictionary, ref int size ) { RedBlack returnValue = new RedBlack(), nextWord = null; tempTree = new RedBlack(); tempTree.DeepCopy( dictionary, ref tempTree ); Random random = new Random(); for( int dictionSize = 0; dictionSize < size; dictionSize++ ) { /* Next random first character in a word to select for insertion. * Randomly select a lower case alphabet letter from (97)'a' to (122)'z'. */ char firstChar = (char)random.Next( 97, 123 ); nextWord = NextWord( dictionary, firstChar ); string word = new string( (char[])nextWord.Key ); if( nextWord == Sentinel.Node ) { dictionSize = dictionSize - 1; usedWords[firstChar] = 0; int actualSize = 0; dictionary.GetActualSize( dictionary, ref actualSize ); if( actualSize == 0 && dictionSize == size - 1 ) { size = dictionSize; return returnValue; } else if( actualSize == 0 && dictionSize != size - 1 ) dictionary = tempTree; } else { dictionary.RB_Delete( ref dictionary, word.ToCharArray() ); returnValue.RB_Insert( ref returnValue, word.ToCharArray() ); } nextWord = null; } return returnValue; }