public WordDataList(int rows, int columns, List <String> originalWordList) { OriginalWordList = originalWordList; AllWordData = new List <WordData>(); HorizontalWordData = new List <WordData>(); VerticalWordData = new List <WordData>(); int wordID = 0; foreach (String word in originalWordList) { // unique for each word in the sequence txt file ++wordID; // All horizontal word possibilities for (int row = 1; row < rows + 1; row++) { for (int column = 1; column < columns + 1; column++) { if (column + (word.Length - 1) <= columns) { WordData wordData = new WordData(Orientation.Row, row, column, word, wordID); this.AllWordData.Add(wordData); this.HorizontalWordData.Add(wordData); } } } // All vertical word possibilities for (int column = 1; column < columns + 1; column++) { for (int row = 1; row < rows + 1; row++) { if (row + (word.Length - 1) <= rows) { WordData wordData = new WordData(Orientation.Column, row, column, word, wordID); this.AllWordData.Add(wordData); this.VerticalWordData.Add(wordData); } } } } }
private void AddVerticalSequences(List <String[]> crozzleColumns) { int columnNumber = 0; int rowIndex; String column; foreach (String[] crozzleColumn in crozzleColumns) { columnNumber++; rowIndex = 0; // Place all letters into one string, so that we can split it later. column = ""; foreach (String letter in crozzleColumn) { column = column + letter; } // Use split to collect all sequences of letters. String[] letterSequences = column.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Collect and store data about each letter sequence of length > 1, as a sequence of one letter is not a word. foreach (String sequence in letterSequences) { if (sequence.Length > 1) { // Set row values. int rowNumber = column.IndexOf(sequence, rowIndex) + 1; // Collect data about the word, and // update the index for the next substring search. WordData word = new WordData(WordData.OrientationColumn, rowNumber, columnNumber, sequence); rowIndex = word.Location.Row - 1 + sequence.Length; // Store data about the word. Sequences.Add(word); VerticalSequences.Add(word); } } } }
private void AddHorizontalSequences(List <String[]> crozzleRows) { int rowNumber = 0; int columnIndex; String row; foreach (String[] crozzleRow in crozzleRows) { rowNumber++; columnIndex = 0; // Place all letters into one string, so that we can split it later. row = ""; foreach (String letter in crozzleRow) { row = row + letter; } // Use split to collect all sequences of letters. String[] letterSequences = row.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Collect and store data about each letter sequence of length > 1, as a sequence of one letter is not a word. foreach (String sequence in letterSequences) { if (sequence.Length > 1) { // Set column values. int columnNumber = row.IndexOf(sequence, columnIndex) + 1; // Collect data about the word, and // update the index for the next substring search. WordData word = new WordData(WordData.OrientationRow, rowNumber, row.IndexOf(sequence, columnIndex) + 1, sequence); columnIndex = word.Location.Column - 1 + sequence.Length; // Store data about the word. Sequences.Add(word); HorizontalSequences.Add(word); } } } }
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); }
private void MapLetters(List <String[]> crozzleRows, WordData wordData) { int row; int column; row = 0; foreach (String[] letters in crozzleRows) { row++; column = 0; foreach (String letter in letters) { column++; if (letter[0] != ' ') { Map[row, column] = true; } } } if (wordData.Orientation.IsHorizontal) { int col = wordData.Location.Column; foreach (Char letter in wordData.Letters) { Map[wordData.Location.Row, col++] = true; } } else { int rOw = wordData.Location.Row; foreach (Char letter in wordData.Letters) { Map[rOw++, wordData.Location.Column] = true; } } }
public Boolean IsValid(WordData wordData) { GridSequences.GridWordDataErrorsDetected = false; // Check that if the word data is not overlapping with opposite oriented words if (wordData.Orientation.IsHorizontal) { GridSequences.CheckVerticalIntersectionOverlapping(wordData); } else { GridSequences.CheckHorizontalIntersectionOverlapping(wordData); } // Check that if the word data is not overlapping with words having same orientation AND // Check that if the word data is not touching other words havings same/opposite orientation if (GridSequences.GridWordDataErrorsDetected == false) { GridSequences.CheckTouchingWords(wordData); } // Check word group count if (GridSequences.GridWordDataErrorsDetected == false) { GridSequences.CheckGroupCount(GridRows, GridColumns, wordData); } // If the grid is valid? if (GridSequences.GridWordDataErrorsDetected) { return(false); } else { return(true); } }
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); }
public CrozzleMap(List <String[]> crozzleRows, List <String[]> crozzleColumns, WordData wordData) { // Create a 2D array of Boolean that is initialised to false. // For coding "neatness", it has an extra row at the top and one at the bottom, and // an extra column on the left and one on the right int numberOfRows = crozzleRows.Count + extraRows; int numberOfColumns = crozzleColumns.Count + extraColumns; Map = new Boolean[numberOfRows, numberOfColumns]; // Store a true value in the map at the same location as each letter this.MapLetters(crozzleRows, wordData); }
public void CheckGroupCount(List <String[]> gridRows, List <String[]> gridColumns, WordData wordData) { CrozzleMap map = new CrozzleMap(gridRows, gridColumns, wordData); int count = map.GroupCount(); if (count < Configuration.MinimumNumberOfGroups || count > Configuration.MaximumNumberOfGroups) { GridWordDataErrors = true; } }
private void CheckTouchingVerticalWordData(WordData wordData) { foreach (WordData vwordData in VerticalSequences) { if (vwordData.Location.Column == wordData.Location.Column) { if (vwordData.Location.Row < wordData.Location.Row - 1 && vwordData.Location.Row + vwordData.Letters.Length >= wordData.Location.Row) { GridWordDataErrors = true; } else if (vwordData.Location.Row >= wordData.Location.Row - 1 && vwordData.Location.Row <= wordData.Location.Row + wordData.Letters.Length) { GridWordDataErrors = true; } } else if (vwordData.Location.Column == wordData.Location.Column - 1 || vwordData.Location.Column == wordData.Location.Column + 1) { if (vwordData.Location.Row < wordData.Location.Row && vwordData.Location.Row + (vwordData.Letters.Length - 1) >= wordData.Location.Row) { if (vwordData.Location.Row + (vwordData.Letters.Length - 1) == wordData.Location.Row) { Boolean valid = false; foreach (WordData hWordData in HorizontalSequences) { if (hWordData.Location.Row == wordData.Location.Row) { if (Math.Min(vwordData.Location.Column, wordData.Location.Column) >= hWordData.Location.Column && Math.Max(vwordData.Location.Column, wordData.Location.Column) <= hWordData.Location.Column + (hWordData.Letters.Length - 1)) { valid = true; } } } if (!valid) { GridWordDataErrors = true; } } else { GridWordDataErrors = true; } } else if (vwordData.Location.Row >= wordData.Location.Row && vwordData.Location.Row <= wordData.Location.Row + (wordData.Letters.Length - 1)) { if (wordData.Location.Row + (wordData.Letters.Length - 1) == vwordData.Location.Row) { Boolean valid = false; foreach (WordData hWordData in HorizontalSequences) { if (hWordData.Location.Row == vwordData.Location.Row) { if (Math.Min(vwordData.Location.Column, wordData.Location.Column) >= hWordData.Location.Column && Math.Max(vwordData.Location.Column, wordData.Location.Column) <= hWordData.Location.Column + (hWordData.Letters.Length - 1)) { valid = true; } } } if (!valid) { GridWordDataErrors = true; } } else { GridWordDataErrors = true; } } } } foreach (WordData hwordData in HorizontalSequences) { if (wordData.Location.Column >= hwordData.Location.Column && wordData.Location.Column <= hwordData.Location.Column + (hwordData.Letters.Length - 1)) { if (wordData.Location.Row + (wordData.Letters.Length - 1) == hwordData.Location.Row - 1 || wordData.Location.Row == hwordData.Location.Row + 1) { GridWordDataErrors = true; } } else if (wordData.Location.Column == hwordData.Location.Column - 1 || wordData.Location.Column == hwordData.Location.Column + hwordData.Letters.Length) { if (hwordData.Location.Row >= wordData.Location.Row && hwordData.Location.Row <= wordData.Location.Row + (wordData.Letters.Length - 1)) { GridWordDataErrors = true; } } } }
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); } }
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); }
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); }