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
|
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int ROWS = 5; // a constant of row
const int COLS = 5; // a constant of column. I don't want the board to be more than 5 X 5
//enum to represent every possible value in the board.
enum Symbols {STAR = 42, AT = 64, X = 88, ZERO = 0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
//Function to cout basic info (Rules, names etc..)
void info();
/*
In this value,passing in the two-dimensional array and
the size. This function will simply initialize each position on the board to the enumerated
type representing the initial value, which is the character β*β (or STAR).
*/void initPosition(Symbols symbolArr[][COLS], int size);
/*
assign the number of mines between 5 and 10, inclusively, specified by
the user to randomly generated locations on the board using a bool function, passing
in the two-dimensional array and the size. This function will generate a random rowcolumn
position and then attempt to place a mine at that row-column position on the
board. Since the row-column position is randomly generated, if the function attempts to
place a mine on a square that already contains a mine, the method will return false,
indicating that the assignment was not successful. If the square, however, does not
already contain a mine, then the function will assign a mine to that location by assigning
that row-column position with the enumerated type representing the mine and return
true, indicative that the assignment was successful. Note that this function attempts to
place only one (1) mine at a randomly generated location on the board, so a loop should
be implemented in the calling function to achieve this functionality.
*/bool checkMine(Symbols symbolArr[][COLS], int size, int total_mines);
int main()
{
int mines;
/*
You shall declare a two-dimensional array in main() to represent the 5-by-5 board as an enum type you declare to represent the various values that a square can assume. *, @, X, 0-9
*/
Symbols symbolArr[][COLS] = {STAR, AT, X, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
do
{
cout << "Enter The Numbers of Mines Between 5 and 10: " << endl;
cin >> mines;
}while(mines <= 4 || mines >=11);
initPosition(symbolArr, ROWS); // call initialboard w/ *
checkMine(symbolArr, ROWS, mines); // call function that randomly assigns bombs
//displayBoard(symbolArr, ROWS); //function to combine the two functions and display with border
return 0;
}
void initPosition(Symbols symbolArr[][COLS], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < COLS; j++)
{
cout <<static_cast<char>(42);
}
cout << endl;
}
}
bool checkMine(Symbols symbolArr[][COLS], int size, int total_mines)
{
srand(time(NULL));
int row = rand() % total_mines;
int col = rand() % total_mines;
for (int mines_placed = 0; mines_placed < total_mines;)
{
if(symbolArr[row][col] != AT)
{
symbolArr[row][col] = AT; // place the mine
mines_placed++;
}
else
{
return -1;
}
}
}
|