Example #1
0
        internal static ConstantList <LetterPlay> GetPlayed(WordPart part, params int[] excluded)
        {
            var set    = new HashSet <int>(excluded);
            var played = new List <LetterPlay>();
            var cell   = part.First;

            for (int i = 0; i < part.Word.Length; i++)
            {
                if (!set.Contains(i))
                {
                    played.Add(new LetterPlay(cell, part.Word[i]));
                }
                if (i < part.Word.Length - 1)
                {
                    switch (part.Direction)
                    {
                    case Direction.Down:
                        cell = cell.Down;
                        break;

                    case Direction.Right:
                        cell = cell.Right;
                        break;
                    }
                }
            }
            return(played.ToConstant());
        }
Example #2
0
        [Test] public void TestFixes1()
        {
            var graph = WordGraph.French;
            var board = new Board();
            var rack  = new Rack("ASSDHS");

            var part1 = new WordPart("DAHS", new Cell(4, 4), Direction.Down);

            board = board.Play(part1);

            rack = new Rack("SSEEEA");
            PlayGraph play       = new PlayGraph(graph, board, rack);
            var       moveFinder = new MoveFinder(graph, board, play);
            List <Tuple <PlayInfo, PlayPath> > moves = moveFinder.GetAllMoves();

            foreach (Tuple <PlayInfo, PlayPath> move in moves)
            {
                if ("ES" != move.Item2.Main.Word ||
                    new Cell(7, 5) != move.Item2.Main.First ||
                    Direction.Down != move.Item2.Main.Direction)
                {
                    continue;
                }
                Assert.IsTrue(move.Item1.HasFixes);
                Assert.IsTrue(move.Item1.HasTwoMoreFixes);
            }
        }
Example #3
0
        [Test] public void TestFixes2()
        {
            var graph = WordGraph.French;
            var board = new Board();
            var rack  = new Rack("DELRIC");

            var part1 = new WordPart("DECRI", new Cell(4, 4), Direction.Right);

            board = board.Play(part1);

            rack = new Rack("LTREIG");
            PlayGraph play       = new PlayGraph(graph, board, rack);
            var       moveFinder = new MoveFinder(graph, board, play);
            List <Tuple <PlayInfo, PlayPath> > moves = moveFinder.GetAllMoves();

            foreach (Tuple <PlayInfo, PlayPath> move in moves)
            {
                if ("GILET" != move.Item2.Main.Word ||
                    new Cell(5, 1) != move.Item2.Main.First ||
                    Direction.Right != move.Item2.Main.Direction)
                {
                    continue;
                }
                Assert.IsTrue(move.Item1.HasFixes);
                Assert.IsFalse(move.Item1.HasTwoMoreFixes);
            }
        }
Example #4
0
 public PlayPath(WordPart main, WordPartCollection extras, ConstantList <LetterPlay> played, ConstantList <char> pending)
 {
     this.main    = main;
     this.extras  = extras;
     this.played  = played;
     this.pending = pending;
 }
Example #5
0
        [Test] public void TestPlayUnrelated()
        {
            var part   = new WordPart("MER", new Cell(4, 4), Direction.Right);
            var played = part.Play(new Cell(0, 0), 'E');

            Assert.AreEqual(part, played);
        }
Example #6
0
        private static Tuple <Fix, Fix> GetFixes(WordPart part, WordPart future)
        {
            Fix one     = Fix.None;
            Fix twoMore = Fix.None;
            int before  = (part.First.Row - future.First.Row)
                          + (part.First.Column - future.First.Column);

            if (before >= 1)
            {
                one |= Fix.Prefix;
            }
            if (before >= 2)
            {
                twoMore |= Fix.Prefix;
            }
            int after = (future.Last.Row - part.Last.Row)
                        + (future.Last.Column - part.Last.Column);

            if (after >= 1)
            {
                one |= Fix.Suffix;
            }
            if (after >= 2)
            {
                twoMore |= Fix.Suffix;
            }
            return(new Tuple <Fix, Fix>(one, twoMore));
        }
