Minefield program

I shaved down the code a bit. Can anyone tell me how to get the sides of the minefield to display correctly?

Thanks

#include <iostream>
using namespace std;

void main()
{
const int size = 20;
int x, y,count;
char array[size][size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
array[i][j] = '.';

srand(2202); //Set a random number seed.
for (int i = 0; i < size * size * 0.1; i++) //Placing mines in 10% of the field.
{
x = rand() % size;
y = rand() % size;
array[x][y] = '#';

}
for (int i = 1; i < size; i++)

for (int j = 1; j < size; j++) // Counting mines in 8 areas surrounding each index.
{
count = 0;
if (array[i][j] == '#')
continue;
if (array[i - 1][j - 1] == '#')
count++;
if (array[i - 1][j] == '#')
count++;
if (array[i - 1][j + 1] == '#')
count++;
if (array[i][j - 1] == '#')
count++;
if (array[i][j + 1] == '#')
count++;
if (array[i + 1][j - 1] == '#')
count++;
if (array[i + 1][j] == '#')
count++;
if (array[i + 1][j + 1] == '#')
count++;
array[i][j] = '0' + count;
}
for (int i = 0; i < size; i++) //Displaying MineField.
{
for (int j = 0; j < size; j++)
cout << array[i][j];
cout << " \n ";
}
}
You mean something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 ..................... 
 .1100001#10000000000. 
 .#100001110011101110. 
 .111110011101#212#21. 
 .002#2001#22323#312#. 
 .113#311112##12#3121. 
 .1#212#1001222333#21. 
 .1110111011101##212#. 
 .001110012#102331011. 
 .012#1001#2101#10000. 
 .01#2100111113220000. 
 .02220000001#2#10111. 
 .01#21001111121101#1. 
 .012#1012#1000000111. 
 .0011101#21011100000. 
 .000000111001#100000. 
 .0000111111011100000. 
 .00112#11#1000111000. 
 .002#3111221012#2110. 
 .002#20001#101#22#10. 
 ..................... 


First of all, use code tags (the <> button on the right)

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
#include <iostream>
using namespace std;

int main()
{
const int size = 20;
int x, y,count;
char array[size+1][size+1];
for (int i = 0; i <= size; i++)
for (int j = 0; j <= size; j++)
array[i][j] = '.';

srand(2202); //Set a random number seed.
for (int i = 0; i < size * size * 0.1; i++) //Placing mines in 10% of the field.
{
x = rand() % (size-1)+1;
y = rand() % (size-1)+1;
array[x][y] = '#';

}
for (int i = 1; i < size; i++)

for (int j = 1; j < size; j++) // Counting mines in 8 areas surrounding each index.
{
count = 0;
if (array[i][j] == '#')
continue;
if (array[i - 1][j - 1] == '#')
count++;
if (array[i - 1][j] == '#')
count++;
if (array[i - 1][j + 1] == '#')
count++;
if (array[i][j - 1] == '#')
count++;
if (array[i][j + 1] == '#')
count++;
if (array[i + 1][j - 1] == '#')
count++;
if (array[i + 1][j] == '#')
count++;
if (array[i + 1][j + 1] == '#')
count++;
array[i][j] = '0' + count;
}
cout << " \n ";
for (int i = 0; i <= size; i++) //Displaying MineField.
{
for (int j = 0; j <= size; j++)
cout << array[i][j];
cout << " \n ";
}
}


I have increased the size of the array, and I have made sure that the x and y for the mines are greater than 1, and less then size
Topic archived. No new replies allowed.