File Exchange

image thumbnail


version 1.0 (124 KB) by

A minesweeper game similar to the one in Windows, but implemented in pure MATLAB using GUIDE.



View License

Minesweeper is a simple one−player computer game commonly found on machines with popular operating systems such as Linux or Microsoft Windows. The object of the game is to clear an abstract minefield without detonating a mine.

The game consists of a 2 dimensional rectangular playing field (or board) where some known numbers of mines have been hidden. Initially, all of the squares on the board are "covered up" and no information is given to indicate the location of the mines. The player’s job is to either deduce or guess which board squares are clear of mines and step there to reveal a number. If successful, this number will indicate the number of mines to be found in the squares adjacent to the square with the number. Obviously, the first move of the game must be a guess because no information has been provided. Since the board is a rectangular grid, each interior square has exactly 8 neighbouring squares, edge squares have 5 neighbours, and corner squares have 3 neighbours. Therefore, the number found under any given square will be in the range of 0 to 8 (inclusive). Game play continues until the player has uncovered (or "stepped" on) each and every square that does not hide a mine, while successfully avoiding all of the mines. If the player can do this, they are considered to have won the game. However, if at any point the player attempts to uncover a square that contains a mine, the game immediately ends and the player is said to have lost.

Any suggestions and bug - fixes are welcome.

The code has been written using GUIDE.
I haven't tested this code on any other platform except R2009a (Matlab 7.8) so am not sure whether it will work for the previous versions of Matlab.

Comments and Ratings (3)

Mihir Shah

The "flag counter" displays the no. of mines.
You need to press the start button to activate it.
And about the clock I tried adding it using the function " tic - toc " but it dint resemble the actuall clock.
Please help me with adding the clock if you can.
Thanks for your comments :)

Jan Simon

Jan Simon (view profile)

See also: XPBOMBS

Sean de

Sean de (view profile)

This is fun! a few things:
-It'd be nice to have the number of mines displayed.
-The 'flag counter' doesn't do anything
-It'd be nice if we could run it with input arguments of: outside dimensions, number of mines.
-The flag does not go away when you 'unflag' it. It should.
-When you lose (embarrassed to say I did the first time :( ) this error appears when you try to reopen it:

??? Error using ==> set
Invalid handle object.

Error in ==> mines_stg1>t_5_9_Callback at 3040

Error in ==> gui_mainfcn at 96

Error in ==> mines_stg1 at 48
    gui_mainfcn(gui_State, varargin{:});

Error in ==> @(hObject,eventdata)mines_stg1('t_5_9_Callback',hObject,eventdata,guidata(hObject))

??? Error while evaluating uicontrol Callback

It still works after the error; however.

If you really want to impress add a clock.

Good submission and good idea!

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video