Пример #1
0
 public Mine(Vector2 position) : base(position)
 {
     this.Energy     = 240;
     this._mineState = new InactiveState(this);
     this.Properties.Set(GameObjectProperties.DrawOrder, (int)SpriteDrawOrder.StaticItem);
     this.Properties.Set(GameObjectProperties.Solidity, ObjectSolidity.Stationary);
 }
Пример #2
0
 public void DeActivate()
 {
     State = MineState.INACTIVE;
     _renderer.enabled = false;
     _bobTimer = 0;
     _counter = 0;
     _renderer.material.SetFloat("_Explosion", 0);
 }
Пример #3
0
 public Mine(int playerId, Vec2Double position, Vec2Double size, MineState state, double?timer, double triggerRadius, ExplosionParameters explosionParameters)
 {
     PlayerId            = playerId;
     Position            = position;
     Size                = size;
     State               = state;
     Timer               = timer;
     TriggerRadius       = triggerRadius;
     ExplosionParameters = explosionParameters;
 }
Пример #4
0
 public Mine(World world, Vector2 position)
     : base(world, position)
 {
     this.Energy = 240;
     this._staticUnarmedAnimation = Animation.StaticAnimation(world, "Sprites/Shot/MineUnarmed");
     this._movingUnarmedAnimation = Animation.SingleRunAnimation(world, "Sprites/Shot/MineUnarmed", 4);
     this._armedAnimation = Animation.LoopingAnimation(world, "Sprites/Shot/MineArmed", 4);
     this.Ap.PlayAnimation(this._staticUnarmedAnimation);
     this._mineState = new InactiveState(this);
 }
        public MineFieldCell(int row, int col, MineCell mineCell, Func <MineFieldCell, Task> leftClick, Func <MineFieldCell, Task> rightClick)
            : this()
        {
            Row      = row;
            Col      = col;
            MineCell = mineCell;

            _previousState = MineState.Covered;
            _leftClick     = leftClick;
            _rightClick    = rightClick;
        }
        public MineFieldCell(int row, int col, MineCell mineCell, Func<MineFieldCell, Task> leftClick, Func<MineFieldCell, Task> rightClick)
            : this()
        {
            Row = row;
            Col = col;
            MineCell = mineCell;

            _previousState = MineState.Covered;
            _leftClick = leftClick;
            _rightClick = rightClick;
        }
Пример #7
0
 void OnTriggerEnter(Collider other)
 {
     if (GameManager.Instance.PlayersArr.Count > 1)
     {
         if (State == MineState.IDLE || State == MineState.RELEASED || State == MineState.BOBBING)
         {
             GetComponent<AudioSource>().Play();
             other.GetComponent<Player>().DIE();
             State = MineState.EXPLODING;
         }
     }
 }
        public void RefreshDisplay(bool flagMode, GameState gameState)
        {
            var mineState = (gameState == GameState.Victory || gameState == GameState.Defeat) ? MineCell.GetState() : MineCell.GetVisibleState(flagMode);

            if (_previousState == mineState)
            {
                return;
            }

            _previousState = mineState;

            switch (mineState)
            {
            case MineState.Covered:
                Cover();
                SetText("");
                break;

            case MineState.CoveredFlagMode:
                Cover();
                SetElementVisible(GrayFlag);
                break;

            case MineState.Uncovered:
                Uncover(new SolidColorBrush(GetNumberColor(MineCell.NeighbourhoodMineCount)));
                SetText(MineCell.NeighbourhoodMineCount.ToString());
                break;

            case MineState.Flagged:
                Cover();
                SetElementVisible(RedFlag);
                break;

            case MineState.IncorrectlyFlagged:
                Cover();
                SetElementVisible(FalseFlag);
                break;

            case MineState.Mine:
                Uncover();
                SetElementVisible(Mine);
                break;

            case MineState.MineDetonated:
                Uncover();
                SetElementVisible(MineDetonated);
                break;
            }
        }
Пример #9
0
    void DoExplode()
    {
        _counter += Time.deltaTime;

        if (_counter >= ExplodeTime) {
            State = MineState.INACTIVE;
            _renderer.enabled = false;
            _counter = 0;
            _renderer.material.SetFloat ("_Explosion", 0);
            return;
        }

        float progress = _counter / ExplodeTime;
        _renderer.material.SetFloat ("_Explosion", progress);
    }
        public void RefreshDisplay(bool flagMode, GameState gameState)
        {
            var mineState = (gameState == GameState.Victory || gameState == GameState.Defeat) ? MineCell.GetState() : MineCell.GetVisibleState(flagMode);

            if (_previousState == mineState)
            {
                return;
            }

            _previousState = mineState;

            switch (mineState)
            {
                case MineState.Covered:
                    Cover();
                    SetText("");
                    break;
                case MineState.CoveredFlagMode:
                    Cover();
                    SetElementVisible(GrayFlag);
                    break;
                case MineState.Uncovered:
                    Uncover(new SolidColorBrush(GetNumberColor(MineCell.NeighbourhoodMineCount)));
                    SetText(MineCell.NeighbourhoodMineCount.ToString());
                    break;
                case MineState.Flagged:
                    Cover();
                    SetElementVisible(RedFlag);
                    break;
                case MineState.IncorrectlyFlagged:
                    Cover();
                    SetElementVisible(FalseFlag);
                    break;
                case MineState.Mine:
                    Uncover();
                    SetElementVisible(Mine);
                    break;
                case MineState.MineDetonated:
                    Uncover();
                    SetElementVisible(MineDetonated);
                    break;
            }
        }
