

And if there is atleast a single adjacent mine to this cell then that count is displayed on the current cell. If there are no adjacent mines to this cell, then we recursively click/step on all the safe adjacent cells (hence reducing the time of the game-play). Since there can be maximum 8 surrounding cells, so we check for all 8 surrounding cells. We use the function countAdjacentMines() to calculate the adjacent mines. This function returns a true if the user steps/clicks on a mine and hence he loses else if he step/click on a safe cell, then we get the count of mines surrounding that cell. The lifeline of this program is the recursive function – playMinesweeperUtil() The makeMove() function inside the while loop gets a move randomly from then randomly assigned moves.Īlso to guarantee that the first move of the user is always safe (because the user can lose in the first step itself by stepping/clicking on a cell having a mine, and this would be very much unfair), we put a check by using the if statement – if ( currentMoveIndex = 0) The while() loop terminates when the user either wins or lose. Then the game is played till the user either wins (when the user never steps/clicks on a mine-containing cell) or lose (when the user steps/clicks on a mine-containing cell). So if you are afraid of losing then uncomment this function and then play ! We can cheat before playing (by knowing the positions of the mines) using the function – cheatMinesweepeer(). We also assign the moves using the function assignMoves() before playing the game. Once the level is chosen, the realBoard and myBoard are initialized accordingly and we place the mines in the realBoard randomly. This is done by passing one of the above in the function – chooseDifficultyLevel(). We can choose any level among – BEGINNER, INTERMEDIATE and ADVANCED. Throughout the game, realBoard remains unchanged whereas myBoard sees many changes according to the user’s move. We play our game in myBoard and realBoard stores the location of the mines.
#MINESWEEPER GENERATOR ALGORITHM MYSQL PC#
#MINESWEEPER GENERATOR ALGORITHM MYSQL CODE#
I have implemented the following code in python, and it does not get terminated while the stack is depleted. If there was no overlap, the conditions can't be usefully combined. The program then associates the square with the expression )Īnyway, to combine two conditions I'd first check that they were overlapping, by intersecting the area sets. The neighbors of the square which are as yet unclicked are (again by way of example) named 'b' and 'c'. For sake of example let the number thus revealed equal 2. Say that the solver clicks a square named 'a'. I have implemented in Python an algorithm for solving the game 'Minesweeper'.