Example #7
0
        public ISet <Cell> GetExcluded(WordPart part)
        {
            var excluded = new HashSet <Cell>();
            var cell     = part.First;

            for (int i = 0; i < part.Word.Length; i++)
            {
                if (board[cell.Row][cell.Column] != Empty)
                {
                    if (board[cell.Row][cell.Column] != part.Word[i])
                    {
                        throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Cannot play {0} at {1}", part.Word, part.First));
                    }
                    excluded.Add(cell);
                }
                if (i == part.Word.Length - 1)
                {
                    break;
                }
                switch (part.Direction)
                {
                case Direction.Down:
                    cell = cell.Down;
                    break;

                case Direction.Right:
                    cell = cell.Right;
                    break;
                }
            }
            return(excluded);
        }
Example #8
0
        public void TestPlay(string word, int points, int stars)
        {
            var part  = new WordPart(word, new Cell(4, 3), Direction.Right);
            var score = PlayerScore.Play(part);

            Assert.AreEqual(points, score.Points);
            Assert.AreEqual(stars, score.Stars);
        }
Example #9
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()));
 }
Example #10
0
        [Test] public void TestPlayFirst()
        {
            var score = new Score();
            var part  = new WordPart("LETTRE", new Cell(4, 3), Direction.Right);
            var board = new Board().Play(part);

            Assert.AreEqual(new PlayerScore(6, 1), board.Score.Other);
            Assert.AreEqual(new PlayerScore(), board.Score.Current);
        }
Example #11
0
        public void TestPlay(string word, Direction direction, int row, int column, char letter, string newWord, int newRow, int newColumn)
        {
            var part   = new WordPart(word, new Cell(4, 4), direction);
            var played = part.Play(new Cell(row, column), letter);

            Assert.AreEqual(newWord, played.Word);
            Assert.AreEqual(new Cell(newRow, newColumn), played.First);
            Assert.AreEqual(direction, played.Direction);
        }
Example #12
0
        public void TestGetStartCellsSecond(string word, int row, int column, Direction direction, int count)
        {
            var board = new Board();
            var part  = new WordPart(word, new Cell(row, column), direction);

            board = board.Play(part);
            var cells = board.GetStartCells();

            Assert.AreEqual(count, cells.Count);
        }
Example #13
0
        [Test] public void TestGetPathArgumentException()
        {
            var board = new Board();
            var part1 = new WordPart("YEWS", new Cell(4, 4), Direction.Right);

            board = board.Play(part1);
            var      rack  = new Rack("ESZZZZ");
            PlayPath path2 = Shell.GetPath(board, rack, "YES", 4, 4, null);

            Assert.IsNotNull(path2);
        }
Example #14
0
        [Test] public void TestPlaySecondNovice()
        {
            var part1  = new WordPart("LETTRE", new Cell(4, 3), Direction.Right);
            var board1 = new Board().Play(part1);
            var part2  = new WordPart("LE", new Cell(4, 3), Direction.Down);
            var play   = new PlayPath(part2, new LetterPlay(new Cell(5, 3), 'E'), new ConstantList <char>("MOTUR".ToList()));
            var board2 = board1.Play(play);

            Assert.AreEqual(new PlayerScore(5, 1), board2.Score.Current);
            Assert.AreEqual(new PlayerScore(2, 0), board2.Score.Other);
        }
Example #15
0
        [Test] public void TestPlaySecondSameDirection()
        {
            var part1  = new WordPart("ET", new Cell(4, 3), Direction.Right);
            var board1 = new Board().Play(part1);
            var part2  = new WordPart("LETTRE", new Cell(4, 2), Direction.Right);
            var played = LetterPlayTest.GetPlayed(part2, 1, 2);
            var play   = new PlayPath(part2, new WordPartCollection(), played, new ConstantList <char>());
            var board2 = board1.Play(play);

            Assert.AreEqual(new PlayerScore(0, 0), board2.Score.Current);
            Assert.AreEqual(new PlayerScore(6, 0), board2.Score.Other);
        }
