Skip to main content

Section ๐Ÿ’ฅ Minesweeper

Subsection Simon Game

The simon game we coded in class shares many features to your minesweeper game. Download the full code base for this game on Canvas.
This should be a very helpful resource for wiring up your callback functions and updating your gui components.

Subsection Difficulty Effects

Table 20. Difficulty Setting Effects
\(\textbf{Difficulty Level}\) \(\textbf{Grid Size}\) \(\textbf{Percent Mines}\) \(\textbf{Time Limit (secs)}\)
โ€œtestโ€ \(5 \times 5\) \(10\%\) \(30\)
โ€œeasyโ€ \(10 \times 10\) \(10\%\) \(\infty\)
โ€œnormalโ€ \(15 \times 15\) \(13\%\) \(150\)
โ€œhardโ€ \(20 \times 20\) \(16\%\) \(100\)
โ€œbrutalโ€ \(20 \times 20\) \(19\%\) \(80\)

Subsection MATLAB Text Object

MATLABโ€™s has a built-in timer class to create a countdown clock. You can read about it in the official documentation: MATLAB Timer Documentation.
The command x = timer initializes a timer object and saves it in the variable x.
The timer, x, can be configured with various properties. This table lists a few:
Table 21. text Properties
\(\textbf{Name}\) \(\textbf{Options}\) \(\textbf{Description}\)
String Any string Display string
FontSize Any integer Font size
HorizontalAlignment "left", "center", "right" Position anchor
FontWeight "bold", "normal" Font thickness
ButtonDownFcn function handle Call if clicked

Subsection Saving and Retrieving Game Data

Saving and Retrieving the updated game components is a critical part of event driven programs.
Whenever the game data changes in anyway, we must save the updated version inside the figure.
  • Saving data to a figure is done with the command:
    guidata(fig, game);	% saves updated game data to the figure
    
  • Retrieving/loading data from a figure is done with the command:
    g = guidata(fig);	% loads data from the figure
    

Subsection MATLAB Clock Timer

MATLABโ€™s has a built-in timer class to create a countdown clock. You can read about it in the official documentation: MATLAB Timer Documentation.
The command x = timer initializes a timer object and saves it in the variable x.
The timer, x, can be configured with various properties. This table lists a few:
Table 22. timer Properties
Name Options Description
ExecutionMode "fixedRate"
TimerFcn @(src,evt) func_handle
Period \((1\times 1)\) double \(\sfrac{1}{\text{Period}}\) = checks\(/\)second
StopFcn @(src,evt) func_handle
Table 23. timer Functions
Name Example Description
start start(x) Starts the timer, x
stop stop(x) Stops the timer, x

Subsection Minesweeper Main Function

Rather than starting with something that doesnโ€™t work, you may optionally start with something that works and slowly replace the provided helpers with your own.
Step 1
Download the Helper Keys.
Step 2
Create an isolated folder and extract ALL the helper keys inside this folder.
Step 3
Inside the same folder, create a new function called minesweeper.m with the following code:
% FINAL PROJECT =========================================
% ๐Ÿ† = BONUS Feature
% == MAIN ===============================================
function minesweeper(difficulty, seed)

	% Clean up
	clear game; close all;

	% Default difficulty
	if nargin < 1, difficulty = "test";
	elseif nargin == 2, rng(seed,"twister"); end

	game.settings    = init_settings(difficulty);
	game.state       = init_state(game.settings.gridSize);
	[game.gui, fig]  = init_gui(game.settings);
	game.clock       = init_clock(fig); % <-- ๐Ÿ† -->

	guidata(fig, game);

end
Step 4
Run the function as normal to play the game.
Step 5
As you write the different helpers for this assignment, replace the helper keys with your helper code.