Generate minesweeper map on Android

posted in: Android, Java, Mobile | 0

What is minesweeper?

Minesweeper is a popular game that was included in the Windows operating system. The aim is to discover the mines. Each tile of the map is either a mine or a number saying how many mines are adjacent to the tile.

mine1

 

Method

The method I use is to initialize the map with 0 values on every tile. The different values that can have a tile are:

We check that the number of mines can be placed in the map.

While we don’t have placed the number of mines, we generate a random x and y where we want to place the mine.

If the x,y position is not a mine, we place a mine and increment the adjacent tiles.

 

Code

Each spot on the map is called a Tile. A tile can have different values (type). We could have only made the map an two dimension array of int but because we may had different objects/bonus in the future I prefer to implement it as an extensible object.

A map is an object containing an two dimensions array of tiles.

The interesting method is generate() and increment().

Conclusion

This is a pretty straight forward method of generating a map. An alternative would have been to place every mines and generate the tile number after.

 

Leave a Reply