Sub Mitted By: Marquez, Jeremiah D. Funcion, Lyndon Jay Malik, Akmad Bilan, Ranilo Comia, Jersy Jonson
Sub Mitted By: Marquez, Jeremiah D. Funcion, Lyndon Jay Malik, Akmad Bilan, Ranilo Comia, Jersy Jonson
mitted by:
Marquez, Jeremiah D.
Funcion, Lyndon Jay
Malik, Akmad
Bilan, Ranilo
Comia, Jersy Jonson
Submitted to:
Engr. M. Loy-a
Date submitted:
October 3, 2016
Description of the project:
Minesweeper in C++
Minesweeper is a single-player puzzle video game. The objective of the
game is to clear a rectangular board containing hidden "mines" without
detonating any of them, with help from clues about the number of
neighboring mines in each field. The game originates from the 1960s, and
has been written for many computing platforms in use today. It has many
variations and offshoots.
The increasing number of tiles raises the difficulty bar. So the complexity
level increases as we proceed to next levels.
It might seem like a complete luck-based game (you are lucky if you don’t
step over any mine over the whole game and unlucky if you have stepped
over one). But this is not a complete luck based game. Instead you can win
almost every time if you follow the hints given by the game itself.
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). This is represented in a while() loop. The while()
loop terminates when the user either wins or lose.
The makeMove() function inside the while loop gets a move randomly from
then randomly assigned moves. [However in the user-input game this
function prompts the user to enter his own move].
Also 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 lifeline of this program is the recursive function
– playMinesweeperUtil()
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. We use the function countAdjacentMines() to
calculate the adjacent mines. Since there can be maximum 8 surrounding
cells, so we check for all 8 surrounding cells.
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). And if
there is atleast a single adjacent mine to this cell then that count is
displayed on the current cell. This is given as a hint to the player so that he
can avoid stepping/clicking on the cells having mines by logic.
Also if you click on a cell having no adjacent mines (in any of the
surrounding eight cells) then all the adjacent cells are automatically
cleared, thus saving our time.
So we can see that we don’t always have to click on all the cells not having
the mines (total number of cells – number of mines) to win. If we are lucky
then we can win in very short time by clicking on the cells which don’t have
any adjacent cells having mines.
The user keeps on playing until he steps/clicks on a cell having a mine (in
this case the user loses) or if he had clicked/stepped on the entire safe cell
(in this case the user wins).
Source Code:
#include<iostream>
#include<time.h>
#include<conio.h>
using namespace std;
#define EASY 0
#define MEDIUM 1
#define HARD 2
#define MAXSIDE 25
#define MAXMINES 99
#define MOVESIZE 526 // (25 * 25 - 99)
printf(" ");
printf("\n\n");
int i;
int count = 0;
/*
Count all the mines in the 8 adjacent
cells
N.W N N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
return (count);
}
int i, j;
printBoard(myBoard);
printf("\nYou lost!\n");
return (true);
}
else
{
// Calculate the number of adjacent mines and put it
// on the board
int count = countAdjacentMines(row, col, mines, realBoard);
(*movesLeft)--;
if (!count)
{
/*
Recur for all 8 adjacent cells
N.W N N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
return (false);
}
}
return;
}
return;
}
initialise(realBoard, myBoard);
// You are in the game until you have not opened a mine
// So keep playing
int currentMoveIndex = 0;
while (gameOver == false)
{
printf("Current Status of Board : \n");
printBoard(myBoard);
makeMove(&x, &y);
int level;
scanf_s("%d", &level);
if (level == EASY)
{
SIDE = 9;
MINES = 10;
}
if (level == MEDIUM)
{
SIDE = 16;
MINES = 40;
}
if (level == HARD)
{
SIDE = 24;
MINES = 99;
}
return;
}
playMinesweeper();
_getch();
return (0);
}
Sample Screenshots/Output: