public PlayPath(WordPart main, WordPartCollection extras, ConstantList <LetterPlay> played, ConstantList <char> pending) { this.main = main; this.extras = extras; this.played = played; this.pending = pending; }
public Rack(ConstantList <char> rack) { this.rack = rack; value = new string(rack.ToArray()); if (value.Length != Size) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Expected {0} letters but got {1} ({2})", Size, value.Length, value)); } }
public CtorHelper(WordGraph graph, Board board, Rack rack) { this.graph = graph; this.board = board; var validMap = new Dictionary <WordPart, PlayPath>(); var cells = board.GetStartCells(); var done = new HashSet <WordPart>(); foreach (Cell cell in cells) { foreach (Direction direction in new[] { Direction.Down, Direction.Right }) { var otherDirection = direction == Direction.Down ? Direction.Right : Direction.Down; var playable = new PlayableLetters(rack.Letters); WordPartPair extraPair = GetBeforeAfter(board, cell, otherDirection, graph, playable); WordPartPair mainPair = GetBeforeAfter(board, cell, direction, graph, playable); if (playable.Count == 0) { continue; } foreach (char letter in playable) { WordPart mainPart = mainPair.Play(cell, letter) ?? new WordPart(letter.ToString(), cell, direction); WordPart extraPart = extraPair.Play(cell, letter); if (extraPart != null && !graph.IsValid(extraPart.Word)) { continue; } var played = new LetterPlay(cell, letter); var pending = new List <char>(rack.Letters); pending.Remove(letter); bool valid = mainPart.Word.Length == 1 || graph.IsValid(mainPart.Word); var path = GetPath(mainPart, extraPart, played, pending.ToConstant()); if (valid && !validMap.ContainsKey(mainPart)) { validMap.Add(mainPart, path); } var min = new Dictionary <Direction, HashSet <int> >(); min.Add(Direction.Down, new HashSet <int>()); min.Add(Direction.Right, new HashSet <int>()); var max = new Dictionary <Direction, HashSet <int> >(); max.Add(Direction.Down, new HashSet <int>()); max.Add(Direction.Right, new HashSet <int>()); GetNewPaths(graph, board, path, path, done, validMap, true, min, max); } } } valids = new ConstantList <PlayPath>(validMap.Values.ToList()); }
internal static PlayPath GetPath(Board board, Rack rack, WordPart part) { ISet <Cell> excluded = board.GetExcluded(part); ConstantList <LetterPlay> played = part.GetPlayed(excluded); var extras = new List <WordPart>(); var otherDirection = part.Direction == Direction.Right ? Direction.Down : Direction.Right; var pending = new List <char>(rack.Letters); foreach (LetterPlay lp in played) { var before = board.GetBeforePart(lp.Cell, otherDirection); var after = board.GetAfterPart(lp.Cell, otherDirection); var pair = new WordPartPair(before, after); WordPart extra = pair.Play(lp.Cell, lp.Letter); if (extra != null) { extras.Add(extra); } pending.Remove(lp.Letter); } return(new PlayPath(part, new WordPartCollection(extras.ToConstant()), played, pending.ToConstant())); }
public WordPartCollection(ConstantList <WordPart> parts) { this.parts = parts; }
public PlayPath(WordPart main, WordPart extra, LetterPlay letter, ConstantList <char> pending) : this(main, new WordPartCollection(extra), new ConstantList <LetterPlay>(letter), pending) { }
public PlayPath(WordPart main, LetterPlay letter, ConstantList <char> pending) : this(main, WordPartCollection.Empty, new ConstantList <LetterPlay>(letter), pending) { }
internal static void GetNewPaths(WordGraph graph, Board board, PlayPath origin, PlayPath path, ISet <WordPart> done, IDictionary <WordPart, PlayPath> newValids, Choices choices, bool validOnly, Dictionary <Direction, HashSet <int> > min, Dictionary <Direction, HashSet <int> > max) { foreach (char letter in choices.Letters) { WordPart mainPart = choices.Main.Play(choices.Cell, letter); if (done.Contains(mainPart)) { continue; } if (!validOnly) { if (choices.Fix == Fix.Prefix) { if (min[path.Main.Direction].Contains(0)) { break; } } else if (choices.Fix == Fix.Suffix) { if (max[path.Main.Direction].Contains(8)) { break; } } } WordPart extraPart = choices.Extra.Play(choices.Cell, letter); if (extraPart != null && (validOnly && !graph.IsValid(extraPart.Word) || !validOnly && !graph.Contains(extraPart.Word))) { continue; } ConstantList <char> pending = null; if (path.Pending != null) { var temp = new List <char>(path.Pending); temp.Remove(letter); if (temp.Count == 0 && !graph.IsValid(mainPart.Word)) { continue; } pending = temp.ToConstant(); } var extras = new List <WordPart>(path.Extras); if (extraPart != null) { extras.Add(extraPart); } var letterPlay = new LetterPlay(choices.Cell, letter); var played = new List <LetterPlay>(path.Played); if (choices.Fix == Fix.Prefix) { played.Insert(0, letterPlay); } else { played.Add(letterPlay); } bool valid = mainPart.Word.Length == 1 || graph.IsValid(mainPart.Word); var newPath = new PlayPath(mainPart, new WordPartCollection(extras.ToConstant()), played.ToConstant(), pending); if (valid && !newValids.ContainsKey(mainPart)) { newValids.Add(mainPart, newPath); } done.Add(mainPart); { if (!validOnly && valid) { min[mainPart.Direction].Add(mainPart.Direction == Direction.Right ? newPath.Main.First.Column : newPath.Main.First.Row); max[mainPart.Direction].Add(mainPart.Direction == Direction.Right ? newPath.Main.Last.Column : newPath.Main.Last.Row); if (choices.Fix == Fix.Prefix) { if (path.Main.Direction == Direction.Right) { if (origin.Main.First.Column - path.Main.First.Column >= 2 || path.Main.First.Column == 0) { break; } } else { if (origin.Main.First.Row - path.Main.First.Row >= 2 || path.Main.First.Row == 0) { break; } } } else { if (path.Main.Direction == Direction.Right) { if (path.Main.Last.Column - origin.Main.Last.Column >= 2 || path.Main.Last.Column == 8) { break; } } else { if (path.Main.Last.Row - origin.Main.Last.Row >= 2 || path.Main.Last.Row == 8) { break; } } } } GetNewPaths(graph, board, origin, newPath, done, newValids, validOnly, min, max); } } }
private PlayGraph(WordGraph graph, Board board, ConstantList <PlayPath> valids) { this.graph = graph; this.board = board; this.valids = valids; }
private static PlayPath GetPath(WordPart mainPart, WordPart extraPart, LetterPlay played, ConstantList <char> pending) { if (extraPart == null) { return(new PlayPath(mainPart, played, pending.ToConstant())); } return(new PlayPath(mainPart, extraPart, played, pending.ToConstant())); }