Esempio n. 1
0
        //method to create a game
        public void Run()
        {
            this.correctFlags = 0;
            this.wrongFlags   = 0;
            this.TimeElapsed  = 0;

            this.plates = new Plate[Width, Height];

            for (int row = 0; row < Width; row++)
            {
                for (int col = 0; col < Height; col++)
                {
                    Plate cell = new Plate(this, row, col);
                    this.plates[row, col] = cell;
                }
            }

            int    minesCounter  = 0;
            Random minesPosition = new Random();

            while (minesCounter < Mines)
            {
                int row = minesPosition.Next(Width);
                int col = minesPosition.Next(Height);

                Plate cell = this.plates[row, col];

                if (!cell.IsMined)
                {
                    cell.IsMined = true;
                    minesCounter++;
                }
            }

            gameTimer          = new DispatcherTimer();
            gameTimer.Tick    += new EventHandler(OnTimeElapsed);
            gameTimer.Interval = new TimeSpan(0, 0, 1);
            gameTimer.Start();
        }
Esempio n. 2
0
        //method to put or remove flag if some cell is selected
        public void FlagMine(int rowPosition, int colPosition)
        {
            if (!this.IsInGrid(rowPosition, colPosition))
            {
                throw new MinesweeperException("Invalid MinesGrid reference call [row, column] on flag");
            }

            Plate currPlate = this.plates[rowPosition, colPosition];

            if (!currPlate.IsFlagged)
            {
                if (currPlate.IsMined)
                {
                    this.correctFlags++;
                }
                else
                {
                    this.wrongFlags++;
                }
            }
            else
            {
                if (currPlate.IsMined)
                {
                    this.correctFlags--;
                }
                else
                {
                    this.wrongFlags--;
                }
            }

            currPlate.IsFlagged = !currPlate.IsFlagged; // updates the flagged value
            CheckFinish();                              // checks for end of game
            // Raises CounterChanged event
            // this.OnCounterChanged(new EventArgs());
        }
Esempio n. 3
0
        //method to create a game
        public void Run(Tuple <int, int>[] minesPositions)
        {
            this.correctFlags = 0;
            this.wrongFlags   = 0;
            this.TimeElapsed  = 0;

            this.plates = new Plate[Width, Height];

            for (int row = 0; row < Width; row++)
            {
                for (int col = 0; col < Height; col++)
                {
                    Plate cell = new Plate(this, row, col);
                    this.plates[row, col] = cell;
                }
            }

            int minesCounter = 0;

            while (minesCounter < Mines)
            {
                Plate cell = this.plates[minesPositions[minesCounter].Item1 /*row*/,
                                         minesPositions[minesCounter].Item2 /*col*/];

                if (!cell.IsMined)
                {
                    cell.IsMined = true;
                    minesCounter++;
                }
            }

            gameTimer          = new DispatcherTimer();
            gameTimer.Tick    += new EventHandler(OnTimeElapsed);
            gameTimer.Interval = new TimeSpan(0, 0, 1);
            gameTimer.Start();
        }