Beispiel #1
0
        /// <summary>
        /// Compute and set the new <see cref="WordEntry.Origin"/> of the <see cref="Candidate"/> entry, so this entry intersects in the common letter
        /// </summary>
        /// <exception cref="InvalidOperationException">If both entries do not intersect or do not have common letters</exception>
        public WordEntry RepositionEntry()
        {
            if (!Intersects)
            {
                throw new InvalidOperationException("Can't reposition an entry that do not intersects with others");
            }
            if (!GetCommonLetters())
            {
                Logger.LogWarning($"Words {Existing} and {Candidate} intersect but don't have common letters");
                return(null);
            }
            bool      reposition       = false;
            WordEntry repositionedWord = null;

            if (Overlaps)
            {
                Logger.LogInformation($"Words {Existing} and {Candidate} overlap {Count} common letters");
                Point target    = Existing.Coordinate(ExistingRange.Init);
                int   increment = Candidate.Direction.IsReverse() ? 1 : 0;
                Point delta     = target.Delta(Candidate.Coordinate(CandidateRange.Init + increment));
                repositionedWord = Candidate.Translate(delta);
                bool insideBoundaries = CheckBoundaries(repositionedWord.Origin);
                reposition = insideBoundaries && !IntersectsWithOthers(repositionedWord, Soup.UsedWords.Values);
            }
            else
            {
                int  i = 0;
                bool insideBoundaries = false;
                Logger.LogInformation($"Candidate intersects at ({this[0]}) with ({Existing})");
                do
                {
                    Point target = Existing.Coordinate(CommonLetters[i].ExistingPos);
                    Point delta  = target.Delta(Candidate.Coordinate(CommonLetters[i].CandidatePos));
                    repositionedWord = Candidate.Translate(delta);
                    Logger.LogDebug($"Try to reposition to: {repositionedWord}");
                    insideBoundaries = CheckBoundaries(repositionedWord.Origin);
                    i++;
                } while (!insideBoundaries && i < CommonLetters.Count);
                reposition = insideBoundaries && !IntersectsWithOthers(repositionedWord, Soup.UsedWords.Values);
            }
            if (reposition)
            {
                Logger.LogWarning($"Candidate repositioned: {repositionedWord}");
                return(repositionedWord);
            }
            return(null);
        }
Beispiel #2
0
 /// <summary>
 /// Returns a list of coordinates where both entries intersects
 /// </summary>
 /// <returns></returns>
 public IEnumerable <Point> GetIntersection()
 {
     Clear();
     CommonLetters.Clear();
     ExistingRange  = new Range();
     CandidateRange = new Range();
     for (int i = 0; i < Existing.Name.Length; i++)
     {
         for (int j = 0; j < Candidate.Name.Length; j++)
         {
             if (Existing.Coordinate(i) == Candidate.Coordinate(j))
             {
                 Add(Existing.Coordinate(i));
                 Logger.LogTrace($"  {Existing.Coordinate(i)}");
             }
         }
     }
     Logger.LogDebug($"Comparing ({Existing}) with candidate ({Candidate}): {Count} intersection(s) found");
     return(this);
 }