Hello all, I have another question today on my console battleship program. The game displays a sample 10x10 grid and allows the user to enter x,y coordinates to guess the location of the 5 battleships. The program runs and does as it should, although I would like to know how I could update the grid, to show the user that the ship has been hit. Thanks.
/* Joe McMahon C++ 4 Battleship*/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <time.h>
usingnamespace std;
//define constants
constint rows = 10;
constint elements = 10;
//num of total ships
int maxships = 5;
//define array
int matrix[rows][elements];
char grid[10][10];
//*****function definitions*****
void Clear()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
matrix[i][j] = 0;
}//end for
}//end for
}//end of clear
void printGrid() {
// print the current grid
cout << endl;
for (int x = 0; x<10; x++) {
for (int y = 0; y<10; y++) {
cout << setw(2) << grid[x][y];
}
cout << endl;
}
cout << endl;
}//end of printGrid
void createGrid() {
// create a blank grid
for (int x = 0; x<10; x++) {
for (int y = 0; y<10; y++) {
grid[x][y] = '0';
}
}
}//end of createGrid
void displayMessage()
{
cout << endl << "Welcome to Battleships!" << endl;
cout << endl << "The rules are simple:" << endl;
cout << endl << "You have to guess the location of all the ships." << endl;
cout << endl << "Type the x and y coordinates, starting with 0." << endl;
cout << endl << "Below is a sample of the grid, choose your coordinates." << endl;
createGrid();
printGrid();
}//end of displayMessage
void resultsMessage()
{
cout << endl << "Thanks for playing!" << endl;
cout << endl << "The number 1 indicates the ships that are still standing." << endl;
cout << endl << "The number 2 indicates the ships you hit!" << endl;
}//end of resultsMessage
void Show()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
cout << matrix[i][j] << " ";
}//end for
cout << endl;
}//end for
}//end of show
int NumberOfShips()
{
int c = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
if (matrix[i][j] == 1)
c++;
}//end for
}//end for
return c;
}//end of NumberOfShips
void SetShips()
{
int s = 0;
while (s < maxships)
{
int x = rand() % rows;
int y = rand() % elements;
if (matrix[x][y] != 1)
{
s++;
matrix[x][y] = 1;
}//end if
}//end while
}//end of SetShips
bool Attack(int x, int y)
{
if (matrix[x][y] == 1)
{
matrix[x][y] = 2;
returntrue;
}//end if
returnfalse;
}//end of Attack
int main()
{
//call functions
srand(time(NULL));
Clear();
SetShips();
displayMessage();
//variables
int pos1 = 0;
int pos2 = 0;
char choice;
char prompt;
//begin while
while (1)
{
//ask for input
cout << "Please input location(x, y): ";
cin >> pos1 >> pos2;
//begin if
if (Attack(pos2, pos1) == true)
{
cout << "You sunk the battleship! :)" << endl;
}
else
{
cout << "Sorry, no ship at that position!" << endl;
}//end
cout << "Number of ships left: " << NumberOfShips() << endl;
if (NumberOfShips() == 0)
{
cout << "Congratulations, you won the game!" << endl;
}//end if
cout << "Do you want to surrender (y/n)? ";
cin >> prompt;
if (prompt == 'y')
break;
//end if
}//end while
cout << endl << "Game over!" << endl;
cout << endl << "Key:" << endl;
Show();
resultsMessage();
//close
system("pause");
return 0;
}//end of main