Пример #11
0
 public void MineFlagged(GameTime gameTime)
 {
     if (mineState == Minesweeper_XNA.MineState.UncoveredMine ||
         mineState == Minesweeper_XNA.MineState.UncoveredNone ||
         mineState == Minesweeper_XNA.MineState.UncoveredNumber)
         return;
     if (mineState == Minesweeper_XNA.MineState.HiddenFlaged)
         mineState = Minesweeper_XNA.MineState.HiddenQuestion;
     else if (mineState == Minesweeper_XNA.MineState.HiddenQuestion)
         mineState = Minesweeper_XNA.MineState.Hidden;
     else if (mineState == Minesweeper_XNA.MineState.Hidden)
         mineState = Minesweeper_XNA.MineState.HiddenFlaged;
 }
Пример #12
0
        //查找某个点附件某种型号的方块
        private List <BoxLocation> SearchMines(int locx, int locy, int MaxX, int MaxY, MineState mineType, MineState[,] mineMaps)
        {
            List <Point> points = new List <Point>();

            points.Add(new Point(locx - 1, locy - 1));
            points.Add(new Point(locx - 1, locy));
            points.Add(new Point(locx - 1, locy + 1));
            points.Add(new Point(locx, locy - 1));
            points.Add(new Point(locx, locy + 1));
            points.Add(new Point(locx + 1, locy - 1));
            points.Add(new Point(locx + 1, locy));
            points.Add(new Point(locx + 1, locy + 1));

            List <BoxLocation> mines = new List <BoxLocation>();

            foreach (Point p in points)
            {
                int x = p.X;
                int y = p.Y;

                if (x >= 0 && x < MaxX && y >= 0 && y < MaxY && mineMaps[x, y] == mineType)
                {
                    mines.Add(new BoxLocation(x, y, mineType));
                }
            }

            return(mines);
        }
