public static Boolean TryParse(List <TitleSequence> originalWordDataList, Crozzle aCrozzle, out WordDataList aWordDataList)
        {
            List <WordData> aList = new List <WordData>();

            Errors        = new List <String>();
            aWordDataList = new WordDataList(originalWordDataList);

            foreach (TitleSequence block in originalWordDataList)
            {
                WordData aWordData;
                if (WordData.TryParse(block, aCrozzle, out aWordData))
                {
                    aWordDataList.Add(aWordData);
                }
                else
                {
                    Errors.AddRange(WordData.Errors);
                }
            }
            aWordDataList.Valid = Errors.Count == 0;
            return(aWordDataList.Valid);
        }
Example #2
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle, bool isRemote = false)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList, isRemote);

            // Open file.
            StreamReader fileIn = null;

            if (!isRemote)
            {
                fileIn = new StreamReader(path);
            }
            else
            {
                WebClient webClient = new WebClient();
                Stream    stream    = webClient.OpenRead(path);
                fileIn = new StreamReader(stream);
            }

            List <String> wordData = new List <string>();

            // Validate file.
            while (!fileIn.EndOfStream)
            {
                // Read a line.
                String line = fileIn.ReadLine();

                // Parse a crozzle file item.
                CrozzleFileItem aCrozzleFileItem;
                if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem))
                {
                    if (aCrozzleFileItem.IsConfigurationFile)
                    {
                        // Get the configuration file name.
                        String configurationPath = aCrozzleFileItem.KeyValue.Value;
                        if (configurationPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                        }
                        else
                        {
                            configurationPath = configurationPath.Trim();
                            if (Validator.IsDelimited(configurationPath, StringDelimiters))
                            {
                                configurationPath = configurationPath.Trim(StringDelimiters);
                            }

                            if (!isRemote && !Path.IsPathRooted(configurationPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                configurationPath = directoryName + @"\" + configurationPath;
                            }

                            aCrozzle.ConfigurationPath = configurationPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsWordListFile)
                    {
                        // Get the word list file name.
                        String wordListPath = aCrozzleFileItem.KeyValue.Value;
                        if (wordListPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.WordlistFilenameMissing);
                        }
                        else
                        {
                            wordListPath = wordListPath.Trim();
                            if (Validator.IsDelimited(wordListPath, StringDelimiters))
                            {
                                wordListPath = wordListPath.Trim(StringDelimiters);
                            }

                            if (!isRemote && !Path.IsPathRooted(wordListPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                wordListPath = directoryName + @"\" + wordListPath;
                            }

                            aCrozzle.WordListPath = wordListPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsRows)
                    {
                        // Get the number of rows.
                        int rows;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows))
                        {
                            aCrozzle.Rows = rows;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }
                    else if (aCrozzleFileItem.IsColumns)
                    {
                        // Get the number of columns.
                        int columns;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns))
                        {
                            aCrozzle.Columns = columns;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }
                    else if (aCrozzleFileItem.IsRow)
                    {
                        // Collect potential word data for a horizontal word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                    else if (aCrozzleFileItem.IsColumn)
                    {
                        // Collect potential word data for a vertical word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                }
                else
                {
                    Errors.AddRange(CrozzleFileItem.Errors);
                }
            }

            // Close files.
            fileIn.Close();

            aCrozzle.Node = Node.GreedyAlgorithm(aCrozzle.Rows, aCrozzle.Columns, aConfiguration, wordList.List);

            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            aCrozzle.CrozzleRows    = aCrozzle.Node.Grid.GridRows;
            aCrozzle.CrozzleColumns = aCrozzle.Node.Grid.GridColumns;

            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
Example #3
0
        public static Boolean TryParse(String originalRowData, String originalColumnData, Crozzle aCrozzle, out Coordinate aCoordinate)
        {
            int anInteger;

            String[] originalCoordinate = new String[] { originalRowData, originalColumnData };

            Errors      = new List <String>();
            aCoordinate = new Coordinate(originalCoordinate);

            // Check that the row value is an integer and in range.
            if (Validator.IsInt32(originalRowData, out anInteger))
            {
                aCoordinate.Row = anInteger;
                if (!Validator.TryRange(aCoordinate.Row, 1, aCrozzle.Rows))
                {
                    Errors.Add(String.Format(CoordinateErrors.RowValueError, originalRowData, Validator.Errors[0]));
                }
            }
            else
            {
                Errors.Add(String.Format(CoordinateErrors.RowValueError, originalRowData, Validator.Errors[0]));
            }

            // Check that the column value is an integer and in range.
            if (Validator.IsInt32(originalColumnData, out anInteger))
            {
                aCoordinate.Column = anInteger;
                if (!Validator.TryRange(aCoordinate.Column, 1, aCrozzle.Columns))
                {
                    Errors.Add(String.Format(CoordinateErrors.ColumnValueError, originalColumnData, Validator.Errors[0]));
                }
            }
            else
            {
                Errors.Add(String.Format(CoordinateErrors.ColumnValueError, originalColumnData, Validator.Errors[0]));
            }

            aCoordinate.Valid = Errors.Count == 0;
            return(aCoordinate.Valid);
        }
Example #4
0
        private void openCrozzleFile()
        {
            DialogResult result;

            // As we are opening a crozzle file,
            // indicate crozzle file, and crozzle are not valid, and clear GUI.
            crozzleToolStripMenuItem.Enabled        = false;
            crozzleWebBrowser.DocumentText          = "";
            ErrorListViewer.WebBrowser.DocumentText = "";

            // Process crozzle file.
            result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Get configuration filename.
                String configurationFileName = GetConfigurationFileName(openFileDialog1.FileName);
                if (configurationFileName == null)
                {
                    MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // Add root to the path
                else
                {
                    String filename = configurationFileName.Trim();
                    if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                    {
                        filename = filename.Trim(Crozzle.StringDelimiters);
                    }
                    configurationFileName = filename;

                    if (!Path.IsPathRooted(configurationFileName))
                    {
                        configurationFileName = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + configurationFileName;
                    }
                }

                // Parse configuration file.
                Configuration aConfiguration = null;
                Configuration.TryParse(configurationFileName, out aConfiguration);

                // Get wordlist filename.
                String wordListFileName = GetWordlistFileName(openFileDialog1.FileName);
                if (wordListFileName == null)
                {
                    MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    String filename = wordListFileName.Trim();
                    if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                    {
                        filename = filename.Trim(Crozzle.StringDelimiters);
                    }
                    wordListFileName = filename;

                    if (!Path.IsPathRooted(wordListFileName))
                    {
                        wordListFileName = Path.GetDirectoryName(openFileDialog1.FileName) + @"\" + wordListFileName;
                    }
                }

                // Parse wordlist file.
                WordList wordList = null;
                WordList.TryParse(wordListFileName, aConfiguration, out wordList);

                // Parse crozzle file.
                Crozzle aCrozzle;
                Crozzle.TryParse(openFileDialog1.FileName, aConfiguration, wordList, out aCrozzle);
                SIT323Crozzle = aCrozzle;

                // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors.
                if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid)
                {
                    crozzleToolStripMenuItem.Enabled = true;
                }

                crozzleWebBrowser.DocumentText          = SIT323Crozzle.ToStringHTML();
                ErrorListViewer.WebBrowser.DocumentText =
                    SIT323Crozzle.FileErrorsHTML +
                    SIT323Crozzle.Configuration.FileErrorsHTML +
                    SIT323Crozzle.WordList.FileErrorsHTML;

                // Log errors.
                SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT);
                SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT);
                SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors);
            }
        }
