I am currently working on solving a minesweeper C++ program, and I am unable to receive the correct output when the program finishes. I know it's a logic error, most likely from the "currentCell" variable, but I cannot figure out how to make it work. My output ends up like this: ____________________________________________________________________________________________________ ----
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
// Global Constants
const int MAX_ROWS = 10;
const int MAX_COLUMNS = 10;
const int EMPTY_SQUARE_DIGIT = 0;
const char EMPTY_SQUARE_SYMBOL = '_';
const int BOMB_DIGIT = -1;
const char BOMB_SYMBOL = '#';
char currentCell = 0;
// Function Prototypes
void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void insertTheMineClues(int board[MAX_ROWS][MAX_COLUMNS]);
void incrementTheNeighborSquares(int board[MAX_ROWS][MAX_COLUMNS], int row, int column);
// ############################################
int main(void)
{
int gameBoard[MAX_ROWS][MAX_COLUMNS];
srand(time(NULL));
fillTheGameBoard(gameBoard);
insertTheMineClues(gameBoard);
displayTheGameBoard(gameBoard);
return 0;
} // End main
// ############################################
void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (rand() % (MAX_ROWS - 3) == 0)
{
currentCell = BOMB_DIGIT;
}
else
{
currentCell = EMPTY_SQUARE_DIGIT;
}
}
}
}
} // End fillTheGameBoard
// ############################################
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (currentCell == BOMB_DIGIT)
{
cout << BOMB_SYMBOL;
}
else if (currentCell == EMPTY_SQUARE_DIGIT)
{
cout << EMPTY_SQUARE_SYMBOL;
}
else
{
cout << currentCell;
}
}
}
}
} // End displayTheGameBoard
// ############################################
void insertTheMineClues(int board[MAX_ROWS][MAX_COLUMNS])
{
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (currentCell == BOMB_DIGIT)
{
incrementTheNeighborSquares(board, row, column);
}
}
}
}
} // End insertTheMineClues
// ############################################
// The function definition below is finished. Make no changes to it.
void incrementTheNeighborSquares(int board[MAX_ROWS][MAX_COLUMNS], int bombRow, int
bombColumn)
{
for (int row = bombRow - 1; row <= bombRow + 1; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
for (int column = bombColumn - 1; column <= bombColumn + 1; column++)
{
if ( (column >= 0) && (column < MAX_COLUMNS) && (board[row][column] != BOMB_DIGIT) )
board[row][column]++;
} // End for column
} // End if
} // End for row
} // incrementTheNeighborSquares
Ok, you have at least two problems.
The biggest one:
You are setting the variable currentCell
, but what you actually mean is to set a cell in the board!
Change all the assignments and references to this:
board[row][column]
Second problem:
The print function does not print the line feed. Just add cout << endl;
to the end of the nested for loop to fix it.
Third problem:
This is not actually a problem but it makes my eyes hurt ;-)
You have:
for (int row = 0; row < MAX_ROWS; row++)
{
if ( (row >= 0) && (row < MAX_ROWS) )
{
That nested if makes no sense since it will always evaluate to true
.