Battleship Program. HELP!

My professor wants to me to create a battleship program that has a grid of 15x15. The battleship program should have five boats in the game:
Frigate: 2 locations
Tender: 2 locations
Destroyer: 3 locations
Cruiser: 3 locations
Carrier: 4 locations

-The ships cannot overlap or be located outside of the board.
-It should allow the user to enter a location.
-The program should display hit or miss..
-The board should display the location the user hit and occupied.
-After the user has sunk the ship, a message should display which ship has been sunk.
-The user gets 60 tries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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()
{
     
// there is no check for collision of the ships

   srand (time(NULL));
   int x = rand() % 15;
   int y = rand() % 15;
 
 
// put a ship with three spaces on the board - vertical
   grid[x][y] = 'c';
   grid[x+1][y] = 'c';
   grid[x+2][y] = 'c';
   
   x = rand() % 15;
   y = rand() % 15;

// put a ship with four spaces on the board  - horizontal
   grid[x][y] = 'd';
   grid[x][y+1] = 'd';
   grid[x][y+2] = 'd';
   grid[x][y+3] = 'd';
   
}

int main ()
{
 createGrid(); 
 placeShips();
 printGrid();
 
 getchar();
 return 0;
 }


This is what I have.
Okay!
Just some basic steps:

- Check if random coordinates on placeShips() don't place ships inside other or outside the grid.
- Write a function that prompts the player for a coordinate.
- Create a variable that indicates the current player. Put the function above inside a loop and change it so that it asks each player in turn.
- Create a function that takes a coordinate as a parameter and checks that position is occupied.
- Change the function above to only check for ships that are enemies of the current player.

Remember that dividing the development in incremental phases like this makes it programming much easier.
use a 2d array and set every spot on the grid to 0, next just come up with ids for each ship and when a ship is placed the squares it takes up are changed to the ship id.
Topic archived. No new replies allowed.