Example #5
0
        public static Boolean TryParse(String originalWordDataData, Crozzle aCrozzle, out WordData aWordData)
        {
            String[] originalWordData = originalWordDataData.Split(new Char[] { '=', ',' });

            Errors    = new List <String>();
            aWordData = new WordData(originalWordData);

            // Check that the original word data has the correct number of fields.
            if (originalWordData.Length != NumberOfFields)
            {
                Errors.Add(String.Format(WordDataErrors.FieldCountError, originalWordData.Length, originalWordDataData, NumberOfFields));
            }

            // Check that each field is not empty.
            for (int field = 0; field < originalWordData.Length; field++)
            {
                if (originalWordData[field].Length == 0)
                {
                    Errors.Add(String.Format(WordDataErrors.BlankFieldError, originalWordDataData, field));
                }
            }

            if (originalWordData.Length > 0)
            {
                // Check that the 1st field is an orientation value.
                Orientation anOrientation;
                if (!Orientation.TryParse(originalWordData[0], out anOrientation))
                {
                    Errors.AddRange(Orientation.Errors);
                }
                aWordData.Orientation = anOrientation;

                if (anOrientation.Valid)
                {
                    // Check that the 2nd and 4th fields are a Coordinate.
                    if (originalWordData.Length >= NumberOfFields)
                    {
                        String rowValue    = "";
                        String columnValue = "";
                        if (anOrientation.IsHorizontal)
                        {
                            // UPDATED
                            rowValue    = originalWordData[3];
                            columnValue = originalWordData[4];
                        }
                        else if (anOrientation.IsVertical)
                        {
                            // UPDATED
                            rowValue    = originalWordData[3];
                            columnValue = originalWordData[4];
                        }

                        if (rowValue.Length > 0 && columnValue.Length > 0)
                        {
                            Coordinate aCoordinate;
                            if (!Coordinate.TryParse(rowValue, columnValue, aCrozzle, out aCoordinate))
                            {
                                Errors.AddRange(Coordinate.Errors);
                            }
                            aWordData.Location = aCoordinate;
                        }
                    }

                    // Check that the 3rd field is alphabetic, and in the wordlist.
                    if (originalWordData.Length >= NumberOfFields - 1)
                    {
                        String originalWord = originalWordData[1];
                        if (originalWord.Length > 0)
                        {
                            if (Regex.IsMatch(originalWord, Configuration.allowedCharacters))
                            {
                                aWordData.Letters = originalWord;

                                // Check that the 3rd field is in the wordlist.
                                if (!aCrozzle.WordList.Contains(originalWord))
                                {
                                    Errors.Add(String.Format(WordDataErrors.MissingWordError, originalWord));
                                }
                            }
                            else
                            {
                                Errors.Add(String.Format(WordDataErrors.AlphabeticError, originalWord));
                            }
                        }
                    }
                }
            }

            aWordData.Valid = Errors.Count == 0;
            return(aWordData.Valid);
        }
Example #6
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            // Open file.
            StreamReader  fileIn   = new StreamReader(path);
            List <String> wordData = new List <string>();

            // Validate file.
            while (!fileIn.EndOfStream)
            {
                // Read a line.
                String line = fileIn.ReadLine();

                // Parse a crozzle file item.
                CrozzleFileItem aCrozzleFileItem;
                if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem))
                {
                    if (aCrozzleFileItem.IsConfigurationFile)
                    {
                        // Get the configuration file name.
                        String configurationPath = aCrozzleFileItem.KeyValue.Value;
                        if (configurationPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                        }
                        else
                        {
                            configurationPath = configurationPath.Trim();
                            if (Validator.IsDelimited(configurationPath, StringDelimiters))
                            {
                                configurationPath = configurationPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(configurationPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                configurationPath = directoryName + @"\" + configurationPath;
                            }

                            aCrozzle.ConfigurationPath = configurationPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsWordListFile)
                    {
                        // Get the word list file name.
                        String wordListPath = aCrozzleFileItem.KeyValue.Value;
                        if (wordListPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.WordlistFilenameMissing);
                        }
                        else
                        {
                            wordListPath = wordListPath.Trim();
                            if (Validator.IsDelimited(wordListPath, StringDelimiters))
                            {
                                wordListPath = wordListPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(wordListPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                wordListPath = directoryName + @"\" + wordListPath;
                            }

                            aCrozzle.WordListPath = wordListPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsRows)
                    {
                        // Get the number of rows.
                        int rows;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows))
                        {
                            aCrozzle.Rows = rows;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }
                    else if (aCrozzleFileItem.IsColumns)
                    {
                        // Get the number of columns.
                        int columns;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns))
                        {
                            aCrozzle.Columns = columns;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }
                    else if (aCrozzleFileItem.IsRow)
                    {
                        // Collect potential word data for a horizontal word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                    else if (aCrozzleFileItem.IsColumn)
                    {
                        // Collect potential word data for a vertical word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                }
                else
                {
                    Errors.AddRange(CrozzleFileItem.Errors);
                }
            }

            // Close files.
            fileIn.Close();


            // Get potential word data list.
            WordDataList wordDataList;

            if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList))
            {
                Errors.AddRange(WordDataList.Errors);
            }
            aCrozzle.WordDataList = wordDataList;


            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            // Raw word data of horizontal and vertical words were obtained when reading the crozzle file,
            // but now we need to create crozzle rows and crozzle columns that represent the crozzle.
            aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList);
            aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList);

            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
