dimanche 24 juillet 2016

How to recursively count the number of mines that touch each square in Minesweeper?

I was assigned to write Minesweeper in C. I'm trying to improve this particular function by writing it with recursion. I was able to write it with iteration, but it was a long and complicated chain of 9 if else statement. I'm trying to figure out how to count the number of mines that touch all the blocks in my game. Here is what I've kind of brainstormed, but the logic is far off.

int countMines(Board* board, int x, int y)
{
    if(x < 0 || x >= board->numRows || y < 0 || y >= board->numColumns)
    {
    }
    else if(board->squares[x][y].T == MINE)
    {
        board->squares[x][y]+=1;
    } // do nothing if you step out of bounds
    countMines(board, x+1, y); // all possible squares to uncover
    countMines(board, x-1, y);
    countMines(board, x, y-1);
    countMines(board, x, y+1);
    countMines(board, x+1, y+1);
    countMines(board, x-1, y+1);
    countMines(board, x+1, y-1);
    countMines(board, x-1, y-1);
}

Aucun commentaire:

Enregistrer un commentaire