There's just this tiny logical error in my code somewhere.
There's mulitple print outs that won't appear in the final program, but I'm using them to figure out were I went wrong. How do I get the B's to change to X's?
Code:
#include <iostream>
#include <iomanip>
#include <conio>
using namespace std;
void reset_array(char minefield[10][10], char temp_minefield[10][10]);
void print_array(char temp_minefield[10][10]);
void print_array_without_bombs(char minefield[10][10], char temp_minefield[10][10]);
void fill_array_with_bombs(char minefield[10][10], int r1, int c1);
bool check_for_hit(char minefield[10][10], char temp_minefield[10][10], int r1, int c1);
//-------------------------------------------------------------------------
// Behold, the main function
//-------------------------------------------------------------------------
int 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];
//This minefield will keep track of bombs
char temp_minefield[10][10];
//Sets all cells in the array to '-'
reset_array(minefield, temp_minefield);
//Prints out the array with everything reveiled
print_array(temp_minefield);
//Plants 5 bombs in the array-minefield
fill_array_with_bombs(temp_minefield, r1, c1);
//clears the screen so 2nd player can't cheat
system("CLS");
//sets the gameboard for the 2nd player to guess for bomb locations
cout<<"\n\nTime to start sweeping for mines!";
//loop until all the bombs are found
while (all_found==false)
{
cout<<"\n\nPlease enter a row and column number: ";
cin>>r1;
cin>>c1;
print_array(temp_minefield);
//If the 2nd player enters two zeros, quit program
if (r1==0 && c1==0)
{
cout<<"\n\n\nYou quit. Game Over.";
all_found=true;
getch();
}
if (check_for_hit(minefield, temp_minefield, r1, c1))
{
//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)
{
print_array(temp_minefield);
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, temp_minefield);
//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
}
}
//-------------------------------------------------------------------------
//Sets all cells in the array to '-'
//-------------------------------------------------------------------------
void reset_array(char minefield[10][10], char temp_minefield[10][10])
{
int row, col;
for (row=1; row<=10; row++)
{
for (col=1; col<=10; col++)
{
minefield[row-1][col-1]='-';
temp_minefield[row-1][col-1]='-';
}
}
}
//-------------------------------------------------------------------------
//Prints out the array with everything reveiled
//-------------------------------------------------------------------------
void print_array(char temp_minefield[10][10])
{
int x=1;
int row, col;
cout<<" 1 2 3 4 5 6 7 8 9 10";
for (row=1; row<=10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x++;
for (col=1; col<=10; col++)
{
cout<<" "<<temp_minefield[row-1][col-1];
}
}
}
//--------------------------------------------------------------------------
//sets the gameboard for the 2nd player to guess for bomb locations
//--------------------------------------------------------------------------
void print_array_without_bombs(char minefield[10][10], char temp_minefield[10][10])
{
int x=1;
int row, col;
cout<<"\n\n 1 2 3 4 5 6 7 8 9 10";
for (row=1; row<=10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x++;
for (col=1; col<=10; col++)
{
cout<<" "<<minefield[row-1][col-1];
}
}
cout<<"\n\n 1 2 3 4 5 6 7 8 9 10";
for (row=1; row<=10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x++;
for (col=1; col<=10; col++)
{
cout<<" "<<temp_minefield[row-1][col-1];
}
}
}
//--------------------------------------------------------------------------
//Plants 5 bombs in the array-minefield
//--------------------------------------------------------------------------
void fill_array_with_bombs(char temp_minefield[][10], int r1, int c1)
{
int t=0;
cout<<"\n\nNow you will place 5 bombs in the minefield, in different locations.";
while (t<5)
{
cout<<"\n\nEnter a row number and column number coordinate ( example:r c ):";
cin>>r1;
cin>>c1;
if (r1>=0 && r1<=10 && c1>=0 && c1<=10)
{
if (temp_minefield[r1-1][c1-1]=='-')
{
temp_minefield[r1-1][c1-1]='B';
t++;
}
else if (temp_minefield[r1-1][c1-1]=='B')
{
cout<<"\n\nThat bomb location has already been picked.";
}
}
else
{
cout<<"Make sure your coordinates are between 0 and 10.";
}
}
int row, col, x=1;
cout<<"\n\n 1 2 3 4 5 6 7 8 9 10";
for (row=1; row<=10; row++)
{
cout<<"\nRow "<<setw(2)<<x<<": ";
x++;
for (col=1; col<=10; col++)
{
cout<<" "<<temp_minefield[row-1][col-1];
}
}
getch();
system("PAUSE");
}
//---------------------------------------------------------------------------
//decides whether location guessed is a bomb location and returns true
//---------------------------------------------------------------------------
bool check_for_hit(char temp_minefield[10][10], char minefield[10][10], int r1, int c1)
{
if (temp_minefield[r1-1][c1-1]== 'B')
{
cout<<"\nHIT!";
minefield[r1-1][c1-1]='X';
return true;
}
else if (temp_minefield[r1-1][c1-1]== '-')
{
cout<<"\nMISS!";
minefield[r1-1][c1-1]='M';
return false;
}
else if (temp_minefield[r1-1][c1-1]== 'X')
{
cout<<"\nAlready blew that bomb.";
return false;
}
else if (temp_minefield[r1-1][c1-1]== 'M')
{
cout<<"\nLocation already tried.";
return false;
}
else
{
return false;
}
};