Example #7
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            // Open file.
            StreamReader  fileIn   = new StreamReader(path);
            List <String> wordData = new List <string>();

            String section  = null;
            bool   newBlock = false;

            // Validate file
            while (!fileIn.EndOfStream)
            {
                // Read a line.
                String line = fileIn.ReadLine();

                // Processing empty lines
                if (Regex.IsMatch(line, @"^\s*$"))
                {
                    continue;
                }
                line = line.Trim();

                // Processing comments
                if (line.Contains("//"))
                {
                    if (line.StartsWith("//"))
                    {
                        continue;
                    }
                    else
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MixConfigWithComentError, line));
                        continue;
                    }
                }

                // Section check
                switch (line)
                {
                case "FILE-DEPENDENCIES":
                case "CROZZLE-SIZE":
                case "HORIZONTAL-SEQUENCES":
                case "VERTICAL-SEQUENCES":
                    section  = line;
                    newBlock = true;
                    break;

                case "END-FILE-DEPENDENCIES":
                case "END-CROZZLE-SIZE":
                case "END-HORIZONTAL-SEQUENCES":
                case "END-VERTICAL-SEQUENCES":
                    section  = null;
                    newBlock = true;
                    break;

                default:
                    break;
                }

                if (newBlock)
                {
                    newBlock = false;
                    continue;
                }

                // Parse a crozzle file item.
                CrozzleFileItem aCrozzleFileItem;

                // Out of section comment
                if (section == null)
                {
                    Errors.Add(String.Format(ConfigurationErrors.OutOfSectionError, line));
                }
                // Parse a crozzle item
                else if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem))
                {
                    if (aCrozzleFileItem.IsConfigurationFile)
                    {
                        // Get the configuration file name.
                        String configurationPath = aCrozzleFileItem.KeyValue.Value;
                        if (configurationPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                        }
                        else
                        {
                            configurationPath = configurationPath.Trim();
                            if (Validator.IsDelimited(configurationPath, StringDelimiters))
                            {
                                configurationPath = configurationPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(configurationPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                configurationPath = directoryName + @"\" + configurationPath;
                            }

                            aCrozzle.ConfigurationPath = configurationPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsWordListFile)
                    {
                        // Get the word list file name.
                        String wordListPath = aCrozzleFileItem.KeyValue.Value;
                        if (wordListPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.WordlistFilenameMissing);
                        }
                        else
                        {
                            wordListPath = wordListPath.Trim();
                            if (Validator.IsDelimited(wordListPath, StringDelimiters))
                            {
                                wordListPath = wordListPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(wordListPath))
                            {
                                String directoryName = Path.GetDirectoryName(path);
                                wordListPath = directoryName + @"\" + wordListPath;
                            }

                            aCrozzle.WordListPath = wordListPath;
                        }
                    }
                    // Replace
                    else if (aCrozzleFileItem.IsSize)
                    {
                        // Split row & col
                        String   rawSizeData = aCrozzleFileItem.KeyValue.Value.Trim();
                        String[] sizeSplit   = rawSizeData.Split(new String[] { SizeDelimiter }, 2, StringSplitOptions.None);
                        if (sizeSplit.Length != SizeValueLength)
                        {
                            Errors.Add(String.Format(KeyValueErrors.FieldCountError, sizeSplit.Length, rawSizeData, SizeValueLength));
                        }
                        else
                        {
                            int rows;
                            if (Validator.IsInt32(sizeSplit[0].Trim(), out rows))
                            {
                                aCrozzle.Rows = rows;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }

                            int columns;
                            if (Validator.IsInt32(sizeSplit[1].Trim(), out columns))
                            {
                                aCrozzle.Columns = columns;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }
                        }
                    }

                    else if (aCrozzleFileItem.IsSequence)
                    {
                        // Collect potential word data for a horizontal word.
                        //wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                        String type;

                        if (section == "HORIZONTAL-SEQUENCES")
                        {
                            type = "ROW";

                            try
                            {
                                String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue;

                                oldFormat = oldFormat.Substring("SEQUENCE=".Length);
                                string[] split;
                                split = oldFormat.Split(',');
                                String key = split[0];
                                String row = split[1].Split('=')[1];
                                String col = split[2];

                                String newFormat = type + "=" + row + "," + key + "," + col;
                                Console.WriteLine("New Format = " + newFormat);
                                wordData.Add(newFormat);
                            }
                            catch (Exception e)
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue));
                            }
                        }
                        else if (section == "VERTICAL-SEQUENCES")
                        {
                            type = "COLUMN";


                            try
                            {
                                String oldFormat = aCrozzleFileItem.KeyValue.OriginalKeyValue;

                                oldFormat = oldFormat.Substring("SEQUENCE=".Length);
                                string[] split;
                                split = oldFormat.Split(',');
                                String key = split[0];
                                String row = split[1].Split('=')[1];
                                String col = split[2];

                                String newFormat = type + "=" + col + "," + key + "," + row;
                                Console.WriteLine("New Format = " + newFormat);
                                wordData.Add(newFormat);
                            }
                            catch (Exception e)
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.SequenceFormatError, aCrozzleFileItem.KeyValue.OriginalKeyValue));
                            }
                        }
                        else
                        {
                            Errors.AddRange(CrozzleFileItem.Errors);
                            break;
                        }
                    }
                }
                else
                {
                    Errors.AddRange(CrozzleFileItem.Errors);
                }
            }

            // Close files.
            fileIn.Close();


            // Get potential word data list.
            WordDataList wordDataList;

            if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList))
            {
                Errors.AddRange(WordDataList.Errors);
            }
            aCrozzle.WordDataList = wordDataList;


            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            // Raw word data of horizontal and vertical words were obtained when reading the crozzle file,
            // but now we need to create crozzle rows and crozzle columns that represent the crozzle.
            aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList);
            aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList);

            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
        public static Boolean TryParse(SequenceFragment fragment, Crozzle aCrozzle, out WordData aWordData)
        {
            Errors = new List <String>();

            aWordData = new WordData();

            Orientation anOrientation;

            if (!Orientation.TryParse(fragment.DirectionIdentifier, out anOrientation))
            {
                Errors.AddRange(Orientation.Errors);
            }
            aWordData.Orientation = anOrientation;

            String[] fields = fragment.OriginalWordData.Split(new char[] { ',' }, 2);
            if (fields.Length != 2)
            {
                Errors.Add(String.Format(WordDataErrors.FieldCountError, fields.Length, (fields.Length == 1) ? String.Empty : "s", fragment.OriginalWordData));
            }
            else
            {
                if (fields.Where(i => i.Contains("SEQUENCE")).Any() && fields.Where(i => i.Contains("LOCATION")).Any())
                {
                    String[] wordField = fields.Where(item => item.Contains("SEQUENCE")).First().Split('=');
                    if (String.IsNullOrEmpty(wordField[1]))
                    {
                        Errors.Add(String.Format(WordDataErrors.BlankFieldError, fragment.OriginalWordData, wordField[0]));
                    }
                    else
                    {
                        if (Regex.IsMatch(wordField[1], Configuration.allowedCharacters))
                        {
                            aWordData.Letters = wordField[1];
                        }
                        else
                        {
                            Errors.Add(String.Format(WordDataErrors.AlphabeticError, wordField[1]));
                        }
                    }

                    String[] locationField = fields.Where(item => item.Contains("LOCATION")).First().Split('=');
                    if (String.IsNullOrEmpty(locationField[1]))
                    {
                        Errors.Add(String.Format(WordDataErrors.BlankFieldError, fragment.OriginalWordData, wordField[0]));
                    }
                    else
                    {
                        String[] values = locationField[1].Split(',');
                        if (values.Length != 2)
                        {
                            Errors.Add(String.Format(WordDataErrors.SequencePositionIncompleteError, fragment.OriginalWordData));
                        }
                        else
                        {
                            int posx, posy;
                            if (!Validator.IsInt32(values[0], out posx) || !Validator.IsInt32(values[1], out posy))
                            {
                                Errors.Add(String.Format(WordDataErrors.SequencePositionInvalidError, fragment.OriginalWordData));
                            }
                            else
                            {
                                aWordData.Location = new Coordinate(posx, posy);
                            }
                        }
                    }
                }
                else
                {
                    Errors.Add(String.Format(WordDataErrors.SeqOrLocFieldMissingError, fragment.OriginalWordData));
                }
            }
            aWordData.OriginalWordData =
                new string[] { (aWordData.Orientation != null) ? aWordData.Orientation.Direction : null,
                               (aWordData.Location != null) ? aWordData.Location.Row.ToString() : null,
                               (aWordData.Location != null) ? aWordData.Location.Column.ToString() : null,
                               (aWordData.Letters != null) ? aWordData.Letters : null };

            aWordData.Valid = Errors.Count == 0;
            return(aWordData.Valid);
        }