Пример #13
0
        private void Recognize()
        {
            //常量
            int MaxX      = 20;
            int MaxY      = 12;
            int RectWidth = 30;

            //获取窗口位置
            Rect rect = GetWindowRect();

            int left = rect.Left;
            int top  = rect.Top;

            //获取游戏状态(笑脸)
            GameState gameState = GetGameState();

            //非游戏状态
            if (gameState != GameState.Ready)
            {
                ShowDebugInfo("\r\nGame over!");
                this.timer.Enabled = false;
                return;
            }


            //截取游戏主要区域

            int width  = rect.Right - rect.Left;
            int height = rect.Bottom - rect.Top;

            int centerleft   = 21;
            int centertop    = 93;
            int centerwidth  = MaxX * RectWidth;
            int centerheight = MaxY * RectWidth;

            Bitmap bitmapCenter = new Bitmap(centerwidth, centerheight);

            using (Graphics graphics = Graphics.FromImage(bitmapCenter))
            {
                graphics.CopyFromScreen(left + centerleft, top + centertop, 0, 0, new Size(centerwidth, centerheight));
                this.pictureBox1.Image?.Dispose();
                this.pictureBox1.Image = bitmapCenter;
            }

            //识别

            MineState[,] MineMap = new MineState[MaxX, MaxY];

            for (int x = 0; x < MaxX; x++)
            {
                for (int y = 0; y < MaxY; y++)
                {
                    Color center = bitmapCenter.GetPixel(x * RectWidth + RectWidth / 2, y * RectWidth + RectWidth / 2);

                    if (center.R == 220 && center.G == 220 && center.B == 220)
                    {
                        MineMap[x, y] = MineState.Unknow;
                    }
                    else if (center.R == 233 && center.G == 233 && center.B == 233)
                    {
                        MineMap[x, y] = MineState.None;
                    }
                    else if (center.R == 90 && center.G == 219 && center.B == 117)
                    {
                        MineMap[x, y] = MineState.One;
                    }
                    else if (center.R == 8 && center.G == 126 && center.B == 33)
                    {
                        MineMap[x, y] = MineState.Two;
                    }
                    else if (center.R == 36 && center.G == 70 && center.B == 243)
                    {
                        MineMap[x, y] = MineState.Three;
                    }
                    else if (center.R == 27 && center.G == 48 && center.B == 155)
                    {
                        MineMap[x, y] = MineState.Four;
                    }
                    else if (center.R == 178 && center.G == 82 && center.B == 209)
                    {
                        MineMap[x, y] = MineState.Five;
                    }
                    else if (center.R == 228 && center.G == 141 && center.B == 28)
                    {
                        MineMap[x, y] = MineState.Six;
                    }
                    else if (center.R == 192 && center.G == 30 && center.B == 227)
                    {
                        MineMap[x, y] = MineState.Seven;
                    }
                    else if (center.R == 218 && center.G == 8 && center.B == 186)
                    {
                        MineMap[x, y] = MineState.Eight;
                    }
                    else if (center.R == 244 && center.G == 35 && center.B == 50)
                    {
                        MineMap[x, y] = MineState.Mine;
                    }
                    else
                    {
                        ShowDebugInfo($"\r\nRecognize Error!({x},{y}):{center.R},{center.G},{center.B}");
                        this.timer.Enabled = false;
                        return;
                    }
                }
            }

            //绘制识别结果
            Bitmap bitmapDebug = new Bitmap(centerwidth, centerheight);

            using (Graphics graphics = Graphics.FromImage(bitmapDebug))
            {
                string s = "";

                for (int x = 0; x < MaxX; x++)
                {
                    for (int y = 0; y < MaxY; y++)
                    {
                        switch (MineMap[x, y])
                        {
                        case MineState.Unknow: s = "?"; break;

                        case MineState.None: s = "0"; break;

                        case MineState.One: s = "1"; break;

                        case MineState.Two: s = "2"; break;

                        case MineState.Three: s = "3"; break;

                        case MineState.Four: s = "4"; break;

                        case MineState.Five: s = "5"; break;

                        case MineState.Six: s = "6"; break;

                        case MineState.Seven: s = "7"; break;

                        case MineState.Eight: s = "8"; break;

                        case MineState.Mine: s = "9"; break;

                        default: s = "X"; break;
                        }
                        graphics.DrawString(s, new Font("宋体", 12), Brushes.Black, x * RectWidth, y * RectWidth);
                    }
                }

                this.pictureBox2.Image?.Dispose();
                this.pictureBox2.Image = bitmapDebug;
            }


            //下面开始计算应该点击的点
            List <ClickEvent> clickEventList = new List <ClickEvent>();
            bool FindGoodPoint = false;   //如果找到合适的点,就完成本次运算


            //第一步:基础算法...计算点击位置
            Debug.WriteLine("1基础算法...");
            for (int x = 0; x < MaxX; x++)
            {
                for (int y = 0; y < MaxY; y++)
                {
                    int MineCount = (int)MineMap[x, y];

                    if (MineCount >= 1)
                    {
                        List <BoxLocation> unknowBoxs = SearchMines(x, y, MaxX, MaxY, MineState.Unknow, MineMap);   //附近未开盒子
                        List <BoxLocation> MineBoxs   = SearchMines(x, y, MaxX, MaxY, MineState.Mine, MineMap);     //附件已确定的雷

                        if (unknowBoxs.Count > 0)
                        {
                            //主要算法1:数字和周围已经标记的雷数一致,所有未知位置都不是雷,左键点开
                            if (MineBoxs.Count == MineCount)
                            {
                                clickEventList.Clear();
                                foreach (var box in unknowBoxs)
                                {
                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.LeftClick));
                                }
                                FindGoodPoint = true;
                                break;
                            }

                            //主要算法2:中心数字=未知位置数量+周围已经标记的雷数 :所有未知位置为雷,右键标记
                            if (MineBoxs.Count + unknowBoxs.Count == MineCount)
                            {
                                clickEventList.Clear();
                                foreach (var box in unknowBoxs)
                                {
                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.RightClick));
                                }
                                FindGoodPoint = true;
                                break;
                            }
                        }
                    }
                }

                if (FindGoodPoint)
                {
                    break;
                }
            }


            //第二步:补充算法.....第一次没有计算出合适的点,用复杂算法再算一次
            if (!FindGoodPoint)
            {
                Debug.WriteLine("2补充算法....");

                //计算
                List <UnknowBoxSum> UnknowBoxSumList = new List <UnknowBoxSum>();

                for (int locx = 0; locx < MaxX; locx++)
                {
                    for (int locy = 0; locy < MaxY; locy++)
                    {
                        int MineCount = (int)MineMap[locx, locy];

                        if (MineCount >= 1 && MineCount <= 6)
                        {
                            List <BoxLocation> unknowBoxs = SearchMines(locx, locy, MaxX, MaxY, MineState.Unknow, MineMap);   //附近未开盒子
                            List <BoxLocation> MineBoxs   = SearchMines(locx, locy, MaxX, MaxY, MineState.Mine, MineMap);     //附件已确定的雷

                            if (unknowBoxs.Count >= 2)
                            {
                                UnknowBoxSum unknowBoxSum = new UnknowBoxSum();
                                unknowBoxSum.Boxes = unknowBoxs;
                                unknowBoxSum.Sum   = MineCount - MineBoxs.Count;
                                UnknowBoxSumList.Add(unknowBoxSum);
                            }
                        }
                    }
                }

                Debug.WriteLine($"UnknowBoxSumList={UnknowBoxSumList.Count}");

                if (UnknowBoxSumList.Count > 0)
                {
                    //匹配
                    for (int locx = 0; locx < MaxX; locx++)
                    {
                        for (int locy = 0; locy < MaxY; locy++)
                        {
                            int MineCount = (int)MineMap[locx, locy];

                            if (MineCount >= 1 && MineCount <= 8)
                            {
                                List <BoxLocation> unknowBoxs = SearchMines(locx, locy, MaxX, MaxY, MineState.Unknow, MineMap);   //附近未开盒子
                                List <BoxLocation> MineBoxs   = SearchMines(locx, locy, MaxX, MaxY, MineState.Mine, MineMap);     //附件已确定的雷

                                if (unknowBoxs.Count >= 2)
                                {
                                    foreach (var UnknowBoxSum in UnknowBoxSumList)
                                    {
                                        if (unknowBoxs.Count - UnknowBoxSum.Boxes.Count == 1)
                                        {
                                            if (UnknowBoxSum.MatchBox(unknowBoxs))   //先判断是否匹配
                                            {
                                                //数字-雷==Sum:必不是雷
                                                if ((MineCount - MineBoxs.Count - UnknowBoxSum.Sum) == 0)
                                                {
                                                    //取出
                                                    BoxLocation box = UnknowBoxSum.GetNotBelongBox(unknowBoxs);

                                                    Debug.WriteLine($"Match(必不是雷):({locx},{locy})->({box.LocationX},{box.LocationY})");

                                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.LeftClick));


                                                    FindGoodPoint = true;
                                                    break;
                                                }

                                                //数字-减雷-Sum=1:必是雷
                                                if ((MineCount - MineBoxs.Count - UnknowBoxSum.Sum) == 1)
                                                {
                                                    //取出
                                                    BoxLocation box = UnknowBoxSum.GetNotBelongBox(unknowBoxs);
                                                    Debug.WriteLine($"Match(必是雷):({locx},{locy})->({box.LocationX},{box.LocationY})");

                                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.RightClick));

                                                    FindGoodPoint = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (FindGoodPoint)
                            {
                                break;
                            }
                        }

                        if (FindGoodPoint)
                        {
                            break;
                        }
                    }
                }
            }


            //第三步:实在没有找到合适的点,只能随机点开
            if (!FindGoodPoint)
            {
                Debug.WriteLine("3随机点开...");

                float[,] ProbabilityMap = new float[MaxX, MaxY];

                //计算最小概率
                float MinProbability = 1;

                for (int x = 0; x < MaxX; x++)
                {
                    for (int y = 0; y < MaxY; y++)
                    {
                        if (MineMap[x, y] == MineState.Unknow)
                        {
                            ProbabilityMap[x, y] = CalcProbability(x, y, MaxX, MaxY, MineMap);

                            if (ProbabilityMap[x, y] < MinProbability)
                            {
                                MinProbability = ProbabilityMap[x, y];
                            }
                        }
                    }
                }
                Debug.WriteLine($"MinProbability={MinProbability}");

                //取出最小概率的点
                for (int x = 0; x < MaxX; x++)
                {
                    for (int y = 0; y < MaxY; y++)
                    {
                        if (MineMap[x, y] == MineState.Unknow && Math.Abs(ProbabilityMap[x, y] - MinProbability) < 0.001)
                        {
                            clickEventList.Add(new ClickEvent(x, y, ClickType.LeftClick));
                        }
                    }
                }

                /*
                 * foreach (var clickEvent in clickEventList)
                 * {
                 *  Debug.WriteLine($"selected clickEvent:{clickEvent}");
                 * }
                 */

                int Count = clickEventList.Count;
                ShowDebugInfo($"\r\nRandom Select:Count={Count}");

                if (Count == 0)
                {
                    ShowDebugInfo("\r\n clickEventList.Count == 0,I can't do anything!");
                    this.timer.Enabled = false;
                    return;
                }
                else
                {
                    if (Count == 1)
                    {
                        ClickEvent clickEvent = clickEventList[0];
                    }
                    else
                    {
                        Random     random     = new Random();
                        ClickEvent clickEvent = clickEventList[random.Next(Count)];
                        clickEventList.Clear();
                        clickEventList.Add(clickEvent);
                    }
                }
            }


            //点击屏幕
            ClickScreen(RectWidth, left, top, centerleft, centertop, clickEventList);
        }
Пример #14
0
 public string GetMineStateStr()
 {
     return(MineState.GetEnumDescription());
 }
Пример #15
0
 public static IEnumerable <MineCell> FilterByVisibleState(this IEnumerable <MineCell> cells, MineState visibleState)
 => cells.Where(c => c.GetVisibleState(false) == visibleState);
Пример #16
0
 public override void Initialize()
 {
     mineState = MineState.Hidden;
     base.Initialize();
 }
Пример #17
0
    public override void Update()
    {
        base.Update();

        if (refineryHomebase == null)
        {
            refineryHomebase = FindOreRefinery();
            if (refineryHomebase == null)
            {
                return;
            }
        }


        switch (mineState)
        {
        case MineState.IDLE:
            //check if both minerals and a ore factory exists, iff -> SEARCH
            float shortestDistance = Vector3.Distance(transform.position, resources[0].transform.position);
            closestResource = resources[0];
            for (int i = 1; i < resources.Length; i++)
            {
                float distance = Vector3.Distance(transform.position, resources[i].transform.position);
                if (distance < shortestDistance)
                {
                    shortestDistance = distance;
                    closestResource  = resources[i];
                }
            }

            ai.destination = closestResource.transform.position;
            ai.SearchPath();

            mineState = MineState.SEARCH;

            break;

        case MineState.SEARCH:
            if ((ai.reachedEndOfPath && !ai.pathPending) || ai.remainingDistance < 6)
            {
                mineState = MineState.MINE;
                mineTime  = Time.time;
            }

            break;

        case MineState.MINE:

            if (mineTime + mineInterval < Time.time)
            {
                addOreToInventory(closestResource.getResourceValue());
                mineTime = Time.time;
            }
            if (inventorySize <= currentInventory)
            {
                if (refineryHomebase)
                {
                    ai.destination = refineryHomebase.GetEntrancePosition();
                    ai.SearchPath();
                    mineState = MineState.RETURN;
                }
            }
            break;

        case MineState.RETURN:

            if ((ai.reachedEndOfPath && !ai.pathPending) && refineryHomebase)
            {
                enterUnloadState();


                Vector3 _direction = (refineryHomebase.transform.position - transform.position).normalized;

                Quaternion _lookRotation = Quaternion.LookRotation(_direction);

                transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, 0.35f);



                transform.position = Vector3.MoveTowards(transform.position, refineryHomebase.GetUnloadPosition(), 0.2f);
                if (Vector3.Distance(transform.position, refineryHomebase.GetUnloadPosition()) < 1f)
                {
                    mineState = MineState.UNLOAD;
                }
            }
            break;

        case MineState.UNLOAD:
            refineryHomebase.AddResources(inventoryValue);
            Reset();
            mineState = MineState.EXIT;
            break;

        case MineState.EXIT:

            if (refineryHomebase)
            {
                transform.position = Vector3.MoveTowards(transform.position, refineryHomebase.GetEntrancePosition(), 0.3f);
                if (Vector3.Distance(transform.position, refineryHomebase.GetEntrancePosition()) < 2f)
                {
                    GetComponent <Collider>().enabled = true;
                    //ai.gravity = savedGravity;
                    ai.enabled = true;
                    mineState  = MineState.IDLE;
                }
            }
            break;
        }
    }
