Page 1 of 1

Reputation:

# Need help in battleship

Posted 09 February 2010 - 07:37 AM

I need help in creating the funtion that allow user enter the coordinate and from this coordinate the program defines whether this point is hit or miss and whether the ship is sunk and which ship is ?
my code is :
```
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>  // For srand() and rand()
#include <time.h>

using namespace std;

enum Damage
{
Frigate, Tender, Destroyer, Cruiser, Carrier

};
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;
for(int i =0; i<=c; i++)
{
cout<<"CHANCE: "<<i+1<< "Please enter the coordinate: \n";
cin>>a>>b;
}
/*
grid[x][y]!=grid[g][h+3];
grid[x+1][y]!=grid[g][h+2];
grid[x+2][y]!=grid[g][h+1];
grid[x+3][y]!=grid[g][h];

*/
}

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');
}
```

This post has been edited by OliveOyl3471: 09 February 2010 - 08:14 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: Need help in battleship

### #2 RBSprogram101

Reputation: 3
• Posts: 38
• Joined: 28-March 09

## Re: Need help in battleship

Posted 09 February 2010 - 09:38 AM

ok why dont ask the user for a pair of coordinates like Enter place to attack--> 1,2
so it check if there is something at 1,2 in your grid array and if so do something.

Reputation:

## Re: Need help in battleship

Posted 09 February 2010 - 10:05 AM

RBSprogram101, on 09 February 2010 - 08:38 AM, said:

ok why dont ask the user for a pair of coordinates like Enter place to attack--> 1,2
so it check if there is something at 1,2 in your grid array and if so do something.

Can you show me the code. I really struggle with this.