private Tile this[Point point] => (Tile)this.Controls[$"Tile_{point.X}_{point.Y}"];               //set the tile point

            private void Tile_MouseDown(Object sender, MouseEventArgs e)                                      // on click
            {
                Tile tile = (Tile)sender;

                if (!tile.Opened)
                {
                    switch (e.Button)
                    {
                    case MouseButtons.Left when !tile.Flagged:                                       //left button clicked
                        if (!this.minesGenerated)                                                    // if mines was no generated
                        {
                            this.GenerateMines(tile);                                                //generate
                            GameWinStatusChanged(this, new GameWinStatusChangedEventArgs(false, 0)); // start clock,set the image button on normal
                        }
                        if (tile.Mined)                                                              // hit a mine
                        {
                            this.DisableTiles(true);                                                 //disable mines
                            GameWinStatusChanged(this, new GameWinStatusChangedEventArgs(true, 2));  //stop clock,set lose image
                        }
                        else
                        {
                            tile.TestAdjacentTiles();     // check the tile
                            gridSearchBlacklist.Clear();
                        }
                        break;

                    case MouseButtons.Right when this.flags > 0:     //right button click
                        if (tile.Flagged)
                        {
                            tile.Flagged = false;     //unflag
                            this.flags++;
                        }
                        else
                        {
                            tile.Flagged = true;     //flag
                            this.flags--;
                        }
                        TileFlagStatusChange(this, new TileFlagStatusChangedEventArgs(this.flags, this.flags < this.mines * 0.25 ? Color.Red : Color.Black));     //flags ammount and text color
                        break;
                    }
                }

                this.CheckForWin(); // check if it win
            }
Esempio n. 2
0
            private void Tile_MouseDown(object sender, MouseEventArgs e)
            {
                Tile tile = (Tile)sender;

                if (!tile.Opened)
                {
                    switch (e.Button)
                    {
                    case MouseButtons.Left when !tile.Flagged:
                        if (!this.minesGenerated)
                        {
                            this.GenerateMines(tile);
                        }

                        if (tile.Mined)
                        {
                            this.DisableTiles(true);
                        }
                        else
                        {
                            tile.TestAdjacentTiles();
                            gridSearchBlacklist.Clear();
                        }
                        break;

                    case MouseButtons.Right:
                        if (tile.Flagged)
                        {
                            tile.Flagged = false;
                            this.flags++;
                        }
                        else if (this.flags > 0)
                        {
                            tile.Flagged = true;
                            this.flags--;
                        }
                        TileFlagStatusChange(this, new TileFlagStatusChangedEventArgs(this.flags, this.flags < this.mines * 0.25 ? Color.Red : Color.Black));
                        break;
                    }
                    this.CheckForWin();
                }
            }
Esempio n. 3
0
            /* Event listener which generates mines, handles flags and invokes CheckForWin. */
            private void Tile_MouseDown(object sender, MouseEventArgs e)
            {
                Tile tile = (Tile)sender;

                if (!tile.Opened)
                {
                    //ShowMinesDebug(); /* UNCOMMENT THIS TO SEE ALL MINES WHEN PLAYING THE GAME */
                    switch (e.Button)
                    {
                    /* When the user left clicks on an unflagged tile. */
                    case MouseButtons.Left when !tile.Flagged:
                        ClickCount++;
                        /* If this is the first tile the user clicks on. Generate mines and start the game. */
                        if (!this.minesGenerated)
                        {
                            this.ClickCount = 0;
                            timer.Start();
                            sw.Restart();
                            this.GenerateMines(tile);
                        }
                        /* If the user clicks on a mine, disable clicking on all tiles and display Game Over image. */
                        if (tile.Mined)
                        {
                            this.DisableTiles(true);
                            Form__.pbGO.Show();
                        }
                        else
                        {
                            /* Open all tiles that do not have any adjacent mines. */
                            tile.TestAdjacentTiles();
                            /* Clear the adjacent tile blacklist. */
                            gridSearchBlacklist.Clear();
                        }
                        /* Regenerate mines every 5 clicks on Insane mode. */
                        if (this.ClickCount % 5 == 0 && this.ClickCount != 0 && difficulty == Difficulty.Insane)
                        {
                            this.RegenerateMines(tile);
                            //ShowMinesDebug(); /* UNCOMMENT THIS TO SEE ALL MINES WHEN PLAYING THE GAME */
                        }
                        /* Trigger the tile click custom event handler. */
                        TileClickStatusChange(this, new TileClickEventArgs(this.ClickCount));
                        break;

                    /* When the user right clicks on a tile when there are flags left. */
                    case MouseButtons.Right when this.flags > 0:
                        /* Prevent the user placing flags before the mines have been generated. */
                        if (!this.minesGenerated)
                        {
                            return;
                        }
                        /* Toggle flag. */
                        if (tile.Flagged)
                        {
                            tile.Flagged = false;
                            this.flags++;
                        }
                        else
                        {
                            tile.Flagged = true;
                            this.flags--;
                        }
                        /* Trigger the flag custom event handler. */
                        TileFlagStatusChange(this, new TileFlagStatusChangedEventArgs(this.flags, this.flags < this.mines * 0.25 ? Color.Red : Color.Black));
                        break;
                    }
                }
                /* Check whether all tiles have been opened and all mines have been flagged. */
                this.CheckForWin();
            }