Пример #18
0
        public void MineMouseDown(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) && ((_state == MineState.Close || State == MineState.Question)))
            {
                if (_hasMine)
                {
                    if (OnGameOver != null)
                    {
                        SoundPlayer snd = new SoundPlayer(Path.Combine(Application.StartupPath, "Bomb.wav"));
                        snd.Play();
                        this.State = MineState.Open;
                        OnGameOver();
                    }
                }

                else if (_mineArround == 0)
                {
                    if (onClickNull != null && onSuccessEndGame != null)
                    {
                        this.State = MineState.Open;
                        if (_gameLevel == MineGameLevel.Beginner)
                        {
                            onClickNull(((this.Index - 1) / 9), ((this.Index - 1) % 9));
                        }
                        if (_gameLevel == MineGameLevel.Intermediate)
                        {
                            onClickNull(((this.Index - 1) / 16), ((this.Index - 1) % 16));
                        }
                        if (_gameLevel == MineGameLevel.Expert)
                        {
                            onClickNull(((this.Index - 1) / 25), ((this.Index - 1) % 25));
                        }

                        onSuccessEndGame();
                    }
                }

                else
                {
                    this.State = MineState.Open;
                    if (onSuccessEndGame != null)
                    {
                        onSuccessEndGame();
                    }
                }
            }

            else if ((e.Button == MouseButtons.Right) && (_state == MineState.Close))
            {
                if (onSuccessEndGame != null && OnClickFlagorQuestion != null)
                {
                    this.State = MineState.Flag;
                    onSuccessEndGame();
                    OnClickFlagorQuestion(true);
                }
            }
            else if ((e.Button == MouseButtons.Right) && (_state == MineState.Flag))
            {
                if (OnClickFlagorQuestion != null)
                {
                    OnClickFlagorQuestion(false);
                    this.State = MineState.Question;
                }
            }
            else if ((e.Button == MouseButtons.Right) && (_state == MineState.Question))
            {
                this.State = MineState.Close;
            }
        }
