/// <summary> /// Sets the specified word on the grid. /// </summary> /// <param name='line'> /// The WordLine to set. /// </param> private void SetWordOnGrid(WordLine line) { Point current_location = line.VirtualPosition.StartPoint; char[] new_word = line.Word.ToCharArray(); int line_length = line.Word.Length; for (int i=0; i<line_length; i++) { this.grid[current_location.X, current_location.Y] = new_word[i]; Point color_point = new Point(current_location.X, current_location.Y); color_points.Add(color_point); current_location+=line.VirtualPosition.Direction; } }
/// <summary> /// Marks the word in the line represented by the two points. Valid lines can only be vertical, /// horizontal or diagonal and must be inside the grid. Point (0,0) represents the top-left cell /// of the grid. /// </summary> /// <returns> /// <c>True</c> if a word was found in the specified coordinates, <c>False</c> otherwise. /// </returns> /// <param name='start_point'> /// Start point of the word, this is where the first letter is located. /// </param> /// <param name='end_point'> /// End point of the word, this is where the last letter is located. /// </param> public bool MarkWord(Point start_point, Point end_point) { GridLine line = new GridLine(start_point,end_point); if (!ValidLine(line)) return false; string word_on_grid = GetStringFromGrid(line); WordLine word = new WordLine(line,word_on_grid); if (notfound_words.Contains(word.Word)) { Console.WriteLine("Word: "+word.Word); notfound_words.Remove(word.Word); found_words.Add(word.Word); for(int i=0; i<word.Word.Length; i++) { Point color_point = word.RealPosition.StartPoint + word.RealPosition.Direction*i; color_points.Add(color_point); } if (notfound_words.Count < 1) state = CLEARED; return true; } return false; }
/// <summary> /// Attempts to place the next word of the unplace_words list in the start point supplied, following /// the direction described by the second Point parameter. /// </summary> /// <returns> /// Returns true if the word was correctly placed. /// </returns> /// <param name='direction'> /// Describes the direction of the word. To correctly set this value, imagine an xy-plane and draw an /// arrow from point (0,0) to point(X,Y) where -1 <= X <= 1, same goes for Y. That arrow represents /// the direction the word should follow. /// Examples: /// Point ( 1, 1) = Going northeast, or diagonal up-right direction. /// Point (-1, 0) = Going west, or horizontal left direction. /// Point ( 1, 0) = Going east, or horizontal right direction. /// Point ( 0,-1) = Going south, or vertical down direction. /// </param> /// <param name='word'> /// The word to place in the grid. /// </param> /// <exception cref="NoWordsToPlaceException">If the list of unplaced words is empty, this exception will be thrown</exception> public bool PlaceWord(Point start_point, Point direction) { if (this.unplaced_words.Count == 0) throw new NoWordsToPlaceException(); if (direction.X==0 && direction.Y==0) throw new NullDirection(); if (!ValidPoint(start_point)) throw new InvalidWordStartPoint(); int slide_attempts = 0, slide_distance = 0; WordLine next_word = new WordLine(start_point,direction,(string) unplaced_words[0]); while(!ValidLine(next_word.VirtualPosition) && slide_attempts<slide_tolerance) { slide_distance = GetNextSlideDistance(slide_distance); next_word.Slide(slide_distance); slide_attempts++; if (slide_tolerance/2 == slide_attempts) next_word.ForceUndefSlope(); } slide_distance = 0; // Reset the slide distance while(!CanPlaceWord(next_word) && slide_attempts<slide_tolerance) { slide_distance = GetNextSlideDistance(slide_distance); next_word.Slide(slide_distance); slide_attempts++; } if (!(slide_attempts>=slide_tolerance)) { SetWordOnGrid(next_word); notfound_words.Add(next_word.Word); unplaced_words.RemoveAt(0); } return !(slide_attempts>=slide_tolerance); }
public string MarkAndGetWord(Point start_point, Point end_point) { GridLine line = new GridLine(start_point,end_point); WordLine word = new WordLine(line,new string(GetWordFromGrid(line))); if (notfound_words.Contains(word.Word)) { notfound_words.Remove(word.Word); found_words.Add(word.Word); for(int i=0; i<word.Word.Length; i++) { Point color_point = word.RealPosition.StartPoint + word.RealPosition.Direction*i; color_points.Add(color_point); } if (notfound_words.Count < 1) state = CLEARED; return word.Word; } return null; }
public bool InsideGridBounds(WordLine word) { return ( (word.VirtualPosition.StartPoint.X|word.VirtualPosition.StartPoint.Y| word.VirtualPosition.EndPoint.X|word.VirtualPosition.EndPoint.Y) >= 0 && (word.VirtualPosition.StartPoint.X|word.VirtualPosition.StartPoint.Y| word.VirtualPosition.EndPoint.X|word.VirtualPosition.EndPoint.Y) < grid_size); }
/// <summary> /// Determines whether the specified word can be placed in the coordinates of it's VirtualPosition. /// </summary> /// <returns> /// <c>true</c> if this instance can place the specified word; otherwise, <c>false</c>. /// </returns> /// <param name='word'> /// If set to <c>true</c> word. /// </param> public bool CanPlaceWord(WordLine word) { if (!InsideGridBounds(word)) return false; int word_length = word.Word.Length; char[] test_aux = new char[word_length], grid_word = GetWordFromGrid(word.VirtualPosition), new_word = word.Word.ToCharArray(); for (int i=0; i<word_length;i++) { test_aux[i] = (grid_word[i]==EMPTY)? new_word[i]: CompareChars(new_word[i],grid_word[i]); } return (new string(test_aux).Equals(new string(new_word))); }
public void WordSearchGridTestSetup() { TestWords = new ArrayList(); FillTestWords(); WordTests = new WordLine[2]; Test1 = new WordSearchGrid(TestWords); WordTests[0] = new WordLine(new Point(1,2), new Point(1,0), (string)TestWords[0]); WordTests[1] = new WordLine(new Point(3,1), new Point(0,1), (string)TestWords[1]); }
public void WordLineTestSetup() { Serge = new WordLine(new Point(6,10),new Point(1,0),"SERGE"); Programming = new WordLine(new Point(6,0),new Point(0,1),"prOgrAmming"); Test = new WordLine(new Point(2,7),new Point(0,1),"Test"); TestFixture = new WordLine(new Point(13,14),new Point(-1,-1),"TestFixture"); }