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; }
static ICrossBoard GenerateFirstCrossWord(ICrossBoard board, ICrossDictionary dictionary) { var gen = new CrossGenerator(dictionary, board); board.Preprocess(dictionary); return gen.Generate().FirstOrDefault(); }
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 !); }
static ICrossBoard GenerateFirstCrossWord(ICrossBoard board, ICrossDictionary dictionary) { var gen = new CrossGenerator(dictionary, board); board.Preprocess(dictionary); return(gen.Generate().FirstOrDefault()); }
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); } } }
public static 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) break; } if (solutionsCount == 0) Console.WriteLine("Solution not found:"); }