Пример #19
0
 public BoxLocation(int x, int y, MineState state)
 {
     LocationX = x;
     LocationY = y;
     MineState = state;
 }
Пример #20
0
 public void Reset()
 {
     State = MineState.IDLE;
     _renderer.enabled = true;
 }
Пример #21
0
 public static IEnumerable<MineCell> FilterByVisibleState(this IEnumerable<MineCell> cells, MineState visibleState)
     => cells.Where(c => c.GetVisibleState(false) == visibleState);
Пример #22
0
 public void Boom()
 {
     State = MineState.Detonated;
 }
Пример #23
0
 /// <summary>
 /// Tells the tile the input device has been released
 /// </summary>
 /// <param name="gameTime">the gameTime object of the update call</param>
 /// <param name="sameMine">True if the mine should be revealed</param>
 /// <returns>Whether or not the tile is a 0 tile</returns>
 public bool MineReleased(GameTime gameTime, bool sameMine)
 {
     if (mineState == Minesweeper_XNA.MineState.HiddenSelected)
     {
         if (!sameMine)
         {
             mineState = Minesweeper_XNA.MineState.Hidden;
         }
         else if (IsMine)
         {
             mineState = Minesweeper_XNA.MineState.UncoveredMine;
         }
         else if (surroundingMines > 0)
         {
             mineState = Minesweeper_XNA.MineState.UncoveredNumber;
         }
         else
         {
             mineState = Minesweeper_XNA.MineState.UncoveredNone;
             return true;
         }
     }
     return false;
 }
