Let's assume that choice is a position like struct position { int x; int y };
The upper left element is choice.x - 1; choice.y - 1;
You may create an array: const position surround[] = { { -1, - 1 }, { 0, -1 }, { 0, 1 }, ... }; // For all 8 possible positions
Now a loop:
1 2 3 4 5 6 7
const position surround[] = { { -1, - 1 }, { 0, -1 }, { 0, 1 }, ... };
for(const position ¤t_pos : surround)
{
const position absolute_pos = choice + current_pos; // or another way to add the x/y values
// Check if absolute_pos is not out of bounds and access the underlying [mine] element
}
Choice represents the users choice on the mine array
I'll try to be a bit clearer:
So the mines array is an int array that is 8x8 filled with 10 random mines
the char reveal array is outputting what the mines and the blacked out squares
as it stands this all works, if a square is revealed it turns into a blank space, if its a mine, it shows a umlaut (ó) which is representive of a mine (a -1 on the mines array)
Now what I need to do now, is count the amount of mines in a 3x3 radius after the player has made there coordinate choice and output the number of mines in that radius to the chosen space.
mines_array[8][8] = { ... };
...
struct coord { int x; int y };
coord operator+(const coord &c1, const coord &c2)
{
coord c = c1;
c.x += c2.x;
c.y += c2.y;
return c;
}
...
const coord relative_radius_coord_array[] = { { -1, -1 }, { 0, -1 }, { 0, 1 }, ... };
// Add relative coordiante for all 8 possible coordinate of the radius
// Note: { -1, -1 } is the upper left corner of the radius. { 1, 1 } is the lower right corner.
...
// let the user enter the choice_coord
...
int mines_count = 0;
for(const coord &relative_radius_coord : relative_radius_coord_array)
{
const position absolute_radius_coord = choice_coord + relative_radius_coord;
if((absolute_radius_coord.x < 0) or (absolute_radius_coord.y < 0))
; // out of bounds
elseif((absolute_radius_coord.x < 8) and (absolute_radius_coord.y < 8))
{
if('ó' == mines_array[absolute_radius_coord.x][absolute_radius_coord.y])
++mines_count;
}
}
// mines_count contains all mines of the radius
...