Code:
#include <iostream>
#include <conio>
#include <iomanip>
using namespace std;
void reset_array(char minefield[][10], int row, int col);
void print_array(char minefield[][10], int row, int col);
void print_array_without_bombs(char minefield[][10], int row, int col);
void fill_array_with_bombs(char minefield[][10], int r1, int c1);
bool check_for_hit(char minefield[][10], int r1, int c1);
//-------------------------------------------------------------------------
// Behold, the main function
//-------------------------------------------------------------------------
void main()
{
int row, col, bombs_left=5, r1, c1;
int num_found=0, num_attempts=0;
bool all_found=false;
//Declaring the two-dimensional array
char minefield[10][10];
//Sets all cells in the array to '-'
reset_array(minefield, row, col);
//Prints out the array with everything reveiled
print_array(minefield, row, col);
//Plants 5 bombs in the array-minefield
fill_array_with_bombs(minefield, row, col);
//clears the screen so 2nd player can't cheat
system("CLS");
//sets the gameboard for the 2nd player to guess for bomb locations
print_array_without_bombs(minefield, row, col);
cout<<"Time to start sweeping for mines!";
//loop until all the bombs are found
while (all_found==false)
{
cout<<"Please enter a row and column number: ";
cin>>r1;
cin>>c1;
//If the 2nd player enters two zeros, quit program
if (r1==0 && c1==0)
{
cout<<"You quit. Gave Over.";
all_found=true;
}
//decides whether location guessed is a bomb location and returns true
check_for_hit(minefield, r1, c1);
if (check_for_hit(minefield, r1, c1)==true)
{
//1 bomb closer to finishing the program
num_found++;
cout<<"There are now "<<bombs_left--<<"bombs left in the minefield";
//If all the bombs are found, the while loop will break
if (bombs_left==0)
{
all_found=true;
}
}
//Counts the number of attempts to poke fun of you later.
num_attempts++;
//Prints the up-to-date minefield
print_array_without_bombs(minefield, row, col);
//Once all the bombs are found, it will print out how many attempts it took
if (all_found==true)
{
cout<<"It took you "<<num_attempts<<" guesses to find all 5 bombs! HAHA!";
}
//Reveils the bomb locations and all other occurances
print_array(minefield, row, col);
}
}
//-------------------------------------------------------------------------
//Sets all cells in the array to '-'
//-------------------------------------------------------------------------
void reset_array(char minefield[][10], int row, int col)
{
for (row=0; row<10; row++)
{
for (col=0; col<10; col++)
{
minefield[row][col]='-';
}
}
}
//-------------------------------------------------------------------------
//Prints out the array with everything reveiled
//-------------------------------------------------------------------------
void print_array(char minefield[][10], int row, int col)
{
int x=1;
cout<<" 1 2 3 4 5 6 7 8 9 10";
for (row=0; row<10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x=x++;
for (col=0; col<10; col++)
{
cout<<" "<<minefield[row][col];
}
}
}
//--------------------------------------------------------------------------
//sets the gameboard for the 2nd player to guess for bomb locations
//--------------------------------------------------------------------------
void print_array_without_bombs(char minefield[][10], int row, int col)
{
int x=1;
cout<<" 1 2 3 4 5 6 7 8 9 10";
for (row=0; row<10; row++)
{
for (col=0; col<10; col++)
{
if (minefield[row][col]== 'B')
{
minefield[row][col]= '-';
}
}
}
for (row=0; row<10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x=x++;
for (col=0; col<10; col++)
{
cout<<" "<<minefield[row][col];
}
}
}
//--------------------------------------------------------------------------
//Plants 5 bombs in the array-minefield
//--------------------------------------------------------------------------
void fill_array_with_bombs(char minefield[][10], int r1, int c1)
{
int t=0;
cout<<"Now you will place 5 bombs in the minefield, in different locations.";
while (t<5)
{
cout<<"Enter a row number and column number coordinate ( example:r c ):";
cin>>r1;
cin>>c1;
if (r1>0 && r1<10 && c1>0 && c1<10)
{
if (minefield[r1][c1]=='B')
{
cout<<"That bomb location has already been picked.";
}
else if (minefield[r1][c1]=='-')
{
minefield[r1][c1]='B';
t++;
}
}
else
{
cout<<"Make sure your coordinates are between 0 and 10.";
}
}
system("PAUSE");
}
//---------------------------------------------------------------------------
//decides whether location guessed is a bomb location and returns true
//---------------------------------------------------------------------------
bool check_for_hit(char minefield[][10], int r1, int c1)
{
if (minefield[r1][c1]== 'X')
{
cout<<"Already blew that bomb.";
return false;
}
else if (minefield[r1][c1]== 'M')
{
cout<<"Location already tried.";
return false;
}
else if (minefield[r1][c1]== '-')
{
cout<<"MISS!";
return false;
}
else if (minefield[r1][c1]== 'B')
{
cout<<"HIT!";
return true;
}
else
{
return false;
}
};