/// <summary> /// Returns the longest possible vector that occupies valid letters in the grid /// </summary> /// <param name="pos">Start pos</param> /// <param name="dir">Direction</param> /// <returns>null id no vector is possible</returns> public static WordVector FindLongestWordVector(this IWordGrid g, Point pos, Dir2D dir) { if (dir == Dir2D.None) { return(null); } Point p = pos; int len = 0; while (g.InBounds(p) && (g[p] != null)) { len++; p = p.NextPoint(dir); } return((len > 0) ? new WordVector(pos, dir, len) : null); }
public static Point NextPoint(this Point p, Dir2D dir, int steps) { switch (dir) { case Dir2D.None: return(p); case Dir2D.Up: return(new Point(p.X, p.Y - steps)); case Dir2D.Down: return(new Point(p.X, p.Y + steps)); case Dir2D.Left: return(new Point(p.X - steps, p.Y)); case Dir2D.Right: return(new Point(p.X + steps, p.Y)); default: return(p); } }
public static Point NextPoint(this Point p, Dir2D dir) { switch (dir) { case Dir2D.None: return(p); case Dir2D.Up: return(p.getPointAbove()); case Dir2D.Down: return(p.getPointBelow()); case Dir2D.Left: return(p.getPointLeft()); case Dir2D.Right: return(p.getPointRight()); default: return(p); } }
protected static Dir2D otherDir(Dir2D dir) { return((dir == Dir2D.Right) ? Dir2D.Down : Dir2D.Right); }
private List <WordVector> GetPossibleVectors_old(string text) { List <WordVector> possibleVecs = new List <WordVector>(); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { //is there a character at the grid which matches our text char?_c = grid[x, y]; if ((_c != null) && (text.Contains(_c.Value))) { List <WordVector> interSectingVecsToCurrentGridPoint = (from v in Words where v.Intersects(x, y) select v).ToList(); //only one word should intersect this letter (so far) //we are trying to place this word orthogonally across it if (interSectingVecsToCurrentGridPoint.Count == 1) { WordVector intersect = interSectingVecsToCurrentGridPoint[0]; Dir2D newDir = otherDir(intersect.Dir); Dir2D newDirOrthogonal = intersect.Dir; //check all possible intersection points foreach (int pos in text.IndexOfAll(_c.Value)) { //create the vector that would position the word orthogonally across the other WordVector newVec; if (newDir == Dir2D.Down) { newVec = new WordVector(new Point(x, y - pos), newDir, text.Length); } else //across { newVec = new WordVector(new Point(x - pos, y), newDir, text.Length); } //check it works (does not put a different letter on top of another word) if (!this.grid.AnyConflicts(newVec, text)) { if (words.TrueForAll(W => canTheseTwoVectorsExistTogether(W, newVec))) { possibleVecs.Add(newVec); } /* * //get the orthogonal words intersected * List<WordVector> interSectingVecsToNewVec = (from v in Words where v.Intersects(newVec) select v).ToList(); * * //can't intersect another WordVector in the same direction * if (interSectingVecsToNewVec.TrueForAll(V => V.Dir == newDirOrthogonal)) * { * //the word should not impinge on exclusion zones of the existing vectors * //other than the one it intersects orthogonally to * List<WordVector> nonIntersectingVectors = Words.Where(V => !interSectingVecsToNewVec.Contains(V)).ToList(); * * if (nonIntersectingVectors.TrueForAll(V => !(newVec.InCrosswordStyleExclusionZoneOF(V)))) * { * possibleVecs.Add(newVec); * } * }*/ } } } } } } //done return(possibleVecs); }
public WordVector(Point pos, Dir2D dir, int length) { this.Pos = pos; this.Dir = dir; this.Length = length; }