示例#1
0
 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()));
 }
示例#2
0
            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());
            }
示例#3
0
 public PlayPath(WordPart main, WordPart extra, LetterPlay letter, ConstantList <char> pending)
     : this(main, new WordPartCollection(extra), new ConstantList <LetterPlay>(letter), pending)
 {
 }
示例#4
0
 public PlayPath(WordPart main, LetterPlay letter, ConstantList <char> pending)
     : this(main, WordPartCollection.Empty, new ConstantList <LetterPlay>(letter), pending)
 {
 }
示例#5
0
        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);
                }
            }
        }