Esempio n. 1
0
        /// <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);
        }