Exemplo n.º 1
0
        public void CrossBoardPreprocessTest()
        {
            ICrossBoard cb = new CrossBoard(5, 5);

            for (int i = 0; i < 5; i++)
            {
                var sw = new StartWord();
                sw.StartX = i;
                sw.StartY = 0;
                cb.AddStartWord(sw);
                if (i > 0)
                {
                    sw.StartX = 0;
                    sw.StartY = i;
                    cb.AddStartWord(sw);
                }
                else
                {
                    sw.StartX = 2;
                    sw.StartY = 2;
                    cb.AddStartWord(sw);
                }
            }

            cb.Preprocess(new Dictionary(cb.MaxWordLength));
            Assert.Equal(8, cb.GetPatternCount());
        }
Exemplo n.º 2
0
 public void CrossBoardPreprocessTest()
 {
     ICrossBoard cb = new CrossBoard();
     cb.SetBoardSize(5, 5);
     for (int i = 0; i < 5; i++)
     {
         var sw = new StartWord();
         sw.StartX = i;
         sw.StartY = 0;
         cb.AddStartWord(sw);
         if (i > 0)
         {
             sw.StartX = 0;
             sw.StartY = i;
             cb.AddStartWord(sw);
         }
         else
         {
             sw.StartX = 2;
             sw.StartY = 2;
             cb.AddStartWord(sw);
         }
     }
     cb.Preprocess(new Dictionary(cb.MaxWordLength));
     Assert.Equal(8, cb.GetPatternCount());
 }
Exemplo n.º 3
0
        public static CrossBoard ToCrossBoard(this CrossWordTimes self)
        {
            var board = new CrossBoard();

            int cols = (int)self.Size.Cols;
            int rows = (int)self.Size.Rows;

            board.SetBoardSize(cols, rows);

            int n = 0;

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    var val = self.Grid[n];
                    if (val == ".")
                    {
                        board.AddStartWord(col, row);
                    }

                    n += 1;
                }
            }

            // debug the generated template
            // using (StreamWriter writer = new StreamWriter("template.txt"))
            // {
            //     board.WriteTemplateTo(writer);
            // }

            return(board);
        }
Exemplo n.º 4
0
    public static ICrossBoard CreateFromStream(Stream s)
    {
        var r     = new StreamReader(s, Encoding.UTF8);
        var lines = new List <string>();

        while (true)
        {
            var line = r.ReadLine();
            if (string.IsNullOrEmpty(line))
            {
                break;
            }
            lines.Add(line);
        }

        int lineLength = -1;

        for (int i = 0; i < lines.Count; i++)
        {
            if (lineLength == -1)
            {
                lineLength = lines[i].Length;
            }
            else if (lines[i].Length != lineLength)
            {
                throw new Exception(
                          $"Line {i} has different length ({lines[i]}) then previous lines ({lineLength})");
            }
        }

        ICrossBoard board = new CrossBoard(lineLength, lines.Count);

        for (int row = 0; row < lines.Count; row++)
        {
            var line = lines[row];
            for (int col = 0; col < lineLength; col++)
            {
                if (line[col] == '-')
                {
                    board.AddStartWord(col, row);
                }
            }
        }

        return(board);
    }
Exemplo n.º 5
0
        static ICrossBoard CreateCross()
        {
            const int   sizex = 11;
            const int   sizey = 15;
            ICrossBoard cb    = new CrossBoard(sizex, sizey);

            for (int i = 0; i < sizey; i++)
            {
                cb.AddStartWord(0, i);
            }

            for (int i = 1; i < sizex; i++)
            {
                cb.AddStartWord(i, 0);
            }

            cb.AddStartWord(7, 1);
            cb.AddStartWord(4, 3);
            cb.AddStartWord(3, 4);
            cb.AddStartWord(6, 4);
            cb.AddStartWord(7, 5);
            cb.AddStartWord(4, 6);
            cb.AddStartWord(1, 7);
            cb.AddStartWord(5, 7);
            cb.AddStartWord(9, 7);
            cb.AddStartWord(10, 7);
            cb.AddStartWord(8, 8);
            cb.AddStartWord(3, 9);
            cb.AddStartWord(6, 9);
            cb.AddStartWord(4, 10);
            cb.AddStartWord(7, 10);
            cb.AddStartWord(8, 11);
            cb.AddStartWord(5, 12);
            cb.AddStartWord(6, 13);
            cb.AddStartWord(6, 14);
            cb.AddStartWord(10, 14);

            return(cb);
        }
Exemplo n.º 6
0
        private CrossBoard GetCrossboard()
        {
            ICrossBoard board = null;
            // var template = GetRandomCrosswordTemplateFromDb();
            CrosswordTemplate template = null;

            if (template != null)
            {
                board = new CrossBoard();

                int cols = (int)template.Cols;
                int rows = (int)template.Rows;

                board.SetBoardSize(cols, rows);

                int n = 0;
                for (int row = 0; row < rows; row++)
                {
                    for (int col = 0; col < cols; col++)
                    {
                        var val = template.Grid[n];
                        if (val == ".")
                        {
                            board.AddStartWord(col, row);
                        }

                        n += 1;
                    }
                }

                // debug the generated template
                // using (StreamWriter writer = new StreamWriter("template.txt"))
                // {
                //     board.WriteTemplateTo(writer);
                // }
            }
            else
            {
                var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random");
                board = model.ToCrossBoard();

                // add in database
                var newTemplate = new CrosswordTemplate()
                {
                    Rows = model.Size.Rows,
                    Cols = model.Size.Cols,
                    Grid = model.Grid
                };

                db.CrosswordTemplates.Add(newTemplate);
                db.SaveChanges();
            }

            var gen = new CrossGenerator(dictionary, board);

            board.Preprocess(dictionary);

            var generated = gen.Generate().FirstOrDefault() as CrossBoard;

            return(generated);
        }