Help with minesweeper program

Hi, I'm new to programming and I'm trying to create a minesweeper program. At the moment it's a bit of a mess but I thought I'd tidy it up once it worked properly. Also I haven't randomly allocated the mines yet.

The problem I'm having is in the function calcSurrounding, the if statement never evaluates to true so the surrounging mines stays at 0. Does anyone know why this is? Thanks for any help.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 #include <iostream>
#include <stdlib.h>
using namespace std;


class board{
public:

int numMines;
int numMarked;
int numUnknown;
bool mines[8][8];
//state of each cell
int state[8][8];
bool gameOver;


board(int m){
numMines=m;
gameOver=false;
numUnknown=64;
//clear board
for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){
        mines[i][j]=false;
        state[i][j]=10;
    }
}
//allocate mines
mines[0][3]=true;
mines[3][7]=true;
mines[7][5]=true;
}

void draw(void){
for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){
            // nothing revealed
        if(state[i][j]==10){
            cout << '#';
        }
            // flagged
        else if(state[i][j]==11){
            cout << 'F';
        }
            // mine revealed
        else if(state[i][j]==12){
            cout << '*';
        }
            // revealed to show surrounding mines
        else{
            cout << state[i][j];
        }
    }
    cout << endl;
}
}
void reveal(int x, int y){
if(mines[x][y]==true){
    state[x][y]=12;
    gameOver=true;
}
else{
state[x][y]=calcSurrounding(x,y);
}
numUnknown--;
}

void flag(int x, int y){
state[x][y]='F';
numMarked++;
}
int calcSurrounding(int x, int y){
int miny;
int minx;
int maxy;
int maxx;
int surroundingMines=0;
if(y > 0){miny=y-1;}
else{miny=y;}
if(x > 0){minx=x-1;}
else{minx = x;}
if(y < 7){maxy=y+1;}
else{maxy=y;}
if(x < 7){maxx=x+1;}
else{maxx=x;}
for(int i=miny;i<=maxy;i++){
    for(int j=minx;j<=maxx;j++){
        if(mines[i][j]==true){surroundingMines++;}
    }
}
return surroundingMines;
}

};


int main()
{
    int xcord;
    int ycord;
    board b=board(3);
    for(;;){
    system("cls");
    b.draw();
    cout << endl;
    cout << endl;
    cout << "Enter co-ordinates: ";
    cin >>xcord>>ycord;
    b.reveal(xcord,ycord);
    if(b.gameOver==true){
        system("cls");
            b.draw();
    cout << endl;
    cout << endl;
    cout << "GAME OVER";
            break;
    }
    }
    //cout << "Hello world!" << endl;
    return 0;
}
Sorry I've figured it out now. The co-ordinates were the wrong way around. Some constructive criticism on the program so far would really help thought.

Thanks
In your reveal function

When you call calc surrounding, you need to refer to the coordinates as (x-1, y-1) because of the way arrays are referenced.
If they choose (1, 1), the array equivalent should be (0, 0).
Topic archived. No new replies allowed.