Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey, i am trying to write the code for a function to reveal the tiles with no bombs around them around the selected code. i have written the code that can reveal the bombs around one tile with the range of 1 and i can use this exact function for all the tiles around the selected one again but that is too much work and doesn't make the whole code portable.

What I have tried:

so what i have done is created two arrays, one of them is the mine field which the user can't make any changes to "hidden_board" and the other one is "my_board" which all of the changes are applied to this array and here are the functions that i have written:
C++
void reveal_tiles_without_bombs_prime(int x, int y, int my_board[][9], int hidden_board[][9]){
--x;
--y;
if(x>=0 && x<9 && y>=0 && y<9){
if (my_board[x][y]==10){
    if (number_of_bombs_around(x,y,hidden_board)==0){
        if (y+1<9){
            if (number_of_bombs_around(x+1, y+1+1, hidden_board)==0)
            my_board[x][y+1]= 0;
            }
        if (y-1>=0){
            if (number_of_bombs_around(x+1, y-1+1, hidden_board)==0)
            my_board[x][y-1]= 0;
            }
        if (x-1>=0){
            if (number_of_bombs_around(x-1+1, y+1, hidden_board)==0)
            my_board[x-1][y]= 0;
            }
        if (x+1<9){
            if (number_of_bombs_around(x+1+1, y+1, hidden_board)==0)
            my_board[x+1][y]= 0;
            }
        if ((y-1>=0) && (x-1>=0)){
            if (number_of_bombs_around(x-1+1, y-1+1, hidden_board)==0)
            my_board[x-1][y-1]= 0;
            }
        if ((y+1<9) && (x-1>=0)){
            if (number_of_bombs_around(x-1+1, y+1+1, hidden_board)==0)
            my_board[x-1][y+1]= 0;
            }
        if ((y-1>=0) && (x+1<9)){
            if (number_of_bombs_around(x+1+1, y-1+1, hidden_board)==0)
            my_board[x+1][y-1]= 0;
            }
        if ((y+1<9) && (x+1<9)){
            if (number_of_bombs_around(x+1+1, y+1+1, hidden_board)==0)
            my_board[x+1][y+1]= 0;
            }

   }
  }
 }
}
int the my_board array i use 10 for an unrevealed tile which is not a bomb and numbers 0 to 8 for number of bombs around a revealed tile and 11 for an unrevealed bomb and the number_of_bombs_around is an another function that returns the number of bombs around the given tile. so this function is actually called only one time in the else part:
C++
if (number_of_bombs_around(x,y, hidden_board)!=0){
                my_board[x-1][y-1]= number_of_bombs_around(x,y,my_board);
                draw_mine_field(my_board);
                           }
            else{
                reveal_tiles_without_bombs_prime(x,y,my_board, hidden_board);
                my_board[x-1][y-1]= 0;

and after using the prime version i have to use the function below to reveal all of the tiles with no bombs around them around the tile that is given:
C++
void reveal_tiles_without_bombs(int x, int y, int my_board[][9], int hidden_board[][9]){
--x;
--y;
if(x>=0 && x<9 && y>=0 && y<9){
if (my_board[x][y]==0){
        if (y+1<9){
            if (number_of_bombs_around(x+1, y+1+1, hidden_board)==0)
            my_board[x][y+1]= 0;
            }
        if (y-1>=0){
            if (number_of_bombs_around(x+1, y-1+1, hidden_board)==0)
            my_board[x][y-1]= 0;
            }
        if (x-1>=0){
            if (number_of_bombs_around(x-1+1, y+1, hidden_board)==0)
            my_board[x-1][y]= 0;
            }
        if (x+1<9){
            if (number_of_bombs_around(x+1+1, y+1, hidden_board)==0)
            my_board[x+1][y]= 0;
            }
        if ((y-1>=0) && (x-1>=0)){
            if (number_of_bombs_around(x-1+1, y-1+1, hidden_board)==0)
            my_board[x-1][y-1]= 0;
            }
        if ((y+1<9) && (x-1>=0)){
            if (number_of_bombs_around(x-1+1, y+1+1, hidden_board)==0)
            my_board[x-1][y+1]= 0;
            }
        if ((y-1>=0) && (x+1<9)){
            if (number_of_bombs_around(x+1+1, y-1+1, hidden_board)==0)
            my_board[x+1][y-1]= 0;
            }
        if ((y+1<9) && (x+1<9)){
            if (number_of_bombs_around(x+1+1, y+1+1, hidden_board)==0)
            my_board[x+1][y+1]= 0;
            }

  }
 }
}
so my question is that can i use a function to call reveal_tiles_without_bombs for all of the tiles around the chosen one until there are no more ? sorry for the long codes :)
Posted
Updated 23-Dec-18 0:33am
v3

1 solution

The way I would do this is to cheat: My two board array would be bigger than the playing area.
If you allow a border round the whole play area:
0 0 0 0 0  
0 . . . 0   3 x 3 play area
0 . . . 0
0 . . . 0
0 0 0 0 0
So the array is [5,5] and the "actual play area" is just where the dots are: indexes 1 through 3 inclusive in both x and y.

This means your "how many round this" becomes simple:
int HowManyAround(int x, int y)
{
    int count = 0;
    count += board[x - 1][y - 1] == mine ? 1 : 0;
    count += board[x - 1][y + 0] == mine ? 1 : 0;
    count += board[x - 1][y + 1] == mine ? 1 : 0;
    count += board[x + 0][y - 1] == mine ? 1 : 0;
    count += board[x + 0][y + 1] == mine ? 1 : 0;
    count += board[x + 1][y - 1] == mine ? 1 : 0;
    count += board[x + 1][y + 0] == mine ? 1 : 0;
    count += board[x + 1][y + 1] == mine ? 1 : 0;
    return count;
}
You don't need to check the edges, because you preset them with "not a mine" and that never changes. Make my_board the same size, and always use 1 based indexes.

Then you can build your whole my_board array with a simple nested loop:
for (int x = 1; x <= board_size; x++)
    {
    for (int y = 1; y < board_size; y++)
        {
        my_board[x][y] = HowManyAround(x, y);
        }
    }

Then all you need to do is look for a "bounded area" (i.e. an area of zeros surrounded by non-zeros).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900