Example #9
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            StreamReader fileIn;

            if (path.Contains("http"))
            {
                WebClient webClient = new WebClient();
                Stream    aStream   = webClient.OpenRead(path);
                fileIn = new StreamReader(aStream);
            }
            else
            {
                fileIn = new StreamReader(path);
            }


            List <String> wordData = new List <string>();

            // Validate file.
            while (!fileIn.EndOfStream)
            {
                // Read a line.
                String line = fileIn.ReadLine();

                // Parse a crozzle file item.
                CrozzleFileItem aCrozzleFileItem;
                if (CrozzleFileItem.TryParse(line, out aCrozzleFileItem))
                {
                    if (aCrozzleFileItem.IsConfigurationFile)
                    {
                        // Get the configuration file name.
                        String configurationPath = aCrozzleFileItem.KeyValue.Value;
                        if (configurationPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                        }
                        else
                        {
                            configurationPath = configurationPath.Trim();
                            if (Validator.IsDelimited(configurationPath, StringDelimiters))
                            {
                                configurationPath = configurationPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(configurationPath))
                            {
                                // Seperate local files and files from web server
                                if (!path.Contains("http"))
                                {
                                    String directoryName = Path.GetDirectoryName(path);
                                    configurationPath = directoryName + @"\" + configurationPath;
                                }
                            }

                            aCrozzle.ConfigurationPath = configurationPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsWordListFile)
                    {
                        // Get the word list file name.
                        String wordListPath = aCrozzleFileItem.KeyValue.Value;
                        if (wordListPath == null)
                        {
                            Errors.Add(CrozzleFileErrors.WordlistFilenameMissing);
                        }
                        else
                        {
                            wordListPath = wordListPath.Trim();
                            if (Validator.IsDelimited(wordListPath, StringDelimiters))
                            {
                                wordListPath = wordListPath.Trim(StringDelimiters);
                            }

                            if (!Path.IsPathRooted(wordListPath))
                            {
                                // Seperate local files and files from web server
                                if (!path.Contains("http"))
                                {
                                    String directoryName = Path.GetDirectoryName(path);
                                    wordListPath = directoryName + @"\" + wordListPath;
                                }
                            }

                            aCrozzle.WordListPath = wordListPath;
                        }
                    }
                    else if (aCrozzleFileItem.IsRows)
                    {
                        // Get the number of rows.
                        int rows;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out rows))
                        {
                            aCrozzle.Rows = rows;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }
                    else if (aCrozzleFileItem.IsColumns)
                    {
                        // Get the number of columns.
                        int columns;
                        if (Validator.IsInt32(aCrozzleFileItem.KeyValue.Value.Trim(), out columns))
                        {
                            aCrozzle.Columns = columns;
                        }
                        else
                        {
                            Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                        }
                    }

                    else if (aCrozzleFileItem.IsRow)
                    {
                        // Collect potential word data for a horizontal word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                    else if (aCrozzleFileItem.IsColumn)
                    {
                        // Collect potential word data for a vertical word.
                        wordData.Add(aCrozzleFileItem.KeyValue.OriginalKeyValue);
                    }
                }
                else
                {
                    Errors.AddRange(CrozzleFileItem.Errors);
                }
            }

            // Close files.
            fileIn.Close();

            // If the file does not have location and orientation of words
            if (wordData.Count == 0)
            {
                // Initialize an empty key-value group
                List <String> GenerateKeyValueGroup = new List <String>();

                // Use an dictionary to store inserting and non-inserting words score
                Dictionary <char, int> IntersectingPointsPerLetter    = new Dictionary <char, int>();
                Dictionary <char, int> NonIntersectingPointsPerLetter = new Dictionary <char, int>();

                // Append score to corresponding letter
                char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
                for (int i = 0; i < alphabet.Length; i++)
                {
                    IntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.IntersectingPointsPerLetter[i]);
                    NonIntersectingPointsPerLetter.Add(alphabet[i], aConfiguration.NonIntersectingPointsPerLetter[i]);
                }

                // Use greedy algorithm to get a group with maximun number of words
                Grid grid = new Grid(aCrozzle.Rows, aCrozzle.Columns, IntersectingPointsPerLetter, NonIntersectingPointsPerLetter, aConfiguration.PointsPerWord);
                grid.GetConfiguration(aCrozzle.Configuration);
                GenerateKeyValueGroup = grid.GreedyAlgorithm(wordList.List, aConfiguration.MaximumNumberOfGroups, aConfiguration.MinimumNumberOfGroups, out aCrozzle.timeConsume);

                // Append NewkeyValue instead of OriginalKeyValue to old program in terms of compatibility
                foreach (string GenerateKeyValue in GenerateKeyValueGroup)
                {
                    wordData.Add(GenerateKeyValue);
                }
            }

            // Get potential word data list.
            WordDataList wordDataList;

            if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList))
            {
                Errors.AddRange(WordDataList.Errors);
            }
            aCrozzle.WordDataList = wordDataList;


            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            // Raw word data of horizontal and vertical words were obtained when reading the crozzle file,
            // but now we need to create crozzle rows and crozzle columns that represent the crozzle.
            aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList);
            aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList);

            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
