Here's what I have now (sources and README):
In this post I would like to share with some interesting moments I faced with during implementation.
Random generation and counting of mines
Here I use one-dimensional array which is initially filled in by '0's (empty cells) and '-1's (mines). Shuffling the array allows me to distribute mines randomly.
To count how many mines are there around an empty cell I use shift-arrays which describe all the neighbors of the given cell.
Opening empty cells
There is useful and interesting case in the original minesweeper when you open an empty cell without neighboring mines. Opening such a cell leads to opening all the connected empty cells and the first non-empty cells which form a sort of border.
I use Breadth-first search (BFS) to find all such connected cells which can be opened at once. BFS works in linear time which makes this algorithm really fast.
GUI style customization
When you run minesweeper it looks like the classic one. Somewhere in the internet I found this sprite:
To make minesweeper's style customizable I singled out sprite description into a separate file. Now I can give a config file as an input to the game and the magic happens :)
Here's how a config file looks like for a customized sprite:
where sprite_txt is the path for a text description of the image sprite. Here's a custom sprite and it's description:
You have to follow the format and the ordering of the elements as shown in the example above when describing your own sprite. That's it.
What is not implemented
- menu bar with options to change game field configuration
- win statistics
P.S.: it is the most pleasant feeling in the world when you enjoy playing the game you wrote by yourself!
P.P.S.: C++ is not my major language, it is more like a hobby, so if you have any notes about the code you're very welcome to share with them.