Minesweeper

MY TASK
Try to write a program to give Minesweeper fields hints.

INPUT
The input will consist of an arbitrary number of fields. The first line of each field contains two integers,n and m(1<=n,m<=100), which stands for the number of lines and columns of the field respectively. The next n lines contain exactly m characters and represent the field. Each safe square is represented by an "." character and each mine square are represented by a "*" character. The first field line where n=m=0 represents the end of input and should not be processed.

OUTPUT
For each field, print "Field #x:"(x = the number of the field starting from 1) in a line alone.
The next n lines should contain the field with the "." characters replaced by the number of adjacent mines to that square. There should be an empty line between field outputs.

PROBLEM
Keep having errors and very buggy :(


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
	int m=0;
	int cField,rField;
int main(){
	cin>>cField>>rField;
	while(cField*rField!=0){

	char char(m+65)[cField][rField];
	for(int i=0;i<cField;i++){
        for(int j=0;j<rField;j++){
			cin>>char(m+65)[i][j];
        }
    }
   int result_m[cField][rField];
    for(int i=0;i<cField;i++){
        for(int j=0;j<rField;j++){
        	if(int(char(m+65)[i][j])==42){
				result_m[i][j]=9;
			}else{
				result_m[i][j]=0;
				//NW
				if(i==0||j==0){
				}else if((int)(char(m+65)[i-1][j-1])==42){
					result_m[i][j]++;
				}
				//N
				if(i==0){
				}else if((int)(char(m+65)[i-1][j])==42){
					result_m[i][j]++;
				}
				//NE
				if(i==0||j==(rField-1)){
				}else if((int)(char(m+65)[i-1][j+1])==42){
					result_m[i][j]++;
				}
				//W
				if(j==0){
				}else if((int)(char(m+65)[i][j-1])==42){
					result_m[i][j]++;
				}
				//E
				if(j==(rField-1)){
				}else if((int)(char(m+65)[i][j+1])==42){
					result_m[i][j]++;
				}
				//SW
				if(i==(cField-1)||j==0){
				}else if((int)(char(m+65)[i+1][j-1])==42){
					result_m[i][j]++;
				}
				//S
				if(i==(cField-1)){
				}else if((int)(char(m+65)[i+1][j])==42){
					result_m[i][j]++;
				}
				//SE
				if(i==(cField-1)||j==(rField-1)){
				}else if((int)(char(m+65)[i+1][j+1])==42){
					result_m[i][j]++;
				}
                cout<<result_m[i][j];
				}//else
			int cField_m=cField;
			int rField_m=rField;
			cin>>cField>>rField;

		}//for j
        cout<<endl;
	}//for i
	
	}//while
	for(int k=0;k<=m;k++){
		for(int l=0;l<=cF_m;l++){
			for(int n=0;n<=rF_m;n++){
				cout<<[k][l][n];
			}
		}
	}
}



Thanks a lot!
Hello kryptomega,

Line 2. Something you should avoid using. In the future it WILL cause you bigger problems. You should learn what is in the "std" name space and how to qualify what is needed like; "std::cout", "std::cin" and "std::endl" for a start.

Lines 3 and 4 are OK, but you should try to avoid global variables. A variable that starts with "constexpr" or const" is OK. Also do not count on a global variable being initialized. You should ALWAYS initialize your variables to be safe.

What is you point with line 9? As is you are trying to give something a type twice, or did you mean to use the second "char" as a type cast? I am not sure if "(m + 65)" will work here. It might be better to define "m" as a "char" and use "(m + 'A') for a better understanding of what you mean to do. Then there is the part
"[cField][rField]". This is not allowed in C++. What is between the "[]"s needs to be a constant value not a variable. Maybe it works in older C compilers, but not today. If you need to use these variables you either need to create a dynamic array or use them to limit the use of a larger array, as in how much of the array is used.

Last point the variable names "cField" and "rField" would lead one to believe "r" stands for row and "c" stands for column. If this is the case you are using them backwards when defining the array because in a 2D array the row comes first then the column. Also instead of using "(m + 65)" for the array name it would be easier to just call it "board". And if you have an idea of changing the value of "m" to create a second or third array what is the point of the additional arrays?

Until I fix the errors I can not test the program yet.

Hope that helps,

Andy
Hello Andy,

OK, I'll try. Thanks a lot!

kryptomega
Topic archived. No new replies allowed.