Example #10
0
        private void openCrozzleFileRemotely()
        {
            crozzleToolStripMenuItem.Enabled = false;

            // Process crozzle file to get configuration filename
            String configurationFileName = GetConfigurationFileNameRemotely(crozzleComboBox.Text);

            if (configurationFileName == null)
            {
                MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = configurationFileName.Trim();
                if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                {
                    filename = filename.Trim(Crozzle.StringDelimiters);
                }
                configurationFileName = filename;
            }

            // Parse configuration file.
            Configuration aConfiguration = null;

            Configuration.TryParse(configurationFileName, out aConfiguration, true);

            // Get wordlist filename.
            String wordListFileName = GetWordlistFileNameRemotely(crozzleComboBox.Text);

            if (wordListFileName == null)
            {
                MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = wordListFileName.Trim();
                if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                {
                    filename = filename.Trim(Crozzle.StringDelimiters);
                }
                wordListFileName = filename;
            }

            // Parse wordlist file.
            WordList wordList = null;

            WordList.TryParse(wordListFileName, aConfiguration, out wordList, true);

            // Parse crozzle file.
            Crozzle aCrozzle;

            Crozzle.TryParse(crozzleComboBox.Text, aConfiguration, wordList, out aCrozzle, true);
            SIT323Crozzle = aCrozzle;

            // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors.
            if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid)
            {
                crozzleToolStripMenuItem.Enabled = true;
            }

            crozzleWebBrowser.DocumentText          = SIT323Crozzle.ToStringHTML();
            ErrorListViewer.WebBrowser.DocumentText =
                SIT323Crozzle.FileErrorsHTML +
                SIT323Crozzle.Configuration.FileErrorsHTML +
                SIT323Crozzle.WordList.FileErrorsHTML;

            // Log errors.
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT, true);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT, true);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors, true);
        }
