/// <summary> /// Counts the number of intersections in a word. /// </summary> /// <param name="word"></param> /// <returns></returns> private int WordIntersectionCount(ActiveWord word) { int count = 0; int rowIndex = word.RowStart; int colIndex = word.ColStart; for (int letterIndex = 0; letterIndex < word.Length; letterIndex++) { // If there is an element if (_BoardGrid[rowIndex, colIndex] != null) { if (_BoardGrid[rowIndex, colIndex].HorizontalWord != null && _BoardGrid[rowIndex, colIndex].VerticalWord != null) { count++; } } // Increment board index if (word.Orientation == Config.HorizontalKeyWord) { colIndex++; } else { rowIndex++; } } return(count); }
/// <summary> /// Adds a word to the magic board. /// </summary> /// <param name="word"></param> public void AddMagicWord(ActiveWord word) { AddWord(word); minRow = Math.Min(word.RowStart, minRow); maxRow = Math.Max(word.RowEnd, maxRow); minCol = Math.Min(word.ColStart, minCol); maxCol = Math.Max(word.ColEnd, maxCol); }
/// <summary> /// Add a word to the Crozzle grid. /// </summary> /// <param name="word"></param> public void AddWord(ActiveWord word) { int row_index = word.RowStart; int col_index = word.ColStart; int group = ++_GroupCount; // Add each letter as an element for (int letterIndex = 0; letterIndex < word.Length; letterIndex++) { // If no element in grid square add a new one if (_BoardGrid[row_index, col_index] == null) { _BoardGrid[row_index, col_index] = new Element(word[letterIndex], word, letterIndex, group); } // Else add intersecting element else { // Check letters are the same if (_BoardGrid[row_index, col_index].Letter != word[letterIndex]) { throw new Exception("Cannot add word " + word + " as the letter " + word[letterIndex] + " cannot be placed onto the letter " + _BoardGrid[row_index, col_index].Letter + " at [" + row_index + "," + col_index + "]"); } // Check not overlapping word of same orientation if ((word.Orientation == Config.HorizontalKeyWord && _BoardGrid[row_index, col_index].HorizontalWord != null) || (word.Orientation == Config.VerticalKeyWord && _BoardGrid[row_index, col_index].VerticalWord != null)) { throw new Exception("Cannot add word " + word + " as it is overlapping another " + word.Orientation + " word."); } // Check if groups can be combined if (_BoardGrid[row_index, col_index].Group != group) { group = CombineGroups(_BoardGrid[row_index, col_index].Group, group); } // Add the element to the grid if (word.Orientation == Config.HorizontalKeyWord) { _BoardGrid[row_index, col_index] = new Element(word[letterIndex], word, letterIndex, _BoardGrid[row_index, col_index].VerticalWord, _BoardGrid[row_index, col_index].VerticalWordLetterIndex, group); } else { _BoardGrid[row_index, col_index] = new Element(word[letterIndex], _BoardGrid[row_index, col_index].HorizontalWord, _BoardGrid[row_index, col_index].HorizontalWordLetterIndex, word, letterIndex, group); } } if (word.Orientation == Config.HorizontalKeyWord) { col_index++; } else { row_index++; } } // Add the word to the words used list _ActiveWordsList.Add(word); }
/// <summary> /// A new nullified Crozzle grid element. /// </summary> public Element() { _Letter = ' '; _HorizontalWord = null; _HorizontalWordLetterIndex = -1; _VerticalWord = null; _VerticalWordLetterIndex = -1; _Group = 0; _Score = 0; }
/// <summary> /// A new Crozzle grid element (intersecting) /// </summary> /// <param name="letter"></param> /// <param name="horizontal word"></param> /// <param name="horizontal word letter index"></param> /// <param name="vertical word"></param> /// <param name="vertical word letter index"></param> /// <param name="group"></param> public Element(char letter, ActiveWord horizontalWord, int horizontalWord_letterIndex, ActiveWord verticalWord, int verticalWord_letterIndex, int group) { _Letter = letter; _HorizontalWord = horizontalWord; _HorizontalWordLetterIndex = horizontalWord_letterIndex; _VerticalWord = verticalWord; _VerticalWordLetterIndex = verticalWord_letterIndex; _Group = group; _Score = CalcScore(); }
/// <summary> /// Sets the element in a word to a new element. /// </summary> /// <param name="word"></param> /// <param name="index"></param> /// <param name="element"></param> public void SetElementIn(ActiveWord word, int index, Element element) { int row = word.RowStart; int col = word.ColStart; if (word.Orientation == Config.HorizontalKeyWord) { col += index; } else { row += index; } _BoardGrid[row, col] = element; }
/// <summary> /// REturns the element a word at a specified index. /// </summary> /// <param name="word"></param> /// <param name="index"></param> /// <returns></returns> public Element ElementIn(ActiveWord word, int index) { int row = word.RowStart; int col = word.ColStart; if (word.Orientation == Config.HorizontalKeyWord) { col += index; } else { row += index; } return(_BoardGrid[row, col]); }
/// <summary> /// A new Crozzle grid element (non-intersecting). /// </summary> /// <param name="letter"></param> /// <param name="word"></param> /// <param name="word letter index"></param> /// <param name="group"></param> public Element(char letter, ActiveWord word, int word_letterIndex, int group) { _Letter = letter; if (word.Orientation == Config.HorizontalKeyWord) { _HorizontalWord = word; _HorizontalWordLetterIndex = word_letterIndex; _VerticalWord = null; _VerticalWordLetterIndex = -1; } else { _HorizontalWord = null; _HorizontalWordLetterIndex = -1; _VerticalWord = word; _VerticalWordLetterIndex = word_letterIndex; } _Group = group; _Score = CalcScore(); }