Example #16
0
        [Test] public void TestPlayAll()
        {
            var graph = WordGraph.French;
            var board = new Board();
            var part  = new WordPart("LETTRE", new Cell(4, 2), Direction.Right);

            board = board.Play(part);
            var rack = new Rack("MOTEUR");
            var play = new PlayGraph(graph, board, rack);

            Assert.AreEqual(701, play.Valids.Count);
        }
Example #17
0
        [Test] public void TestMerge()
        {
            var part = new WordPart("MER", new Cell(4, 4), Direction.Right);

            Assert.Throws <ArgumentException>(() => part.Merge(new WordPart("RE", new Cell(0, 0), Direction.Right)));
            Assert.Throws <ArgumentException>(() => part.Merge(new WordPart("RE", new Cell(4, 6), Direction.Down)));
            Assert.Throws <ArgumentException>(() => part.Merge(new WordPart("ER", new Cell(4, 6), Direction.Right)));
            var merge = part.Merge(new WordPart("RE", new Cell(4, 6), Direction.Right));

            Assert.AreEqual(part.First, merge.First);
            Assert.AreEqual("MERE", merge.Word);
            Assert.AreEqual(Direction.Right, merge.Direction);
        }
Example #18
0
        [Test] public void TestPlayVortexAddStars()
        {
            var part1  = new WordPart("LETTRE", new Cell(4, 2), Direction.Right);
            var board1 = new Board().Play(part1);
            var part2  = new WordPart("JOUES", new Cell(0, 8), Direction.Down);
            var played = LetterPlayTest.GetPlayed(part2);
            var extra  = part1.Play(new Cell(4, 8), 'S');
            var play   = new PlayPath(part2, new WordPartCollection(extra), played.ToConstant(), new ConstantList <char>());
            var board2 = board1.Play(play);

            Assert.AreEqual(new PlayerScore(0, 0), board2.Score.Current);
            Assert.AreEqual(new PlayerScore(12, 0), board2.Score.Other);
        }
Example #19
0
        [Test] public void TestPlaySecondExpert()
        {
            var part1  = new WordPart("LETTRE", new Cell(4, 2), Direction.Right);
            var board1 = new Board().Play(part1);
            var part2  = new WordPart("MOTEUR", new Cell(1, 8), Direction.Down);
            var played = LetterPlayTest.GetPlayed(part2);
            var extra  = part1.Play(new Cell(4, 8), 'E');
            var play   = new PlayPath(part2, new WordPartCollection(extra), played, new ConstantList <char>());
            var board2 = board1.Play(play);

            Assert.AreEqual(new PlayerScore(0, 0), board2.Score.Current);
            Assert.AreEqual(new PlayerScore(12, 1), board2.Score.Other);
        }
Example #20
0
        private static Tuple <Fix, Fix> GetFixes(WordPart part, IEnumerable <PlayPath> allValids)
        {
            Fix one     = Fix.None;
            Fix twoMore = Fix.None;

            foreach (PlayPath future in allValids)
            {
                Tuple <Fix, Fix> tuple = GetFixes(part, future.Main);
                one     |= tuple.Item1;
                twoMore |= tuple.Item2;
            }
            return(new Tuple <Fix, Fix>(one, twoMore));
        }
Example #21
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());
            }
Example #22
0
            public Choices(WordGraph graph, Board board, PlayPath path, Fix fix, Cell cell)
            {
                this.fix  = fix;
                this.cell = cell;
                Direction direction      = path.Main.Direction;
                var       otherDirection = direction == Direction.Down ? Direction.Right : Direction.Down;
                var       playable       = new PlayableLetters(path.Pending);
                WordPart  before         = fix == Fix.Suffix ? path.Main : null;
                WordPart  after          = fix == Fix.Prefix ? path.Main : null;

                main    = GetBeforeAfter(board, cell, direction, graph, playable, before, after);
                extra   = GetBeforeAfter(board, cell, otherDirection, graph, playable);
                letters = playable.Choices.ToConstant();
            }
Example #23
0
        [Test] public void TestSkip()
        {
            var board = new Board();
            var part1 = new WordPart("TRICHE", new Cell(4, 0), Direction.Right);

            board = board.Play(part1);
            var first  = new PlayerScore(6, 1);
            var second = new PlayerScore();

            Assert.AreEqual(second, board.Score.Current);
            Assert.AreEqual(first, board.Score.Other);
            board = board.Skip();
            Assert.AreEqual(first, board.Score.Current);
            Assert.AreEqual(second, board.Score.Other);
        }
