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:"); } }
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 !); }
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); }