Пример #24
0
 public void MinePressed(GameTime gameTime)
 {
     if (mineState == Minesweeper_XNA.MineState.Hidden ||
         mineState == Minesweeper_XNA.MineState.HiddenQuestion)
         mineState = Minesweeper_XNA.MineState.HiddenSelected;
 }
Пример #25
0
        private void Recognize()
        {
            //Không đổi


            //Nhận trạng thái trò chơi
            GameState gameState = GetGameState();

            //Trạng thái không phải trò chơi
            if (gameState != GameState.Ready)
            {
                ShowDebugInfo("\r\nGame over!");


                if (chkAutoRestart.Checked)
                {
                    StartGame();
                }
                else
                {
                    this.timer.Enabled = false;
                    return;
                }
            }


            //Xác định ví trí của trò chơi
            //Chiều ngang max ô là :
            int MaxX = 30;
            //Chiều dọc max ô là :
            int MaxY = 16;
            //chu vi  :
            int RectWidth = 16;
            // Tọa độ ô đầu tiên bên trái
            int left = 12;
            // Tọa độ ô đầu tiên bên  trên
            int top = 55;
            // chiều ngang bao nhiêu ô( tính pixel )
            int width = 16;
            // chiều dọc có bao nhiêu ô ( tính pixel )
            int height = 16;
            // vị trí trung tâm ô đầu tiên
            int centerleft = 20;
            int centertop  = 63;


            // bỏ ảnh vào ô
            this.pictureBox1.Image?.Dispose();
            dm.FreePic("info.bmp");

            dm.DeleteFile("info.bmp");
            dm.Capture(14, 56, 495, 313, "info.bmp");

            this.pictureBox1.Image = Image.FromFile("info.bmp");
            //

            //Xác định ma trận

            MineState[,] MineMap = new MineState[MaxX, MaxY];

            for (int x = 0; x < MaxX; x++)
            {
                for (int y = 0; y < MaxY; y++)
                {
                    // unknow là chưa khám phá , do trung tâm trùng màu trắng với ô None ( ô không có gì ) nên để lệch pixel
                    if (dm.CmpColor(12 + x * width, 63 + y * height, "ffffffff", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Unknow;
                    }

                    if (dm.CmpColor(20 + x * width, 63 + y * height, "0000ff", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.One;
                    }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "008000", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Two;
                    }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "ff0000", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Three;
                    }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "000080", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Four;
                    }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "800000", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Five;
                    }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "008080", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Six;
                    }
                    // chưa rảnh làm , chia 2 thằng này do nó trùng với mine nên gây lỗi
                    //   if (dm.CmpColor(22 + x * width, 63 + y * height, "000000" , 1.0) == 0) { MineMap[x, y] = MineState.Seven; }
                    //  if (dm.CmpColor(20 + x * width, 63 + y * height, "808080", 1.0) == 0) { MineMap[x, y] = MineState.Eight; }
                    if (dm.CmpColor(20 + x * width, 63 + y * height, "000000", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.Mine;
                    }
                    //tương tự unknow tìm pixel trong ô cho nó có đặc trưng riêng
                    if (dm.CmpColor(12 + x * width, 63 + y * height, "c0c0c0", 1.0) == 0)
                    {
                        MineMap[x, y] = MineState.None;
                    }
                }
            }

            //Vẽ vào ô - thấy thừa quá nên bỏ
            //Bitmap bitmapDebug = new Bitmap(255, 185);
            //using (Graphics graphics = Graphics.FromImage(bitmapDebug))
            //{
            //    string s = "";

            //    for (int x = 0; x < MaxX; x++)
            //        for (int y = 0; y < MaxY; y++)
            //        {
            //            switch (MineMap[x, y])
            //            {
            //                case MineState.Unknow: s = "?"; break;
            //                case MineState.None: s = "0"; break;
            //                case MineState.One: s = "1"; break;
            //                case MineState.Two: s = "2"; break;
            //                case MineState.Three: s = "3"; break;
            //                case MineState.Four: s = "4"; break;
            //                case MineState.Five: s = "5"; break;
            //                case MineState.Six: s = "6"; break;
            //                case MineState.Seven: s = "7"; break;
            //                case MineState.Eight: s = "8"; break;
            //                case MineState.Mine: s = "9"; break;
            //                default: s = "X"; break;
            //            }
            //            graphics.DrawString(s, new Font("Arial", 8), Brushes.Black, x * 8, y * 11);
            //        }

            //    this.pictureBox2.Image?.Dispose();
            //    this.pictureBox2.Image = bitmapDebug;
            //}

            List <ClickEvent> clickEventList = new List <ClickEvent>();
            bool FindGoodPoint = false;   //Nếu tìm thấy một điểm thích hợp, việc tính toán được hoàn thành


            //Bước 1: Thuật toán cơ bản ...
            //  Debug.WriteLine("1 thuật toán cơ bản ...");
            for (int x = 0; x < MaxX; x++)
            {
                for (int y = 0; y < MaxY; y++)
                {
                    int MineCount = (int)MineMap[x, y];

                    if (MineCount >= 1)
                    {
                        List <BoxLocation> unknowBoxs = SearchMines(x, y, MaxX, MaxY, MineState.Unknow, MineMap);   //Chưa mở gần đó
                        List <BoxLocation> MineBoxs   = SearchMines(x, y, MaxX, MaxY, MineState.Mine, MineMap);     //mỏ  xác định

                        if (unknowBoxs.Count > 0)
                        {
                            //Thuật toán chính 1:
                            if (MineBoxs.Count == MineCount)
                            {
                                clickEventList.Clear();
                                foreach (var box in unknowBoxs)
                                {
                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.LeftClick));
                                }
                                FindGoodPoint = true;
                                break;
                            }

                            //Thuật toán chính 2:
                            if (MineBoxs.Count + unknowBoxs.Count == MineCount)
                            {
                                clickEventList.Clear();
                                foreach (var box in unknowBoxs)
                                {
                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.RightClick));
                                }
                                FindGoodPoint = true;
                                break;
                            }
                        }
                    }
                }

                if (FindGoodPoint)
                {
                    break;
                }
            }


            //Bước 2: Bổ sung thuật toán ..... Lần đầu tiên không tính được điểm phù hợp, hãy sử dụng thuật toán phức tạp để tính lại.
            if (!FindGoodPoint)
            {
                //Tính toán
                List <UnknowBoxSum> UnknowBoxSumList = new List <UnknowBoxSum>();

                for (int locx = 0; locx < MaxX; locx++)
                {
                    for (int locy = 0; locy < MaxY; locy++)
                    {
                        int MineCount = (int)MineMap[locx, locy];

                        if (MineCount >= 1 && MineCount <= 6)
                        {
                            List <BoxLocation> unknowBoxs = SearchMines(locx, locy, MaxX, MaxY, MineState.Unknow, MineMap);   //Chưa mở gần đó
                            List <BoxLocation> MineBoxs   = SearchMines(locx, locy, MaxX, MaxY, MineState.Mine, MineMap);     //Mỏ đã được xác định

                            if (unknowBoxs.Count >= 2)
                            {
                                UnknowBoxSum unknowBoxSum = new UnknowBoxSum();
                                unknowBoxSum.Boxes = unknowBoxs;
                                unknowBoxSum.Sum   = MineCount - MineBoxs.Count;
                                UnknowBoxSumList.Add(unknowBoxSum);
                            }
                        }
                    }
                }

                //    Debug.WriteLine($"UnknowBoxSumList={UnknowBoxSumList.Count}");

                if (UnknowBoxSumList.Count > 0)
                {
                    //Trận đấu
                    for (int locx = 0; locx < MaxX; locx++)
                    {
                        for (int locy = 0; locy < MaxY; locy++)
                        {
                            int MineCount = (int)MineMap[locx, locy];

                            if (MineCount >= 1 && MineCount <= 8)
                            {
                                List <BoxLocation> unknowBoxs = SearchMines(locx, locy, MaxX, MaxY, MineState.Unknow, MineMap);   //Chưa mở gần đó
                                List <BoxLocation> MineBoxs   = SearchMines(locx, locy, MaxX, MaxY, MineState.Mine, MineMap);     //Mỏ đã được xác định

                                if (unknowBoxs.Count >= 2)
                                {
                                    foreach (var UnknowBoxSum in UnknowBoxSumList)
                                    {
                                        if (unknowBoxs.Count - UnknowBoxSum.Boxes.Count == 1)
                                        {
                                            if (UnknowBoxSum.MatchBox(unknowBoxs))   //Đầu tiên xác định xem nó có khớp không
                                            {
                                                //Số mìn  == Tổng: không phải mìn
                                                if ((MineCount - MineBoxs.Count - UnknowBoxSum.Sum) == 0)
                                                {
                                                    BoxLocation box = UnknowBoxSum.GetNotBelongBox(unknowBoxs);

                                                    //                             Debug.WriteLine($"Match(Không có bơm ):({locx},{locy})->({box.LocationX},{box.LocationY})");

                                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.LeftClick));


                                                    FindGoodPoint = true;
                                                    break;
                                                }

                                                //Số giảm trừ-Sum = 1:
                                                if ((MineCount - MineBoxs.Count - UnknowBoxSum.Sum) == 1)
                                                {
                                                    //Đưa ra
                                                    BoxLocation box = UnknowBoxSum.GetNotBelongBox(unknowBoxs);
                                                    //        Debug.WriteLine($"Match(á bơm ):({locx},{locy})->({box.LocationX},{box.LocationY})");

                                                    clickEventList.Add(new ClickEvent(box.LocationX, box.LocationY, ClickType.RightClick));

                                                    FindGoodPoint = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            if (FindGoodPoint)
                            {
                                break;
                            }
                        }

                        if (FindGoodPoint)
                        {
                            break;
                        }
                    }
                }
            }


            //Bước 3:  không tìm thấy một vị trí phù hợp, chỉ có thể mở ngẫu nhiên
            if (!FindGoodPoint)
            {
                //    Debug.WriteLine("3Mở ngẫu nhiên...");

                float[,] ProbabilityMap = new float[MaxX, MaxY];

                //Tính xác suất tối thiểu
                float MinProbability = 1;

                for (int x = 0; x < MaxX; x++)
                {
                    for (int y = 0; y < MaxY; y++)
                    {
                        if (MineMap[x, y] == MineState.Unknow)
                        {
                            ProbabilityMap[x, y] = CalcProbability(x, y, MaxX, MaxY, MineMap);

                            if (ProbabilityMap[x, y] < MinProbability)
                            {
                                MinProbability = ProbabilityMap[x, y];
                            }
                        }
                    }
                }
                //   Debug.WriteLine($"MinProbability={MinProbability}");

                //Lấy điểm với xác suất nhỏ nhất
                for (int x = 0; x < MaxX; x++)
                {
                    for (int y = 0; y < MaxY; y++)
                    {
                        if (MineMap[x, y] == MineState.Unknow && Math.Abs(ProbabilityMap[x, y] - MinProbability) < 0.001)
                        {
                            clickEventList.Add(new ClickEvent(x, y, ClickType.LeftClick));
                        }
                    }
                }

                /*
                 * foreach (var clickEvent in clickEventList)
                 * {
                 *  Debug.WriteLine($"selected clickEvent:{clickEvent}");
                 * }
                 */

                int Count = clickEventList.Count;
                //ShowDebugInfo($"\r\nRandom Select:Count={Count}");

                if (Count == 0)
                {
                    //ShowDebugInfo("\r\n clickEventList.Count == 0,I can't do anything!");
                    this.timer.Enabled = false;
                    return;
                }
                else
                {
                    if (Count == 1)
                    {
                        ClickEvent clickEvent = clickEventList[0];
                    }
                    else
                    {
                        Random     random     = new Random();
                        ClickEvent clickEvent = clickEventList[random.Next(Count)];
                        clickEventList.Clear();
                        clickEventList.Add(clickEvent);
                    }
                }
            }


            // Click
            ClickScreen(RectWidth, left, top, centerleft, centertop, clickEventList);
        }
Пример #26
0
        public void Initialize()
        {
            this.DisplayState = Enumerable.Repeat(CellState.Unknown, Count(Size)).ToArray();

            this.MineState =
                Enumerable.Repeat(CellState.Mine, MineGenerates)
                .Concat(Enumerable.Repeat(CellState.Void, Count(Size) - MineGenerates))
                .OrderBy((_) => random.Next()).ToArray();

            List <int> safe_list = MineState
                                   .Select((state, index) => index)
                                   .Where((index) => MineState[index] == CellState.Void).ToList();

            while (safe_list.Count > 0)
            {
                int killmines = 0;

                foreach (Cell cell in Grid.Cells)
                {
                    int detects =
                        cell.IndexList.Select((link) => MineState[link.index])
                        .Where((state) => state == CellState.Mine)
                        .Count();

                    int links = cell.Links;

                    if (detects >= links)
                    {
                        MineState[cell.IndexList.ToArray()[random.Next(links)].index] = CellState.Void;
                        killmines++;
                    }
                }

                if (killmines <= 0)
                {
                    break;
                }

                while (killmines > 0 && safe_list.Count > 0)
                {
                    int index = safe_list[random.Next(safe_list.Count)];

                    MineState[index] = CellState.Mine;

                    safe_list.Remove(index);

                    killmines--;
                }

                if (killmines > 0)
                {
                    List <int> void_list = MineState
                                           .Select((state, index) => index)
                                           .Where((index) => MineState[index] == CellState.Void).ToList();

                    while (killmines > 0 && void_list.Count > 0)
                    {
                        int index = void_list[random.Next(void_list.Count)];

                        MineState[index] = CellState.Mine;

                        void_list.Remove(index);

                        killmines--;
                    }

                    if (killmines > 0)
                    {
                        throw new Exception("Faild Initialize.");
                    }

                    break;
                }
            }

            int mines = MineState.Where((state) => state == CellState.Mine).Count();

            if (mines != MineGenerates)
            {
                throw new Exception("Faild Initialize.");
            }

            foreach (Cell cell in Grid.Cells)
            {
                int detects =
                    cell.IndexList.Select((link) => MineState[link.index])
                    .Where((state) => state == CellState.Mine)
                    .Count();

                if (MineState[cell.Index] == CellState.Mine)
                {
                    continue;
                }

                if (detects == 0)
                {
                    continue;
                }

                switch (detects)
                {
                case 1:
                    MineState[cell.Index] = CellState.Detect1;
                    break;

                case 2:
                    MineState[cell.Index] = CellState.Detect2;
                    break;

                case 3:
                    MineState[cell.Index] = CellState.Detect3;
                    break;

                case 4:
                    MineState[cell.Index] = CellState.Detect4;
                    break;

                case 5:
                    MineState[cell.Index] = CellState.Detect5;
                    break;

                case 6:
                    MineState[cell.Index] = CellState.Detect6;
                    break;
                }
            }
        }
Пример #27
0
 public void SetMineState(MineState state)
 {
     mineState = state;
 }
Пример #28
0
 public Mine()
 {
     State = MineState.Armed;
 }
Пример #29
0
 void ProcessReleaseMovement()
 {
     // Move up
     if (_transf.position.y < GM.WaterLevel) {
         Vector3 newPos = transform.position;
         newPos += Vector3.up * _velocity * Time.deltaTime + Vector3.forward * Mathf.Sin (Time.time * 3.5f) * .005f;
     //			Debug.Log (newPos);
         transform.position = newPos;
     }
     // Bobbing
     else
     {
         State = MineState.BOBBING;
     }
 }
Пример #30
0
 public void SetState(MineState state) => MineData.State = state;