Example #24
0
        [Test] public void TestGetExcluded()
        {
            var graph = WordGraph.French;
            var board = new Board();
            var rack  = new Rack("MEREOT");

            var part1 = new WordPart("MERE", new Cell(4, 1), Direction.Right);

            board = board.Play(part1);

            rack = new Rack("OTMSZZ");
            var part2 = new WordPart("MOTS", new Cell(1, 4), Direction.Down);

            Assert.Throws <ArgumentException>(() => board.GetExcluded(part2));
        }
Example #25
0
        [Test] public void TestGetPath()
        {
            var board = new Board();
            var part1 = new WordPart("PUIS", new Cell(4, 4), Direction.Down);

            board = board.Play(part1);
            var part2 = new WordPart("PIED", new Cell(4, 4), Direction.Right);
            var path2 = Shell.GetPath(board, new Rack("PIEDQZ"), part2);

            Assert.AreEqual(3, path2.Played.Count);
            Assert.AreEqual(0, path2.Extras.Count);
            board = board.Play(path2);
            Assert.AreEqual(new PlayerScore(3), board.Score.Current);
            Assert.AreEqual(new PlayerScore(4), board.Score.Other);
        }
Example #26
0
        public void TestGetAfterPart(int row, int column, string word, Direction direction)
        {
            var board = new Board();
            var part  = new WordPart("MER", new Cell(4, 4), Direction.Right);

            board = board.Play(part);
            WordPart after = board.GetAfterPart(new Cell(row, column), direction);

            if (word != null)
            {
                Assert.AreEqual(new WordPart(word, new Cell(4, 4), direction), after);
            }
            else
            {
                Assert.IsNull(after);
            }
        }
Example #27
0
        private static PlayPath GetPath(Board board, Rack rack, string word, Cell cell, Direction?direction)
        {
            var directions = new List <Direction>();

            if (!direction.HasValue)
            {
                directions.AddRange(new[] { Direction.Down, Direction.Right });
            }
            else
            {
                directions.Add(direction.Value);
            }
            var paths = new List <PlayPath>();

            foreach (Direction d in directions)
            {
                try
                {
                    var      part = new WordPart(word, cell, d);
                    PlayPath path = GetPath(board, rack, part);
                    if (Check(board, rack, path, false))
                    {
                        paths.Add(path);
                    }
                }
                catch (ArgumentException)
                {
                }
                catch (OutOfBoardException)
                {
                }
            }
            if (paths.Count == 1)
            {
                return(paths[0]);
            }
            if (paths.Count > 1)
            {
                Log(new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Cannot guess direction to play {0} at {1}", word, cell)));
            }
            else
            {
                Log(new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Cannot play {0} at {1}", word, cell)));
            }
            return(null);
        }
Example #28
0
        public static PlayerScore Play(WordPart part)
        {
            int  stars = 0;
            Cell cell  = part.First;

            for (int i = 0; i < part.Word.Length; i++)
            {
                if (cell.IsStar)
                {
                    stars++;
                }
                if (i == part.Word.Length - 1)
                {
                    break;
                }
                cell = part.Direction == Direction.Right ? cell.Right : cell.Down;
            }
            return(new PlayerScore(part.Word.Length, stars));
        }
Example #29
0
        public WordPart Play(Cell cell, char letter)
        {
            WordPart part         = null;
            var      beforePlayed = before == null ? (WordPart)null : before.Play(cell, letter);
            var      afterPlayed  = after == null ? (WordPart)null : after.Play(cell, letter);

            if (beforePlayed != null && afterPlayed != null)
            {
                part = beforePlayed.Merge(afterPlayed);
            }
            else if (beforePlayed != null)
            {
                part = beforePlayed;
            }
            else if (afterPlayed != null)
            {
                part = afterPlayed;
            }
            return(part);
        }
Example #30
0
        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()));
        }