private bool PuzzleWordIsValid(PuzzleWord word, int startCol, int startRow, int directionCol, int directionRow)
        {
            for (int i = 0; i < word.Length; i++)
            {
                var currentCol = startCol + directionCol * i;
                var currentRow = startRow + directionRow * i;

                if ((currentCol >= ControlPresent.GetLength(0)) || (currentRow >= ControlPresent.GetLength(1)))
                {
                    return(false);
                }

                if (ControlPresent[currentCol, currentRow])
                {
                    //Check expected letter if the textbox does exist
                    var theBoxList = TheGrid.Children.Cast <UIElement>().Where(k => (Grid.GetRow(k) == currentRow) && (Grid.GetColumn(k) == currentCol)).ToList();
                    var theBox     = theBoxList[theBoxList.Count - 1] as PuzzleLetter; //Some grid boxes contain labels as well as textboxes. The textboxes are always added last

                    //Check that the expected letter in the textbox is equal to the expected letter we were trying to add
                    //If they aren't then the puzzle isn't valid and shouldn't be loaded
                    if (Char.ToUpper(word.Word[i]) != theBox.ExpectedLetter)
                    {
                        return(false);
                    }
                }
            }
            //If there were no conflicts then the word should fit on the grid
            return(true);
        }
        public void DrawPuzzleWord(PuzzleWord word, bool letterVisible)
        {
            int startCol = word.StartColumn;
            int startRow = word.StartRow;

            //determine the direction
            int directionCol = Direction.across == word.WordDirection ? 1 : 0;
            int directionRow = Direction.down == word.WordDirection ? 1 : 0;

            //Check whether the puzzleword will fit on the grid with the existing Puzzleboxes
            if (PuzzleWordIsValid(word, startCol, startRow, directionCol, directionRow))
            {
                //foreach character in the word
                foreach (char letter in word.GetLetters())
                {
                    DrawLetterBox(word, startCol, startRow, letter, letterVisible);

                    //move in the direction
                    startCol += directionCol;
                    startRow += directionRow;
                }

                word.IsValid = true;
            }
            else
            {
                //Throw a warning for this invalid word
                InvalidWords.Add(word);
                word.IsValid = false;
            }
        }
 public void PopulateFrom(PuzzleWord source)
 {
     ClueNumber    = source.ClueNumber;
     Clue          = source.Clue;
     WordDirection = source.WordDirection;
     StartColumn   = source.StartColumn;
     StartRow      = source.StartRow;
 }
        private void EditExistingBox(PuzzleWord word, int startCol, int startRow)
        {
            //Don't need to check if the expected letters are the same because we already checked them
            var          theBoxList = TheGrid.Children.Cast <UIElement>().Where(k => (Grid.GetRow(k) == startRow) && (Grid.GetColumn(k) == startCol)).ToList();
            PuzzleLetter theBox     = theBoxList[theBoxList.Count - 1] as PuzzleLetter; //Some grid boxes contain labels as well as textboxes. The textboxes are always added last

            if ((HeaderTemp.GetDefaultNumber(theBox) == "") && ((startCol == word.StartColumn) && (startRow == word.StartRow)))
            {
                HeaderTemp.SetDefaultNumber(theBox, word.ClueNumber.ToString());
            }
        }
        private void DrawLetterBox(PuzzleWord word, int startCol, int startRow, char letter, bool letterVisible)
        {
            //Check the textbox doesn't exist
            if (ControlPresent[startCol, startRow])
            {
                EditExistingBox(word, startCol, startRow);
            }
            else
            {
                PuzzleLetter box = CreateNewBox(word, startCol, startRow, letter, letterVisible);

                TheGrid.Children.Add(box);
                ControlPresent[startCol, startRow] = true;
            }
        }
        private static PuzzleLetter CreateNewBox(PuzzleWord word, int startCol, int startRow, char letter, bool letterVisible)
        {
            string cornerNumber = "";

            if ((startCol == word.StartColumn) && (startRow == word.StartRow))
            {
                cornerNumber = word.ClueNumber.ToString();
            }
            //Create textbox because it doesn't already exist
            PuzzleLetter box = new PuzzleLetter(letter, cornerNumber);

            if (letterVisible)
            {
                box.Text = box.ExpectedLetter.ToString();
            }

            //determine the starting position
            Grid.SetColumn(box, startCol);
            Grid.SetRow(box, startRow);
            return(box);
        }
 public void Remove(PuzzleWord oldword)
 {
     Words.Remove(oldword);
 }
 public void Add(PuzzleWord newWord)
 {
     Words.Add(newWord);
 }