Exemplo n.º 1
0
        /// <summary>
        /// 处理单个方块
        /// </summary>
        /// <param name="mine"></param>
        /// <returns></returns>
        private bool SweepMine(Mine mine)
        {
            if (!mine.Opened && !mine.Flagged)  //如果没打开,也没标记,则不处理当前方块
            {
                return(false);
            }

            if (mine.Opened && mine.MineNumber == 0) //如果是空白块,也不处理
            {
                return(false);
            }

            if (!FormHelper.IsWindow(WindowHandle))
            {
                Failed = true;
                return(false);
            }

            IntPtr Handle = FormHelper.FindWindow(null, "游戏胜利");

            if (Handle.ToInt32() > 0)
            {
                Failed = true;
                return(false);
            }
            Handle = FormHelper.FindWindow(null, "退出游戏");
            if (Handle.ToInt32() > 0)
            {
                Failed = true;
                return(false);
            }
            Handle = FormHelper.FindWindow(null, "游戏失败");
            if (Handle.ToInt32() > 0)
            {
                Failed = true;
                if (AutoRestart)
                {
                    RECT rc = new RECT();
                    FormHelper.GetWindowRect(Handle, ref rc);
                    Cursor.Position = new Point(rc.Right - 20, rc.Bottom - 30);
                    MouseHelper.Click(rc.Right - 20, rc.Bottom - 30);
                    Mines.Clear();
                    Thread.Sleep(1000);
                    Refresh();
                    return(Sweep());
                }
                else
                {
                    return(false);
                }
            }

            List <Mine> Neighbors = GetNeighbors(mine);

            bool hasMove = false;

            if (mine.MineNumber > 0)
            {
                var NotOpenedMines = Neighbors.FindAll(item => !item.Opened);            //周围没有点开的数量
                if (NotOpenedMines.Count > 0 && NotOpenedMines.Count == mine.MineNumber) //如果方格周围有等同数量未打开的方格,则标记
                {
                    NotOpenedMines.ForEach(item =>
                    {
                        if (!item.Opened) //有必要进行判断,集合里的属性有可能已经变动
                        {
                            if (!item.Flagged)
                            {
                                item.Flag();
                                hasMove = true;
                                Thread.Sleep(SleepMiliSeconds);
                            }
                        }
                    });
                }

                var flaggedmines = Neighbors.FindAll(item => item.Flagged);
                if (flaggedmines.Count > 0 && flaggedmines.Count == mine.MineNumber) //如果格子周围已标记了等同个数的方格,则每一个都点一遍
                {
                    Neighbors.ForEach(item =>
                    {
                        if (!flaggedmines.Contains(item) && !item.Opened && !item.Flagged) //有必要进行判断,集合里的属性有可能已经变动
                        {
                            item.Click();                                                  //点开
                            Thread.Sleep(SleepMiliSeconds);
                            //item.RefreshImage(); //刷新图片
                            //item.Recognize(); //再识别当前图片
                            //SweepMine(item); //对已经点开的方块进行排雷
                            hasMove = true;
                        }
                    });
                }
            }

            if (mine.Flagged)
            {
                SweepFlag(mine);
                Thread.Sleep(SleepMiliSeconds);
            }

            return(hasMove);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 刷新整个扫雷区的图片,并更新各个方块的图片
        /// </summary>
        public void Refresh()
        {
            FormHelper.SetForegroundWindow(WindowHandle);

            RECT rc = new RECT();

            FormHelper.GetWindowRect(WindowHandle, ref rc);

            int windowwidth  = rc.Right - rc.Left;
            int windowheight = rc.Bottom - rc.Top;

            Point startPoint = new Point(38, 80);
            Point endPoint   = new Point(580, 370);

            MineXCount = 30;
            MineYCount = 16;

            if (windowwidth < 300 && windowheight < 300)
            {
                startPoint = new Point(38, 80);
                endPoint   = new Point(202, 244);
                MineXCount = 9;
                MineYCount = 9;
            }
            else if (windowwidth < 380 && windowheight < 420)
            {
                startPoint = new Point(38, 80);
                endPoint   = new Point(328, 370);
                MineXCount = 16;
                MineYCount = 16;
            }

            int zonewidth  = Width = endPoint.X - startPoint.X;
            int zoneheight = Height = endPoint.Y - startPoint.Y;

            //Mines.Clear();

            Bitmap zoneImage = new Bitmap(zonewidth, zoneheight);

            using (Bitmap windowimage = new Bitmap(windowwidth, windowheight))
            {
                using (Graphics windowgp = Graphics.FromImage(windowimage), zonegp = Graphics.FromImage(zoneImage))
                {
                    IntPtr windowdc = windowgp.GetHdc();
                    FormHelper.PrintWindow(WindowHandle, windowdc, 0);
                    windowgp.ReleaseHdc();

                    zonegp.DrawImage(windowimage, 0, 0, new Rectangle(startPoint.X, startPoint.Y, zonewidth, zoneheight), GraphicsUnit.Pixel);
                    float  MineWidth  = 18.06f;
                    float  MineHeight = 18.06f;
                    float  MineY      = 0;
                    float  MineX      = 0;
                    Bitmap mineImage  = null;
                    for (int xindex = 0; xindex < MineXCount; xindex++)
                    {
                        for (int yindex = 0; yindex < MineYCount; yindex++)
                        {
                            Mine mineItem = Mines.Find(item => item.XIndex == xindex && item.YIndex == yindex);
                            if (mineItem == null)
                            {
                                MineX     = xindex * MineWidth + startPoint.X;
                                MineY     = yindex * MineHeight + startPoint.Y;
                                mineImage = new Bitmap(18, 18);
                                using (Graphics minegp = Graphics.FromImage(mineImage))
                                {
                                    minegp.DrawImage(windowimage, 0, 0, new RectangleF(MineX, MineY, 18, 18), GraphicsUnit.Pixel);
                                }
                                mineItem            = new Mine(mineImage, WindowHandle);
                                mineItem.XIndex     = xindex;
                                mineItem.YIndex     = yindex;
                                mineItem.Width      = MineWidth;
                                mineItem.Height     = MineHeight;
                                mineItem.Position.X = rc.Left + MineX;
                                mineItem.Position.Y = rc.Top + MineY;
                                Mines.Add(mineItem);
                            }
                            else // if (!mineItem.Recoginized)
                            {
                                MineX     = xindex * MineWidth + startPoint.X;
                                MineY     = yindex * MineHeight + startPoint.Y;
                                mineImage = new Bitmap(18, 18);
                                using (Graphics minegp = Graphics.FromImage(mineImage))
                                {
                                    minegp.DrawImage(windowimage, 0, 0, new RectangleF(MineX, MineY, 18, 18), GraphicsUnit.Pixel);
                                }
                                mineItem.MineImage = mineImage;
                            }
                        }
                    }
                }
            }
            ZoneImage = zoneImage;
        }
Exemplo n.º 3
0
        /// <summary>
        /// 扫雷
        /// </summary>
        public bool Sweep()
        {
            if (!FormHelper.IsWindow(WindowHandle))
            {
                return(false);
            }

            IntPtr Handle = FormHelper.FindWindow(null, "游戏胜利");

            if (Handle.ToInt32() > 0)
            {
                return(false);
            }
            Handle = FormHelper.FindWindow(null, "退出游戏");
            if (Handle.ToInt32() > 0)
            {
                return(false);
            }
            Handle = FormHelper.FindWindow(null, "游戏失败");
            if (Handle.ToInt32() > 0)
            {
                if (AutoRestart)
                {
                    RECT rc = new RECT();
                    FormHelper.GetWindowRect(Handle, ref rc);
                    Cursor.Position = new Point(rc.Right - 20, rc.Bottom - 30);
                    MouseHelper.Click(rc.Right - 20, rc.Bottom - 30);
                    Mines.Clear();
                    Thread.Sleep(1000);
                    Refresh();
                    return(Sweep());
                }
                else
                {
                    return(false);
                }
            }

            FormHelper.SetForegroundWindow(WindowHandle);
            Thread.Sleep(20);

            var opened = Mines.FindAll(item => item.Opened);

            if (opened.Count == 0)
            {
                RandomClick();
                Thread.Sleep(200);
                return(true);
            }
            int move = 0;

            foreach (Mine mine in Mines)
            {
                if (SweepMine(mine))
                {
                    move++;
                }

                if (Failed)
                {
                    break;
                }
            }

            if (!Failed)
            {
                if (move == 0) //没有动作时,随便点击一个
                {
                    RandomClick();
                    Thread.Sleep(300);
                    move++;
                }
            }

            Failed = false;

            return(move > 0);
        }