Exemplo n.º 1
0
 private static void Main(string[] args)
 {
     try
     {
         AdbWrapper adbWrapper = new AdbWrapper();
         using (Bitmap screenshot = adbWrapper.GetScreenshot())
         {
             using (Form form = new Form())
             {
                 int maxWinSize = Math.Min(Screen.PrimaryScreen.WorkingArea.Height, Screen.PrimaryScreen.WorkingArea.Width);
                 int maxImgSize = Math.Max(screenshot.Width, screenshot.Height);
                 int scale      = (int)Math.Ceiling((double)maxImgSize / (double)maxWinSize);
                 int w          = 1080 / scale;
                 int h          = 1920 / scale;
                 form.ClientSize      = new Size(w, h);
                 form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                 form.BackgroundImage = screenshot.GetThumbnailImage(w, h, null, IntPtr.Zero);
                 form.MouseClick     += Program.Form_MouseClick;
                 form.ShowDialog();
                 Point  point  = (Point)form.Tag;
                 Random random = new Random();
                 for (; ;)
                 {
                     int dx = (int)((random.NextDouble() * 2.0 - 1.0) * 10.0);
                     int dy = (int)((random.NextDouble() * 2.0 - 1.0) * 10.0);
                     int x  = point.X * scale + dx;
                     int y  = point.Y * scale + dy;
                     Console.WriteLine(string.Format("tap {0} {1} ({2})", x, y, DateTime.Now));
                     adbWrapper.Tap(x, y, 200);
                     Thread.Sleep(1000 + (int)(random.NextDouble() * 1000.0));
                 }
             }
         }
     }
     catch (Exception innerException)
     {
         while (innerException != null)
         {
             Console.WriteLine(innerException.Message);
             Console.WriteLine(innerException.StackTrace);
             Console.WriteLine(innerException.Source);
             innerException = innerException.InnerException;
         }
         Console.ReadKey();
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("x for demo");
            bool demo = Console.ReadLine() == "x";

            if (demo)
            {
                ROITop    = 268;
                ROIBottom = 1651;
            }

            Color[][] raw   = new Color[Rows][];
            int[][]   board = new int[Rows][];
            for (int i = 0; i < Rows; i++)
            {
                raw[i]   = new Color[Cols];
                board[i] = new int[Cols];
            }

            AdbWrapper adb      = new AdbWrapper();
            Rectangle  ROI      = new Rectangle(ROILeft, ROITop, ROIRight - ROILeft + 1, ROIBottom - ROITop + 1);
            double     cwidth   = (double)ROI.Width / Cols;
            double     cheight  = (double)ROI.Height / Rows;
            int        icwidth  = (int)Math.Round(cwidth);
            int        icheight = (int)Math.Round(cheight);

            using (var img = adb.GetScreenshot())
                //using (var img = new Bitmap(@"D:\WeChat Image_20180711125059.jpg"))
                using (var crop = img.Clone(ROI, img.PixelFormat))
                {
                    BitmapData srcData = crop.LockBits(
                        new Rectangle(0, 0, crop.Width, crop.Height),
                        ImageLockMode.ReadOnly,
                        PixelFormat.Format32bppArgb);

                    for (int i = 0; i < Rows; i++)
                    {
                        for (int j = 0; j < Cols; j++)
                        {
                            int oft  = 20;
                            var rect = new Rectangle(
                                (int)Math.Round(j * cwidth) + oft,
                                (int)Math.Round(i * cheight) + oft,
                                icwidth - oft * 2,
                                icheight - oft * 2);
                            raw[i][j]   = CalculateAverageColor(srcData, rect);
                            board[i][j] = ClosestColorIndex(raw[i][j]);
                        }
                    }

                    crop.UnlockBits(srcData);
                }

            if (demo)
            {
                using (var sw = new StreamWriter(@"D:\a.txt"))
                {
                    sw.WriteLine("[");
                    for (int i = 0; i < Rows; i++)
                    {
                        for (int j = 0; j < Cols; j++)
                        {
                            var c = raw[i][j];
                            sw.WriteLine($"{c.R},{c.G},{c.B};");
                        }
                    }
                    sw.WriteLine("]");
                }
            }

            PrintBoard(board);

            Queue <Tuple <Point, Point> > solution = new Queue <Tuple <Point, Point> >();

            for (int i = 0; i < Rows - 1; i++)
            {
                for (int j = 0; j < Cols - 1; j++)
                {
                    if (board[i][j] > 0)
                    {
                        if (board[i][j] == board[i + 1][j])
                        {
                            solution.Enqueue(Tuple.Create(new Point(i, j), new Point(i + 1, j)));
                            board[i][j] = board[i + 1][j] = 0;
                        }
                        if (board[i][j] == board[i][j + 1])
                        {
                            solution.Enqueue(Tuple.Create(new Point(i, j), new Point(i, j + 1)));
                            board[i][j] = board[i][j + 1] = 0;
                        }
                    }
                }
            }

            while (solution.Count < Rows * Cols / 2)
            {
                for (int i = 0; i < Rows; i++)
                {
                    for (int j = 0; j < Cols; j++)
                    {
                        if (board[i][j] > 0)
                        {
                            var tar = BFS(new Point(i, j), board);
                            if (tar.X >= 0)
                            {
                                board[tar.X][tar.Y] = board[i][j] = 0;
                                solution.Enqueue(Tuple.Create(new Point(i, j), tar));
                                Console.WriteLine(solution.Count);
                            }
                        }
                    }
                }
            }

            PrintSolution(solution);

            while (solution.Count > 0)
            {
                var p = solution.Dequeue();
                adb.Tap(ROILeft + p.Item1.Y * icwidth + Offset, ROITop + p.Item1.X * icheight + Offset);
                adb.Tap(ROILeft + p.Item2.Y * icwidth + Offset, ROITop + p.Item2.X * icheight + Offset);
            }

            Console.WriteLine("End");
            Console.ReadKey();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Single Plyaer Mode?(Y/y)");
            bool singlePlayer = Console.ReadLine().ToLower().Contains("y");

            AdbWrapper adb = new AdbWrapper();

            int[][]       board       = new int[9][];
            StringBuilder sboard      = new StringBuilder();
            Rectangle     boardRegion = singlePlayer ? new Rectangle(55, 407, 966, 966) : new Rectangle(55, 330, 966, 966);
            float         offset      = 5;

            using (var img = adb.GetScreenshot())
                using (var boardImg = img.Clone(boardRegion, img.PixelFormat))
                {
                    using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.TesseractAndCube))
                    {
                        engine.SetVariable("tessedit_char_whitelist", "0123456789");
                        for (int i = 0; i < 9; i++)
                        {
                            board[i] = new int[9];
                            for (int j = 0; j < 9; j++)
                            {
                                float x = boardImg.Width / 9f * j + offset;
                                float y = boardImg.Height / 9f * i + offset;
                                float w = boardImg.Width / 9f - offset * 2f;
                                float h = boardImg.Height / 9f - offset * 2f;
                                using (var cell = boardImg.Clone(new RectangleF(x, y, w, h), boardImg.PixelFormat))
                                    using (var cellx = new Bitmap(cell, new Size(cell.Width / 4, cell.Height / 4)))
                                        using (var page = engine.Process(cellx, PageSegMode.SingleChar))
                                        {
                                            var text = page.GetText();
                                            if (string.IsNullOrWhiteSpace(text))
                                            {
                                                board[i][j] = 0;
                                                sboard.Append('.');
                                            }
                                            else
                                            {
                                                board[i][j] = text[0] - '0';
                                                sboard.Append(text[0]);
                                            }
                                        }
                            }
                            sboard.Append('\n');
                        }
                    }
                }

            Console.WriteLine(sboard);
            var option = StringSplitOptions.RemoveEmptyEntries;
            var mod    = Console.ReadLine().Split(new char[] { ' ' }, option).Select(a => int.Parse(a)).ToArray();

            while (mod.Length == 3)
            {
                sboard[mod[0] * 10 + mod[1]] = (char)(mod[2] + '0');
                mod = Console.ReadLine().Split(new char[] { ' ' }, option).Select(a => int.Parse(a)).ToArray();
            }
            Console.WriteLine(sboard);

            var puzzle = SudokuPuzzle.FromString(sboard.ToString(), 3, 3);

            puzzle.SolveGrid();
            Console.WriteLine(puzzle);

            int kx    = 160 + 75;
            int ky    = singlePlayer ? 1460 + 75 : 1356 + 75;
            int cellh = boardRegion.Height / 9;
            int cellw = boardRegion.Width / 9;
            int bx    = boardRegion.X + boardRegion.Width / 18;
            int by    = boardRegion.Y + boardRegion.Height / 18;

            int index = 0;

            foreach (var node in puzzle.GetNodes())
            {
                int v = node.Value;
                adb.Tap(kx + v % 5 * 150, ky + v / 5 * 150);
                adb.Tap(bx + index % 9 * cellw, by + index / 9 * cellh);
                index++;
            }
        }