Exemplo n.º 1
22
 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;
 }
Exemplo n.º 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 !);
        }
Exemplo n.º 3
0
        private static IEnumerable <ICrossBoard> GenerateCrossWords(ICrossBoard board, ICrossDictionary dictionary, string puzzle, CancellationToken cancellationToken)
        {
            if (puzzle != null)
            {
                var placer = new PuzzlePlacer(board, puzzle);
                foreach (var boardWithPuzzle in placer.GetAllPossiblePlacements(dictionary))
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var gen = new CrossGenerator(dictionary, boardWithPuzzle);

                    // limit
                    int generatedCount = 0;

                    var generated = gen.Generate();
                    foreach (var solution in generated)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        generatedCount++;

                        if (generatedCount >= MAX_GENERATOR_COUNT)
                        {
                            break;
                        }

                        yield return(solution);
                    }
                }
            }
            else
            {
                var gen = new CrossGenerator(dictionary, board);
                board.Preprocess(dictionary);

                var crosswords = gen.Generate();

                // limit
                int generatedCount = 0;

                foreach (var resultBoard in crosswords)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    generatedCount++;

                    if (generatedCount >= MAX_GENERATOR_COUNT)
                    {
                        break;
                    }

                    yield return(resultBoard);
                }
            }
        }