Example #11
0
        private void saveAndOpenWebCrozzleFile()
        {
            string tempCrozzlePath       = @"..\..\..\TempCrozzleFile\TempCrozzle.txt";
            string tempWordlistPath      = @"..\..\..\TempCrozzleFile\TempWordlist.txt";
            string tempConfigurationPath = @"..\..\..\TempCrozzleFile\TempConfiguration.txt";

            this.downloadFile(this.comboBox1.Text, tempCrozzlePath);

            // As we are opening a crozzle file,
            // indicate crozzle file, and crozzle are not valid, and clear GUI.
            crozzleToolStripMenuItem.Enabled        = false;
            crozzleWebBrowser.DocumentText          = "";
            ErrorListViewer.WebBrowser.DocumentText = "";

            // Process crozzle file.
            // Get configuration filename.
            String configurationFileName = GetConfigurationFileName(tempCrozzlePath);

            if (configurationFileName == null)
            {
                MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = configurationFileName.Trim().Replace("\"", string.Empty);
                this.downloadFile(filename, tempConfigurationPath);
            }

            // Parse configuration file.
            Configuration aConfiguration = null;

            Configuration.TryParse(tempConfigurationPath, out aConfiguration);

            // Get wordlist filename.
            String wordListFileName = GetWordlistFileName(tempCrozzlePath);

            if (wordListFileName == null)
            {
                MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = wordListFileName.Trim().Replace("\"", string.Empty);
                this.downloadFile(filename, tempWordlistPath);
            }

            // Parse wordlist file.
            WordList wordList = null;

            WordList.TryParse(tempWordlistPath, aConfiguration, out wordList);

            // Parse crozzle file.
            Crozzle aCrozzle;

            Crozzle.TryParse(tempCrozzlePath, aConfiguration, wordList, out aCrozzle);
            SIT323Crozzle = aCrozzle;

            // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors.
            if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid)
            {
                crozzleToolStripMenuItem.Enabled = true;
            }

            crozzleWebBrowser.DocumentText          = SIT323Crozzle.ToStringHTML();
            ErrorListViewer.WebBrowser.DocumentText =
                SIT323Crozzle.FileErrorsHTML +
                SIT323Crozzle.Configuration.FileErrorsHTML +
                SIT323Crozzle.WordList.FileErrorsHTML;

            // Log errors.
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors);
        }
Example #12
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            // Open file.
            StreamReader            fileIn   = new StreamReader(path);
            List <SequenceFragment> wordData = new List <SequenceFragment>();

            // Validate file.
            while (!fileIn.EndOfStream)
            {
                List <String> fragment = new List <String>();
                do
                {
                    /// Add multiple lines as part of a text fragment until the last element is empty or null.
                    fragment.Add(fileIn.ReadLine());
                } while (!String.IsNullOrEmpty(fragment.Last()));

                // Parse a crozzle file fragment.
                FileFragment <CrozzleFileItem> aCrozzleFileFragment;
                if (CrozzleFileItem.TryParse(fragment, out aCrozzleFileFragment))
                {
                    String aFragmentKey = aCrozzleFileFragment.Name;

                    foreach (CrozzleFileItem aItem in aCrozzleFileFragment.Items)
                    {
                        /// Process the key-value, given the current fragment key.
                        if (aFragmentKey == CrozzleFileKeys.DEPENDENCIES_OPENBRACKET)
                        {
                            if (aItem.Name == CrozzleFileKeys.DEPENDENCIES_CONFIGDATA)
                            {
                                String configurationPath = aItem.KeyValue.Value;
                                if (configurationPath == null)
                                {
                                    Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                                }
                                else
                                {
                                    configurationPath = configurationPath.Trim();
                                    if (Validator.IsDelimited(configurationPath, StringDelimiters))
                                    {
                                        configurationPath = configurationPath.Trim(StringDelimiters);
                                    }

                                    if (!Path.IsPathRooted(configurationPath))
                                    {
                                        String directoryName = Path.GetDirectoryName(path);
                                        configurationPath = directoryName + @"\" + configurationPath;
                                    }

                                    aCrozzle.ConfigurationPath = configurationPath;
                                }
                            }
                            else if (aItem.Name == CrozzleFileKeys.DEPENDENCIES_SEQDATA)
                            {
                                String wordListPath = aItem.KeyValue.Value;
                                if (wordListPath == null)
                                {
                                    Errors.Add(CrozzleFileErrors.WordlistFilenameMissing);
                                }
                                else
                                {
                                    wordListPath = wordListPath.Trim();
                                    if (Validator.IsDelimited(wordListPath, StringDelimiters))
                                    {
                                        wordListPath = wordListPath.Trim(StringDelimiters);
                                    }

                                    if (!Path.IsPathRooted(wordListPath))
                                    {
                                        String directoryName = Path.GetDirectoryName(path);
                                        wordListPath = directoryName + @"\" + wordListPath;
                                    }

                                    aCrozzle.WordListPath = wordListPath;
                                }
                            }
                        }
                        else if (aFragmentKey == CrozzleFileKeys.SIZE_OPENBRACKET)
                        {
                            int      rows, columns;
                            String[] values = aItem.KeyValue.Value.Trim().Split(',');
                            if (Validator.IsInt32(values[0], out rows))
                            {
                                aCrozzle.Rows = rows;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.RowError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }

                            if (Validator.IsInt32(values[1], out columns))
                            {
                                aCrozzle.Columns = columns;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                            }
                        }
                        else if (new[] { CrozzleFileKeys.HORZSEQ_OPENBRACKET, CrozzleFileKeys.VERTSEQ_OPENBRACKET }.Contains(aFragmentKey))
                        {
                            wordData.Add(new SequenceFragment(aFragmentKey, aItem.KeyValue.OriginalKeyValue));
                        }
                        else
                        {
                            Errors.AddRange(CrozzleFileItem.Errors);
                        }
                    }
                }
            }

            // Close files.
            fileIn.Close();


            // Get potential word data list.
            WordDataList wordDataList;

            if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList))
            {
                Errors.AddRange(WordDataList.Errors);
            }
            aCrozzle.WordDataList = wordDataList;


            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationFileName));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            // Raw word data of horizontal and vertical words were obtained when reading the crozzle file,
            // but now we need to create crozzle rows and crozzle columns that represent the crozzle.
            aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList);
            aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList);

            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
