Beispiel #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);
        }
Beispiel #2
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);
        }