Minefield program

This program fills a 20x20 field with mines then identify the number of mines in the 8 surrounding areas of each point. After that it will display the number in that block. Can anyone tell me what is going on with the;

MineField[0][0]=48+ count; in each block. I am trying to figure out how this program is working. Thanks

#include <iostream>
#include<time.h>
#define FIELD_SIZE 20
using namespace std;
int main()
{
int x, y;
char MineField[FIELD_SIZE][FIELD_SIZE];
for (int i = 0; i < FIELD_SIZE; i++)
for (int j = 0; j < FIELD_SIZE; j++)
MineField[i][j] = '.';

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

int count = 0;
if (MineField[0][0] != '#') //Counting Mines for top left corner cell.
{
if (MineField[0][1] == '#')
count++;
if (MineField[1][1] == '#')
count++;
if (MineField[1][0] == '#')
count++;
MineField[0][0]=48+ count;
}
for (int j = 1; j < FIELD_SIZE - 1; j++) //For the first row, other than the extreme cells.
{
count = 0;
if (MineField[1][j - 1] == '#')
count++;
if (MineField[1][j] == '#')
count++;
if (MineField[1][j + 1] == '#')
count++;
if (MineField[0][j - 1] == '#')
count++;
if (MineField[0][j + 1] == '#')
count++;
if (MineField[0][j] != '#')
MineField[0][j] = 48 + count;
}
count = 0;
if (MineField[0][FIELD_SIZE - 1] != '#') //For the top right corner cell.
{
if (MineField[0][FIELD_SIZE - 2] == '#')
count++;
if (MineField[1][FIELD_SIZE - 2] == '#')
count++;
if (MineField[1][FIELD_SIZE - 1] == '#')
count++;
MineField[0][FIELD_SIZE - 1] = 48 + count;
}
for (int i = 1; i < FIELD_SIZE - 1; i++) //For the cells from second row to penultimate row.
{
if (MineField[i][0] != '#') //For the first cell in each row.
{
count = 0;
if (MineField[i - 1][0] == '#')
count++;
if (MineField[i + 1][0] == '#')
count++;
if (MineField[i - 1][1] == '#')
count++;
if (MineField[i][1] == '#')
count++;
if (MineField[i + 1][1] == '#')
count++;
MineField[i][0] = 48 + count;
}
for (int j = 1; j < FIELD_SIZE - 1; j++) //For the remaining cells.
{
count = 0;
if (MineField[i][j] == '#')
continue;
if (MineField[i - 1][j - 1] == '#')
count++;
if (MineField[i - 1][j] == '#')
count++;
if (MineField[i - 1][j + 1] == '#')
count++;
if (MineField[i][j - 1] == '#')
count++;
if (MineField[i][j + 1] == '#')
count++;
if (MineField[i + 1][j - 1] == '#')
count++;
if (MineField[i + 1][j] == '#')
count++;
if (MineField[i + 1][j + 1] == '#')
count++;
MineField[i][j] = 48 + count;
}
if (MineField[i][FIELD_SIZE - 1] != '#') //For the last cell in each row.
{
count = 0;
if (MineField[i - 1][FIELD_SIZE - 1] == '#')
count++;
if (MineField[i + 1][FIELD_SIZE - 1] == '#')
count++;
if (MineField[i - 1][FIELD_SIZE - 2] == '#')
count++;
if (MineField[i][FIELD_SIZE - 2] == '#')
count++;
if (MineField[i + 1][FIELD_SIZE - 2] == '#')
count++;
MineField[i][FIELD_SIZE - 1] = 48 + count;
}
}
count = 0;
if (MineField[FIELD_SIZE - 1][0] != '#') //For bottom left corner cell.
{
if (MineField[FIELD_SIZE - 2][0] == '#')
count++;
if (MineField[FIELD_SIZE - 2][1] == '#')
count++;
if (MineField[FIELD_SIZE - 1][1] == '#')
count++;
MineField[FIELD_SIZE - 1][0] = 48 + count;
}
for (int j = 1; j < FIELD_SIZE - 1; j++) //For the last row, other than the extreme cells.
{
count = 0;
if (MineField[FIELD_SIZE - 1][j - 1] == '#')
count++;
if (MineField[FIELD_SIZE - 1][j + 1] == '#')
count++;
if (MineField[FIELD_SIZE - 2][j - 1] == '#')
count++;
if (MineField[FIELD_SIZE - 2][j] == '#')
count++;
if (MineField[FIELD_SIZE - 2][j + 1] == '#')
count++;
if (MineField[FIELD_SIZE - 1][j] != '#')
MineField[FIELD_SIZE - 1][j] = 48 + count;
}
count = 0;
if (MineField[FIELD_SIZE - 1][FIELD_SIZE - 1] != '#') //For the bottom right corner cell.
{
if (MineField[FIELD_SIZE - 2][FIELD_SIZE - 1] == '#')
count++;
if (MineField[FIELD_SIZE - 2][FIELD_SIZE - 2] == '#')
count++;
if (MineField[FIELD_SIZE - 1][FIELD_SIZE - 2] == '#')
count++;
MineField[FIELD_SIZE - 1][FIELD_SIZE - 1] = 48 + count;
}
for (int i = 0; i < FIELD_SIZE; i++) //Displaying MineField.
{
for (int j = 0; j < FIELD_SIZE; j++)
printf("%c ", MineField[i][j]);
printf("\n");
}
printf("Press any key to continue. . . .\n");
}
Can anyone tell me what is going on with the MineField[0][0]=48+ count; in each block.

48 is the ASCII code for '0', so it is setting MineField[0][0] to 1, 2, 3, .... A better way to code this would be
MineField[0][0] = '0'+count;

By the way this code can be greatly simplified with a little trick: If the field is 20x20, make the array 22x22, adding an extra hidden square all the way around the board. None of these squares contain mines. If you do this, then you don't need any special cases for computing the number of neighboring mines.

Thank you so much.
Topic archived. No new replies allowed.