Example #13
0
        /* This section is added for Assessment 2 */
        private void button1_Click(object sender, EventArgs e)
        {
            // Crozzle generataion start
            label1.Text = "Generating crozzle...";
            // Get URL address
            crozzleName = URL.Text;

            String configurationFileName = GetConfigurationFileName(crozzleName);

            if (configurationFileName == null)
            {
                MessageBox.Show("configuration filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = configurationFileName.Trim();
                if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                {
                    filename = filename.Trim(Crozzle.StringDelimiters);
                }
                configurationFileName = filename;

                if (!Path.IsPathRooted(configurationFileName) && !crozzleName.Contains("http"))
                {
                    configurationFileName = Path.GetDirectoryName(crozzleName) + @"\" + configurationFileName;
                }
            }

            // Parse configuration file.
            Configuration aConfiguration = null;

            Configuration.TryParse(configurationFileName, out aConfiguration);

            // Get wordlist filename.
            String wordListFileName = GetWordlistFileName(crozzleName);

            if (wordListFileName == null)
            {
                MessageBox.Show("wordlist filename is missing from the crozzle file", ApplicationAboutBox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                String filename = wordListFileName.Trim();
                if (Validator.IsDelimited(filename, Crozzle.StringDelimiters))
                {
                    filename = filename.Trim(Crozzle.StringDelimiters);
                }
                wordListFileName = filename;

                if (!Path.IsPathRooted(wordListFileName) && !crozzleName.Contains("http"))
                {
                    wordListFileName = Path.GetDirectoryName(crozzleName) + @"\" + wordListFileName;
                }
            }

            // Parse wordlist file.
            WordList wordList = null;

            WordList.TryParse(wordListFileName, aConfiguration, out wordList);

            // Parse crozzle file.
            Crozzle aCrozzle;

            Crozzle.TryParse(crozzleName, aConfiguration, wordList, out aCrozzle);
            SIT323Crozzle = aCrozzle;

            // Update GUI - menu enabled, display crozzle data (whether valid or invalid), and crozzle file errors.
            if (SIT323Crozzle.FileValid && SIT323Crozzle.Configuration.Valid && SIT323Crozzle.WordList.Valid)
            {
                crozzleToolStripMenuItem.Enabled = true;
            }

            crozzleWebBrowser.DocumentText          = SIT323Crozzle.ToStringHTML();
            ErrorListViewer.WebBrowser.DocumentText =
                SIT323Crozzle.FileErrorsHTML +
                SIT323Crozzle.Configuration.FileErrorsHTML +
                SIT323Crozzle.WordList.FileErrorsHTML;

            // Crozzle generataion complete
            label1.Text = "Crozzle generation succeed";

            // Log errors.
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.FileErrorsTXT);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.Configuration.FileErrorsTXT);
            SIT323Crozzle.LogFileErrors(SIT323Crozzle.WordList.FileErrors);
        }
Example #14
0
        public static Boolean TryParse(String path, Configuration aConfiguration, WordList wordList, out Crozzle aCrozzle)
        {
            Errors   = new List <String>();
            aCrozzle = new Crozzle(path, aConfiguration, wordList);

            // Open file.
            StreamReader         fileIn   = new StreamReader(path);
            List <TitleSequence> wordData = new List <TitleSequence>();

            // Validate file.
            while (!fileIn.EndOfStream)
            {
                // Create a Block to store a group of data
                List <String> Block = new List <String>();

                // Read a line.
                String line = fileIn.ReadLine();
                while (!String.IsNullOrEmpty(line))
                {
                    Block.Add(line);
                    line = fileIn.ReadLine();
                }

                // Parse a block in crozzle file
                CzlGroup aCrozzleFileGroup;
                if (CrozzleFileItem.TryParse(Block, out aCrozzleFileGroup))
                {
                    String title = aCrozzleFileGroup.CzlTitle;

                    for (int i = 0; i < aCrozzleFileGroup.Lines.Count; i++)
                    {
                        // Whether the title equals to "FILE-DEPENDENCIES"
                        if (title == CrozzleKeys.FileDependencies)
                        {
                            if (aCrozzleFileGroup.Lines[i].Name == CrozzleKeys.ConfigData)
                            {
                                String configurationPath = aCrozzleFileGroup.Lines[i].KeyValue.Value;
                                if (configurationPath == null)
                                {
                                    Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                                }
                                else
                                {
                                    configurationPath = configurationPath.Trim();

                                    if (Validator.IsDelimited(configurationPath, StringDelimiters))
                                    {
                                        configurationPath = configurationPath.Trim(StringDelimiters);
                                    }

                                    if (!Path.IsPathRooted(configurationPath))
                                    {
                                        String directoryName = Path.GetDirectoryName(path);
                                        configurationPath = directoryName + @"\" + configurationPath;
                                    }

                                    aCrozzle.ConfigurationPath = configurationPath;
                                }
                            }
                            else if (aCrozzleFileGroup.Lines[i].Name == CrozzleKeys.SequenceData)
                            {
                                String wordListPath = aCrozzleFileGroup.Lines[i].KeyValue.Value;
                                if (wordListPath == null)
                                {
                                    Errors.Add(CrozzleFileErrors.ConfigurationFilenameMissing);
                                }
                                else
                                {
                                    wordListPath = wordListPath.Trim();

                                    if (Validator.IsDelimited(wordListPath, StringDelimiters))
                                    {
                                        wordListPath = wordListPath.Trim(StringDelimiters);
                                    }

                                    if (!Path.IsPathRooted(wordListPath))
                                    {
                                        String directoryName = Path.GetDirectoryName(path);
                                        wordListPath = directoryName + @"\" + wordListPath;
                                    }

                                    aCrozzle.WordListPath = wordListPath;
                                }
                            }
                        }

                        // Whether the title equals to "CROZZLE-SIZE"
                        else if (title == CrozzleKeys.CrozzleSize)
                        {
                            int      rows, columns;
                            String[] coordinate = aCrozzleFileGroup.Lines[i].KeyValue.Value.Split(',');
                            if (Validator.IsInt32(coordinate[0].Trim(), out rows))
                            {
                                aCrozzle.Rows = rows;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.RowError, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue, Errors[0]));
                            }

                            if (Validator.IsInt32(coordinate[1].Trim(), out columns))
                            {
                                aCrozzle.Columns = columns;
                            }
                            else
                            {
                                Errors.Add(String.Format(CrozzleFileErrors.ColumnError, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue, Errors[1]));
                            }
                        }

                        // Whether the title equals to "HORIZONTAL-SEQUENCES" or "VERTICAL-SEQUENCE"
                        else if (title == CrozzleKeys.HorizontalSequence || title == CrozzleKeys.VerticalSequence)
                        {
                            TitleSequence temp = new TitleSequence(title, aCrozzleFileGroup.Lines[i].KeyValue.OriginalKeyValue);
                            wordData.Add(temp);
                        }

                        // If there exist unidentified titles
                        else
                        {
                            Errors.AddRange(CrozzleFileItem.Errors);
                        }
                    }
                }
            }
            // Close files.
            fileIn.Close();

            // Get potential word data list.
            WordDataList wordDataList;

            if (!WordDataList.TryParse(wordData, aCrozzle, out wordDataList))
            {
                Errors.AddRange(WordDataList.Errors);
            }
            aCrozzle.WordDataList = wordDataList;


            // Validate file sections.
            // Check the configuration file name.
            if (aCrozzle.Configuration != null)
            {
                if (aCrozzle.Configuration.ConfigurationPath != aCrozzle.ConfigurationPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.ConfigurationFilenameError, aCrozzle.ConfigurationPath, aCrozzle.Configuration.ConfigurationPath));
                }
            }

            // Check the word list file name.
            if (aCrozzle.WordList != null)
            {
                if (aCrozzle.WordList.WordlistPath != aCrozzle.WordListPath)
                {
                    Errors.Add(String.Format(CrozzleFileErrors.WordlistFilenameError, aCrozzle.WordListPath, aCrozzle.WordList.WordlistFileName));
                }
            }

            // Raw word data of horizontal and vertical words were obtained when reading the crozzle file,
            // but now we need to create crozzle rows and crozzle columns that represent the crozzle.
            aCrozzle.CreateCrozzleRows(aCrozzle.WordDataList);
            aCrozzle.CreateCrozzleColumns(aCrozzle.WordDataList);


            // Store validity.
            aCrozzle.FileValid = Errors.Count == 0;
            return(aCrozzle.FileValid);
        }
