project

I am having a problem for how to let the user enters coordinates in the range of 1 though 15 for the rows and A though 0 for the columns. The program checks this location, and reports whether it is a hit or a miss. If it is a hit, the program also checks whether the ship has been hit in every location that it occupies. If so, the ship is reported as sunk, and the program identifies which ship it is.
The user gets 60 shots to attempt to sink the fleet. If the user sinks all of the ships before using all 60 shots, then he or she wins the game. At the end of the game, the program should output the grid, so that the user can see where the shops are located.

Here is my code so far
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib> // For srand() and rand()
#include <time.h>

using namespace std;


char grid[15][15];

void printGrid() {
// print the current grid
cout << endl;
for (int x=0; x<15; x++) {

for (int y=0; y<15; y++) {

cout << setw(2) << grid[x][y];
}
cout << endl;
}
cout << endl;
}

void createGrid() {

// create a blank grid
for (int x=0; x<15; x++) {
for (int y=0; y<15; y++) {

grid[x][y] = '*';
}
}
}
void placeShips()
{


srand (time(NULL));
int x = rand() % 2;
int y = rand()% 2;
// put a ship with three spaces on the board - vertical


grid[x][y] = 'A';
grid[x+1][y] = 'N';
grid[x+2][y] = 'H';


int g = 1+rand() % 2;
int h =1+rand() % 2;

grid[7*g][h] = 'Y';
grid[7*g][h+1] = 'E';
grid[7*g][h+2] = 'U';
int l = rand() % 2 ;
int k =1+ rand() % 2;
grid[l][7*k] = 'E';
grid[l+1][7*k] ='M';
int m = rand() % 2;
int n = 1+ rand() % 2;


grid[m+7][5*n] = 'M';
grid[m+8][5*n] ='U';
grid[m+9][5*n] = 'C';
grid[m+10][5*n] ='H';
int o = 1+rand() % 2 ;
int p =rand() % 2;
grid[5*o][p] = '?';
grid[5*o][p+1] ='?';
int a ;
char b;
int c;
cout<<"How many times you want to play the abttleship game>: \n";
cin>>c;
// I have problem here for(int i =0; i<=c; i++)
{
cout<<"CHANCE: "<<i+1<< "Please enter the coordinate: \n";
cin>>a>>b;
if(grid[a][b]==grid[x][y]||)
cout<<"HIT one time"
}



}


int main ()
{
char quit;
cout<<"Welcome to the battle ship game!\n";
cout<<"__________________________________\n";
cout<<"There are five ships that you need to destroy:\n";
cout<<"\t\t\t\t 1.Frigate\n";
cout<<"\t\t\t\t 2.Tender\n";
cout<<"\t\t\t\t 3.Destroyer\n";
cout<<"\t\t\t\t 4.Cruiser\n";
cout<<"\t\t\t\t 5.Carrier\n";
do
{

createGrid();
placeShips();
printGrid();


cout<<"Yo!Run again?(y)";
cin>>quit;
}
while(quit =='y');
}


use a different function for placing the ships and then use another for asking the user to the coordinates and put the latter function within a for loop :
1
2
3
4
5
6
for{i=0;i<60;i++) 
{cout<<"CHANCE: "<<i+1<< "Please enter the coordinate: \n";
cin>>a>>b;
check(a,b);

} 


and check should be something like
1
2
3
4
void check(a,b)
{if(grid[a][b]==grid[x][y])
cout<<"HIT one time";
}
I know that part but how can we count the hit times in order to make one ship sink and how to define which sunk ship is?
THanks
Topic archived. No new replies allowed.