Java Minesweeper

This Java Minesweeper is my first programming assignment at Avans. Here I needed to build a MineSweeper clone. Because I already have some experience in game programming I could easily make this. For those who don’t understand Minesweeper I will explain how it works.

minesweeper fieldMinesweeper field

Somewhere in the field there are mines, these mines you need to avoid. Because if you hit one you will lose the game. A field contains a number, this number shows how much mines there are in the surrounding fields.

1. To start off click any square, preferably towards the middle. Most Minesweeper players click random squares until a group of squares “opens” up. If 4 or 5 squares opened after your click, it’s time to evaluate the numbers. If only one square opens after your click, find another random square.

2. Based on the opened fields you are going to search for mines. So the top right “1”, in the image above, has one mine in his surrounding fields. But because the “1” below him is not a “2” means that the bottom right field is a mine.

3. Once you found a mine mark it with the right mouse button.

With this technique you collect all the mines. Once you found all those mines the mine field will open and you win the game. If you still don’t understand it please watch the video below.

Minesweeper FieldMinesweeper field


This is where the fun begins:

BuildField() gets initialized from the constructor. This is where I iterate threw the whole field. I used a HashMap to store the Field objects. And for the Hash key i used its location such as C1, A3 etc.

I create random Mines based on a X amount of chance, this is something that the player is able to manipulate in the beginning of the game.

When the Hashmap is filled with objects I want to set the values of those fields. This is done by countAdjacentMines(), I iterate once again threw the whole field and set the amount of mines that is surrounding a field.