Example #15
0
        public static Boolean TryParse(TitleSequence Block, Crozzle aCrozzle, out WordData aWordData)
        {
            String[] originalWordData = Block.SequenceLine.Split(new Char[] { ',' }, 2);

            Errors    = new List <String>();
            aWordData = new WordData();
            String[] OldOriginalWordData = new string[4];


            // Check that the original word data has the correct number of fields.
            if (originalWordData.Length != NumberOfFields)
            {
                Errors.Add(String.Format(WordDataErrors.FieldCountError, originalWordData.Length, Block.SequenceLine, NumberOfFields));
            }

            // If the title symbol exist
            if (Block.Title.Length > 0)
            {
                // Check whether the title is an orientation value.
                Orientation anOrientation;
                if (!Orientation.TryParse(Block.Title, out anOrientation))
                {
                    Errors.AddRange(Orientation.Errors);
                }

                aWordData.Orientation = anOrientation;

                if (anOrientation.Valid)
                {
                    // Handle the name & sequence side
                    String[] WordName = originalWordData[0].Split('=');

                    // Figure out whether WordName = ['SEQUENCE','(words)']
                    if (WordName[0] != "SEQUENCE")
                    {
                        Errors.Add(String.Format(WordDataErrors.MissingSymbol, Block.SequenceLine));
                    }
                    else
                    {   // Figure out whether the word is missing
                        if (String.IsNullOrEmpty(WordName[1]))
                        {
                            Errors.Add(String.Format(WordDataErrors.BlankFieldError, Block.SequenceLine, WordName[0]));
                        }
                        // Find out whether the word is alphabet
                        else if (!Regex.IsMatch(WordName[1], Configuration.allowedCharacters))
                        {
                            Errors.Add(String.Format(WordDataErrors.AlphabeticError, WordName[1]));
                        }
                        else
                        {
                            aWordData.Letters = WordName[1];
                        }
                    }

                    // Handle the coordinate side
                    String[] WordCoordinate = originalWordData[1].Split('=');

                    // If the format is correct
                    if (WordName[0] == "SEQUENCE")
                    {
                        //find out whether the right side is seperated by equeal symbol
                        if (WordCoordinate.Length != 2)
                        {
                            Errors.Add(String.Format(WordDataErrors.MissingSymbol, Block.SequenceLine));
                        }

                        // Find out whether the coordinate exists
                        else if (String.IsNullOrEmpty(WordCoordinate[1]))
                        {
                            Errors.Add(String.Format(WordDataErrors.BlankFieldError, Block.SequenceLine, WordCoordinate[0]));
                        }
                        else
                        {
                            // Get the Coordinate in string format
                            String[] CoordinateString = WordCoordinate[1].Split(',');

                            // Find out whether the CoordinateString is complete
                            if (CoordinateString.Length != 2)
                            {
                                Errors.Add(String.Format(WordDataErrors.CoordinateIncomplete, CoordinateString[0], originalWordData[1]));
                            }
                            else
                            {
                                // Get the coordinate of each word
                                String rowValue    = CoordinateString[0];
                                String columnValue = CoordinateString[1];

                                if (rowValue.Length > 0 && columnValue.Length > 0)
                                {
                                    Coordinate aCoordinate;
                                    if (!Coordinate.TryParse(rowValue, columnValue, aCrozzle, out aCoordinate))
                                    {
                                        Errors.AddRange(Coordinate.Errors);
                                    }
                                    aWordData.Location = aCoordinate;
                                }
                            }
                        }
                    }
                }
            }

            // Switch to the old format
            if (aWordData.Orientation != null)
            {
                OldOriginalWordData[0] = aWordData.Orientation.Direction;
            }
            else if (aWordData.Location != null)
            {
                OldOriginalWordData[1] = aWordData.Location.Row.ToString();
                OldOriginalWordData[3] = aWordData.Location.Column.ToString();
            }
            else if (aWordData.Letters != null)
            {
                OldOriginalWordData[2] = aWordData.Letters;
            }

            // Pass the old format to old OldOriginalWordData
            aWordData.OriginalWordData = OldOriginalWordData;

            aWordData.Valid = Errors.Count == 0;
            return(aWordData.Valid);
        }