Blog

Minesweeper (Android)

Original

In grade 12, my class had a unit on android development using the Android Studio IDE. Closer to the end of the year we were given a final project with the choice of using Android Studio or Java in Eclipse. I choose to do minesweeper with android studio. The final product of that project is as given in the screen shots below. I lost the source code for this project after high school.

Remake

That final project was one of my “best” works by the time I finished the first semester of my second year at UTM. I felt very confident using design patterns after taking a design patterns course (CSC 207). After the semester had ended, I found the source code files in the USB drive and was inspired to completely remake the game with the new skills I had.

Planning

Have some previous knowledge of Android Studio and Java I was quickly able to setup the work environment. The previous game had the grid and number of mines fixed. I wanted to make it adjustable for the new version, so I had to make the grid somewhat dynamic and adjust to the size of the screen.

Design Pattern

I decided to use a modified version of the MVC (Model View Controller) design pattern. This pattern allowed me to isolate the array representation of the grid and direct user control. The view was handled by the XML layout of the grid.

Grid Function

All that was left to do what create the internal working of the game. I simply extended the Button view and gave it a x and y attribute to store its position on the grid. I also made a board generator by randomly placing “mines” on the gird and then going back and updating the value at each tile according to the mines in the proximity. I also created a mask array to only show tiles that had been clicked and only update them on the mask. With that I had the solution grid and a “display” grid.

Challenges

The greatest challenge of the project, relatively speaking was making the grid dynamic and opening adjacent tiles when an empty tile was clicked. The solution to the first problem was obtaining the size of the users display through the ____ function and then adjusting the size of each tile on the grid accordingly. The solution to the second problem is using a recursive function to open adjacent tiles until a non-empty tile is encountered.

Current Progress

There are many things left unfinished. The Options page is completely broken. If a non-numeric character is put into the text-fields it will crash the application, or if you put the same number of mines as total tiles, the application goes into an infinite loop. The easiest way to avoid these problems is to use sliders to set the parameters on a mathematically correct scale. For example, setting the width and height of the tiles can be adjusted according to the display size so the final tile size is sensible and still clickable, furthermore upper limit can be set to (width*length)-1 of the number of mines to avoid infinite looping.