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 105 106 107 108 109 110 111 112 113 114
|
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//---------------------------Global Constants-----------------------------
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 = 1, TWO = 2, THREE = 3,
FOUR = 4, FIVE = 5, SIX = 6, SEVEN = 7, EIGHT = 8, NINE = 9
};
typedef Symbols* SymbolsPtr;
//------------------
//--------------Structure-----------
struct coordinates
{
int Rows[5]; //an integer member that holds the row position on the board
int Cols[5]; //an integer member that holds the column position on the board
};
//-----------------------------------
//------------------------------------function-------------------------
void info();
void initPosition(Symbols symbolArr[ROWS][COLS]);
bool checkMine(Symbols symbolArr[ROWS][COLS]);
void displayBoard(Symbols symbolArr[ROWS][COLS]);
//------------------------------------function-------------------------
int main()
{
srand(time(NULL));
int mines;
Symbols symbolArr[ROWS][COLS] = {STAR, AT, X, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
do //ask for number of mines between 5 and 10
{
cout << "Enter The Numbers of Mines Between 5 and 10: ";
cin >> mines;
}while(mines <= 4 || mines >=11);
initPosition(symbolArr); //calling the function that initalizes the stars
int count = 0; // counter set to 0
do
{
if(checkMine(symbolArr) == true) //only if bool returns true, countinue
{
count++;
}
}while(count < mines); //loop the amount of time specifed by the user;
displayBoard(symbolArr);
return 0;
}
void initPosition(Symbols symbolArr[ROWS][COLS]) //initalizes array so board starts with *
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
symbolArr[i][j] = STAR;
}
}
return;
}
bool checkMine(Symbols symbolArr[ROWS][COLS]) // function checks if at is in a array
{
int randomRow = (rand() % ROWS); //randomize rows
int randomCol = (rand() % COLS); //randomize column
if(symbolArr[randomRow][randomCol] != AT) // if the function doesn't already contain a bomb, place and and return it
{
symbolArr[randomRow][randomCol] = AT;
return true;
}
else // else return false
{
return false;
}
}
void displayBoard(Symbols symbolArr[ROWS][COLS]) // This function displays the board and the instructions is asking me to modify it. I am not sure how?
{
{
cout << " 0 1 2 3 4" << endl;
cout << "+-------------+" << endl;
for (int i = 0; i < ROWS; i++) //ROWS = 5
{
cout << i <<'|';
for (int j = 0; j < COLS; j++) //COLS = 5
{
cout << " ";
cout << static_cast<char>(symbolArr[i][j]) << "";
}
cout << " |"<<endl;
}
cout << "+-------------+" << endl;
}
return;
}
}
|