コード例 #1
0
        public WordDataList(WordDataList wordDataList)
        {
            // copy OriginalWordList
            List <String> copyOriginalWordList = new List <String>();

            foreach (String word in wordDataList.OriginalWordList)
            {
                copyOriginalWordList.Add(String.Copy(word));
            }
            OriginalWordList = copyOriginalWordList;

            // copy AllWordData
            List <WordData> copyAllWordData = new List <WordData>();
            // copy HorizontalWordData
            List <WordData> copyHorizontalWordData = new List <WordData>();
            // copy VerticalWordData
            List <WordData> copyVerticalWordData = new List <WordData>();

            foreach (WordData wordData in wordDataList.AllWordData)
            {
                if (wordData.IsHorizontal)
                {
                    copyHorizontalWordData.Add(new WordData(wordData));
                }
                else
                {
                    copyVerticalWordData.Add(new WordData(wordData));
                }

                copyAllWordData.Add(new WordData(wordData));
            }
            AllWordData        = copyAllWordData;
            HorizontalWordData = copyHorizontalWordData;
            VerticalWordData   = copyVerticalWordData;
        }
コード例 #2
0
ファイル: Crozzle.cs プロジェクト: PatelKeviin/Crozzle-App
        private void CreateCrozzleColumns(WordDataList wordDataList)
        {
            // Create a List to store String arrays, one String[] for each column, one String for each letter.
            CrozzleColumns = new List <String[]>();

            // Create and store empty columns into the list.
            for (int i = 0; i < Columns; i++)
            {
                String[] column = new String[Rows];
                for (int j = 0; j < column.Length; j++)
                {
                    column[j] = " ";
                }
                CrozzleColumns.Add(column);
            }

            // Store VERTICAL words into the columns.
            foreach (WordData vWordData in wordDataList.VerticalWordData)
            {
                if (vWordData.Location.Row >= 1 && vWordData.Location.Row <= Rows &&
                    vWordData.Location.Column >= 1 && vWordData.Location.Column <= Columns)
                {
                    // Store each letter into the approriate column.
                    String[] column = CrozzleColumns[vWordData.Location.Column - 1];
                    int      row    = vWordData.Location.Row - 1;
                    foreach (Char c in vWordData.Letters)
                    {
                        if (row < Rows)
                        {
                            column[row++] = new String(c, 1);
                        }
                    }
                }
            }

            // Store HORIZONTAL words into the columns.
            foreach (WordData hWordData in wordDataList.HorizontalWordData)
            {
                if (hWordData.Location.Row >= 1 && hWordData.Location.Row <= Rows &&
                    hWordData.Location.Column >= 1 && hWordData.Location.Column <= Columns)
                {
                    // Store each letter into the ith column, but the same row location.
                    int i = hWordData.Location.Column - 1;
                    foreach (Char c in hWordData.Letters)
                    {
                        if (i < Columns)
                        {
                            String[] column = CrozzleColumns[i];
                            column[hWordData.Location.Row - 1] = new String(c, 1);
                            i++;
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: Crozzle.cs プロジェクト: PatelKeviin/Crozzle-App
        private void CreateCrozzleRows(WordDataList wordDataList)
        {
            // Create a List to store String arrays, one String[] for each row, one String for each letter.
            CrozzleRows = new List <String[]>();

            // Create and store empty rows into the list.
            for (int i = 0; i < Rows; i++)
            {
                String[] row = new String[Columns];
                for (int j = 0; j < row.Length; j++)
                {
                    row[j] = " ";
                }
                CrozzleRows.Add(row);
            }

            // Store HORIZONTAL words into the rows.
            foreach (WordData hWordData in wordDataList.HorizontalWordData)
            {
                if (hWordData.Location.Row >= 1 && hWordData.Location.Row <= Rows &&
                    hWordData.Location.Column >= 1 && hWordData.Location.Column <= Columns)
                {
                    // Store each letter into the approriate row.
                    String[] row = CrozzleRows[hWordData.Location.Row - 1];
                    int      col = hWordData.Location.Column - 1;
                    foreach (Char c in hWordData.Letters)
                    {
                        if (col < Columns)
                        {
                            row[col++] = new String(c, 1);
                        }
                    }
                }
            }

            // Store VERTICAL words into the rows.
            foreach (WordData vWordData in wordDataList.VerticalWordData)
            {
                if (vWordData.Location.Row >= 1 && vWordData.Location.Row <= Rows &&
                    vWordData.Location.Column >= 1 && vWordData.Location.Column <= Columns)
                {
                    // Store each letter into the ith row, but the same column location.
                    int i = vWordData.Location.Row - 1;
                    foreach (Char c in vWordData.Letters)
                    {
                        if (i < Rows)
                        {
                            String[] row = CrozzleRows[i];
                            row[vWordData.Location.Column - 1] = new String(c, 1);
                            i++;
                        }
                    }
                }
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public static List <WordData> GetBestWordData(Grid grid, WordDataList wordDataList, Configuration configuration)
        {
            List <WordData> validWordDataList = new List <WordData>();

            foreach (WordData wordData in wordDataList.AllWordData)
            {
                if (grid.IsValid(wordData))
                {
                    validWordDataList.Add(wordData);
                }
            }

            foreach (WordData wordData in validWordDataList)
            {
                wordData.Score = 0;

                // Increase the score for the word.
                wordData.Score += configuration.PointsPerWord;

                // Increase the score for intersecting letters.
                List <Char> intersectingLetters = new List <Char>();
                if (wordData.IsHorizontal)
                {
                    intersectingLetters = grid.GridSequences.GetVerticalIntersections(wordData);
                }
                else
                {
                    intersectingLetters = grid.GridSequences.GetHorizontalIntersections(wordData);
                }
                foreach (Char letter in intersectingLetters)
                {
                    wordData.Score += configuration.IntersectingPointsPerLetter[(int)letter - (int)'A'];
                }

                // Get all letters from the word
                List <Char> allLetters = new List <Char>();
                foreach (Char letter in wordData.Letters)
                {
                    allLetters.Add(letter);
                }

                // Remove each intersecting letter from allLetters.
                List <Char> nonIntersectingLetters = allLetters;
                foreach (Char letter in intersectingLetters)
                {
                    nonIntersectingLetters.Remove(letter);
                }

                // Increase the score for non-intersecting letters.
                foreach (Char letter in nonIntersectingLetters)
                {
                    wordData.Score += configuration.NonIntersectingPointsPerLetter[(int)letter - (int)'A'];
                }
            }

            int maxScore = 0;

            foreach (WordData wordData in validWordDataList)
            {
                if (wordData.Score > maxScore)
                {
                    maxScore = wordData.Score;
                }
            }

            List <WordData> bestWordData = new List <WordData>();

            foreach (WordData wordData in validWordDataList)
            {
                if (wordData.Score == maxScore)
                {
                    bestWordData.Add(wordData);
                }
            }

            return(bestWordData);
        }
コード例 #6
0
 public Node(int rows, int columns, Configuration configuration, List <String> originalWordsList)
 {
     Grid         = new Grid(rows, columns, configuration);
     WordDataList = new WordDataList(rows, columns, originalWordsList);
 }
コード例 #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>();

            // 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);
        }
コード例 #8
0
ファイル: Grid.cs プロジェクト: PatelKeviin/Crozzle-App
        public void Insert(WordData wordData)
        {
            if (wordData.Location.Row >= 1 && wordData.Location.Row <= Rows &&
                wordData.Location.Column >= 1 && wordData.Location.Column <= Columns)
            {
                if (wordData.Orientation.Direction == Orientation.Row)
                {
                    // Store the letter into the approriate row.
                    String[] row = GridRows[wordData.Location.Row - 1];
                    int      col = wordData.Location.Column - 1;
                    foreach (Char c in wordData.Letters)
                    {
                        if (col < Columns)
                        {
                            row[col++] = new String(c, 1);
                        }
                    }

                    // Store each letter into the ith column, but the same row location.
                    int j = wordData.Location.Column - 1;
                    foreach (Char c in wordData.Letters)
                    {
                        if (j < Columns)
                        {
                            String[] column = GridColumns[j];
                            column[wordData.Location.Row - 1] = new String(c, 1);
                            j++;
                        }
                    }

                    HorizontalWordDataList.Add(wordData);
                }
                else
                {
                    // Store the letter into the ith row, but the same column location.
                    int j = wordData.Location.Row - 1;
                    foreach (Char c in wordData.Letters)
                    {
                        if (j < Rows)
                        {
                            String[] currentRow = GridRows[j];
                            currentRow[wordData.Location.Column - 1] = new String(c, 1);
                            j++;
                        }
                    }

                    // Store each letter into the approriate column.
                    String[] column = GridColumns[wordData.Location.Column - 1];
                    int      row    = wordData.Location.Row - 1;
                    foreach (Char c in wordData.Letters)
                    {
                        if (row < Rows)
                        {
                            column[row++] = new String(c, 1);
                        }
                    }

                    VerticalWordDataList.Add(wordData);
                }

                GridSequences = new CrozzleSequences(GridRows, GridColumns, Configuration);
                WordDataList.Add(wordData);
            }
        }
コード例 #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);

            // 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);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #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 <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);
        }