I'm currently making a minesweeper game on C++, but there seems to be a bug. There's a function I wrote to return the amount of mines around the spot, just like in a classic game of minesweeper. However, the code fails near the end. I have provided some code to better explain.
int findBeginnerMines(string actualBoard[BEGINNER_DIMENSION][BEGINNER_DIMENSION], int x, int y)
{
int mines = 0;
if (actualBoard[x][y] == MINE)
{
mines = -1;
}
else
{
if (actualBoard[x - 1][y - 1] == MINE)
{
mines++;
}
if (actualBoard[x][y - 1] == MINE)
{
mines++;
}
if (actualBoard[x + 1][y - 1] == MINE && x != BEGINNER_DIMENSION - 1)
{
mines++;
}
if (actualBoard[x - 1][y] == MINE)
{
mines++;
}
if (actualBoard[x + 1][y] == MINE && x != BEGINNER_DIMENSION - 1)
{
mines++;
}
if (actualBoard[x - 1][y + 1] == MINE && y != BEGINNER_DIMENSION - 1)
{
mines++;
}
if (actualBoard[x][y + 1] == MINE && y != BEGINNER_DIMENSION - 1)
{
mines++;
}
if (actualBoard[x + 1][y + 1] == MINE && x != BEGINNER_DIMENSION - 1 && y != BEGINNER_DIMENSION - 1)
{
mines++;
}
}
return mines;
}
In this case, BEGINNER_DIMENSION = 8. When x = 7, this function returns an error.