Have you ever played Minesweeper This cute little game comes

Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can’t remember. The goal of the game is to find where all the mines are located within a M × N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 × 4 field on the left contains two mines, each represented by a “*” character. If we represent the same field by the hint numbers described above, we end up with the field on the right: *... *100 .... 2210 .*.. 1*10 .... 1110 Input (inputdata.dat file) The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m (0 < n, m 100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by “.” and mine squares by “*,” both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed. Output (outputdata.dat file) For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the “.” characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.

Solution

#include <iostream>

#include <fstream>

using namespace std;

//print minesweeper

void display(fstream &out, int **arr, int n, int m, int count) {

int i, j;

out << \"Field #\" << count << endl;

for (i = 0; i < n; i++) {

for (j = 0; j < m; j++) {

if (arr[i][j] == -1) {

out << \"*\";

}

else{

out << arr[i][j];

}

}

out << endl;

}

out << endl;

}

/*transform mine array, replace every empty space by number of mine adjacent to it*/

void transform(int **arr, int n, int m) {

int i, j, count;

for (i = 0; i < n; i++) {

for (j = 0; j < m; j++) {

count = 0;

if (arr[i][j] == -2) {

if (i - 1 >= 0 && arr[i - 1][j] == -1) //upward

count++;

if (i - 1 >= 0 && j - 1 >= 0 && arr[i - 1][j - 1] == -1) //left upward

count++;

if (i - 1 >= 0 && j + 1 < m && arr[i - 1][j + 1] == -1) //right upward

count++;

if (j - 1 >= 0 && arr[i][j - 1] == -1) //left

count++;

if (j + 1 < m && arr[i][j + 1] == -1) //right

count++;

if (i + 1 < n && arr[i + 1][j] == -1) // downward

count++;

if (i + 1 < n && j - 1 >= 0 && arr[i + 1][j - 1] == -1) //left downward

count++;

if (i + 1 < n && j + 1 < m && arr[i + 1][j + 1] == -1) //right downward

count++;

arr[i][j] = count;

}

}

}

}

int main() {

//open file stream

fstream inputFile(\"input.dat\", ios::in);

fstream outputFile(\"output.dat\", ios::out);

int n = -1, m = -1, i, j, count = 1;

char ch;

inputFile >> n >> m;

while (n > 0 && m > 0) {

/*allocate memory from heap of size n x m */

int ** arr = new int*[n];

for (i = 0; i < n; i++) {

arr[i] = new int[m];

}

/* fill arr with -1 indicating mine and -2 indicating free space*/

for ( i = 0; i < n; i++) {

for ( j = 0; j < m; j++) {

inputFile >> ch;

if (ch == \'*\')

arr[i][j] = -1;

else

arr[i][j] = -2;

}

}

/*transform mine array, replace every empty space by number of mine adjacent to it*/

transform(arr, n, m);

//display results

display(outputFile, arr, n, m, count);

//deallocate memory

for (i = 0; i < n; i++) {

delete arr[i];

}

delete arr;

/* set it to -1 for next input*/

n = -1;

m = -1;

/*read next n and m*/

inputFile >> n >> m;

/*increment field count*/

count++;

}

//close corresponding file

inputFile.close();

outputFile.close();

return 0;

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site