Minesweeper implementation on a 15x15 grid with "fair" rules:
- if a player can deduce that at least one unopened tile is definitely a number, and the player chooses to open another tile whose value cannot be for sure known, then the opened tile will contain a mine.
- if there are no tiles whose value can be deduced, then the player may choose any unopened unknown tile and it will not contain a mine.
Space/MouseLeft- reveal a tileR- restart the gameQ- quit the gameF/MouseRight- place/remove a flag on a tileM- toggle debug mode
You may choose from 3 difficulties: EASY, NORMAL and HARD.
EASY- 10% of the tiles are minesNORMAL- 20% of the tiles are minesHARD- 30% of the tiles are mines
By pressing M on your keyboard, you can toggle the debug mode on and off. In the debug mode, unopened tiles that are definitely a mine are shown as red. Unopened tiles that are definitely a number are shown as green.
By default, the debug mode is disabled.
This application was written in Rider IDE and uses Raylib library https://www.raylib.com/index.html. This library is supported across multiple platforms and has over 60 bindings to the library so be sure to check them out!
If you want to launch an executable file, you can go to the /FairMines/bin/Release/net8.0 folder and choose Windows (x64 only), Linux (x64 only) or macOS (arm64 only) and inside those folders should be executable files named FairMines (I worked on this game on macOS, but also tested the executable file on Windows, but not on Linux :( ).
This staic class contains information about the whole game and the whole UI code also written there. It's got a few variables needed for calculations such as:
Cell[,] grid- 2d array that contains cells/tiles of the gameDateTime timeStart- info about when user started current gameDateTime timeEnd- info about when user ended current gameint tilesRevealed- number of tiles that are revealedDifficulty difficulty- game difficulty (default value -EASY)Mode mode- game mode (default value -NORMAL)int numOfAllMines- number of all mines based on game difficultyint numOfUnknownMines- number of those mines that are unknown
This class contains information about each individual cell inside the grid. Each cell contains these variables and functions:
int posRowint posColint numOfMinesAround- number of mines aroundint numOfUnknownMinesAround- number of unknown mines aroundint numOfUnknownTilesAround- number of unknown tiles aroundbool flagged- if a flag is put on top of the cellbool revealed- if a tile is revealedbool shouldBeKnown- if a played should be able to deduce that said tile is for sure a number or a mineCellValue valueForAlgorithmandpublic CellValue finalValue- variables used in the algorithm for computations
GetNeighbours- returns a list of cells that are neighbours of current cellGetNumOfNeighbouringKnownMines- returns a list of neighbours that are a mine and should be known by the playerGetNumOfUnknownNeighbours- returns a list of neighbours that are unknownGetNumOfNeighbouringMines- returns a list of all neighbouring mines for current grid stateGetNumOfNeighbouringMinesForAlgorithm- returns a list of all neighbouring mines for the current grid computation (using thevalueForAlgorithmvalue)GetNumOfNeighbouringNumbersForAlgorithm- returns a list of all neighbouring numbers for the current grid computation (using thevalueForAlgorithmvalue)
This class takes care of the initial generation of the grid and it also takes care of the regeneration when user clicked a unknown mine, so the program has to change the value of that cell to a number and regenerate all other cells so that a correct position is found.
If a user does not have a cell whose value should be known and it is a number, then if the user clicks on a unknown cell that contains a mine, we have to regenerate the current mine placement on the grid. Firstly we try to find correct mine placement for all the cells that are neighbours to the already opened cells. If such a mine placement is found, then we just have to place the remaining mines randomly throughout the unknown cells.
void Generate- function that generates the initial game gridbool Regenerate- function that regenerates all the unknown cell after a unknown cell has been clicked.void PlaceMines- function that places all the remaining mines that are unknown, used either in generating the grid, or in regenerating the grid.void CalculateNeighbouringMines- sets the correct number of mines around to each numberint UpdateValuesAfterCorrectCombination- this function updates values of the cells after every regeneration
This class handles all the algorithmic stuff of this program. Main functions:
void FindAnyCombination- function used in regenerating the grid and finding the right combination for already opened cells that still have unknown, unopened neighboursvoid TryAllGridCombinations- function used in every update of the game, it's purpose is to try all the mine placement combinations around all opened cells, and stores the computation values insidevalueForAlgorithmandfinalValuevariablesvoid UpdateGameAfterOpenedCell- called after any cell is opened, it uses theTryAllGridCombinationsfunction and based on the final values it determines whether the value of a cell should be known to the user or not
This file contains some constants and functions that I didn't want in other files