public Grid(Grid grid) { Rows = grid.Rows; Columns = grid.Columns; // copy grid rows List <String[]> copyGridRows = new List <String[]>(); for (int i = 0; i < grid.GridRows.Count; i++) { String[] copyStringArray = new String[grid.GridRows[i].Length]; Array.Copy(grid.GridRows[i], copyStringArray, grid.GridRows[i].Length); copyGridRows.Add(copyStringArray); } GridRows = copyGridRows; // copy grid columns List <String[]> copyGridColumns = new List <String[]>(); for (int i = 0; i < grid.GridColumns.Count; i++) { String[] copyStringArray = new String[grid.GridColumns[i].Length]; Array.Copy(grid.GridColumns[i], copyStringArray, grid.GridColumns[i].Length); copyGridColumns.Add(copyStringArray); } GridColumns = copyGridColumns; // copy grid sequences GridSequences = new CrozzleSequences(grid.GridSequences); // copy WordDataList List <WordData> copyWordDataList = new List <WordData>(); foreach (WordData wordData in grid.WordDataList) { copyWordDataList.Add(new WordData(wordData)); } WordDataList = copyWordDataList; // copy HorizontalWordDataList List <WordData> copyHorizontalWordDataList = new List <WordData>(); foreach (WordData wordData in grid.HorizontalWordDataList) { copyHorizontalWordDataList.Add(new WordData(wordData)); } HorizontalWordDataList = copyHorizontalWordDataList; // copy VerticalWordDataList List <WordData> copyVerticalWordDataList = new List <WordData>(); foreach (WordData wordData in grid.VerticalWordDataList) { copyVerticalWordDataList.Add(new WordData(wordData)); } VerticalWordDataList = copyVerticalWordDataList; // shallow copying Configuration (as it is only used for readOnly purposes) Configuration = grid.Configuration; }
private void EmptyGrid(Configuration configuration) { // Create a List to store String arrays, one String[] for each row, one String for each letter. GridRows = 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] = " "; } GridRows.Add(row); } // Create a List to store String arrays, one String[] for each column, one String for each letter. GridColumns = 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] = " "; } GridColumns.Add(column); } WordDataList = new List <WordData>(); GridSequences = new CrozzleSequences(GridRows, GridColumns, configuration); HorizontalWordDataList = new List <WordData>(); VerticalWordDataList = new List <WordData>(); }
public void Validate() { CrozzleGridErrors = new List <String>(); // Indicate that validity has been attempted. ValidityChecked = true; // Get all sequences of 2 or more letters. CrozzleSequences = new CrozzleSequences(CrozzleRows, CrozzleColumns, Configuration); // Check that the number of rows is within limits. if (Rows < Configuration.MinimumNumberOfRows || Rows > Configuration.MaximumNumberOfRows) { CrozzleGridErrors.Add(String.Format(CrozzleFileErrors.RowCountError, Rows, Configuration.MinimumNumberOfRows, Configuration.MaximumNumberOfRows)); } // Check that the number of columns is within limits. if (Columns < Configuration.MinimumNumberOfColumns || Columns > Configuration.MaximumNumberOfColumns) { CrozzleGridErrors.Add(String.Format(CrozzleFileErrors.ColumnCountError, Columns, Configuration.MinimumNumberOfColumns, Configuration.MaximumNumberOfColumns)); } // Check that the number of words is within limits. // Check that the number of horizontal words is within limits. if (Node.Grid.HorizontalWordDataList.Count < Configuration.MinimumHorizontalWords || Node.Grid.HorizontalWordDataList.Count > Configuration.MaximumHorizontalWords) { CrozzleGridErrors.Add(String.Format(CrozzleErrors.HorizontalWordCountError, Node.Grid.HorizontalWordDataList.Count, Configuration.MinimumHorizontalWords, Configuration.MaximumHorizontalWords)); } // Check that the number of vertical words is within limits. if (Node.Grid.VerticalWordDataList.Count < Configuration.MinimumVerticalWords || Node.Grid.VerticalWordDataList.Count > Configuration.MaximumVerticalWords) { CrozzleGridErrors.Add(String.Format(CrozzleErrors.VerticalWordCountError, Node.Grid.VerticalWordDataList.Count, Configuration.MinimumVerticalWords, Configuration.MaximumVerticalWords)); } // Check that the number of vertical words that intersect a horizontal word is within limits. CrozzleSequences.CheckHorizontalIntersections(Configuration.MinimumIntersectionsInHorizontalWords, Configuration.MaximumIntersectionsInHorizontalWords); // Check that the number of horizontal words that intersect a vertical word is within limits. CrozzleSequences.CheckVerticalIntersections(Configuration.MinimumIntersectionsInVerticalWords, Configuration.MaximumIntersectionsInVerticalWords); // Check that the number of duplicate words is within limits. CrozzleSequences.CheckDuplicateWords(Configuration.MinimumNumberOfTheSameWord, Configuration.MaximumNumberOfTheSameWord); // Check that the number of groups of connected words is within the limit. CrozzleSequences.CheckConnectivity(Configuration.MinimumNumberOfGroups, Configuration.MaximumNumberOfGroups, CrozzleRows, CrozzleColumns); // Check that each sequence is in the wordlist. CrozzleSequences.FindMissingWords(WordList); // Is this crozzle valid? CrozzleValid = true; if (CrozzleSequences.ErrorsDetected) { CrozzleValid = false; CrozzleGridErrors.AddRange(CrozzleSequences.ErrorMessages); } }
public int CrozzleScore() { int score = 0; if (CrozzleSequences == null) { return(-1); } // Increase the score for each word. score += CrozzleSequences.Count * Configuration.PointsPerWord; // Increase the score for intersecting letters. List <Char> intersectingLetters = CrozzleSequences.GetIntersectingLetters(); foreach (Char letter in intersectingLetters) { score += Configuration.IntersectingPointsPerLetter[(int)letter - (int)'A']; } // Get all letters. List <Char> allLetters = new List <Char>(); foreach (String[] letters in CrozzleRows) { foreach (String letter in letters) { if (letter[0] != ' ') { allLetters.Add(letter[0]); } } } // 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) { score += Configuration.NonIntersectingPointsPerLetter[(int)letter - (int)'A']; } return(score); }
public CrozzleSequences(CrozzleSequences crozzleSequences) { // copy Sequences List <WordData> copySequences = new List <WordData>(); // copy HorizontalSequences List <WordData> copyHorizontalSequences = new List <WordData>(); // copy VerticalSequences List <WordData> copyVerticalSequences = new List <WordData>(); foreach (WordData wordData in crozzleSequences.Sequences) { if (wordData.IsHorizontal) { copyHorizontalSequences.Add(new WordData(wordData)); } else { copyVerticalSequences.Add(new WordData(wordData)); } copySequences.Add(new WordData(wordData)); } Sequences = copySequences; HorizontalSequences = copyHorizontalSequences; VerticalSequences = copyVerticalSequences; // copy ErrorMessages List <String> copyErrorMessages = new List <String>(); foreach (String error in crozzleSequences.ErrorMessages) { copyErrorMessages.Add(String.Copy(error)); } ErrorMessages = copyErrorMessages; // shallow copying Configuration (as it is only used for readOnly purposes) Configuration = crozzleSequences.Configuration; }
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); } }
/// <summary> /// Validate each generated crozzle /// </summary> /// <param name="testGrid"></param> public bool Validate(Grid testGrid) { CrozzleRows = new List <String[]>(); CrozzleColumns = new List <String[]>(); // Get all row data for (int r = 0; r < CrozzleRow; r++) { String[] row = new String[CrozzleColumn]; for (int c = 0; c < row.Length; c++) { row[c] = testGrid.GridCoordinate[r, c].ToString(); } CrozzleRows.Add(row); } // Get all column data for (int c = 0; c < CrozzleColumn; c++) { String[] col = new String[CrozzleRow]; for (int r = 0; r < col.Length; r++) { col[r] = testGrid.GridCoordinate[r, c].ToString(); } CrozzleColumns.Add(col); } CrozzleSequences = new CrozzleSequences(CrozzleRows, CrozzleColumns, Configuration); // Check that the number of rows is within limits. if (CrozzleRow < Configuration.MinimumNumberOfRows || CrozzleRow > Configuration.MaximumNumberOfRows) { //CrozzleGridErrors.Add(String.Format(CrozzleFileErrors.RowCountError, Rows, Configuration.MinimumNumberOfRows, Configuration.MaximumNumberOfRows)); return(false); } // Check that the number of columns is within limits. if (CrozzleColumn < Configuration.MinimumNumberOfColumns || CrozzleColumn > Configuration.MaximumNumberOfColumns) { //CrozzleGridErrors.Add(String.Format(CrozzleFileErrors.ColumnCountError, Columns, Configuration.MinimumNumberOfColumns, Configuration.MaximumNumberOfColumns)); return(false); } // Check that the number of vertical words that intersect a horizontal word is within limits. CrozzleSequences.CheckHorizontalIntersections(Configuration.MinimumIntersectionsInHorizontalWords, Configuration.MaximumIntersectionsInHorizontalWords); // Check that the number of horizontal words that intersect a vertical word is within limits. CrozzleSequences.CheckVerticalIntersections(Configuration.MinimumIntersectionsInVerticalWords, Configuration.MaximumIntersectionsInVerticalWords); // Check that the number of duplicate words is within limits. CrozzleSequences.CheckDuplicateWords(Configuration.MinimumNumberOfTheSameWord, Configuration.MaximumNumberOfTheSameWord); // Check that the number of groups of connected words is within the limit. CrozzleSequences.CheckConnectivity(Configuration.MinimumNumberOfGroups, Configuration.MaximumNumberOfGroups, CrozzleRows, CrozzleColumns); if (CrozzleSequences.ErrorsDetected) { return(false); } else { return(true); } }