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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Declare global constants here
const int NUM_ROWS = 10; // Number of rows and columns in the gameboard.
const int NUM_COLS = 10;
const int MAX_MISS = 20; // maximum number of misses required to win
const int MAX_BOMBS = 20; // maximum number of bombs to place on the game board
const int MIN_MISS = 5; // minimum number of misses required to win
const int MIN_BOMBS = 5; // minimum number of bombs to place on the game board
const string HASH = "####################"; // used when printing out the header
// create new data type for the two-dimensional array used for the game board
// The game board is to be an array of integers. Characters that will be stored in
// the array elements are 'X', 'O', 'B', '.', and a space ' '.
typedef char GameBoard[NUM_ROWS][NUM_COLS];
// Function Prototypes
// Provided function prototypes
void PlaceBombs(GameBoard); // determine placement of bombs on the board
void PrintBoard(GameBoard); // prints game board status during the playing of the game
void PrintHeaderInfo(); // print out the title for the game
// user defined function prototypes
int main()
{
return 0;
} // End main()
//********************************************************************************
//********************************************************************************
//********************************************************************************
void PrintHeaderInfo()
{
// Print out the game header information using # symbols and the
// title of the game.
string hash = HASH+HASH+HASH;
cout << hash << endl;
cout << hash << endl;
cout << HASH << "- AVOID THE BOMB -" << HASH << endl;
cout << hash << endl;
cout << hash << endl << endl;
} // end of PrintHeaderInfo
void PrintBoard(GameBoard board)
{
// This function prints out the game board after every move has been
// made. The first line output is a header line indicating the current
// status of the board. This line is followed by the board.
int row, col; // loop index values for the row and column indexes of the array
cout << "\n =====> Current Board Status <=====\n\n";
// Print out the top information line for the column number of the array
cout << setw(7) << "0";
for (col = 1; col < NUM_COLS; col++)
cout << setw(3) << col;
cout << endl;
// Output dashes under the column headings
cout << setw(7) << "-";
for (col = 1; col < NUM_COLS; col++)
cout << setw(3) << "---";
cout << endl;
// output the values of the array. At the start of each row, print out
// the row index number followed by a :. This value is then followed by
// all elements of the array for that row. use a field width of 3, ane
// the output is right justified by default.
// Except for the bombs, all values in the game board are printed
// out as they are stored. In the case that an array element holds a 'B'
// indicating a bomb, an 'X' is printed out instead of the 'B'. An 'X' in
// the game board indicates a hidden unpicked location.
for (row = 0; row < NUM_ROWS ; row++)
{
cout << setw(3) << row << ":";
for (col = 0; col < NUM_COLS ; col++)
{
if (board[row][col] == 'B')
cout << setw(3) << 'X';
else
cout << setw(3) << board[row][col];
}
cout << endl;
}
} // end of PrintBoard
void PlaceBombs(GameBoard board)
{
// This function places the bombs into the board space. A random number
// generator is used to find row and column positions to place the bombs.
// Checks are made to ensure that once a bomb has been placed that no other
// bomb is placed in the same location
int seed; // starting seed value for the random number generator
int row, col; // row and column of the array for bomb placement
int numBombs = 0; // number of bombs that have been placed so far
int maxBombs; // number of bombs that are to be placed in the board
cout << "\n\nPlacing bombs onto the game board....\n\n"; // information line
// Obtain the seed number for the random number generator. RAND_MAX is the
// largest value output by the random number generator. Also largest value
// used for the seed.
cout << "Enter in the starting seed for the random number generator.\n"
<< "Value entered must be a positive integer value < "
<< RAND_MAX << ": ";
cin >> seed;
if (seed < 1 || seed > RAND_MAX)
{
// limit seed value to be between 1 and RAND_MAX. for all out
// of range values, assign a value of 1
cout << "Invalid value for the seed. setting it to 1\n";
seed = 1;
}
// obtain number of bombs to place. Set a minimum of 5 and a Maximum
// as defined by the constant MAX_BOMBS. For values less than 5 use 5,
// for values greater than MAX_BOMBS use MAX_BOMBS
cout << "\nEnter in the number of bombs to be placed on the game board\n";
cout << "The value must be between " << MIN_BOMBS << " and " << MAX_BOMBS << ": ";
cin >> maxBombs;
if (maxBombs < MIN_BOMBS)
{
cout << "\nValue entered is too low. Setting number of bombs to "
<< MIN_BOMBS << "\n\n";
maxBombs = MIN_BOMBS;
}
else if (maxBombs > MAX_BOMBS)
{
cout << "\nValue entered is too high. Setting number of bombs to "
<< MAX_BOMBS << "\n\n";
maxBombs = MAX_BOMBS;
}
// set the seed value for the random number generator. Using a particular
// seed value assures that the same random numbers are generated every time
// using a seed value allows for comparing one run with another run from a
// different program
srand(seed);
// use a loop to place the bombs. As long as the number of bombs placed
// is less than the number of bombs required, continue to place bombs.
while (numBombs < maxBombs)
{
row = rand()%NUM_ROWS; // determine a row index value
col = rand()%NUM_COLS; // determine a column index value
// initially, The board contains all X's, so as long as the row and column picked
// already has a bomb placed there, continue to generate new row and column
// values.
while (board[row][col] == 'B')
{
row = rand()%NUM_ROWS;
col = rand()%NUM_COLS;
}
// place the bomb in the row and column location randomly generated and
// increment the counter for number of bombs placed.
board[row][col] = 'B';
numBombs++;
}
}// end of PlaceBombs
|