コード例 #1
0
        public static Boolean TryParse(List <String> originalWordDataList, Crozzle aCrozzle, out WordDataList aWordDataList)
        {
            List <WordData> aList = new List <WordData>();

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

            foreach (String line in originalWordDataList)
            {
                WordData aWordData;
                if (WordData.TryParse(line, aCrozzle, out aWordData))
                {
                    aWordDataList.Add(aWordData);
                }
                else
                {
                    Errors.AddRange(WordData.Errors);
                }
            }

            aWordDataList.Valid = Errors.Count == 0;
            return(aWordDataList.Valid);
        }
コード例 #2
0
ファイル: WordData.cs プロジェクト: mcnorth/Crozzle-Project
        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)
                        {
                            rowValue    = originalWordData[1];
                            columnValue = originalWordData[3];
                        }
                        else if (anOrientation.IsVertical)
                        {
                            rowValue    = originalWordData[3];
                            columnValue = originalWordData[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;
                        }
                    }

                    // Check that the 3rd field is alphabetic, and in the wordlist.
                    if (originalWordData.Length >= NumberOfFields - 1)
                    {
                        String originalWord = originalWordData[2];
                        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);
        }