PLEASE HELP WITH MATLAB CODE: Background: Minesweeper is a game that has been th
ID: 2084959 • Letter: P
Question
PLEASE HELP WITH MATLAB CODE:
Background: Minesweeper is a game that has been thrilling people since 1989. The goal of the game is to not hit the bombs that are randomly placed in the gird. When you hit a spot that doesn’t contain a bomb, it will tell you the number of bombs around that spot. Your boss has tasked you with recreating the game board of Minesweeper and she wants it flexible to any size game board, and any number of bombs. The goal of your code will be to create the solved board. First place all the bombs using the bomb.jpg, and then calculate the amount of bombs surrounding every square. Instead of numbers for each square, you will color coordinate to how many bombs are around the square. The board will always be a square board, but it can be any sized square.
1 bomb= red 2 bombs=blue 3 bombs=white 4 bombs=black 5 bombs= yellow 6 bombs = orange 7 bombs=pink 8 bombs=purple 0 bombs= green
Data Validation • If you have more bombs than half the area of the board, ask the user to input a smaller amount of bombs
• If the inputted bombs is not a single integer (i.e. a matrix) ask for a single integer
• Inputted minesweeper board dimensions must be a single integer, if it is not must issue a warning and round down to the closest integer
• If the minesweeper board dimension has to be a single whole number (i.e. no matrix, vectors) Coding Requirements:
• The size of the field must be square and flexible to any size • The amount of bombs must be flexible
• Input statement for the amount of bombs, this must be a single number • Input statement for the size of the minesweeper board, this must be the dimensions (i.e. a single number to create a square matrix)
| file:///C:/Usersjesw/Desktop/MA6-ENGI 1331-SU2017.pdf Bow Bany baees do yo vant on your Minesweeper board?A$ >> 1 your board needs to be 1 nuuber. Re-enter 5ingle integer:5.6 arning: Tou did not enter n nteger the progEan 1 Found down How many bombs do you want on your Hineaveeper board?70 You have too many bonbs for your board. Enter a smaller anounti,1 You have not entered number. Plea e enternunber:20 You hare too many bomba foz your board. Ente maller amount:10 figre1 200 10:28 PMM O Type here to search 7/22/2017 2Explanation / Answer
Answer:
#include<bits/stdc++.h>
using namespace std;
#define BEGINNER 0
#define INTERMEDIATE 1
#define ADVANCED 2
#define MAXSIDE 25
#define MAXMINES 99
#define MOVESIZE 526
int SIDE ; // side length of the board
int MINES ;
bool isValid(int row, int col)
{
return (row >= 0) && (row < SIDE) &&
(col >= 0) && (col < SIDE);
}
// A Utility Function to check whether given cell (row, col)
// has a mine or not.
bool isMine (int row, int col, char board[][MAXSIDE])
{
if (board[row][col] == '*')
return (true);
else
return (false);
}
// A Function to get the user's move
void makeMove(int *x, int *y)
{
// Take the input move
printf("Enter your move, (row, column) -> ");
scanf("%d %d", x, y);
return;
}
// A Function to print the current gameplay board
void printBoard(char myBoard[][MAXSIDE])
{
int i, j;
printf (" ");
for (i=0; i<SIDE; i++)
printf ("%d ", i);
printf (" ");
for (i=0; i<SIDE; i++)
{
printf ("%d ", i);
for (j=0; j<SIDE; j++)
printf ("%c ", myBoard[i][j]);
printf (" ");
}
return;
}
// A Function to count the number of
// mines in the adjacent cells
int countAdjacentMines(int row, int col, int mines[][2],
char realBoard[][MAXSIDE])
{
int i;
int count = 0;
// Only process this cell if this is a valid one
if (isValid (row-1, col) == true)
{
if (isMine (row-1, col, realBoard) == true)
count++;
}
if (isValid (row+1, col) == true)
{
if (isMine (row+1, col, realBoard) == true)
count++;
}
if (isValid (row, col+1) == true)
{
if (isMine (row, col+1, realBoard) == true)
count++;
}
if (isValid (row, col-1) == true)
{
if (isMine (row, col-1, realBoard) == true)
count++;
}
if (isValid (row-1, col+1) == true)
{
if (isMine (row-1, col+1, realBoard) == true)
count++;
}
// Only process this cell if this is a valid one
if (isValid (row-1, col-1) == true)
{
if (isMine (row-1, col-1, realBoard) == true)
count++;
}
// Only process this cell if this is a valid one
if (isValid (row+1, col+1) == true)
{
if (isMine (row+1, col+1, realBoard) == true)
count++;
}
//----------- 8th Neighbour (South-West) ------------
// Only process this cell if this is a valid one
if (isValid (row+1, col-1) == true)
{
if (isMine (row+1, col-1, realBoard) == true)
count++;
}
return (count);
}
// A Recursive Fucntion to play the Minesweeper Game
bool playMinesweeperUtil(char myBoard[][MAXSIDE], char realBoard[][MAXSIDE],
int mines[][2], int row, int col, int *movesLeft)
{
// Base Case of Recursion
if (myBoard[row][col] != '-')
return (false);
int i, j;
if (realBoard[row][col] == '*')
{
myBoard[row][col]='*';
for (i=0; i<MINES; i++)
myBoard[mines[i][0]][mines[i][1]]='*';
printBoard (myBoard);
printf (" You lost! ");
return (true) ;
}
else
{
int count = countAdjacentMines(row, col, mines, realBoard);
(*movesLeft)--;
myBoard[row][col] = count + '0';
if (!count)
{
//----------- 1st Neighbour (North) ------------
// Only process this cell if this is a valid one
if (isValid (row-1, col) == true)
{
if (isMine (row-1, col, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row-1, col, movesLeft);
}
//----------- 2nd Neighbour (South) ------------
// Only process this cell if this is a valid one
if (isValid (row+1, col) == true)
{
if (isMine (row+1, col, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row+1, col, movesLeft);
}
//----------- 3rd Neighbour (East) ------------
// Only process this cell if this is a valid one
if (isValid (row, col+1) == true)
{
if (isMine (row, col+1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row, col+1, movesLeft);
}
//----------- 4th Neighbour (West) ------------
// Only process this cell if this is a valid one
if (isValid (row, col-1) == true)
{
if (isMine (row, col-1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row, col-1, movesLeft);
}
//----------- 5th Neighbour (North-East) ------------
// Only process this cell if this is a valid one
if (isValid (row-1, col+1) == true)
{
if (isMine (row-1, col+1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row-1, col+1, movesLeft);
}
//----------- 6th Neighbour (North-West) ------------
// Only process this cell if this is a valid one
if (isValid (row-1, col-1) == true)
{
if (isMine (row-1, col-1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row-1, col-1, movesLeft);
}
//----------- 7th Neighbour (South-East) ------------
// Only process this cell if this is a valid one
if (isValid (row+1, col+1) == true)
{
if (isMine (row+1, col+1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row+1, col+1, movesLeft);
}
//----------- 8th Neighbour (South-West) ------------
// Only process this cell if this is a valid one
if (isValid (row+1, col-1) == true)
{
if (isMine (row+1, col-1, realBoard) == false)
playMinesweeperUtil(myBoard, realBoard, mines, row+1, col-1, movesLeft);
}
}
return (false);
}
}
void placeMines(int mines[][2], char realBoard[][MAXSIDE])
{
bool mark[MAXSIDE*MAXSIDE];
memset (mark, false, sizeof (mark));
// Continue until all random mines have been created.
for (int i=0; i<MINES; )
{
int random = rand() % (SIDE*SIDE);
int x = random / SIDE;
int y = random % SIDE;
if (mark[random] == false)
{
// Row Index of the Mine
mines[i][0]= x;
// Column Index of the Mine
mines[i][1] = y;
// Place the mine
realBoard[mines[i][0]][mines[i][1]] = '*';
mark[random] = true;
i++;
}
}
return;
}
void initialise(char realBoard[][MAXSIDE], char myBoard[][MAXSIDE])
{
srand(time (NULL));
// Assign all the cells as mine-free
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
{
myBoard[i][j] = realBoard[i][j] = '-';
}
}
return;
}
void cheatMinesweeper (char realBoard[][MAXSIDE])
{
printf ("The mines locations are- ");
printBoard (realBoard);
return;
}
void replaceMine (int row, int col, char board[][MAXSIDE])
{
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
{
if (board[i][j] != '*')
{
board[i][j] = '*';
board[row][col] = '-';
return;
}
}
}
return;
}
void playMinesweeper ()
{
// Initially the game is not over
bool gameOver = false;
char realBoard[MAXSIDE][MAXSIDE], myBoard[MAXSIDE][MAXSIDE];
int movesLeft = SIDE * SIDE - MINES, x, y;
int mines[MAXMINES][2];
initialise (realBoard, myBoard);
placeMines (mines, realBoard);
int currentMoveIndex = 0;
while (gameOver == false)
{
printf ("Current Status of Board : ");
printBoard (myBoard);
makeMove (&x, &y);
// This is to guarantee that the first move is
// always safe
// If it is the first move of the game
if (currentMoveIndex == 0)
{
// If the first move itself is a mine
// then we remove the mine from that location
if (isMine (x, y, realBoard) == true)
replaceMine (x, y, realBoard);
}
currentMoveIndex ++;
gameOver = playMinesweeperUtil (myBoard, realBoard, mines, x, y, &movesLeft);
if ((gameOver == false) && (movesLeft == 0))
{
printf (" You won ! ");
gameOver = true;
}
}
return;
}
// A Function to choose the difficulty level
// of the game
void chooseDifficultyLevel ()
{
int level;
printf ("Enter the Difficulty Level ");
printf ("Press 0 for BEGINNER (9 * 9 Cells and 10 Mines) ");
printf ("Press 1 for INTERMEDIATE (16 * 16 Cells and 40 Mines) ");
printf ("Press 2 for ADVANCED (24 * 24 Cells and 99 Mines) ");
scanf ("%d", &level);
if (level == BEGINNER)
{
SIDE = 9;
MINES = 10;
}
if (level == INTERMEDIATE)
{
SIDE = 16;
MINES = 40;
}
if (level == ADVANCED)
{
SIDE = 24;
MINES = 99;
}
return;
}
// Driver Program to test above functions
int main()
{
chooseDifficultyLevel ();
playMinesweeper ();
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.