Exemple #1
0
        public bool ParseScreen(Board board, out List<Block> nonZeroValues)
        {
            Surface surface = dx.CaptureScreen();
            DataRectangle dr = surface.LockRectangle(LockFlags.None);
            DataStream gs = dr.Data;

            nonZeroValues = new List<Block>();
            bool initialBoard = true;

            if (xoffset == 0 || yoffset == 0)
                setOffset(gs);

            byte[] buffer = new byte[blockWidth * 4 * blockHeight];
            PixelColor[,] pixels = new PixelColor[blockWidth, blockHeight];
            for (int i = 0; i < board.Width; i++)
            {
                for (int j = 0; j < board.Height; j++)
                {
                    for (int k = 0; k < blockHeight; k++)
                    {
                        gs.Position = (k + GetYCoord(j)) * screenWidth * 4 + GetXCoord(i) * 4;
                        gs.Read(buffer, blockWidth * 4 * k, blockWidth * 4);
                    }
                    for (int a = 0; a < blockWidth; a++)
                    {
                        for (int b = 0; b < blockHeight; b++)
                        {
                            int offset = b * blockWidth * 4 + (a * 4);
                            pixels[b, a].Red = buffer[offset + 2];
                            pixels[b, a].Green = buffer[offset + 1];
                            pixels[b, a].Blue = buffer[offset + 0];
                        }
                    }
                    board.Grid[i, j] = blockParser.Parse(i, j, pixels);

                    if (board.Grid[i, j].State == BlockState.ParseFailed)
                    {
                        throw new ParserException("Point [" + i + ", " + j + "] failed! Pixel [" + GetXCoord(i) + "," + GetYCoord(j) + "]");
                    }
                    if (board.Grid[i, j].Value > 0)
                        nonZeroValues.Add(board.Grid[i, j]);

                    if (board.Grid[i, j].State != BlockState.Unknown)
                        initialBoard = false;
                }
            }

            gs.Close();
            surface.UnlockRectangle();
            surface.Dispose();

            return initialBoard;
        }
        //private void writeImage()
        //{
        //    BitmapSource currentScreen = Win8Parser.copyScreen();
        //    CroppedBitmap testBMP = new CroppedBitmap(currentScreen, new Int32Rect(new Win8Parser().GetXCoord(10),
        //        new Win8Parser().GetYCoord(13), 54, 54));
        //    var encoder = new PngBitmapEncoder();
        //    encoder.Frames.Add(BitmapFrame.Create(testBMP));
        //    using (var stream = File.OpenWrite("test.png"))
        //        encoder.Save(stream);
        //}
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            //writeImage();
            txtProgress.Text = "";
            Board board;
            try
            {
                board = new Board(new Win8Parser(), 30, 16);
            }
            catch (ParserException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            board.AutoGuess = chkAutoGuess.IsChecked.GetValueOrDefault();
            board.ProgressUpdate += board_ProgressUpdate;
            board.Finished += board_Finished;
            board.StartSolver();
        }
 private void board_ProgressUpdate(Board sender, ProgressArgs args)
 {
     txtProgress.Text += args.Message + "\n";
 }
 private void board_Finished(Board sender)
 {
     txtProgress.Text += "\n\n" + sender.Stats;
 }