Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
//Initialize 9x9 grid
const int ROWS = 9;
const int COLUMNS = 9;
char board[ROWS][COLUMNS] = {{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'}};
//Welcome player and explain how to play game
cout << "Welcome to [Name Witheld]'s Minesweeper: Duke Nukem Edition" << endl;
cout << "Instead of placing flags, you must place a single bullet" << endl;
cout << "where you believe a mine is to blow them all up." << endl;
cout << "If you run out of ammo, you lose.\n" << endl;
cout << "Below is the 9x9 grid:\n\n" << endl;
srand(static_cast<unsigned int>(time(0))); //Seed Random number
//Replace 15 O's with X's
for (int count = 0; count < 15; count++) //Continue loop until 15 O's were replaced with X's
{
int randomNumberX = rand() % ROWS; //Assign random number to i
int randomNumberY = rand() % COLUMNS; //Assign random number to j
//Check if slot is not already an X. If not, replace existing character with X.
if (board[randomNumberX][randomNumberY] == 'X')
{
randomNumberX--;
}
else
{
board[randomNumberX][randomNumberY] = 'X';
}
}
//Displays 9x9 tile grid
for (int i=0; i < ROWS; ++i)
{
for (int j=0; j < COLUMNS; ++j)
{
cout << board[i][j];
}
cout << endl;
}
//Present player a list of options
int modeChoice;
cout << "\nEnter '1' to enter reticle placement mode." << endl;
std::cin >> modeChoice;
//Gunfire program is chosen
//Get player input
if (modeChoice == 1)
{
int ammo = 1;
int fireInputX;
int fireInputY;
cout << "Select 'X' and 'Y' values between 0-8" <<endl;
cout << ammo << " round(s) are in the chamber.\n" << endl;
cout << "Enter fire input Y" << endl;
std::cin >> fireInputY;
cout << endl;
cout << "Enter fire input X" << endl;
std::cin >> fireInputX;
cout << endl;
//If coordinates are an 'X', you win
if (board[fireInputY][fireInputX] == 'X')
{
cout << "YOU WIN" << endl;
cin.get();
}
//If coordinates are an 'O', you lose
elseif (board[fireInputY][fireInputX] == 'O')
{
ammo == ammo - 1;
cout << "YOU LOSE" << endl;
cin.get();
}
}
cin.get();
return
0;
}