Wednesday, February 19, 2020

Excel Minesweeper

In this post we learn to make the famous Minesweeper game in Excel. It is an excellent opportunity for VBA beginners to understand FOR and DO loops, and IF/ELSEIF/ELSE conditions, as well as some new VBA Events. There is a more complicated logic to open the cells though. Excel Minesweeper is fun and quite similar to the original Microsoft game.


The Layout

The layout of Excel Minesweeper shows a grid of cells with interior grey color. The grey shade changes when opening the cell to give a depth effect. Another possibility to build the layout could have been to add buttons to each cell (I can update the post if anybody is interested to know how to do it).

Additionally, there is a mine counter on the left and a timer on the right side of the smiley face command button that restarts the Excel Minesweeper game. The timer will start as soon as the first cell is open and it is working through the Application.OnTime VBA function as follows:

 
  Sub TimeCount()
      t = t + 1
      MyTime = Now + TimeValue("00:00:01")
      Application.OnTime MyTime, "UpdateTime"
  End Sub
 
  Sub UpdateTime()
      If GameStart = True Then
          ws.Range("H3").Value = t
          TimeCount
      End If
  End Sub
 


The Placement

The placement of mines in Excel Minesweeper occurs when opening the workbook through a Workbook_Open () VBA Event or when clicking the command button with the smiley face. In both cases, the Sub “NewGame” is called and triggers “AddBombs” and “AddNumbers” subroutines. Excel Minesweeper uses the Randomize/Rnd functions to set random coordinates for placing mines. Then, numbers are placed in each cell surrounded by mines by looping throughout the game board. Please note this happens and is recorded in a separate sheet called “Solution”, which is hidden.


The Movement

Excel Minesweeper uses the following VBA Events of the Worksheet object to either open the cell or set the flag on a cell, and both can be found under the worksheet VBAProject window: Sheet1 (Buscaminas).

In order to open a cell the mouse double click is used by:

 
  Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      If GameOver = False Then ShowCell
  End Sub
 


In order to set the flag the right mouse click is used by:

 
  Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
      Cancel = True 'avoids Excel right click cell options to appear
      If GameOver = False Then SetFlag
  End Sub
 


The “ShowCell” subroutine will copy the value from the “Solution” hidden tab and follow a logic to open cells around. With that purpose, there is a number of Subs to check for adjacent cells in the “Move” module. 

Note the mines are represented with a bomb symbol in this version of Excel Minesweeper. Hitting a mine will end the Excel Minesweeper game and open all cells (i.e. copying the entire solution to the active sheet).


The “CheckMinesDeactivated” subroutine is triggered after opening a cell or placing a flag, and will check for the number of covered cells and placed flags. The Excel Minesweeper game will conclude when there are only 10 remaining cells covered and 10 flags placed. 

Now you can leverage the code and create a bigger grid to add difficulty, or just try to beat your best record on this version of Excel Minesweeper. Enjoy!
 
 

No comments:

Post a Comment

Popular Posts