/** Purpose : Initialization of the global variable * */ private void Initialize() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { cellObject[i, j] = new CellDetails(FindName("Button" + i + j) as Button, i, j); } } goAnimation = new DispatcherTimer(DispatcherPriority.Send); goAnimation.Interval = TimeSpan.FromMilliseconds(30); goAnimation.Tick += GoAnimation_Tick; selectionAnimation = new DispatcherTimer(DispatcherPriority.Send); selectionAnimation.Interval = TimeSpan.FromMilliseconds(30); selectionAnimation.Tick += SelectionAnimation_Tick; }
/** Purpose : Selects the next possible letters and pushes them into the stack * * First take the previous letter and assume that the letter is not found * Now, search all possible locations and add each of these which can be in the stacktree to the stack * If we have found atleast one match return that a letter has been found otherwise return false * Note : * * It is important to prevent the ArrayIndexOutOfBounds exception * * Prevent locked items from being pushed */ private bool IsNextLetterAvilable(char nextLetter) { CellDetails previousLetter = cellBuffer.Peek(); bool letterFound = false; for (int i = previousLetter.rowIndex - 1; i <= previousLetter.rowIndex + 1; i++) { for (int j = previousLetter.columnIndex - 1; j <= previousLetter.columnIndex + 1; j++) { if (i < 4 && j < 4 && i > -1 && j > -1) { if (cellObject[i, j].isLocked == false && nextLetter == cellObject[i, j].letter) { cellBuffer.Push(cellObject[i, j]); letterFound = true; } } } } return(letterFound); }
/** Purpose : Finds and optionally stores the sequence of characters * * Clear the buffer * * Push the first Box into the stack and set it as used * * Starting from the next element till * * We find the word - marked by i reaching the end * * We don't find the word - marked by the stack becoming empty * * Check whether the next element is in the sequence * * Goto the next character of the word * * Use the element at the top * Otherwise * * The letter used for finding the next letter will not be correct - so check other possibilities * * Pop out the top of the stack and set it as not used * * Now, pop out all the elements until the previously popped element and un-use it * * Decrement the letter index so as to search the previous element only when they are not equal * * * Check whether the no of blocks in the stack is greater than 0 * * If so then the word we are searching for is found * * If this function is called by the user generated selection then * * For the animation purpose we must store the original order of the sequence * * So, every element that is in the stack and which is locked is a part of the sequence * * Moreover when popped out they will be in the reverse order * * So, store it in another stack - reverse(reverse) = forward * * Unlock all the Boxes so that a new word can be found * * Save this local stack to the original stack as we can access it anywhere - global variable * * Otherwise * * This was called by the computer for only listing purpose * * So there is no need to save the order * * Hence simply empty the stack and unlock all the items * * return true * otherwise * * The word we are searching for is not found * * return false */ private bool IsWordPresent(string currentWord, int rowIndex, int columnIndex, bool userDriven) { cellBuffer.Clear(); cellBuffer.Push(cellObject[rowIndex, columnIndex]); cellBuffer.Peek().isLocked = true; for (int i = 1; i < currentWord.Length && cellBuffer.Count > 0;) { if (IsNextLetterAvilable(currentWord[i])) { i++; cellBuffer.Peek().isLocked = true; } else { CellDetails cellBufferTop = cellBuffer.Pop(); cellBufferTop.isLocked = false; while (cellBuffer.Count > 0 && cellBufferTop.letter != cellBuffer.Peek().letter) { cellBufferTop = cellBuffer.Pop(); cellBufferTop.isLocked = false; i--; } if (cellBuffer.Count > 0) { cellBuffer.Peek().isLocked = true; } } } if (cellBuffer.Count > 0) { if (userDriven) { Stack <CellDetails> userSelectedBuffer = new Stack <CellDetails>(); while (cellBuffer.Count > 0) { if (cellBuffer.Peek().isLocked == true) { userSelectedBuffer.Push(cellBuffer.Pop()); userSelectedBuffer.Peek().isLocked = false; userSelectedBuffer.Peek().button.Height = userSelectedBuffer.Peek().button.Width = 0; } else { cellBuffer.Pop(); } } cellBuffer = userSelectedBuffer; } else { while (cellBuffer.Count > 0) { cellBuffer.Pop().isLocked = false; } } return(true); } else { return(false); } }