static void Main(string[] args) { if (args.Length > 0) { var values = File.ReadAllText(args[0]); solveData(ColumnAndRowData.Parse(values)); } var id = HotKeyManager.RegisterHotKey(Keys.P, KeyModifiers.Control | KeyModifiers.Shift); HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; Console.OutputEncoding = Encoding.Unicode; Console.WriteLine("Press Ctrl-Shift-P over the Pixelo window to solve it."); Console.WriteLine("Press q in this window to close it"); Console.WriteLine("Press o in this window to show the partial solutions overlapping (ON by default)"); Console.WriteLine("Press w in this window to have to press a key to see the next partial solution (ON by default)"); Console.WriteLine("Press c in this window to change the characters used to display the solutions"); Console.WriteLine("Press r in this window to replay the last solve"); while (true) { if (!Console.KeyAvailable) { Thread.Sleep(500); continue; } var key = Console.ReadKey(true).Key; if (key == ConsoleKey.Q) { break; } if (key == ConsoleKey.O) { _overlap = !_overlap; Console.WriteLine("Overlap mode for partial solutions is now " + (_overlap ? "ON" : "OFF")); } if (key == ConsoleKey.W) { _pressKeyToSeeSolutions = !_pressKeyToSeeSolutions; Console.WriteLine("Press a key to see next partial or total solution mode is now " + (_pressKeyToSeeSolutions ? "ON" : "OFF")); } if (key == ConsoleKey.C) { _charsIndex = (_charsIndex + 1) % _chars.Length; Console.WriteLine("The display charset is now " + string.Join(", ", _chars[_charsIndex])); } if (key == ConsoleKey.R && _data != null) { solveData(_data); } } HotKeyManager.UnregisterHotKey(id); }
public void Load(ColumnAndRowData data) { _data = data; // all possible combinations for each column _colPossibilities = _data.Columns.Select((col, idx) => new PossibilitiesRecord { Index = idx, Possibilities = getPossibleValues(col, _data.Height).ToList() }).OrderBy(pos => pos.Possibilities.Count).ToList(); // all possible combinations for each row _rowPossibilities = _data.Rows.Select((row, idx) => new PossibilitiesRecord { Index = idx, Possibilities = getPossibleValues(row, _data.Width).ToList() }).OrderBy(pos => pos.Possibilities.Count).ToList(); }
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { if (e.Modifiers != (KeyModifiers.Control | KeyModifiers.Shift) || e.Key != Keys.P) { return; } try { using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)) { Console.WriteLine("Getting screenshot"); using (Graphics g = Graphics.FromImage(bmpScreenCapture)) { g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy); } SetFocus(); var parser = new ImageParser(); parser.Parse(bmpScreenCapture); Console.WriteLine(parser.Output); if (string.IsNullOrWhiteSpace(parser.Values)) { Console.WriteLine("Could not understand the screenshot"); return; } _data = ColumnAndRowData.Parse(parser.Values); solveData(_data); } } catch (Exception ex) { Console.WriteLine("There was an error in the program: " + ex.Message); } }
private static void solveData(ColumnAndRowData data) { var solver = new Solver { MaxPuzzleLevel = 3 }; solver.Load(data); var now = DateTime.Now; solver.Solve(); var timespan = (DateTime.Now - now); Console.WriteLine("Evolution:"); displayEvolution(solver.Combinations); Console.WriteLine("Solved in: " + timespan.TotalMilliseconds + " milliseconds"); if (solver.MaxLevelReached > 1) { Console.WriteLine("Detected puzzle level:" + solver.MaxLevelReached); } Console.WriteLine(); var quit = false; Console.WriteLine("Partial solutions:"); var cursor = new[] { Console.CursorLeft, Console.CursorTop }; foreach (var board in solver.Boards) { if (_overlap) { Console.SetCursorPosition(cursor[0], cursor[1]); } displayBoard(board); if (_pressKeyToSeeSolutions) { Console.WriteLine("Press Space to see next partial solution, w to go to the end, q to stop display"); Readagain: var key = Console.ReadKey(true).Key; switch (key) { case ConsoleKey.Spacebar: break; case ConsoleKey.W: _pressKeyToSeeSolutions = false; break; case ConsoleKey.C: _charsIndex = (_charsIndex + 1) % _chars.Length; displayBoard(board); goto Readagain; case ConsoleKey.Q: quit = true; break; default: goto Readagain; } } if (quit) { break; } } ; if (quit) { Console.WriteLine("Solution display aborted"); } else { ConsoleColor?saved = null; if (solver.Solutions.Count != 1) { saved = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; } Console.WriteLine(); Console.WriteLine("Final solutions displayed (" + solver.Solutions.Count + ") :"); if (saved != null) { Console.ForegroundColor = saved.Value; } foreach (var solution in solver.Solutions) { displayBoard(solution); } saved = Console.ForegroundColor; if (solver.Solutions.Count != 1) { displayOverlapBoards(solver.Solutions); Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine("Final solution count = " + solver.FinalSolutionCount); if (saved != null) { Console.ForegroundColor = saved.Value; } Console.WriteLine(); } }