Пример #1
0
        void GenerateAndOutput(CrossGenerator generator, CommandStore commands, int maxSolutionsCount)
        {
            int solutionsCount = 0;

            foreach (var solution in generator.Generate())
            {
                lock (commands.Lock)
                {
                    Console.WriteLine($"Solution {solutionsCount} found:");
                    using (var w = OpenConsoleWriter())
                        solution.WriteTo(w);
                }

                if (++solutionsCount == maxSolutionsCount)
                {
                    Console.WriteLine($"{solutionsCount} solutions found.");
                    break;
                }
            }

            if (solutionsCount == 0)
            {
                Console.WriteLine("Solution not found:");
            }
        }
Пример #2
0
    static ICrossBoard GenerateFirstCrossWord(ICrossBoard board, ICrossDictionary dictionary, string puzzle)
    {
        var         placer           = new PuzzlePlacer(board, puzzle);
        var         cts              = new CancellationTokenSource();
        var         mre              = new ManualResetEvent(false);
        ICrossBoard?successFullBoard = null;

        foreach (var boardWithPuzzle in placer.GetAllPossiblePlacements(dictionary))
        {
            //boardWithPuzzle.WriteTo(new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding) { AutoFlush = true });
            var gen = new CrossGenerator(dictionary, boardWithPuzzle);
            var t   = Task.Factory.StartNew(() =>
            {
                foreach (var solution in gen.Generate())
                {
                    successFullBoard = solution;
                    cts.Cancel();
                    mre.Set();
                    break; //interested in the first one
                }
            }, cts.Token);
            if (cts.IsCancellationRequested)
            {
                break;
            }
        }

        mre.WaitOne();
        return(successFullBoard !);
    }
Пример #3
0
    static ICrossBoard?GenerateFirstCrossWord(ICrossBoard board, ICrossDictionary dictionary)
    {
        var gen = new CrossGenerator(dictionary, board);

        board.Preprocess(dictionary);
        return(gen.Generate().FirstOrDefault());
    }
Пример #4
0
        CrossGenerator CreateGenerator(string file, string dictFile, CommandStore commands)
        {
            DateTime startTime = DateTime.Now;
            var      cb        = CrossBoardCreator.CreateFromFile(file);
            var      dict      = new Dictionary(dictFile, cb.MaxWordLength);

            cb.Preprocess(dict);
            var gen = new CrossGenerator(dict, cb);

            gen.Watcher += GeneratorWatcher;
            return(gen);
        }
Пример #5
0
 void GeneratorWatcher(CrossGenerator generator)
 {
     while (_commandStore.Count > 0)
     {
         var command = _commandStore.PopCommand();
         if (command == null)
         {
             break;
         }
         if (command.Equals("h"))
         {
             //write help
             Console.WriteLine("Commands help: ");
             Console.WriteLine("h - show this help");
             Console.WriteLine("d - display cross");
             Console.WriteLine("p - display patterns");
             Console.WriteLine("c - check");
         }
         else if (command.Equals("d"))
         {
             using (var w = OpenConsoleWriter())
                 generator.Board.WriteTo(w);
         }
         else if (command.Equals("p"))
         {
             using (var w = OpenConsoleWriter())
                 generator.Board.WritePatternsTo(w);
         }
         else if (command.Equals("c"))
         {
             generator.Board.CheckPatternValidity();
         }
         else
         {
             Console.WriteLine("unknown command: {0}", command);
         }
     }
 }
Пример #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);
        }