trying to make a minesweeper array check for mines in a certain selection


So, I am making a minesweeper game and I want it to display how many mines are around the players choice like this



1
2
3
[]  []  []
[]choice[]
[]  []  []


where choice equals the number of mines in the square around it, I'm not sure how to put it into code
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 &current_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
}
Last edited on
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.

Too advanced?
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
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
  else if((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
...
Topic archived. No new replies allowed.