Minesweeper problem

I recently tried solving some challenging (for noobs like me) problems to test my half-baked coding skills. In programming-challenges.com, I found this problem called Minesweeper (one of the easiest there). To those who are familiar with the game, this might be easy to understand. Here's the program specifications:

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 x 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 x 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

The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m100) 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

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.

Sample Input

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0


Sample Output

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100




I've figured out the algorithm and coded it. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

int main() {
	int m, n;
	int count = 0;
	
	while ( cin >> m >> n ) {
		char field[m][n];
		string line;
		
		//INPUT
		if ((m != 0 && n != 0) && (m <= 100 && n <= 100)) {
			count++;
			for (int i = 0; i < m; i++ ) {
				cin >> line;
				for (int j = 0; j < n; j++ )
					field[i][j] = line[j];
			}
		}
		
		else
			break;
			
		//INITIALIZE COUNTERS
		int ctr[m][n];
		for (int x = 0; x < m; x++) {
			for (int y = 0; y < n; y++) 
				ctr[x][y] = 0;
		}

		//CASES
		for (int row = 0; row < m; row++) {
			for (int col = 0; col < n; col++) {
				if (field[row][col] == '*') {

					ctr[row - 1][col - 1]++;
					ctr[row - 1][col]++;
					ctr[row - 1][col + 1]++;
					ctr[row][col - 1]++;
					ctr[row][col + 1]++;
					ctr[row + 1][col - 1]++;
					ctr[row + 1][col]++;
					ctr[row + 1][col + 1]++;
					
				}
			}
		}
		
		//PRINTING
		cout << "Field #" << count << ":\n";
		for (int r = 0; r < m; r++) {
			for (int c = 0; c < n; c++) {
				if (field[r][c] == '*')
					cout << '*';
				else
					cout << ctr[r][c];
			}
			cout << endl;
		}
		cout << endl;
	}
	return 0;
}


As you may notice, in the //CASES fragment of the code, I traverse through the rows and columns of the whole field. As a field having a bomb indicated by an '*' value is encountered, I increment all the counters corresponding every square/space/cell around that bomb.

I was quite confident that it will work since I find no flaw in it, but since I'm a novice programmer, I might be missing something. When I tested some inputs including the sample inputs, it produced the following outputs:

Input (First two are sample inputs)
4 4
*...
....
.*..
....

3 5
**...
.....
.*...

3 3
***
*.*
***

4 4
....
**..
..*.
....

0 0


Output
Field #1:
*101
2210
1*10
1110

Field #2:
**101
33200
1*100

Field #3:
***
*8*
***

Field #4:
2211
**22
23*1
0111



I noticed that the only thing wrong in the outputs are those 1s on the upper-rightmost corner of the fields, which based on the inputs are supposed to have 0 values.

Field #3 is correct since the upper-rightmost corner has a bomb in it, and in my //PRINTING fragment, if a cell has a bomb, I automatically print an asterisk rather than printing its corresponding counter.

Since I believe the error here is more on the syntax and not on the logic, I think it would be alright to post it here in the Beginners forum.

Any mistakes you have noticed? What might I alter to produce correct results? Any help would be appreciated. Thanks!
Well I can tell you just by looking at the output, that your array is "wrapping" around the board at the last m of n=0 maybe this will help you figure it out.
Topic archived. No new replies allowed.