This is my current code. I have to follow the instructions below but keep getting errors. Can you please help?
C++ that produces a two-dimensional board of bombs and numbers that could be used as a data structure inside a minesweeper game such as the one that comes with Microsoft Windows.
The display of the board shall be text-based and appear in the format shown in the example-program-output.txt file with each digit or character printed in a field width of three. A blank square shall appear as an underscore character, a bomb square shall appear as a '#' character, and a number square shall appear as the number.
The game board data structure shall consist of a two-dimensional integer array. A blank square shall be stored as a zero, a bomb shall be stored as a -1, and a number (i.e., clue) square shall be stored as the respective number.
The program shall follow this basic algorithm:
Create a game board as a two-dimensional integer array with MAX_ROWS and MAX_COLUMNS
Randomly fill the squares on the game board with bombs or zeros
Go through the board, square by square, looking for each bomb. When one is found, increment (by one) the values in the number squares surrounding a bomb
Display the game board on the screen
The program shall have no interaction with a user except for starting the program.
Software Design
To satisfy the software requirements, implement the design described below.
int main(void)
This function definition is finished. Make no changes to it. It contains an implementation of the basic algorithm described in the software requirements.
void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
Set up a double for loop that will iterate through each row and column of the board in order to fill the squares. Be sure to use the MAX_ROWS and MAX_COLUMNS constants in your 'for loop' source code instead of the literal numbers given for the those constants in the skeleton file.
Inside the innermost 'for' loop, store a BOMB_DIGIT in a square when the following probability formula is true: (rand() % (MAX_ROWS - 3) == 0), otherwise, put an EMPTY_SQUARE_DIGIT in the square. Be sure to use the BOMB_DIGIT and EMPTY_SQUARE_DIGIT constants in your source code.
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
Set up a double for loop that will iterate through each row and column of the board in order to print (in a width of 3) the contents of each square as shown in the example-program-output.txt file. Inside the inner for loop, use an if-else structure to do the following. If a square contains the BOMB_DIGIT, then print the BOMB_SYMBOL. If, instead, a square contains the EMPTY_SQUARE_DIGIT then, print the EMPTY_SQUARE_SYMBOL; otherwise, print the numeric value stored in the square.
void insertTheMineClues(int board[MAX_ROWS][MAX_COLUMNS])
This function works in conjunction with the incrementTheNeighborSquares function to put the mine number clues onto the board. To modularize the implementation, this function handles the part of finding each bomb on the board. The other function handles the part about correctly storing the number clues.
Set up a double for loop that will iterate through each row and column of the board. Do the following inside the inner for loop. If a square contains the BOMB_DIGIT then call the incrementTheNeighborSquares function and pass it the board, the current row value, and the current column value.
void incrementTheNeighborSquares(int board[MAX_ROWS][MAX_COLUMNS], int bombRow, int bombColumn)
The algorithm checks all of the neighbor (i.e., adjacent) squares surrounding a bomb square. If a neighbor square does not contain the BOMB_DIGIT, then the value in the square is incremented by one. A double for loop is used to iterate through the row and column location of each neighbor square surrounding the bomb square. Because C++ does no range checking when an array is indexed, the algorithm checks that, also.
This function definition is already finished. Make no changes to it.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
# include <iomanip>
# include <time.h>
# include <cstdlib>
using namespace std;
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 = '#';
int square, bombRow, bombColumn;
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 main (void)
{
int gameBoard [MAX_ROWS] [MAX_COLUMNS];
srand(time(NULL));
fillTheGameBoard (gameBoard);
insertTheMineClues (gameBoard);
displayTheGameBoard (gameBoard);
system("pause");
return 0;
}
//#########################################################################################################
void fillTheGameBoard (int board[MAX_ROWS] [MAX_COLUMNS])
{
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (rand () % (MAX_ROWS - 3) == 0)
{
board[row][column] = BOMB_DIGIT;
}
else
{
board [row] [column] = EMPTY_SQUARE_DIGIT;
}
}
}
//##################################################################################################################
void displayTheGameBoard (int board [MAX_ROWS] [MAX_COLUMNS]);
{
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (board [row] [column] == BOMB_DIGIT)
{
cout << setw(3) << BOMB_SYMBOL;
}
else if (board [row] [column] == EMPTY_SQUARE_DIGIT)
{
cout << setw(3) << EMPTY_SQUARE_SYMBOL;
}
else
{
cout << setw (3) << board [row] [column];
}
}
cout << endl << endl;
}
}
//######################################################################################################
void insertTheMineClues (int board [MAX_ROWS] [MAX_COLUMNS]);
{
for (int row = 0; row < MAX_ROWS; row++)
{
for (int column = 0; column < MAX_COLUMNS; column++)
{
if (board [row] [column] == BOMB_DIGIT)
{
incrementTheNeighborSquares (board, row, column);
}
}
}
}
// #####################################################################################################
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] ++;
}
}
}
}
| |